Example #1
0
func TestCustomerGetLatestCustomerFromDb(t *testing.T) {
	test_utils.DropAllCollections()
	customer, err := NewCustomer(MOCK_EMAIL, MOCK_PASSWORD, nil)
	if err != nil {
		t.Fatal(err)
	}
	// Perform 3 Upserts
	customer.Person.FirstName = "Foo"
	err = customer.Upsert()
	customer.Person.MiddleName = "Bob"
	err = customer.Upsert()
	customer.Person.LastName = "Bar"
	err = customer.Upsert()
	if err != nil {
		t.Fatal(err)
	}

	//Check if version number is 3
	customer, err = GetCurrentCustomerByIdFromVersionsHistory(customer.GetID(), nil)
	if customer.GetVersion().Current != 3 {
		log.Println("Version is ", customer.GetVersion().Current, "- should have been 3.")
		t.Fail()
	}

}
Example #2
0
// Try to get a customer which is not in the db
func TestCustomerTryRetrieveNonExistent(t *testing.T) {
	test_utils.DropAllCollections()
	_, err := GetCustomerByEmail("*****@*****.**", nil)
	if !shop_error.IsError(err, shop_error.ErrorNotInDatabase) {
		t.Fail()
	}
	log.Println(err)
}
Example #3
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{})
}
Example #4
0
func create2CustomersAndPerformSomeUpserts(t *testing.T) (*Customer, *Customer) {
	test_utils.DropAllCollections()
	customer, err := NewCustomer(MOCK_EMAIL, MOCK_PASSWORD, nil)
	if err != nil {
		t.Fatal(err)
	}
	// Perform 3 Upserts
	customer.Person.FirstName = "Foo"
	err = customer.Upsert()
	customer.Person.MiddleName = "Bob"
	err = customer.Upsert()
	customer.Person.LastName = "Bar"
	err = customer.Upsert()
	address := &address.Address{
		Person: &address.Person{
			Salutation: address.SalutationTypeMr,
			FirstName:  "Foo",
			MiddleName: "Bob",
			LastName:   "Bar",
		},
		Street:       "Holzweg",
		StreetNumber: "5",
		City:         "Bern",
		Country:      "CH",
		ZIP:          "1234",
	}
	err = customer.Upsert()
	// Create a second customer to make the history a little more interesting
	customer2, err := NewCustomer(MOCK_EMAIL2, MOCK_PASSWORD2, nil)
	if err != nil {
		t.Fatal(err)
	}
	customer2.Person.FirstName = "Trent"
	customer2.Upsert()
	customer2.Person.MiddleName = "The"
	customer2.Upsert()
	customer2.Person.LastName = "Second"
	customer2, err = customer2.UpsertAndGetCustomer(nil)
	if err != nil {
		t.Fatal(err)
	}

	_, err = customer.AddAddress(address)
	if err != nil {
		t.Fatal(err)
	}
	err = customer.Upsert()
	if err != nil {
		t.Fatal(err)
	}
	customer.Person.FirstName = "Alice"
	customer.RemoveAddress(customer.GetAddresses()[0].GetID())
	customer, err = customer.UpsertAndGetCustomer(nil)
	if err != nil {
		t.Fatal(err)
	}
	return customer, customer2
}
Example #5
0
func TestCredentials(t *testing.T) {

	test_utils.DropAllCollections()

	// Create credentials for a user
	email := "*****@*****.**"
	password := "******"
	err := CreateCustomerCredentials(email, password)
	if err != nil {
		t.Fatal(err)
	}
	// Create credentials for another user
	email = "*****@*****.**"
	password = "******"
	err = CreateCustomerCredentials(email, password)
	if err != nil {
		t.Fatal(err)
	}
	// Try to create credentials for already taken email.
	// This should fail
	email = "*****@*****.**"
	password = "******"
	err = CreateCustomerCredentials(email, password)
	if err == nil {
		t.Fatal(err)
	}

	// Change email
	err = ChangeEmail("*****@*****.**", "*****@*****.**")
	if err != nil {
		t.Fatal(err)
	}
	// Try to change email that does not exist.
	err = ChangeEmail("*****@*****.**", "*****@*****.**")
	if err == nil {
		t.Fatal(err)
	}

	// Try to change email with incorrect password
	err = ChangePassword("*****@*****.**", "wrong", "myNewPassWord", false)
	if err == nil {
		t.Fatal(err)
	}
	// Try to change email with correct password
	err = ChangePassword("*****@*****.**", "wonderland", "myNewPassWord", false)
	if err != nil {
		t.Fatal(err)
	}
	// Try new Password
	auth, err := CheckLoginCredentials("*****@*****.**", "myNewPassWord")
	if !auth || err != nil {
		t.Fatal(err)
	}

	// Delete Credentials of [email protected]
	err = DeleteCredential("*****@*****.**")
}
Example #6
0
func TestCustomerCreateGuest(t *testing.T) {
	test_utils.DropAllCollections()
	guest, err := NewGuestCustomer(MOCK_EMAIL, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !guest.IsGuest {
		t.Fatal("Expected isGuest to be true, but is false")
	}
}
Example #7
0
func TestSmurfProcessor(t *testing.T) {
	//log.Println("runtime.GOMAXPROCS(16)", runtime.GOMAXPROCS(16))
	test_utils.DropAllCollections()

	const (
		pete = "pete"
		joe  = "joe"
	)

	// add some products in status a
	smurfOrders := map[string]int{
		pete: 1000,
		joe:  2000,
	}

	numberOfOrders := 0
	for smurf, smurfOrderCount := range smurfOrders {
		for i := 0; i < smurfOrderCount; i++ {
			mock.MakeMockOrder(smurf)

			numberOfOrders++
			if numberOfOrders%100 == 0 {
				log.Println(smurf, numberOfOrders)
			}
		}
	}

	log.Println("done writing orders")

	maxConcurrency := 16
	queue := queue.NewQueue()
	joeProcessor := examples.NewSmurfProcessor()
	joeProcessor.SetQuery(&bson.M{"custom.responsiblesmurf": joe})
	joeProcessor.SetMaxConcurrency(maxConcurrency)
	joeProcessor.SetJobsAssigned(smurfOrders[joe])
	peteProcessor := examples.NewSmurfProcessor()
	peteProcessor.SetQuery(&bson.M{"custom.responsiblesmurf": pete})
	peteProcessor.SetMaxConcurrency(maxConcurrency)
	peteProcessor.SetJobsAssigned(smurfOrders[pete])
	queue.AddProcessor(joeProcessor)
	queue.AddProcessor(peteProcessor)

	queue.Start()

	fmt.Println("number of orders:", numberOfOrders, ", processed by joe:", joeProcessor.CountProcessed, ", processed by pete:", peteProcessor.CountProcessed)
	// Output: number of orders: 300 , processed by joe: 2000 , processed by pete: 1000
	if numberOfOrders != smurfOrders["pete"]+smurfOrders["joe"] || joeProcessor.CountProcessed != smurfOrders["joe"] || peteProcessor.CountProcessed != smurfOrders["pete"] {
		t.Fatal("number of orders:", numberOfOrders, ", processed by joe:", joeProcessor.CountProcessed, ", processed by pete:", peteProcessor.CountProcessed)
	}

}
Example #8
0
func TestCredentialsForGuestCustomer(t *testing.T) {

	test_utils.DropAllCollections()
	// Create credentials for a user
	email := "*****@*****.**"
	password := ""
	err := CreateCustomerCredentials(email, password)
	if err != nil {
		t.Fatal(err)
	}
	auth, err := CheckLoginCredentials(email, password)
	if auth || err != nil {
		t.Fatal(err)
	}

}
Example #9
0
func TestCustomerChangeAddress(t *testing.T) {
	test_utils.DropAllCollections()
	customer, err := NewCustomer(MOCK_EMAIL, MOCK_PASSWORD, nil)
	if err != nil {
		t.Fatal(err)
	}

	addr := &address.Address{
		Person: &address.Person{
			Salutation: address.SalutationTypeMr,
			FirstName:  "Foo",
			MiddleName: "Bob",
			LastName:   "Bar",
		},
		Street:       "Holzweg",
		StreetNumber: "5",
		City:         "Bern",
		Country:      "CH",
		ZIP:          "1234",
	}
	log.Println("Original Address:")
	utils.PrintJSON(addr)
	id, err := customer.AddAddress(addr)
	log.Println("Added Address with id ", id)
	if err != nil {
		t.Fatal(err)
	}
	addressNew := &address.Address{
		Id: id, // Set id of address we want to replace
		Person: &address.Person{
			Salutation: address.SalutationTypeMr,
			FirstName:  "FooChanged",
			MiddleName: "Bob",
			LastName:   "Bar",
		},
		Street:       "Steinweg",
		StreetNumber: "5",
		City:         "Bern",
		Country:      "CH",
		ZIP:          "1234",
	}
	err = customer.ChangeAddress(addressNew)
	if err != nil {
		t.Fatal(err)
	}

	changedAddress, err := customer.GetAddressById(id)
	if err != nil {
		t.Fatal(err)
	}
	log.Println("Changed Address:")
	utils.PrintJSON(changedAddress)

	if changedAddress.Street != "Steinweg" {
		t.Fatal("Expected customer.Person.FirstName == \"FooChanged\" but got " + changedAddress.Street)
	}
	if changedAddress.Person.FirstName != "FooChanged" {
		t.Fatal("Expected changedAddress.Person.FirstName == \"FooChanged\" but got " + changedAddress.Person.FirstName)
	}

}
Example #10
0
func TestWatchListsManipulate(t *testing.T) {
	test_utils.DropAllCollections()
	customerID := unique.GetNewID()
	_, err := NewCustomerWatchListsFromCustomerID(customerID)
	if err != nil {
		t.Fatal(err)
	}
	cw, err := GetCustomerWatchListsByCustomerID(customerID)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	// Create List
	listA, err := cw.AddList("TypeX", "ListA", true, "me", utils.GetDateYYYY_MM_DD(), "My awesome list")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	// Add item to list
	err = cw.ListAddItem(listA.Id, "item1", 2)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	// Increase Quantity of item
	err = cw.ListAddItem(listA.Id, "item1", 3)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	// Add another item
	err = cw.ListAddItem(listA.Id, "item2", 2)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	item, err := cw.GetItem(listA.Id, "item1")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.Quantity != 5 {
		utils.PrintJSON(cw)
		t.Fatal("Wrong Quantity, expected 5")
	}
	// Reduce Quantity of item by 1
	err = cw.ListRemoveItem(listA.Id, "item2", 1)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	item, err = cw.GetItem(listA.Id, "item2")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.Quantity != 1 {
		t.Fatal("Wrong Quantity, expected 1")
	}
	// Remove last of item2
	err = cw.ListRemoveItem(listA.Id, "item2", 1)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}

	if len(listA.Items) != 1 {
		utils.PrintJSON(cw)
		t.Fatal("Expected 1 item in ListA")
	}
	newDescription := "new description"
	newName := "newName"
	// Edit list
	_, err = cw.EditList(listA.Id, newName, false, "", "", newDescription)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if listA.Name != newName || listA.Description != newDescription || listA.PublicURIHash != "" {
		utils.PrintJSON(cw)
		t.Fatal("EditList failed")
	}
	// Set item fulfilled
	cw.ListSetItemFulfilled(listA.Id, "item1", 2)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	item, err = cw.GetItem(listA.Id, "item1")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.QtyFulfilled != 2 {
		utils.PrintJSON(cw)
		t.Fatal("Expected QtyFulfilled == 2")
	}

	// Create second CustomerWatchLists and merge
	sessionID := unique.GetNewID()
	_, err = NewCustomerWatchListsFromSessionID(sessionID)
	if err != nil {
		t.Fatal(err)
	}
	cw2, err := GetCustomerWatchListsBySessionID(sessionID)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	listB, err := cw2.AddList("TypeX", "ListB", false, "recipient string", "targetDate string", "watchlist from session")
	if err != nil {
		t.Fatal(err)
	}
	err = cw2.ListAddItem(listB.Id, "item1b", 2)
	if err != nil {
		utils.PrintJSON(cw2)
		t.Fatal(err)
	}
	err = cw2.ListAddItem(listB.Id, "item1", 2)
	if err != nil {
		utils.PrintJSON(cw2)
		t.Fatal(err)
	}

	// Merge ListB from cw2 into ListA from cw
	err = MergeLists(cw2, listB.Id, cw, listA.Id)
	if err != nil {
		t.Fatal(err)
	}
	item, err = cw.GetItem(listA.Id, "item1")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.Quantity != 7 {
		utils.PrintJSON(cw)
		t.Fatal("Expected Quantity == 7")
	}
	item, err = cw2.GetItem(listB.Id, "item1b")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.Quantity != 2 {
		utils.PrintJSON(cw)
		t.Fatal("Expected Quantity == 2")
	}
	utils.PrintJSON(cw)

	// Test Getter
	cw, err = GetCustomerWatchListsByCustomerID(customerID)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}

	// Test for non existant Id
	_, err = GetCustomerWatchListsByCustomerID("InvalidID")
	if err == nil {
		t.Fatal("Expected error not found", err)
	}

	watchList, err := cw2.EditList(listB.Id, "", true, "", "", "")
	if err != nil {
		t.Fatal("Edit List failed", err)
	}

	cw, err = GetCustomerWatchListsByURIHash(watchList.PublicURIHash)
	if err != nil {
		t.Fatal(err)
	}
	watchList, err = cw.GetListByURIHash(watchList.PublicURIHash)
	if err != nil {
		t.Fatal(err)
	}
	utils.PrintJSON(watchList)
}
Example #11
0
func TestEventsSaveShopEvent(t *testing.T) {
	test_utils.DropAllCollections()
	foo()
}