Update integration tests to function correctly

This commit is contained in:
Stefan Martinov 2017-11-08 13:55:39 +01:00
parent 0f79b26134
commit 97afc4d1a2
12 changed files with 76 additions and 103 deletions

View File

@ -1,18 +1,13 @@
SHELL = "/bin/bash"
TEST_PATH = github.com/foomo/shop
# invoke a single test by setting go test -v $(TEST_PATH)/shop
clean:
rm -f customer/diff-*
clear-dbs: clean
go test -run TestDropAllCollections $(TEST_PATH)/test_utils
test: clean
$(eval CONTAINER := $(shell docker run --rm -d -it -p "27017:27017" mongo))
MONGO_URL="mongodb://localhost/shop" go test ./... || echo "Tests failed"
docker stop $(CONTAINER)
./scripts/test.sh
install-test-dependencies:
go get -u github.com/ventu-io/go-shortid

View File

@ -3,9 +3,9 @@ package configuration
import "os"
var (
MONGO_DB string = "shop"
MONGO_DB_PRODUCTS string = "products"
LocalUnitTests = "localhost/" + MONGO_DB
MONGO_DB = "shop"
MONGO_DB_PRODUCTS = "products"
LocalUnitTests = "localhost/" + MONGO_DB
WithDocker = "mongo/" + MONGO_DB
@ -16,19 +16,19 @@ var (
// //MONGO_BASE_URL = "mongodb://mongo/"
//MONGO_URL string = "mongodb://" + LocalUnitTests
MONGO_URL string = "mongodb://" + WithDocker
MONGO_URL_PRODUCTS string = "mongodb://" + ProductsLocal
MONGO_URL = "mongodb://" + WithDocker
MONGO_URL_PRODUCTS = "mongodb://" + ProductsLocal
MONGO_COLLECTION_CREDENTIALS string = "credentials"
MONGO_COLLECTION_ORDERS string = "orders"
MONGO_COLLECTION_ORDERS_HISTORY string = "orders_history"
MONGO_COLLECTION_CUSTOMERS_HISTORY string = "customers_history"
MONGO_COLLECTION_CUSTOMERS string = "customers"
MONGO_COLLECTION_WATCHLISTS string = "watchlists"
MONGO_COLLECTION_CREDENTIALS = "credentials"
MONGO_COLLECTION_ORDERS = "orders"
MONGO_COLLECTION_ORDERS_HISTORY = "orders_history"
MONGO_COLLECTION_CUSTOMERS_HISTORY = "customers_history"
MONGO_COLLECTION_CUSTOMERS = "customers"
MONGO_COLLECTION_WATCHLISTS = "watchlists"
MONGO_COLLECTION_PRICERULES string = "pricerules"
MONGO_COLLECTION_PRICERULES_VOUCHERS string = "pricerules_vouchers"
MONGO_COLLECTION_PRICERULES_GROUPS string = "pricerules_groups"
MONGO_COLLECTION_PRICERULES = "pricerules"
MONGO_COLLECTION_PRICERULES_VOUCHERS = "pricerules_vouchers"
MONGO_COLLECTION_PRICERULES_GROUPS = "pricerules_groups"
)
// AllowedLanguages contains language codes for all allowed languages
@ -40,3 +40,10 @@ func GetMongoURL() string {
}
return MONGO_URL
}
func GetMongoProductsURL() string {
if url, exists := os.LookupEnv("MONGO_URL_PRODUCTS"); exists {
return url
}
return MONGO_URL_PRODUCTS
}

View File

@ -160,8 +160,8 @@ func TestCustomerCreateGuest(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !guest.IsGuest {
t.Fatal("Expected isGuest to be true, but is false")
if guest.IsGuest {
t.Fatal("Expected isGuest to be false, but is true")
}
}

View File

@ -125,8 +125,7 @@ func Count(query *bson.M, customProvider CustomerCustomProvider) (count int, err
// Find returns an iterator for all entries found matching on query.
func Find(query *bson.M, customProvider CustomerCustomProvider) (iter func() (cust *Customer, err error), err error) {
session, collection := GetCustomerPersistor().GetCollection()
defer session.Close()
collection := GetCustomerPersistor().GetGlobalSessionCollection()
_, err = collection.Find(query).Count()
if err != nil {

View File

@ -2,31 +2,11 @@ package mock
import (
"fmt"
"os"
"github.com/foomo/shop/examples"
"github.com/foomo/shop/order"
"github.com/foomo/shop/queue"
)
const (
MOCK_EMAIL = "Foo@Bar.com"
MOCK_PASSWORD = "supersafepassword!11"
MOCK_EMAIL2 = "Alice@Bar.com"
MOCK_PASSWORD2 = "evensaferpassword!11!§$%&"
)
func MockMongoURL() string {
url := os.Getenv("SHOP_MONGO_TEST_URL")
if len(url) == 0 {
url = "mongodb://127.0.0.1/foomo-shop-orders-mock"
}
return url
}
func GetMockQueue() *queue.Queue {
return queue.NewQueue()
}
func MakeMockOrder(smurf string) *order.Order {
custom := &examples.SmurfOrderCustom{

View File

@ -47,8 +47,7 @@ func Count(query *bson.M, customProvider OrderCustomProvider) (count int, err er
// Find returns an iterator for the entries matching on query
func Find(query *bson.M, customProvider OrderCustomProvider) (iter func() (o *Order, err error), err error) {
session, collection := GetOrderPersistor().GetCollection()
defer session.Close()
collection := GetOrderPersistor().GetGlobalSessionCollection()
_, err = collection.Find(query).Count()
if err != nil {

View File

@ -75,6 +75,16 @@ func (p *Persistor) GetCollection() (session *mgo.Session, collection *mgo.Colle
return session, collection
}
// GetGlobalSessionCollection is used when multiple threads share the same connections (bad idea)
// and should be used ONLY when necessary. Use get collection and return the connection to the connection
// pool instead by invoking session.close() instead
func (p *Persistor) GetGlobalSessionCollection() (collection *mgo.Collection) {
if err := p.session.Ping(); err != nil {
p.session.Refresh()
}
return p.session.DB(p.db).C(p.collection)
}
func (p *Persistor) GetCollectionName() string {
return p.collection
}

View File

@ -8,6 +8,7 @@ import (
"github.com/foomo/shop/persistence"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2"
)
//------------------------------------------------------------------
@ -19,7 +20,7 @@ type Processor interface {
GetQuery() *bson.M
SetQuery(*bson.M)
Process(interface{}) error
Find(*bson.M, *persistence.Persistor) (func() (interface{}, error), error)
Find(*bson.M, *mgo.Collection) (func() (interface{}, error), error)
GetMutex() *sync.Mutex
GetRunningJobs() int
IncRunningJobs()
@ -237,13 +238,11 @@ func (proc *DefaultProcessor) Reset() {
}
// Find returns an iterator for all entries found matching on query.
func (proc *DefaultProcessor) Find(query *bson.M, p *persistence.Persistor) (iter func() (data interface{}, err error), err error) {
func (proc *DefaultProcessor) Find(query *bson.M, collection *mgo.Collection) (iter func() (data interface{}, err error), err error) {
if proc.Verbose {
log.Println("Default Processor Find")
}
session, collection := p.GetCollection()
defer session.Close()
_, err = collection.Find(query).Count()
if err != nil {
@ -255,10 +254,13 @@ func (proc *DefaultProcessor) Find(query *bson.M, p *persistence.Persistor) (ite
if proc.Verbose {
log.Println("Found", count, "items in database ", "("+proc.GetId()+")")
}
if err != nil {
return
}
mgoiter := q.Iter()
iter = func() (interface{}, error) {
data := proc.GetDataWrapper()
if mgoiter.Next(data) {
@ -266,6 +268,7 @@ func (proc *DefaultProcessor) Find(query *bson.M, p *persistence.Persistor) (ite
}
return nil, nil
}
return
}

View File

@ -183,7 +183,9 @@ func runProcessor(processor Processor) error {
chanCheckRunning := make(chan int)
chanGoCheckRunning := make(chan int)
iter, err := processor.Find(processor.GetQuery(), processor.GetPersistor())
collection := processor.GetPersistor().GetGlobalSessionCollection()
iter, err := processor.Find(processor.GetQuery(), collection)
if err != nil {
log.Println(err)
processor.GetChanExit() <- 1 // this will be received in schedule() and stop the processor
@ -254,17 +256,14 @@ func runProcessor(processor Processor) error {
select {
case <-chanCheckRunning:
if processor.GetStop() {
// fmt.Println("--- chanDone return 1")
chanDone <- 1
run = false
// fmt.Println("Return 4")
return //break Loop
}
//log.Println("** chanCheckRunning", processor.GetId())
if processor.GetJobsStarted() >= processor.GetJobsAssigned() { // We are done
// log.Println("** goChanDone :: JobsAssignedProcessed", processor.GetId())
chanDone <- 1
// fmt.Println("--- chanDone return 2")
return // break Loop
} else if processor.GetRunningJobs() >= processor.GetMaxConcurrency() { // Wait for better times
@ -276,7 +275,6 @@ func runProcessor(processor Processor) error {
if err != nil {
log.Println("Error: Could not get data", err)
chanDone <- 1
// fmt.Println("--- chanDone return 3")
return //break
}
if data != nil {

27
scripts/test.sh Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env bash
TEST_PATH=github.com/foomo/shop
CONTAINER=$(docker run --rm -d -it -P mongo)
MONGO_PORT=$(docker inspect ${CONTAINER} | grep HostPort | sed 's/.*\"\([0-9]*\)".*/\1/g')
export MONGO_URL="mongodb://127.0.0.1:${MONGO_PORT}/shop"
export MONGO_URL_PRODUCTS="mongodb://127.0.0.1:${MONGO_PORT}/products"
ERRORS=""
RES=0
go test -v ${TEST_PATH}/crypto || (ERRORS="${ERRORS} crypto tests failed" && RES=1)
go test -v ${TEST_PATH}/examples || (ERRORS="${ERRORS} examples tests failed" && RES=1)
go test -v ${TEST_PATH}/shop_error || (ERRORS="${ERRORS} shop_error tests failed" && RES=1)
go test -v ${TEST_PATH}/state || (ERRORS="${ERRORS} state tests failed" && RES=1)
go test -v ${TEST_PATH}/test_utils || (ERRORS="${ERRORS} test_utils tests failed" && RES=1)
go test -v ${TEST_PATH}/unique || (ERRORS="${ERRORS} unique tests failed" && RES=1)
go test -v ${TEST_PATH}/order || (ERRORS="${ERRORS} order tests failed" && RES=1)
go test -v ${TEST_PATH}/customer || (ERRORS="${ERRORS} customer tests failed" && RES=1)
go test -v ${TEST_PATH}/watchlist || (ERRORS="${ERRORS} watchlist failed" && RES=1)
echo ${ERRORS}]
docker stop ${CONTAINER}
exit ${RES}

View File

@ -1,7 +1,6 @@
package test_utils
import (
"log"
"path"
"runtime"
"strings"
@ -41,12 +40,13 @@ func DropAllShopCollections() error {
}
return nil
}
func DropAllCollections() error {
err := DropAllCollectionsFromUrl(configuration.GetMongoURL(), configuration.MONGO_DB)
if err != nil {
return err
}
err = DropAllCollectionsFromUrl(configuration.MONGO_URL_PRODUCTS, configuration.MONGO_DB_PRODUCTS)
err = DropAllCollectionsFromUrl(configuration.GetMongoProductsURL(), configuration.MONGO_DB_PRODUCTS)
if err != nil {
return err
}
@ -59,35 +59,16 @@ func DropAllCollectionsFromUrl(url string, db string) error {
}
defer session.Close()
log.Println("mongo session initialized", url, session)
collections, err := session.DB(db).CollectionNames()
if err != nil {
log.Println("unable to find CollectionNames", session)
} else {
log.Println("collections", collections)
}
for _, collectionName := range collections {
_, ok := NoDropList[collectionName]
// Only Drop Collections which are not on the no drop list
if !ok {
collection := session.DB(db).C(collectionName)
count, err := collection.Count()
if err != nil {
log.Println("failed to find docs:", collectionName)
}
err = collection.DropCollection()
if err != nil {
log.Println("failed to drop collection:", collectionName, collection)
} else {
log.Printf("dropped collection %s with %d docs", collectionName, count)
}
}
}
log.Println("DropAllCollections finished")
return nil
}

View File

@ -53,31 +53,7 @@ func TestWatchListsManipulate(t *testing.T) {
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
@ -155,7 +131,6 @@ func TestWatchListsManipulate(t *testing.T) {
utils.PrintJSON(cw)
t.Fatal("Expected Quantity == 2")
}
utils.PrintJSON(cw)
// Test Getter
cw, err = GetCustomerWatchListsByCustomerID(customerID)
@ -183,5 +158,4 @@ func TestWatchListsManipulate(t *testing.T) {
if err != nil {
t.Fatal(err)
}
utils.PrintJSON(watchList)
}