def test__update_db_cache_should_copy_data_to_data_in_db(self): """ Test Method: """ # Given I've got a doc model doc = FakeDoc() # And I know it has some data that is not stored in db (and not cached as stored in db) doc._data = Map({random_str(): random_str() for _ in range(5)}) # When I call doc._update_db_cache() # Then I expect self.assertEqual(doc._data, doc._data_in_db)
def test__set_should_add_key_and_value_when_key_does_not_exist_yet(self): """ Test Method: """ # Given I've got a doc model doc = FakeDoc() # And I know the doc has no data doc._data.clear() # And I've got key = random_str() value = random_str() # When I call doc._set(key, value) # Then I expect self.assertEqual(value, doc._data[key])
def test__set_should_do_nothing_when_given_value_matches_the_existing_one( self): """ Test Method: """ # Given I've got a doc model doc = FakeDoc() # And I know the doc has some data key = random_str() value = random_str() doc._data[key] = value # When I call doc._set(key, value) # Then I expect self.assertEqual(value, doc._data[key])
def test__set_should_update_value_when_is_different_than_exiting_value( self): """ Test Method: """ # Given I've got a doc model doc = FakeDoc() # And I know the doc has some data key = random_str() value = random_str() doc._data[key] = value new_value = random_str() # When I call doc._set(key, new_value) # Then I expect self.assertEqual(new_value, doc._data[key])
def test__to_map_should_return_a_map_with_the_given_keys(self): """ Test Method: """ # Given I've got a doc model doc = FakeDoc() # And It has some data data = {'a': random_str(), 'b': random_str(), 'c': random_str()} doc._data = data # When I call result = doc._to_map('a', 'c', 'F') # Then I expect expected = Map({ '_id': None, 'created': None, 'modified': None, 'a': data['a'], 'c': data['c'], 'F': None }) self.assertEqual(expected, result)
def test_saving_an_existing_doc_should_raise_because_it_is_not_supported( self, mock__insert, mock__update, mock__update_db_cache): """ Test Method: """ # Given I've got an existing doc instance doc = FakeDoc() doc._data_in_db._id = random_str() # I expect it to raise with self.assertRaises(RuntimeError): # When I call result = doc.save() # Then I expect mock__insert.assert_not_called() mock__update_db_cache.assert_not_called()
def test__load_args_should_set_given_data_and_update_cache_when_doc_is_created( self, mock__set, mock__update_db_cache): """ Test Method: """ # Given I've got a new doc model doc = FakeDoc() # And I've got a a raw doc raw_doc = { '_id': random_str(), 'created': random_datetime(), 'modified': random_datetime() } # And I want to set the doc as created in db is_created = True # When I call doc._load_args(raw_doc, is_created) # Then I expect calls = [call(key, raw_doc[key]) for key in raw_doc] mock__set.assert_has_calls(calls) mock__update_db_cache.assert_called_once()
def test__load_args_should_set_given_data_and_not_update_cache_when_doc_is_not_created( self, mock__set, mock__update_db_cache): """ Test Method: """ # Given I've got a new doc model doc = FakeDoc() # And I've got a a raw doc raw_doc = { '_id': random_str(), 'created': random_datetime(), 'modified': random_datetime() } # And I don't won't to treat it as a document that exists in db yet is_created = False # When I call doc._load_args(raw_doc, is_created) # Then I expect calls = [call(key, raw_doc[key]) for key in raw_doc] mock__set.assert_has_calls(calls) mock__update_db_cache.assert_not_called()