Ejemplo n.º 1
1
func CreateProduct(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
	}

	if product.ID < 1 {
		t.Error("Expected ID of created product to be positive non-zero")
		t.Fail()
	}

}
Ejemplo n.º 2
0
func UpdateProduct(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
	}

	product.Name = "Product Name Changed"
	product.Price.Net = 200

	if err := tx.UpdateProduct(product); err != nil {
		t.Error("Failed to update product", err)
	}

}
Ejemplo n.º 3
0
func RemoveOrderItem(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.Fatal("Failed to create order item", err)
		return
	}

	if err := tx.RemoveOrderItem(order.ID, item.Product.ID); err != nil {
		t.Error("Failed to remove order item", err)
	}

}
Ejemplo n.º 4
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))
	}

}
Ejemplo n.º 5
0
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))
	}

}
Ejemplo n.º 6
0
func CreateOrder(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
	}

	if order.ID < 1 {
		t.Error("Expected ID of created order to be positive non-zero")
		t.Fail()
	}

}
Ejemplo n.º 7
0
func UpdateOrder(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
	}

	order.State = data.OrderStateCancelled
	order.Time = uint64(time.Now().UTC().Unix())

	if err := tx.UpdateOrder(order); err != nil {
		t.Error("Failed to update order", err)
	}

}
Ejemplo n.º 8
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))
	}

}
Ejemplo n.º 9
0
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))
	}

}
Ejemplo n.º 10
0
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))
	}

}
Ejemplo n.º 11
0
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))
	}

}