class DatabaseTestCase(unittest.TestCase): def setUp(self): self.collection = Mock() self.driver = MongoDriver(self.collection) def test_save_retries_on_auto_reconnect(self): self.collection.save.side_effect = [AutoReconnect, None] self.driver.save({}) assert_that(self.collection.save.call_count, is_(2)) def test_save_stops_retrying_after_3_attempts(self): self.collection.save.side_effect = AutoReconnect self.assertRaises(AutoReconnect, self.driver.save, {}) assert_that(self.collection.save.call_count, is_(3)) def test_save_only_calls_once_on_success(self): self.collection.save.return_value = None self.driver.save({}) assert_that(self.collection.save.call_count, is_(1))
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_driver = MongoDriver( MongoClient(HOST, PORT)[DB_NAME][BUCKET]) self.mongo_collection = MongoClient(HOST, PORT)[DB_NAME][BUCKET] self.mongo_collection.drop()
class TestMongoDriver(unittest.TestCase): def setUp(self): self.mongo_driver = MongoDriver( MongoClient(HOST, PORT)[DB_NAME][BUCKET]) self.mongo_collection = MongoClient(HOST, PORT)[DB_NAME][BUCKET] self.mongo_collection.drop() def test_save(self): thing_to_save = {'name': 'test_document'} another_thing_to_save = {'name': '2nd_test_document'} self.mongo_driver.save(thing_to_save) self.mongo_driver.save(another_thing_to_save) results = self.mongo_collection.find() assert_that(results, has_item(thing_to_save)) assert_that(results, has_item(another_thing_to_save)) def test_save_updates_document_with_id(self): a_document = {"_id": "event1", "title": "I'm an event"} updated_document = {"_id": "event1", "title": "I'm another event"} self.mongo_driver.save(a_document) self.mongo_driver.save(updated_document) saved_documents = self.mongo_collection.find() assert_that(saved_documents, only_contains(updated_document)) def test_find(self): self._setup_people() results = self.mongo_driver.find(query={"plays": "guitar"}, sort=["name", "ascending"], limit=None) assert_that( results, contains( has_entries({ "name": "George", "plays": "guitar" }), has_entries({ "name": "John", "plays": "guitar" }), )) def test_find_sort_descending(self): self._setup_people() results = self.mongo_driver.find(query={"plays": "guitar"}, sort=["name", "descending"], limit=None) assert_that( results, contains( has_entries({ "name": "John", "plays": "guitar" }), has_entries({ "name": "George", "plays": "guitar" }), )) def test_find_with_limit(self): self._setup_people() results = self.mongo_driver.find(query={"plays": { "$ne": "guitar" }}, sort=["name", "descending"], limit=1) assert_that(results, contains(has_entries({ "name": "Ringo", "plays": "drums" }))) def test_group(self): self._setup_musical_instruments() results = self.mongo_driver.group(keys=["type"], query={}, collect_fields=[]) assert_that( results, contains_inanyorder( has_entries({ "_count": is_(2), "type": "wind" }), has_entries({ "_count": is_(3), "type": "string" }))) def test_group_with_query(self): self._setup_musical_instruments() results = self.mongo_driver.group(keys=["type"], query={"range": "high"}, collect_fields=[]) assert_that( results, contains_inanyorder( has_entries({ "_count": is_(1), "type": "wind" }), has_entries({ "_count": is_(2), "type": "string" }))) def test_group_and_collect_additional_properties(self): self._setup_musical_instruments() results = self.mongo_driver.group(keys=["type"], query={}, collect_fields=["range"]) assert_that( results, contains( has_entries({ "_count": is_(2), "type": "wind", "range": ["high", "low"] }), has_entries({ "_count": is_(3), "type": "string", "range": ["high", "high", "low"] }))) def test_group_and_collect_with_hyphen_in_field_name(self): self.mongo_collection.save({"type": "foo", "this-name": "bar"}) self.mongo_collection.save({"type": "foo", "this-name": "bar"}) self.mongo_collection.save({"type": "bar", "this-name": "bar"}) self.mongo_collection.save({"type": "bar", "this-name": "foo"}) results = self.mongo_driver.group(keys=["type"], query={}, collect_fields=["this-name"]) assert_that( results, contains( has_entries({ "_count": is_(2), "type": "foo", "this-name": ["bar", "bar"] }), has_entries({ "_count": is_(2), "type": "bar", "this-name": ["bar", "foo"] }))) def test_group_and_collect_with_injection_attempt(self): self.mongo_collection.save({"type": "foo", "this-name": "bar"}) self.mongo_collection.save({"type": "foo", "this-name": "bar"}) self.mongo_collection.save({"type": "bar", "this-name": "bar"}) self.mongo_collection.save({"type": "bar", "this-name": "foo"}) for collect_field in ["name']-foo", "name\\']-foo"]: results = self.mongo_driver.group(keys=["type"], query={}, collect_fields=[collect_field]) assert_that( results, contains(has_entries({ "_count": is_(2), "type": "foo" }), has_entries({ "_count": is_(2), "type": "bar" }))) def test_group_and_collect_with_false_value(self): self.mongo_collection.save({"foo": "one", "bar": False}) self.mongo_collection.save({"foo": "two", "bar": True}) self.mongo_collection.save({"foo": "two", "bar": True}) self.mongo_collection.save({"foo": "one", "bar": False}) results = self.mongo_driver.group(["foo"], {}, ["bar"]) assert_that( results, contains(has_entries({"bar": [False, False]}), has_entries({"bar": [True, True]}))) def test_group_without_keys(self): self._setup_people() results = self.mongo_driver.group(keys=[], query={}, collect_fields=[]) assert_that(results, contains(has_entries({"_count": is_(4)}), )) # this responsibility does not belong here def test_group_ignores_documents_without_grouping_keys(self): self._setup_people() self.mongo_collection.save({"name": "Yoko"}) results = self.mongo_driver.group(keys=["plays"], query={}, collect_fields=[]) assert_that( results, contains( has_entries({ "_count": is_(2), "plays": "guitar" }), has_entries({ "_count": is_(1), "plays": "bass" }), has_entries({ "_count": is_(1), "plays": "drums" }), )) def _setup_people(self): self.mongo_collection.save({"name": "George", "plays": "guitar"}) self.mongo_collection.save({"name": "John", "plays": "guitar"}) self.mongo_collection.save({"name": "Paul", "plays": "bass"}) self.mongo_collection.save({"name": "Ringo", "plays": "drums"}) def _setup_musical_instruments(self): self.mongo_collection.save({ "instrument": "flute", "type": "wind", "range": "high" }) self.mongo_collection.save({ "instrument": "contrabassoon", "type": "wind", "range": "low" }) self.mongo_collection.save({ "instrument": "violin", "type": "string", "range": "high" }) self.mongo_collection.save({ "instrument": "viola", "type": "string", "range": "high" }) self.mongo_collection.save({ "instrument": "cello", "type": "string", "range": "low" })
def setUp(self): self.collection = Mock() self.driver = MongoDriver(self.collection)
def setUp(self): self.mongo_driver = MongoDriver(MongoClient(HOST, PORT)[DB_NAME][BUCKET]) self.mongo_collection = MongoClient(HOST, PORT)[DB_NAME][BUCKET] self.mongo_collection.drop()
class TestMongoDriver(unittest.TestCase): def setUp(self): self.mongo_driver = MongoDriver(MongoClient(HOST, PORT)[DB_NAME][BUCKET]) self.mongo_collection = MongoClient(HOST, PORT)[DB_NAME][BUCKET] self.mongo_collection.drop() def test_save(self): thing_to_save = {'name': 'test_document'} another_thing_to_save = {'name': '2nd_test_document'} self.mongo_driver.save(thing_to_save) self.mongo_driver.save(another_thing_to_save) results = self.mongo_collection.find() assert_that(results, has_item(thing_to_save)) assert_that(results, has_item(another_thing_to_save)) def test_save_updates_document_with_id(self): a_document = {"_id": "event1", "title": "I'm an event"} updated_document = {"_id": "event1", "title": "I'm another event"} self.mongo_driver.save(a_document) self.mongo_driver.save(updated_document) saved_documents = self.mongo_collection.find() assert_that(saved_documents, only_contains(updated_document)) def test_find_one(self): self._setup_people() result = self.mongo_driver.find_one(query={"name": "George"}) assert_that(result, has_entries({ "name": "George", "plays": "guitar" })) def test_find(self): self._setup_people() results = self.mongo_driver.find(query={"plays": "guitar"}, sort=["name", "ascending"], limit=None) assert_that(results, contains( has_entries({"name": "George", "plays": "guitar"}), has_entries({"name": "John", "plays": "guitar"}), )) def test_find_sort_descending(self): self._setup_people() results = self.mongo_driver.find(query={"plays": "guitar"}, sort=["name", "descending"], limit=None) assert_that(results, contains( has_entries({"name": "John", "plays": "guitar"}), has_entries({"name": "George", "plays": "guitar"}), )) def test_find_with_limit(self): self._setup_people() results = self.mongo_driver.find(query={"plays": {"$ne": "guitar"}}, sort=["name", "descending"], limit=1) assert_that(results, contains( has_entries({"name": "Ringo", "plays": "drums"}) )) def test_group(self): self._setup_musical_instruments() results = self.mongo_driver.group(keys=["type"], query={}, collect_fields=[]) assert_that(results, contains_inanyorder( has_entries({"_count": is_(2), "type": "wind"}), has_entries({"_count": is_(3), "type": "string"}) )) def test_group_with_query(self): self._setup_musical_instruments() results = self.mongo_driver.group(keys=["type"], query={"range": "high"}, collect_fields=[]) assert_that(results, contains_inanyorder( has_entries({"_count": is_(1), "type": "wind"}), has_entries({"_count": is_(2), "type": "string"}) )) def test_group_and_collect_additional_properties(self): self._setup_musical_instruments() results = self.mongo_driver.group(keys=["type"], query={}, collect_fields=["range"]) assert_that(results, contains( has_entries( {"_count": is_(2), "type": "wind", "range": ["high", "low"]}), has_entries( {"_count": is_(3), "type": "string", "range": ["high", "high", "low"]}) )) def test_group_and_collect_with_hyphen_in_field_name(self): self.mongo_collection.save({"type": "foo", "this-name": "bar"}) self.mongo_collection.save({"type": "foo", "this-name": "bar"}) self.mongo_collection.save({"type": "bar", "this-name": "bar"}) self.mongo_collection.save({"type": "bar", "this-name": "foo"}) results = self.mongo_driver.group(keys=["type"], query={}, collect_fields=["this-name"]) assert_that(results, contains( has_entries( {"_count": is_(2), "type": "foo", "this-name": ["bar", "bar"]}), has_entries( {"_count": is_(2), "type": "bar", "this-name": ["bar", "foo"]}) )) def test_group_and_collect_with_injection_attempt(self): self.mongo_collection.save({"type": "foo", "this-name": "bar"}) self.mongo_collection.save({"type": "foo", "this-name": "bar"}) self.mongo_collection.save({"type": "bar", "this-name": "bar"}) self.mongo_collection.save({"type": "bar", "this-name": "foo"}) for collect_field in ["name']-foo", "name\\']-foo"]: results = self.mongo_driver.group(keys=["type"], query={}, collect_fields=[collect_field]) assert_that(results, contains( has_entries( {"_count": is_(2), "type": "foo"}), has_entries( {"_count": is_(2), "type": "bar"}) )) def test_group_and_collect_with_false_value(self): self.mongo_collection.save({"foo": "one", "bar": False}) self.mongo_collection.save({"foo": "two", "bar": True}) self.mongo_collection.save({"foo": "two", "bar": True}) self.mongo_collection.save({"foo": "one", "bar": False}) results = self.mongo_driver.group(["foo"], {}, ["bar"]) assert_that(results, contains( has_entries({ "bar": [False, False] }), has_entries({ "bar": [True, True] }) )) def test_group_without_keys(self): self._setup_people() results = self.mongo_driver.group(keys=[], query={}, collect_fields=[]) assert_that(results, contains( has_entries({"_count": is_(4)}), )) # this responsibility does not belong here def test_group_ignores_documents_without_grouping_keys(self): self._setup_people() self.mongo_collection.save({"name": "Yoko"}) results = self.mongo_driver.group(keys=["plays"], query={}, collect_fields=[]) assert_that(results, contains( has_entries({"_count": is_(2), "plays": "guitar"}), has_entries({"_count": is_(1), "plays": "bass"}), has_entries({"_count": is_(1), "plays": "drums"}), )) def _setup_people(self): self.mongo_collection.save({"name": "George", "plays": "guitar"}) self.mongo_collection.save({"name": "John", "plays": "guitar"}) self.mongo_collection.save({"name": "Paul", "plays": "bass"}) self.mongo_collection.save({"name": "Ringo", "plays": "drums"}) def _setup_musical_instruments(self): self.mongo_collection.save( {"instrument": "flute", "type": "wind", "range": "high"}) self.mongo_collection.save( {"instrument": "contrabassoon", "type": "wind", "range": "low"}) self.mongo_collection.save( {"instrument": "violin", "type": "string", "range": "high"}) self.mongo_collection.save( {"instrument": "viola", "type": "string", "range": "high"}) self.mongo_collection.save( {"instrument": "cello", "type": "string", "range": "low"})