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 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. 3
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. 4
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. 5
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. 6
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. 7
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. 8
0
func TestMongoModelImplementsModel(t *testing.T) {
	model := Model(&MockMongoModel{})
	test.AssertNotEqual(t, model, nil)
}
Esempio n. 9
0
func TestMongoGatewayImplementsDbGateway(t *testing.T) {
	gateway := DbGateway(&MongoGateway{})
	test.AssertNotEqual(t, gateway, nil)
}