Exemplo n.º 1
0
func TestPersistor(t *testing.T) {
	test_utils.DropAllCollections()

	ptp := "Pete the persistor"

	customProvider := examples.SmurfOrderCustomProvider{}
	for i := 0; i < NumOrders; i++ {
		newOrder, err := order.NewOrder(customProvider)
		newOrder.Custom = &examples.SmurfOrderCustom{
			ResponsibleSmurf: ptp,
		}
		err = newOrder.Upsert()
		if err != nil {
			panic(err)
		}

		if err != nil {
			panic(err)
		}
	}

	orderIter, err := order.Find(&bson.M{"custom.responsiblesmurf": ptp}, customProvider)
	if err != nil {
		panic(err)
	}
	loadedOrders := []*order.Order{}
	for {
		loadedOrder, err := orderIter()
		if loadedOrder != nil {
			loadedOrders = append(loadedOrders, loadedOrder)
		} else {
			break
		}
		if err != nil {
			panic(err)
		}
	}

	t.Log("loaded orders")
	for i, loadedOrder := range loadedOrders {
		t.Log(i, loadedOrder.Custom.(*examples.SmurfOrderCustom).ResponsibleSmurf)
	}

	if len(loadedOrders) != NumOrders {
		t.Fatal("wrong number of orders returned", len(loadedOrders))
	}

	for i, newOrder := range loadedOrders {
		loadedOrder := loadedOrders[i].Custom.(*examples.SmurfOrderCustom)
		if !reflect.DeepEqual(loadedOrder, newOrder.Custom) {
			dump("newOrder", newOrder)
			dump("loadedOrder", loadedOrder)

			t.Fatal("should have been equal", loadedOrder, newOrder)
		}
	}
	//LoadOrder(query *bson.M{}, customOrder interface{})
}
Exemplo n.º 2
0
func ExampleOrderCustom_createCart() {
	// a cart is an incomplete order
	o, _ := order.NewOrder(&examples.SmurfOrderCustomProvider{})
	o.Custom = &examples.SmurfOrderCustom{
		ResponsibleSmurf: "Pete",
	}
	const (
		positionIDA = "awesome-computer-a"
		positionIDB = "awesome-computer-b"
	)

	// add a product
	o.AddPosition(&order.Position{
		ItemID:   positionIDA,
		Name:     "an awesome computer",
		Quantity: 1.0,
		Custom: &examples.SmurfPositionCustom{
			Foo: "foo",
		},
	})

	// set qty
	if o.SetPositionQuantity(positionIDA, 3.01, MOCK_PRICE, MOCK_PRICE, nil) != nil {
		panic("could not set qty")
	}

	// add another position
	o.AddPosition(&order.Position{
		ItemID:   positionIDB,
		Name:     "an awesome computer",
		Quantity: 1.0,
		Custom: &examples.SmurfPositionCustom{
			Foo: "bar",
		},
	})

	o.SetPositionQuantity(positionIDB, 0, MOCK_PRICE, MOCK_PRICE, nil)

	fmt.Println(
		"responsible smurf:",
		o.Custom.(*examples.SmurfOrderCustom).ResponsibleSmurf,
		", position foo:",
		o.Positions[0].Custom.(*examples.SmurfPositionCustom).Foo,
		", qty:",
		o.Positions[0].Quantity,
		", number of positions:",
		len(o.Positions),
	)
	// Output: responsible smurf: Pete , position foo: foo , qty: 3.01 , number of positions: 1
}
Exemplo n.º 3
0
func createMockOrderXY(t *testing.T) (*order.Order, error) {
	orderVo, err := order.NewOrder(nil)
	if err != nil {
		panic(err)
	}
	orderVo.CustomerData.CustomerId = CustomerID1
	var i int
	for _, positionID := range []string{ProductID1SKU1, ProductID1SKU2, ProductID3SKU2} {
		i++
		positionVo := new(order.Position)

		positionVo.ItemID = positionID
		positionVo.Name = positionID
		positionVo.Price = 100 * float64(i)
		positionVo.Quantity = float64(1)

		err := orderVo.AddPosition(positionVo)
		if err != nil {
			panic(err)
		}
	}
	return orderVo, nil
}
Exemplo n.º 4
0
Arquivo: mock.go Projeto: foomo/shop
func MakeMockOrder(smurf string) *order.Order {
	custom := &examples.SmurfOrderCustom{
		ResponsibleSmurf: smurf,
	}
	o, err := order.NewOrder(&examples.SmurfOrderCustomProvider{})
	if err != nil {
		panic(err)
	}
	o.Custom = custom
	for i := 0; i < 5; i++ {
		// add a product
		o.AddPosition(&order.Position{
			ItemID:   "asdf",
			Name:     fmt.Sprintf("an awesome computer - %d", i),
			Quantity: float64(i),
			Custom: &examples.SmurfPositionCustom{
				Foo: fmt.Sprintf("foo - %d", i),
			},
		})

	}
	return o
}