コード例 #1
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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")
}
コード例 #2
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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")
}
コード例 #3
0
ファイル: router_test.go プロジェクト: repp/armadillo
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/")
}
コード例 #4
0
ファイル: router_test.go プロジェクト: repp/armadillo
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")
}
コード例 #5
0
ファイル: router_test.go プロジェクト: repp/armadillo
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/")
}
コード例 #6
0
ファイル: router_test.go プロジェクト: repp/armadillo
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")
}
コード例 #7
0
ファイル: router_test.go プロジェクト: repp/armadillo
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")
}
コード例 #8
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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")
}
コード例 #9
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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")
}
コード例 #10
0
ファイル: router_test.go プロジェクト: repp/armadillo
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)
}
コード例 #11
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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")
}
コード例 #12
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
func TestNewSession(t *testing.T) {
	newSession := TestGateway.NewSession()
	test.AssertTypeMatch(t, newSession, &mgo.Session{})

	err := newSession.Ping()
	test.AssertEqual(t, err, nil)
}
コード例 #13
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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})
}
コード例 #14
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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")
}
コード例 #15
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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
}
コード例 #16
0
ファイル: server_test.go プロジェクト: repp/armadillo
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/")
}
コード例 #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())
}
コード例 #18
0
ファイル: router_test.go プロジェクト: repp/armadillo
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(""), "/")
}
コード例 #19
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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
}
コード例 #20
0
ファイル: server_test.go プロジェクト: repp/armadillo
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
}
コード例 #21
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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")
}
コード例 #22
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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")
}
コード例 #23
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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")
}
コード例 #24
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
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)
}
コード例 #25
0
ファイル: mongo_gateway_test.go プロジェクト: repp/armadillo
func TestCollectionName(t *testing.T) {
	mock := &MockMongoModel{Name: "Ryan"}
	collectionName := collectionName(mock)

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