def setUp(self): mongo = MongoDriver(MongoClient(HOST, PORT)[DB_NAME][BUCKET]) self.repo = Repository(mongo) self.mongo_collection = MongoClient(HOST, PORT)[DB_NAME][BUCKET] self.mongo_collection.drop()
def setUp(self): self.mongo = Mock() self.repo = Repository(self.mongo)
class TestRepository(unittest.TestCase): def setUp(self): self.mongo = Mock() self.repo = Repository(self.mongo) @patch('backdrop.core.timeutils.now') def test_save_document_adding_timestamps(self, now): now.return_value = d_tz(2013, 4, 9, 13, 32, 5) self.repo.save({"name": "Gummo"}) self.mongo.save.assert_called_once_with({ "name": "Gummo", "_updated_at": d_tz(2013, 4, 9, 13, 32, 5) }) # ========================= # Tests for repository.find # ========================= def test_find(self): self.mongo.find.return_value = "a_cursor" results = self.repo.find(Query.create(filter_by=[["plays", "guitar"]]), sort=["name", "ascending"]) self.mongo.find.assert_called_once_with({"plays": "guitar"}, ["name", "ascending"], None) assert_that(results, is_("a_cursor")) def test_find_with_descending_sort(self): self.mongo.find.return_value = "a_cursor" results = self.repo.find(Query.create(filter_by=[["plays", "guitar"]]), sort=["name", "descending"]) self.mongo.find.assert_called_once_with({"plays": "guitar"}, ["name", "descending"], None) assert_that(results, is_("a_cursor")) def test_find_with_default_sorting(self): self.mongo.find.return_value = "a_cursor" results = self.repo.find(Query.create(filter_by=[["plays", "guitar"]])) self.mongo.find.assert_called_once_with({"plays": "guitar"}, ["_timestamp", "ascending"], None) assert_that(results, is_("a_cursor")) def test_find_with_limit(self): self.mongo.find.return_value = "a_cursor" results = self.repo.find(Query.create(filter_by=[["plays", "guitar"]]), limit=10) self.mongo.find.assert_called_once_with({"plays": "guitar"}, ["_timestamp", "ascending"], 10) assert_that(results, is_("a_cursor")) def test_sort_raises_error_if_sort_does_not_have_two_elements(self): self.assertRaises(InvalidSortError, self.repo.find, Query.create(), ["a_key"]) def test_sort_raises_error_if_sort_direction_invalid(self): self.assertRaises(InvalidSortError, self.repo.find, Query.create(), ["a_key", "blah"])
class TestRepository(unittest.TestCase): def setUp(self): self.mongo = Mock() self.repo = Repository(self.mongo) @patch('backdrop.core.timeutils.now') def test_save_document_adding_timestamps(self, now): now.return_value = d_tz(2013, 4, 9, 13, 32, 5) self.repo.save({"name": "Gummo"}) self.mongo.save.assert_called_once_with({ "name": "Gummo", "_updated_at": d_tz(2013, 4, 9, 13, 32, 5) }) # ========================= # Tests for repository.find # ========================= def test_find(self): self.mongo.find.return_value = "a_cursor" results = self.repo.find( Query.create(filter_by=[["plays", "guitar"]]), sort= ["name", "ascending"]) self.mongo.find.assert_called_once_with({"plays": "guitar"}, ["name", "ascending"], None) assert_that(results, is_("a_cursor")) def test_find_with_descending_sort(self): self.mongo.find.return_value = "a_cursor" results = self.repo.find( Query.create(filter_by=[["plays", "guitar"]]), sort= ["name", "descending"]) self.mongo.find.assert_called_once_with({"plays": "guitar"}, ["name", "descending"], None) assert_that(results, is_("a_cursor")) def test_find_with_default_sorting(self): self.mongo.find.return_value = "a_cursor" results = self.repo.find( Query.create(filter_by=[["plays", "guitar"]])) self.mongo.find.assert_called_once_with({"plays": "guitar"}, ["_timestamp", "ascending"], None) assert_that(results, is_("a_cursor")) def test_find_with_limit(self): self.mongo.find.return_value = "a_cursor" results = self.repo.find( Query.create(filter_by=[["plays", "guitar"]]), limit=10) self.mongo.find.assert_called_once_with({"plays": "guitar"}, ["_timestamp", "ascending"], 10) assert_that(results, is_("a_cursor")) def test_sort_raises_error_if_sort_does_not_have_two_elements(self): self.assertRaises( InvalidSortError, self.repo.find, Query.create(), ["a_key"] ) def test_sort_raises_error_if_sort_direction_invalid(self): self.assertRaises( InvalidSortError, self.repo.find, Query.create(), ["a_key", "blah"] )