Esempio n. 1
0
func TestDeleteAllWhere(t *testing.T) {
	resetDb()

	mock1 := &MockMongoModel{Name: "Lemming"}
	mock2 := &MockMongoModel{Name: "Dodo"}
	mock3 := &MockMongoModel{Name: "Dodo"}
	mock1.Initialize()
	mock2.Initialize()
	mock3.Initialize()
	insertTestFixture(mock1)
	insertTestFixture(mock2)
	insertTestFixture(mock3)

	deleted, err := TestGateway.DeleteAllWhere(&MockMongoModel{}, map[string]interface{}{"name": "Dodo"})
	var notFoundAnimal MockMongoModel
	notFound := TestGateway.NewSession().DB("").C(collectionName(mock2)).FindId(mock2.GetId()).One(&notFoundAnimal)

	var foundAnimal MockMongoModel
	foundErr := TestGateway.NewSession().DB("").C(collectionName(mock1)).FindId(mock1.GetId()).One(&foundAnimal)

	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, deleted, 2)
	test.AssertNotEqual(t, notFound, nil)
	test.AssertEqual(t, notFound.Error(), "not found")

	test.AssertEqual(t, foundErr, nil)
	test.AssertEqual(t, foundAnimal.Name, "Lemming")
}
Esempio n. 2
0
func TestGatewayInitialize(t *testing.T) {
	err := TestGateway.Initialize()
	test.AssertEqual(t, err, nil)

	buildInfo, err := TestGateway.BaseSession.BuildInfo()
	test.AssertEqual(t, err, nil)
	test.AssertTypeMatch(t, buildInfo.Version, "2.6.whatever")
}
Esempio n. 3
0
func TestAppendRoute(t *testing.T) {
	var router Router
	router.appendRoute("POST", "/dogs", mockHandler)

	route := router.Routes[0]
	test.AssertEqual(t, route.Method, "POST")
	test.AssertEqual(t, route.Path, "/dogs/")
}
Esempio n. 4
0
func TestAppendModelRoute(t *testing.T) {
	var router Router
	router.appendModelRoute("FindAll", "mocks", &test.MockModel{})

	findAllRoute := router.ModelRoutes[0]
	test.AssertEqual(t, findAllRoute.Action, "FindAll")
	test.AssertEqual(t, findAllRoute.ModelName, "mocks")
}
Esempio n. 5
0
func TestGet(t *testing.T) {
	var router Router
	router.Get("/test/", mockHandler)

	getRoute := router.Routes[0]
	test.AssertEqual(t, getRoute.Method, "GET")
	test.AssertEqual(t, getRoute.Path, "/test/")
}
Esempio n. 6
0
func TestCreate(t *testing.T) {
	var router Router
	//Create(publicName string, modelInstance model.Model) {
	router.Create("mocks", &test.MockModel{})

	createRoute := router.ModelRoutes[0]
	test.AssertEqual(t, createRoute.Action, "Create")
	test.AssertEqual(t, createRoute.ModelName, "mocks")
}
Esempio n. 7
0
func TestModel(t *testing.T) {
	var router Router
	mock := model.Model(&test.MockModel{7, "Test"})
	router.Model("cat", mock, MethodRules{Allow: []string{"Create"}})

	test.AssertEqual(t, len(router.ModelRoutes), 1)
	createRoute := router.ModelRoutes[0]
	test.AssertEqual(t, createRoute.Action, "Create")
	test.AssertEqual(t, createRoute.ModelName, "cat")
}
Esempio n. 8
0
func TestUpdate(t *testing.T) {
	resetDb()

	mock := &MockMongoModel{Name: "Robert Zimmerman"}
	mock.Initialize()
	insertTestFixture(mock)

	err := TestGateway.Update(mock, map[string]interface{}{"name": "Bob Dylan"})
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, mock.Name, "Bob Dylan")

	foundModel := findMockModel(mock.GetId())
	test.AssertEqual(t, foundModel.Name, "Bob Dylan")
}
Esempio n. 9
0
func TestSave(t *testing.T) {
	resetDb()

	// New Record using save:
	mock := &MockMongoModel{Name: "Casscius Clay"}
	err := TestGateway.Save(mock)
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, mock.Name, "Casscius Clay")
	test.AssertNotEqual(t, mock.GetId(), nil)

	var foundMock MockMongoModel
	err = TestGateway.NewSession().DB("").C(collectionName(mock)).FindId(mock.GetId()).One(&foundMock)
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, mock.GetId(), foundMock.GetId())
	test.AssertEqual(t, foundMock.Name, "Casscius Clay")

	//Existing record using save
	mock.Name = "Muhammud Ali"
	err = TestGateway.Save(mock)
	test.AssertEqual(t, err, nil)

	err = TestGateway.NewSession().DB("").C(collectionName(mock)).FindId(mock.GetId()).One(&foundMock)
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, foundMock.Name, "Muhammud Ali")
}
Esempio n. 10
0
func TestAllowedModelMethods(t *testing.T) {
	var r Router
	ruleSet1 := MethodRules{}
	allowedMethods1 := r.allowedModelMethods(ruleSet1)
	// Order isn't ensured so a deep equivalency test isn't possible
	test.AssertEqual(t, len(allowedMethods1), 9) // TODO: remove hard coded number, get list from api

	ruleSet2 := MethodRules{Allow: []string{"Create"}}
	allowedMethods2 := r.allowedModelMethods(ruleSet2)
	test.AssertEqual(t, allowedMethods2[0], "Create")

	ruleSet3 := MethodRules{Forbid: []string{"Destroy"}}
	allowedMethods3 := r.allowedModelMethods(ruleSet3)
	test.AssertEqual(t, len(allowedMethods3), 8)
}
Esempio n. 11
0
func TestFindById(t *testing.T) {
	resetDb()

	mock := &MockMongoModel{Name: "Fuu"}
	mock.Initialize()
	insertTestFixture(mock)

	newMock := &MockMongoModel{}
	newMock.SetId(mock.GetId())
	err := TestGateway.FindById(newMock)

	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, newMock.GetCreated(), mock.GetCreated())
	test.AssertEqual(t, newMock.Name, "Fuu")
}
Esempio n. 12
0
func TestNewSession(t *testing.T) {
	newSession := TestGateway.NewSession()
	test.AssertTypeMatch(t, newSession, &mgo.Session{})

	err := newSession.Ping()
	test.AssertEqual(t, err, nil)
}
Esempio n. 13
0
func TestFindAll(t *testing.T) {
	resetDb()

	thing1 := &MockMongoModel{Name: "Thing 1"}
	thing2 := &MockMongoModel{Name: "Thing 2"}
	thing1.Initialize()
	thing2.Initialize()
	insertTestFixture(thing1)
	insertTestFixture(thing2)

	modelsInterface, err := TestGateway.FindAll(&MockMongoModel{})
	models := *modelsInterface.(*[]*MockMongoModel)
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, len(models), 2)
	test.AssertTypeMatch(t, models, []*MockMongoModel{thing1, thing2})
}
Esempio n. 14
0
func TestDeleteId(t *testing.T) {
	resetDb()

	mock1 := &MockMongoModel{Name: "Hitler"}
	mock1.Initialize()
	insertTestFixture(mock1)

	err := TestGateway.DeleteById(mock1)

	// Look for something that shouldn't be there
	var foundHitler MockMongoModel
	notFound := TestGateway.NewSession().DB("").C(collectionName(mock1)).FindId(mock1.GetId()).One(&foundHitler)
	test.AssertEqual(t, err, nil)
	test.AssertNotEqual(t, notFound, nil)
	test.AssertEqual(t, notFound.Error(), "not found")
}
Esempio n. 15
0
func TestCreate(t *testing.T) {
	resetDb()
	mock := &MockMongoModel{Name: "Ryan"}
	mock.Initialize()
	err := TestGateway.Create(mock)

	test.AssertEqual(t, err, nil)

	var foundMock MockMongoModel
	err = TestGateway.NewSession().DB("").C(collectionName(mock)).FindId(mock.GetId()).One(&foundMock)
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, mock.GetId(), foundMock.GetId())
	test.AssertEqual(t, foundMock.Name, "Ryan")

	err = TestGateway.Create(mock)
	test.AssertNotEqual(t, err, nil) // Duplicate Index Key Error expected
}
Esempio n. 16
0
func TestExtractPathFromTemplate(t *testing.T) {
	pathTemplate := "/{{.api_prefix}}/{{.model_name}}"
	config := Config{
		"api_prefix": "api",
		"model_name": "user",
	}
	path := extractPathFromTemplate(pathTemplate, config)
	test.AssertEqual(t, path, "/api/user/")
}
Esempio n. 17
0
func TestModelInitialize(t *testing.T) {
	mock := &MockMongoModel{Name: "Mogi"}
	mock.Initialize()

	test.AssertEqual(t, mock.Name, "Mogi")
	test.AssertNotEqual(t, mock.GetId(), nil)
	test.AssertTypeMatch(t, mock.GetId(), bson.NewObjectId())
	test.AssertTypeMatch(t, mock.GetUpdated(), time.Now())
	test.AssertTypeMatch(t, mock.GetCreated(), time.Now())
}
Esempio n. 18
0
func TestSafeFormatPath(t *testing.T) {
	test.AssertEqual(t, safeFormatPath("dogs"), "/dogs/")
	test.AssertEqual(t, safeFormatPath("//dogs"), "/dogs/")
	test.AssertEqual(t, safeFormatPath("//dogs//"), "/dogs/")
	test.AssertEqual(t, safeFormatPath("dog//more/cat"), "/dog/more/cat/")
	test.AssertEqual(t, safeFormatPath("//////dog///more/cat"), "/dog/more/cat/")
	test.AssertEqual(t, safeFormatPath("//////dog///more//cat////"), "/dog/more/cat/")
	test.AssertEqual(t, safeFormatPath(""), "/")
}
Esempio n. 19
0
func TestFindAllBy(t *testing.T) {
	resetDb()

	thing1 := &MockMongoModel{Name: "Thing"}
	thing2 := &MockMongoModel{Name: "Thing"}
	thing3 := &MockMongoModel{Name: "Bob"}
	thing1.Initialize()
	thing2.Initialize()
	thing3.Initialize()
	insertTestFixture(thing1)
	insertTestFixture(thing2)
	insertTestFixture(thing3)

	modelsInterface, err := TestGateway.FindAllBy(&MockMongoModel{}, Query{Conditions: map[string]interface{}{"name": "Thing"}})
	models := *modelsInterface.(*[]*MockMongoModel)
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, len(models), 2)
	test.AssertTypeMatch(t, models, []*MockMongoModel{thing1, thing2})
	// Todo: extend this test to hit order, offset and limit
}
Esempio n. 20
0
func TestConvertToRoutes(t *testing.T) {
	model := &test.MockModel{1, "Test Model"}
	mockModelRoutes := []ModelRoute{
		ModelRoute{"Create", "mocks", model},
		ModelRoute{"Find", "mocks", model},
		ModelRoute{"FindAll", "mocks", model},
	}
	routes := convertToRoutes(mockModelRoutes, mockConfig)
	test.AssertEqual(t, len(routes), len(mockModelRoutes))
	// Todo: this better
}
Esempio n. 21
0
func TestUpdateAll(t *testing.T) {
	resetDb()

	mock1 := &MockMongoModel{Name: "Neo"}
	mock2 := &MockMongoModel{Name: "Morpheus"}
	mock3 := &MockMongoModel{Name: "Trinity"}
	mock1.Initialize()
	mock2.Initialize()
	mock3.Initialize()
	insertTestFixture(mock1)
	insertTestFixture(mock2)
	insertTestFixture(mock3)

	updateCount, err := TestGateway.UpdateAll(&MockMongoModel{}, map[string]interface{}{"$set": map[string]string{"name": "Agent Smith"}})
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, updateCount, 3)

	foundModel := findMockModel(mock1.GetId())
	test.AssertEqual(t, foundModel.Name, "Agent Smith")
}
Esempio n. 22
0
func TestFindBy(t *testing.T) {
	resetDb()

	// Setup
	mock1 := &MockMongoModel{Name: "Trevor"}
	mock2 := &MockMongoModel{Name: "Emma"}
	mock1.Initialize()
	mock2.Initialize()

	insertTestFixture(mock1)
	insertTestFixture(mock2)

	// Find Emma
	emma := &MockMongoModel{}
	err := TestGateway.FindBy(emma, Query{Conditions: map[string]interface{}{"name": "Emma"}})
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, emma.GetId(), mock2.GetId())
	test.AssertEqual(t, emma.Name, "Emma")

	// Find the second mock not named "Joyce" order by name, in this case "Trevor"
	trevor := &MockMongoModel{}
	offset := 1
	qry := Query{
		Conditions: map[string]interface{}{"name": map[string]string{"$ne": "Joyce"}},
		Order:      []string{"name"},
		Offset:     &offset,
	}
	err = TestGateway.FindBy(trevor, qry)
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, trevor.GetId(), mock1.GetId())
	test.AssertEqual(t, trevor.Name, "Trevor")
}
Esempio n. 23
0
func TestDeleteAll(t *testing.T) {
	resetDb()

	mock1 := &MockMongoModel{Name: "Lemming"}
	mock2 := &MockMongoModel{Name: "Dodo"}
	mock3 := &MockMongoModel{Name: "White Rhino"}
	mock1.Initialize()
	mock2.Initialize()
	mock3.Initialize()
	insertTestFixture(mock1)
	insertTestFixture(mock2)
	insertTestFixture(mock3)

	deleted, err := TestGateway.DeleteAll(&MockMongoModel{})
	var foundAnimal MockMongoModel
	notFound := TestGateway.NewSession().DB("").C(collectionName(mock1)).FindId(mock1.GetId()).One(&foundAnimal)

	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, deleted, 3)
	test.AssertNotEqual(t, notFound, nil)
	test.AssertEqual(t, notFound.Error(), "not found")
}
Esempio n. 24
0
func TestDeleteWhere(t *testing.T) {
	resetDb()

	mock1 := &MockMongoModel{Name: "Lemming"}
	mock2 := &MockMongoModel{Name: "Dodo"}
	mock3 := &MockMongoModel{Name: "House Cat"}
	mock1.Initialize()
	mock2.Initialize()
	mock3.Initialize()
	insertTestFixture(mock1)
	insertTestFixture(mock2)
	insertTestFixture(mock3)

	err := TestGateway.DeleteWhere(&MockMongoModel{}, map[string]interface{}{"name": "Dodo"})

	// Look for something that shouldn't be there
	var foundDodo MockMongoModel
	notFound := TestGateway.NewSession().DB("").C(collectionName(mock2)).FindId(mock2.GetId()).One(&foundDodo)
	lemming := findMockModel(mock1.GetId())
	test.AssertEqual(t, err, nil)
	test.AssertEqual(t, notFound.Error(), "not found")
	test.AssertNotEqual(t, lemming, nil)
}
Esempio n. 25
0
func TestCollectionName(t *testing.T) {
	mock := &MockMongoModel{Name: "Ryan"}
	collectionName := collectionName(mock)

	test.AssertEqual(t, collectionName, "mock_mongo_models")
}