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

	expected := []data.Order{}

	for i := 1; i <= 3; i++ {

		_time := uint64(time.Now().UTC().Unix())

		order := data.Order{
			Time: _time,
		}

		createOrder(&order)

		// newest come first:
		expected = append([]data.Order{order}, expected...)

	}

	got := []data.Order{}

	r := getForestClient().GET(t, forest.Path("/orders/{}", "").Query("limit", len(expected)))

	forest.ExpectStatus(t, r, http.StatusOK)

	forest.ExpectJSONDocument(t, r, &got)

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

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

	target := data.Product{
		ID:   1,
		Name: "Product Name",
		Price: data.Price{
			Vat:   20,
			Net:   100,
			Gross: 0, // should be: 120
		},
	}

	expected := data.Product{
		ID:   1,
		Name: "Product Name",
		Price: data.Price{
			Vat:   20,
			Net:   100,
			Gross: 120, // should be: 120
		},
	}

	Product(&target)

	if !reflect.DeepEqual(expected, target) {
		t.Errorf("expected: %s\n\nactual: %s", testutils.JSON(expected), testutils.JSON(target))
	}

}
예제 #3
0
func TestForestUpdateOrder(t *testing.T) {

	_time := uint64(time.Now().UTC().Unix())
	_newTime := _time + 300 // 5 min

	order := data.Order{
		Time: _time,
	}

	createOrder(&order)

	expected := order

	expected.Time = _newTime

	payload := data.Order{
		Time: _newTime,
	}

	got := data.Order{}

	r := getForestClient().PUT(t, forest.Path("/orders/{id}", order.ID).Content(payload, MimeJSON))

	forest.ExpectStatus(t, r, http.StatusOK)

	forest.ExpectJSONDocument(t, r, &got)

	expected.ID = got.ID

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

}
예제 #4
0
func TestLoadProduct(t *testing.T) {

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

	createProduct(&product)

	expected := product

	got := data.Product{}

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

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

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

}
예제 #5
0
func TestForestUpdateOrderDraftCancelled(t *testing.T) {

	order := data.Order{}

	createOrder(&order)

	expected := order

	expected.State = data.OrderStateCancelled

	payload := data.Order{
		State: data.OrderStateCancelled,
	}

	got := data.Order{}

	r := getForestClient().PUT(t, forest.Path("/orders/{id}", order.ID).Content(payload, MimeJSON))

	forest.ExpectStatus(t, r, http.StatusOK)

	forest.ExpectJSONDocument(t, r, &got)

	expected.ID = got.ID

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

}
예제 #6
0
func TestForestCreateOrder(t *testing.T) {

	_time := uint64(time.Now().UTC().Unix())

	expected := data.Order{
		State: data.OrderStateDraft,
		Time:  _time,
	}
	populate.Order(&expected)

	payload := data.Order{
		Time: _time,
	}

	got := data.Order{}

	r := getForestClient().POST(t, forest.Path("/orders/{}", "").Content(payload, MimeJSON))

	forest.ExpectStatus(t, r, http.StatusCreated)

	forest.ExpectJSONDocument(t, r, &got)

	expected.ID = got.ID

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

}
예제 #7
0
func TestUpdateProduct(t *testing.T) {

	_name := generateName("Product")

	product := data.Product{
		Name: _name,
		Price: data.Price{
			Vat: 20,
			Net: 100,
		},
	}

	createProduct(&product)

	payload := data.Product{
		ID:   0, // route (path) params prevail over payload data
		Name: _name + " Changed",
		Price: data.Price{
			Vat:   30,
			Net:   300,
			Gross: 0,
		},
	}

	got := data.Product{}

	expected := data.Product{
		ID:   product.ID,
		Name: payload.Name,
		Price: data.Price{
			Vat:   30,
			Net:   300,
			Gross: 390,
		},
	}

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

	// TODO: test for Location header later?

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

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

}
예제 #8
0
func TestForestCreateOrderWithItems(t *testing.T) {

	_time := uint64(time.Now().UTC().Unix())

	expected := data.Order{
		State: data.OrderStateDraft,
		Time:  _time,
	}

	payload := data.Order{
		Time: _time,
	}

	for i := 1; i <= 3; i++ {

		product := data.Product{
			Name: generateName("Product"),
			Price: data.Price{
				Vat: 20,
				Net: uint64(100 * i),
			},
		}

		createProduct(&product)

		item := data.OrderItem{
			Product:  product,
			Quantity: uint64(i),
		}

		// newest come first:
		payload.Items = append([]data.OrderItem{item}, payload.Items...)
		expected.Items = append([]data.OrderItem{item}, expected.Items...)

	}

	populate.Order(&expected)

	got := data.Order{}

	r := getForestClient().POST(t, forest.Path("/orders/{}", "").Content(payload, MimeJSON))

	forest.ExpectStatus(t, r, http.StatusCreated)

	forest.ExpectJSONDocument(t, r, &got)

	expected.ID = got.ID

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

}
예제 #9
0
func TestForestOrderItemUpdate(t *testing.T) {

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

	createProduct(&product)

	order := data.Order{
		Items: []data.OrderItem{
			data.OrderItem{
				Product:  product,
				Quantity: 1,
			},
		},
	}

	createOrder(&order)

	expected := data.OrderItem{
		Product:  product,
		Quantity: 20,
	}
	populate.OrderItem(&expected)

	payload := data.OrderItem{
		Product: data.Product{
			ID: product.ID,
		},
		Quantity: 20,
	}

	got := data.OrderItem{}

	r := getForestClient().PUT(t, forest.Path("/orders/{orderID}/items/{productID}", order.ID, product.ID).Content(payload, MimeJSON))

	forest.ExpectStatus(t, r, http.StatusOK)

	forest.ExpectJSONDocument(t, r, &got)

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

}
예제 #10
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
	}

}
예제 #11
0
파일: order.go 프로젝트: mxmCherry/goshop
func LoadOrder(s storage.Adapter, t *testing.T) {

	tx, err := s.Begin()
	if err != nil {
		t.Fatal("Failed to begin transaction", err)
		return
	}
	defer tx.Rollback(nil)

	order := &data.Order{
		State: data.OrderStateDraft,
		Time:  uint64(time.Now().UTC().Unix()),
	}

	if err := tx.CreateOrder(order); err != nil {
		t.Fatal("Failed to create order", err)
		return
	}

	loadedOrder := &data.Order{
		ID: order.ID,
	}

	if err := tx.LoadOrder(loadedOrder); err != nil {
		t.Fatal("Failed to load order", err)
		return
	}

	if !reflect.DeepEqual(order, loadedOrder) {
		t.Errorf("Loaded order differs from created one:\n\ncreated: %s\n\nloaded: %s", testutils.JSON(order), testutils.JSON(loadedOrder))
	}

}
예제 #12
0
func TestForestUpdateOrderDraftPlaced(t *testing.T) {

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

	createProduct(&product)

	order := data.Order{
		Items: []data.OrderItem{
			data.OrderItem{
				Product:  product,
				Quantity: 1,
			},
		},
	}

	createOrder(&order)

	expected := order

	expected.State = data.OrderStatePlaced

	payload := data.Order{
		State: data.OrderStatePlaced,
	}

	got := data.Order{}

	r := getForestClient().PUT(t, forest.Path("/orders/{id}", order.ID).Content(payload, MimeJSON))

	forest.ExpectStatus(t, r, http.StatusOK)

	forest.ExpectJSONDocument(t, r, &got)

	expected.ID = got.ID

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

}
예제 #13
0
func TestOrderItem(t *testing.T) {

	target := data.OrderItem{
		Product: data.Product{
			ID:   1,
			Name: "Product Name",
			Price: data.Price{
				Vat:   20,
				Net:   100,
				Gross: 0, // should be: 120
			},
		},
		Quantity: 10,
		Price: data.Price{
			Vat:   0, // should be: 20
			Net:   0, // should be: 100 * 10 = 1000
			Gross: 0, // should be: 100 * 10 * 20% = 1200
		},
	}

	expected := data.OrderItem{
		Product: data.Product{
			ID:   1,
			Name: "Product Name",
			Price: data.Price{
				Vat:   20,
				Net:   100,
				Gross: 120, // should be: 120
			},
		},
		Quantity: 10,
		Price: data.Price{
			Vat:   20,   // should be: 20
			Net:   1000, // should be: 100 * 10 = 1000
			Gross: 1200, // should be: 100 * 10 * 20% = 1200
		},
	}

	OrderItem(&target)

	if !reflect.DeepEqual(expected, target) {
		t.Errorf("expected: %s\n\nactual: %s", testutils.JSON(expected), testutils.JSON(target))
	}

}
예제 #14
0
func TestForestListOrderItems(t *testing.T) {

	order := data.Order{}

	for i := 1; i <= 3; i++ {

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

		createProduct(&product)

		item := data.OrderItem{
			Product:  product,
			Quantity: 1,
		}

		order.Items = append([]data.OrderItem{item}, order.Items...)

	}

	createOrder(&order)

	expected := order.Items

	got := []data.OrderItem{}

	r := getForestClient().GET(t, forest.Path("/orders/{orderID}/items/{}", order.ID, "").Query("limit", len(expected)))

	forest.ExpectStatus(t, r, http.StatusOK)

	forest.ExpectJSONDocument(t, r, &got)

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

}
예제 #15
0
func TestForestLoadOrder(t *testing.T) {

	_time := uint64(time.Now().UTC().Unix())

	expected := data.Order{
		Time: _time,
	}

	createOrder(&expected)

	got := data.Order{}

	r := getForestClient().GET(t, forest.Path("/orders/{id}", expected.ID))

	forest.ExpectStatus(t, r, http.StatusOK)

	forest.ExpectJSONDocument(t, r, &got)

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

}
예제 #16
0
func TestListProducts(t *testing.T) {

	products := []data.Product{}

	for i := 0; i < 3; i++ {

		product := data.Product{
			Name: generateName("Product"),
			Price: data.Price{
				Vat: 20,
				Net: uint64(100 * (i + 1)),
			},
		}

		createProduct(&product)

		// latest come first:
		products = append([]data.Product{product}, products...)

	}

	expected := products

	got := []data.Product{}

	resp := getJSON(fmt.Sprintf("%s/products/?limit=%d&offset=0", getURL(), len(products)), &got)

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

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

}
예제 #17
0
파일: product.go 프로젝트: mxmCherry/goshop
func ListProducts(s storage.Adapter, t *testing.T) {

	tx, err := s.Begin()
	if err != nil {
		t.Fatal("Failed to begin transaction", err)
		return
	}
	defer tx.Rollback(nil)

	products := []data.Product{}

	for i := 1; i <= 3; i++ {

		product := data.Product{
			Name: fmt.Sprintf("Product #%d", i),
			Price: data.Price{
				Vat: 20,
				Net: uint64(100 * i),
			},
		}

		if err := tx.CreateProduct(&product); err != nil {
			t.Fatal("Failed to create product", err)
			return
		}

		products = append([]data.Product{product}, products...)

	}

	listedProducts, err := tx.ListProducts(uint64(len(products)), 0)
	if err != nil {
		t.Fatal("Failed to list products", err)
		return
	}

	if !reflect.DeepEqual(products, listedProducts) {
		t.Errorf("Listed products differ from created ones:\n\ncreated: %s\n\nloaded: %s", testutils.JSON(products), testutils.JSON(listedProducts))
	}

}
예제 #18
0
파일: order.go 프로젝트: mxmCherry/goshop
func ListOrders(s storage.Adapter, t *testing.T) {

	tx, err := s.Begin()
	if err != nil {
		t.Fatal("Failed to begin transaction", err)
		return
	}
	defer tx.Rollback(nil)

	orders := []data.Order{}

	for i := 1; i <= 3; i++ {

		order := data.Order{
			State: data.OrderStateDraft,
			Time:  uint64(time.Now().UTC().Unix()),
		}

		if err := tx.CreateOrder(&order); err != nil {
			t.Fatal("Failed to create order", err)
			return
		}

		orders = append([]data.Order{order}, orders...)

	}

	listedOrders, err := tx.ListOrders(uint64(len(orders)), 0)
	if err != nil {
		t.Fatal("Failed to list orders", err)
		return
	}

	if !reflect.DeepEqual(orders, listedOrders) {
		t.Errorf("Listed orders differ from created ones:\n\ncreated: %s\n\nloaded: %s", testutils.JSON(orders), testutils.JSON(listedOrders))
	}

}
예제 #19
0
파일: product.go 프로젝트: mxmCherry/goshop
func LoadProduct(s storage.Adapter, t *testing.T) {

	tx, err := s.Begin()
	if err != nil {
		t.Fatal("Failed to begin transaction", err)
		return
	}
	defer tx.Rollback(nil)

	product := &data.Product{
		Name: "Product Name",
		Price: data.Price{
			Vat: 20,
			Net: 100,
		},
	}

	if err := tx.CreateProduct(product); err != nil {
		t.Fatal("Failed to create product", err)
		return
	}

	loadedProduct := &data.Product{
		ID: product.ID,
	}

	if err := tx.LoadProduct(loadedProduct); err != nil {
		t.Fatal("Failed to load product", err)
		return
	}

	if !reflect.DeepEqual(product, loadedProduct) {
		t.Errorf("Loaded product differs from created one:\n\ncreated: %s\n\nloaded: %s", testutils.JSON(product), testutils.JSON(loadedProduct))
	}

}
예제 #20
0
func ListOrderItems(s storage.Adapter, t *testing.T) {

	tx, err := s.Begin()
	if err != nil {
		t.Fatal("Failed to begin transaction", err)
		return
	}
	defer tx.Rollback(nil)

	items := []data.OrderItem{}
	orderID := uint64(0)

	for i := 1; i <= 3; i++ {

		product, order := testProductAndOrder(tx, i)

		// all items should be in the same order
		if orderID == 0 {
			orderID = order.ID
		}

		item := data.OrderItem{
			Product:  *product,
			Quantity: uint64(i),
		}

		if err := tx.CreateOrderItem(orderID, &item); err != nil {
			t.Fatal("Failed to create order item", err)
			return
		}

		items = append([]data.OrderItem{item}, items...)

	}

	listedOrderItems, err := tx.ListOrderItems(orderID, uint64(len(items)), 0)
	if err != nil {
		t.Fatal("Failed to list order items", err)
		return
	}

	if !reflect.DeepEqual(items, listedOrderItems) {
		t.Errorf("Listed order items differ from created ones:\n\ncreated: %s\n\nloaded: %s", testutils.JSON(items), testutils.JSON(listedOrderItems))
	}

}
예제 #21
0
func LoadOrderItem(s storage.Adapter, t *testing.T) {

	tx, err := s.Begin()
	if err != nil {
		t.Fatal("Failed to begin transaction", err)
		return
	}
	defer tx.Rollback(nil)

	product, order := testProductAndOrder(tx, 1)

	item := &data.OrderItem{
		Product:  *product,
		Quantity: 1,
	}

	if err := tx.CreateOrderItem(order.ID, item); err != nil {
		t.Error("Failed to create order item", err)
		return
	}

	loadedItem := &data.OrderItem{
		Product: *product,
	}

	if err := tx.LoadOrderItem(order.ID, loadedItem); err != nil {
		t.Error("Failed to load order item", err)
		return
	}

	if !reflect.DeepEqual(item, loadedItem) {
		t.Errorf("Loaded order item differs from created one:\n\ncreated: %s\n\nloaded: %s", testutils.JSON(item), testutils.JSON(loadedItem))
	}

}