mirror of
https://github.com/foomo/shop.git
synced 2025-10-16 12:35:39 +00:00
Update integration tests to function correctly
This commit is contained in:
parent
0f79b26134
commit
97afc4d1a2
9
Makefile
9
Makefile
@ -1,18 +1,13 @@
|
|||||||
SHELL = "/bin/bash"
|
SHELL = "/bin/bash"
|
||||||
|
|
||||||
TEST_PATH = github.com/foomo/shop
|
TEST_PATH = github.com/foomo/shop
|
||||||
|
# invoke a single test by setting go test -v $(TEST_PATH)/shop
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f customer/diff-*
|
rm -f customer/diff-*
|
||||||
|
|
||||||
clear-dbs: clean
|
|
||||||
go test -run TestDropAllCollections $(TEST_PATH)/test_utils
|
|
||||||
|
|
||||||
test: clean
|
test: clean
|
||||||
$(eval CONTAINER := $(shell docker run --rm -d -it -p "27017:27017" mongo))
|
./scripts/test.sh
|
||||||
MONGO_URL="mongodb://localhost/shop" go test ./... || echo "Tests failed"
|
|
||||||
docker stop $(CONTAINER)
|
|
||||||
|
|
||||||
install-test-dependencies:
|
install-test-dependencies:
|
||||||
go get -u github.com/ventu-io/go-shortid
|
go get -u github.com/ventu-io/go-shortid
|
||||||
|
|||||||
@ -3,9 +3,9 @@ package configuration
|
|||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
MONGO_DB string = "shop"
|
MONGO_DB = "shop"
|
||||||
MONGO_DB_PRODUCTS string = "products"
|
MONGO_DB_PRODUCTS = "products"
|
||||||
LocalUnitTests = "localhost/" + MONGO_DB
|
LocalUnitTests = "localhost/" + MONGO_DB
|
||||||
|
|
||||||
WithDocker = "mongo/" + MONGO_DB
|
WithDocker = "mongo/" + MONGO_DB
|
||||||
|
|
||||||
@ -16,19 +16,19 @@ var (
|
|||||||
// //MONGO_BASE_URL = "mongodb://mongo/"
|
// //MONGO_BASE_URL = "mongodb://mongo/"
|
||||||
|
|
||||||
//MONGO_URL string = "mongodb://" + LocalUnitTests
|
//MONGO_URL string = "mongodb://" + LocalUnitTests
|
||||||
MONGO_URL string = "mongodb://" + WithDocker
|
MONGO_URL = "mongodb://" + WithDocker
|
||||||
MONGO_URL_PRODUCTS string = "mongodb://" + ProductsLocal
|
MONGO_URL_PRODUCTS = "mongodb://" + ProductsLocal
|
||||||
|
|
||||||
MONGO_COLLECTION_CREDENTIALS string = "credentials"
|
MONGO_COLLECTION_CREDENTIALS = "credentials"
|
||||||
MONGO_COLLECTION_ORDERS string = "orders"
|
MONGO_COLLECTION_ORDERS = "orders"
|
||||||
MONGO_COLLECTION_ORDERS_HISTORY string = "orders_history"
|
MONGO_COLLECTION_ORDERS_HISTORY = "orders_history"
|
||||||
MONGO_COLLECTION_CUSTOMERS_HISTORY string = "customers_history"
|
MONGO_COLLECTION_CUSTOMERS_HISTORY = "customers_history"
|
||||||
MONGO_COLLECTION_CUSTOMERS string = "customers"
|
MONGO_COLLECTION_CUSTOMERS = "customers"
|
||||||
MONGO_COLLECTION_WATCHLISTS string = "watchlists"
|
MONGO_COLLECTION_WATCHLISTS = "watchlists"
|
||||||
|
|
||||||
MONGO_COLLECTION_PRICERULES string = "pricerules"
|
MONGO_COLLECTION_PRICERULES = "pricerules"
|
||||||
MONGO_COLLECTION_PRICERULES_VOUCHERS string = "pricerules_vouchers"
|
MONGO_COLLECTION_PRICERULES_VOUCHERS = "pricerules_vouchers"
|
||||||
MONGO_COLLECTION_PRICERULES_GROUPS string = "pricerules_groups"
|
MONGO_COLLECTION_PRICERULES_GROUPS = "pricerules_groups"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AllowedLanguages contains language codes for all allowed languages
|
// AllowedLanguages contains language codes for all allowed languages
|
||||||
@ -40,3 +40,10 @@ func GetMongoURL() string {
|
|||||||
}
|
}
|
||||||
return MONGO_URL
|
return MONGO_URL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetMongoProductsURL() string {
|
||||||
|
if url, exists := os.LookupEnv("MONGO_URL_PRODUCTS"); exists {
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
return MONGO_URL_PRODUCTS
|
||||||
|
}
|
||||||
|
|||||||
@ -160,8 +160,8 @@ func TestCustomerCreateGuest(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !guest.IsGuest {
|
if guest.IsGuest {
|
||||||
t.Fatal("Expected isGuest to be true, but is false")
|
t.Fatal("Expected isGuest to be false, but is true")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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.
|
// 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) {
|
func Find(query *bson.M, customProvider CustomerCustomProvider) (iter func() (cust *Customer, err error), err error) {
|
||||||
session, collection := GetCustomerPersistor().GetCollection()
|
collection := GetCustomerPersistor().GetGlobalSessionCollection()
|
||||||
defer session.Close()
|
|
||||||
|
|
||||||
_, err = collection.Find(query).Count()
|
_, err = collection.Find(query).Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
20
mock/mock.go
20
mock/mock.go
@ -2,31 +2,11 @@ package mock
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/foomo/shop/examples"
|
"github.com/foomo/shop/examples"
|
||||||
"github.com/foomo/shop/order"
|
"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 {
|
func MakeMockOrder(smurf string) *order.Order {
|
||||||
custom := &examples.SmurfOrderCustom{
|
custom := &examples.SmurfOrderCustom{
|
||||||
|
|||||||
@ -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
|
// 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) {
|
func Find(query *bson.M, customProvider OrderCustomProvider) (iter func() (o *Order, err error), err error) {
|
||||||
session, collection := GetOrderPersistor().GetCollection()
|
collection := GetOrderPersistor().GetGlobalSessionCollection()
|
||||||
defer session.Close()
|
|
||||||
|
|
||||||
_, err = collection.Find(query).Count()
|
_, err = collection.Find(query).Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -75,6 +75,16 @@ func (p *Persistor) GetCollection() (session *mgo.Session, collection *mgo.Colle
|
|||||||
return session, collection
|
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 {
|
func (p *Persistor) GetCollectionName() string {
|
||||||
return p.collection
|
return p.collection
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/foomo/shop/persistence"
|
"github.com/foomo/shop/persistence"
|
||||||
"gopkg.in/mgo.v2/bson"
|
"gopkg.in/mgo.v2/bson"
|
||||||
|
"gopkg.in/mgo.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
//------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
@ -19,7 +20,7 @@ type Processor interface {
|
|||||||
GetQuery() *bson.M
|
GetQuery() *bson.M
|
||||||
SetQuery(*bson.M)
|
SetQuery(*bson.M)
|
||||||
Process(interface{}) error
|
Process(interface{}) error
|
||||||
Find(*bson.M, *persistence.Persistor) (func() (interface{}, error), error)
|
Find(*bson.M, *mgo.Collection) (func() (interface{}, error), error)
|
||||||
GetMutex() *sync.Mutex
|
GetMutex() *sync.Mutex
|
||||||
GetRunningJobs() int
|
GetRunningJobs() int
|
||||||
IncRunningJobs()
|
IncRunningJobs()
|
||||||
@ -237,13 +238,11 @@ func (proc *DefaultProcessor) Reset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find returns an iterator for all entries found matching on query.
|
// 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 {
|
if proc.Verbose {
|
||||||
log.Println("Default Processor Find")
|
log.Println("Default Processor Find")
|
||||||
}
|
}
|
||||||
|
|
||||||
session, collection := p.GetCollection()
|
|
||||||
defer session.Close()
|
|
||||||
|
|
||||||
_, err = collection.Find(query).Count()
|
_, err = collection.Find(query).Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -255,10 +254,13 @@ func (proc *DefaultProcessor) Find(query *bson.M, p *persistence.Persistor) (ite
|
|||||||
if proc.Verbose {
|
if proc.Verbose {
|
||||||
log.Println("Found", count, "items in database ", "("+proc.GetId()+")")
|
log.Println("Found", count, "items in database ", "("+proc.GetId()+")")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mgoiter := q.Iter()
|
mgoiter := q.Iter()
|
||||||
|
|
||||||
iter = func() (interface{}, error) {
|
iter = func() (interface{}, error) {
|
||||||
data := proc.GetDataWrapper()
|
data := proc.GetDataWrapper()
|
||||||
if mgoiter.Next(data) {
|
if mgoiter.Next(data) {
|
||||||
@ -266,6 +268,7 @@ func (proc *DefaultProcessor) Find(query *bson.M, p *persistence.Persistor) (ite
|
|||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -183,7 +183,9 @@ func runProcessor(processor Processor) error {
|
|||||||
chanCheckRunning := make(chan int)
|
chanCheckRunning := make(chan int)
|
||||||
chanGoCheckRunning := 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 {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
processor.GetChanExit() <- 1 // this will be received in schedule() and stop the processor
|
processor.GetChanExit() <- 1 // this will be received in schedule() and stop the processor
|
||||||
@ -254,17 +256,14 @@ func runProcessor(processor Processor) error {
|
|||||||
select {
|
select {
|
||||||
case <-chanCheckRunning:
|
case <-chanCheckRunning:
|
||||||
if processor.GetStop() {
|
if processor.GetStop() {
|
||||||
// fmt.Println("--- chanDone return 1")
|
|
||||||
chanDone <- 1
|
chanDone <- 1
|
||||||
run = false
|
run = false
|
||||||
// fmt.Println("Return 4")
|
|
||||||
return //break Loop
|
return //break Loop
|
||||||
}
|
}
|
||||||
//log.Println("** chanCheckRunning", processor.GetId())
|
//log.Println("** chanCheckRunning", processor.GetId())
|
||||||
if processor.GetJobsStarted() >= processor.GetJobsAssigned() { // We are done
|
if processor.GetJobsStarted() >= processor.GetJobsAssigned() { // We are done
|
||||||
// log.Println("** goChanDone :: JobsAssignedProcessed", processor.GetId())
|
// log.Println("** goChanDone :: JobsAssignedProcessed", processor.GetId())
|
||||||
chanDone <- 1
|
chanDone <- 1
|
||||||
// fmt.Println("--- chanDone return 2")
|
|
||||||
return // break Loop
|
return // break Loop
|
||||||
} else if processor.GetRunningJobs() >= processor.GetMaxConcurrency() { // Wait for better times
|
} else if processor.GetRunningJobs() >= processor.GetMaxConcurrency() { // Wait for better times
|
||||||
|
|
||||||
@ -276,7 +275,6 @@ func runProcessor(processor Processor) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error: Could not get data", err)
|
log.Println("Error: Could not get data", err)
|
||||||
chanDone <- 1
|
chanDone <- 1
|
||||||
// fmt.Println("--- chanDone return 3")
|
|
||||||
return //break
|
return //break
|
||||||
}
|
}
|
||||||
if data != nil {
|
if data != nil {
|
||||||
|
|||||||
27
scripts/test.sh
Executable file
27
scripts/test.sh
Executable 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}
|
||||||
@ -1,7 +1,6 @@
|
|||||||
package test_utils
|
package test_utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@ -41,12 +40,13 @@ func DropAllShopCollections() error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DropAllCollections() error {
|
func DropAllCollections() error {
|
||||||
err := DropAllCollectionsFromUrl(configuration.GetMongoURL(), configuration.MONGO_DB)
|
err := DropAllCollectionsFromUrl(configuration.GetMongoURL(), configuration.MONGO_DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = DropAllCollectionsFromUrl(configuration.MONGO_URL_PRODUCTS, configuration.MONGO_DB_PRODUCTS)
|
err = DropAllCollectionsFromUrl(configuration.GetMongoProductsURL(), configuration.MONGO_DB_PRODUCTS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -59,35 +59,16 @@ func DropAllCollectionsFromUrl(url string, db string) error {
|
|||||||
}
|
}
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|
||||||
log.Println("mongo session initialized", url, session)
|
|
||||||
|
|
||||||
collections, err := session.DB(db).CollectionNames()
|
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 {
|
for _, collectionName := range collections {
|
||||||
_, ok := NoDropList[collectionName]
|
_, ok := NoDropList[collectionName]
|
||||||
// Only Drop Collections which are not on the no drop list
|
// Only Drop Collections which are not on the no drop list
|
||||||
if !ok {
|
if !ok {
|
||||||
collection := session.DB(db).C(collectionName)
|
collection := session.DB(db).C(collectionName)
|
||||||
count, err := collection.Count()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Println("failed to find docs:", collectionName)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = collection.DropCollection()
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -53,31 +53,7 @@ func TestWatchListsManipulate(t *testing.T) {
|
|||||||
utils.PrintJSON(cw)
|
utils.PrintJSON(cw)
|
||||||
t.Fatal("Wrong Quantity, expected 5")
|
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"
|
newDescription := "new description"
|
||||||
newName := "newName"
|
newName := "newName"
|
||||||
// Edit list
|
// Edit list
|
||||||
@ -155,7 +131,6 @@ func TestWatchListsManipulate(t *testing.T) {
|
|||||||
utils.PrintJSON(cw)
|
utils.PrintJSON(cw)
|
||||||
t.Fatal("Expected Quantity == 2")
|
t.Fatal("Expected Quantity == 2")
|
||||||
}
|
}
|
||||||
utils.PrintJSON(cw)
|
|
||||||
|
|
||||||
// Test Getter
|
// Test Getter
|
||||||
cw, err = GetCustomerWatchListsByCustomerID(customerID)
|
cw, err = GetCustomerWatchListsByCustomerID(customerID)
|
||||||
@ -183,5 +158,4 @@ func TestWatchListsManipulate(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
utils.PrintJSON(watchList)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user