예제 #1
0
func TestCreateProduct(t *testing.T) {

	_name := generateName("Product")

	payload := data.Product{
		ID:   0,
		Name: _name,
		Price: data.Price{
			Vat:   20,
			Net:   100,
			Gross: 0,
		},
	}

	got := data.Product{}

	expected := data.Product{
		ID:   0,
		Name: _name,
		Price: data.Price{
			Vat:   20,
			Net:   100,
			Gross: 120,
		},
	}

	resp := postJSON(
		fmt.Sprintf("%s/products/", getURL()),
		payload,
		&got,
	)

	if resp.StatusCode != http.StatusCreated {
		t.Errorf("Got status: %d, expected: %d", resp.StatusCode, http.StatusCreated)
	}

	if got.ID == 0 {
		t.Errorf("Got ID: %d, expected: > 0", got.ID)
	}

	expected.ID = got.ID

	if !reflect.DeepEqual(got, expected) {
		t.Fatalf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected))
		return
	}

}
예제 #2
0
func TestUpdateProductInvalidName(t *testing.T) {

	invalidNames := []string{
		"",              // empty
		" ",             // whitespaces only
		" Left space",   // non-left-trimmed
		"Right space ",  // non-right-trimmed
		" Not trimmed ", // non-trimmed
	}

	for _, invalidName := range invalidNames {

		product := data.Product{
			ID:   0,
			Name: generateName("Product"),
			Price: data.Price{
				Vat:   20,
				Net:   100,
				Gross: 0,
			},
		}

		createProduct(&product)

		product.Name = invalidName

		err := Error{}

		resp := putJSON(
			fmt.Sprintf("%s/products/%d", getURL(), product.ID),
			product,
			&err,
		)

		// permit non-400 status in favor of 500 for now;
		// TODO: allow only 400-status
		if !(resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusInternalServerError) {
			t.Errorf("Got status: %d, expected: %d or %d", resp.StatusCode, http.StatusBadRequest, http.StatusInternalServerError)
		}

		if err.Message == "" {
			t.Error("Got empty error message")
		}

	}

}