コード例 #1
0
ファイル: factory_test.go プロジェクト: ddmendes/go-boutique
// TestRelations tests if the models relations is ok.
func TestRelations(t *testing.T) {
	customer := factory.NewCustomer()
	product := factory.NewProduct()

	order := factory.NewOrder(customer)
	if order.OrderCustomer != customer {
		t.Error("order.OrderCustomer and customer differ.")
	}

	orderItem := factory.NewOrderItem(order, product)
	if orderItem.ItemProduct != product {
		t.Error("orderItem.ItemProduct and product differ.")
	} else if orderItem.ItemOrder != order {
		t.Error("orderItem.ItemOrder and order differ.")
	}
}
コード例 #2
0
ファイル: database_test.go プロジェクト: ddmendes/go-boutique
func TestCrud(t *testing.T) {
	customer := factory.NewCustomer()
	customer.Name = "Davi Diório Mendes"
	customer.Save()

	customer.Name = "Davi Mendes"
	customer.Save()

	prod1 := factory.NewProduct()
	prod1.Name = "Prod1"
	prod1.Description = "Description of product 1."
	prod1.Price = 10.0
	prod1.Save()

	prod2 := factory.NewProduct()
	prod2.Name = "Prod2"
	prod2.Description = "Description of product 2."
	prod2.Price = 20.0
	prod2.Save()

	order := factory.NewOrder(customer)

	item := factory.NewOrderItem(order, prod1)
	order.Items = append(order.Items, item)

	item = factory.NewOrderItem(order, prod2)
	order.Items = append(order.Items, item)

	order.Save()

	testCustomer := storage.GetCustomer(customer.ID)
	if testCustomer.ID != customer.ID ||
		testCustomer.Name != customer.Name {
		t.Error("Customer read differ from customer saved.")
	}

	testProduct := storage.GetProduct(prod1.ID)
	if testProduct.ID != prod1.ID ||
		testProduct.Name != prod1.Name ||
		testProduct.Description != prod1.Description {
		t.Error("Product read differ from prod1 saved.")
	}

	testProduct.Read(prod2.ID)
	if testProduct.ID != prod2.ID ||
		testProduct.Name != prod2.Name ||
		testProduct.Description != prod2.Description {
		t.Error("Product read differ from prod2 saved.")
	}

	testOrder := storage.GetOrder(order.ID)
	if testOrder.ID != order.ID ||
		testOrder.OrderCustomer.ID != order.OrderCustomer.ID {
		t.Errorf("Order read differ from order saved.\n"+
			"ID:\n\ttestOrder: %s\n\torder: %s\n\n"+
			"Customer ID:\n\ttestOrder: %s\n\torder: %s",
			testOrder.ID, order.ID,
			testOrder.OrderCustomer.ID, order.OrderCustomer.ID)
	} else {
		item1 := testOrder.Items[0]
		item2 := testOrder.Items[1]

		if item1.ItemProduct.ID == order.Items[1].ItemProduct.ID {
			item1, item2 = item2, item1
		}

		if item1.ItemProduct.ID != order.Items[0].ItemProduct.ID ||
			item1.Quantity != order.Items[0].Quantity ||
			item1.UnitPrice != order.Items[0].UnitPrice {
			t.Error("Item1 read differ from item1 saved.")
		}

		if item2.ItemProduct.ID != order.Items[1].ItemProduct.ID ||
			item2.Quantity != order.Items[1].Quantity ||
			item2.UnitPrice != order.Items[1].UnitPrice {
			t.Error("Item2 read differ from item2 saved.")
		}
	}

	customer.Delete()
	prod1.Delete()
	prod2.Delete()
}