def test_subscriber_is_notified_on_delete(): data_source = MongoDataSource(User) user = User(name='Anders', age=12) user.save() notified = { 'notified_data_source': None, 'notified_entity_id': None } def notify(data_source, entity): notified['notified_data_source'] = data_source notified['notified_entity_id'] = entity.id data_source.subscribe(notify) user.delete() assert notified == { 'notified_data_source': data_source, 'notified_entity_id': user.id } data_source.disconnect()
def test_serialization(): data_source = MongoDataSource(User) user = User(name='Anders', age=12) user.save() assert data_source.serialize(user) == str(user.id) data_source.disconnect()
def test_finding_record(): data_source = MongoDataSource(User) user = User(name='Anders', age=12) user.save() assert data_source.find(str(user.id)) == user data_source.disconnect()
def test_subscriber_is_not_notified_when_bulk_inserting_without_load(): data_source = MongoDataSource(User) user1 = User(name='Anders', age=12) user2 = User(name='Henrik', age=50) notified = False def notify(data_source, entity): notified = True # NOQA data_source.subscribe(notify) User.objects.insert([user1, user2], load_bulk=False) assert notified == False data_source.disconnect()
def test_awesome_stuff(relation_detector): # NOQA users = [User(name='Anders', age=12) for _ in range(1)] User.objects.insert(users) handin = Handin(score=1, users=users[0]) handin.save() assert score() == 50
def test_raising_exceptions_on_missing_subscriptions(relation_detector): # NOQA users = [User(name='Anders', age=12) for _ in range(1)] inserted_users = User.objects.insert(users) handin = Handin(score=1, users=inserted_users) handin.save() with pytest.raises(RelationMissingError): with relation_detector(score_with_missing_relations): assert score_with_missing_relations() == 50
def test_no_exceptions_raised_with_all_subscriptions(relation_detector): # NOQA users = [User(name='Anders', age=12) for _ in range(1)] inserted_users = User.objects.insert(users) handin = Handin(score=1, users=inserted_users) handin.save() with relation_detector(score_with_sources): assert score_with_sources() == 50 with relation_detector(score_with_relations): assert score_with_relations() == 50
def test_collection_whide_invalidation(): user = User(name='Anders', age=12) user.save() assert score() == 50 assert smache.is_fun_fresh(score) == True user.name = 'Emil' user.save() assert smache.is_fun_fresh(score) == False
def test_depending_computed_are_invalidated_on_save(): user = User(name='Anders', age=12) user.save() assert name(user) == 'Anders' assert smache.is_fun_fresh(name, user) == True user.name = 'Emil' user.save() assert smache.is_fun_fresh(name, user) == False
def test_subscriber_is_notified_on_bulk_insert_with_load(): data_source = MongoDataSource(User) user1 = User(name='Anders', age=12) user2 = User(name='Henrik', age=50) notified = {'notified_data_sources': [], 'notified_entity_ids': []} def notify(data_source, entity): notified['notified_data_sources'].append(data_source) notified['notified_entity_ids'].append(str(entity.id)) data_source.subscribe(notify) users = User.objects.insert([user1, user2]) user_ids = [str(user.id) for user in users] assert notified == { 'notified_data_sources': [data_source, data_source], 'notified_entity_ids': user_ids } data_source.disconnect()
def test_subscriber_is_notified_on_update(): data_source = MongoDataSource(User) user = User(name='Anders', age=12) user.save() notified = {'notified_data_source': None, 'notified_entity_id': None} def notify(data_source, entity): notified['notified_data_source'] = data_source notified['notified_entity_id'] = entity.id data_source.subscribe(notify) user.name = 'Emil' user.save() assert notified == { 'notified_data_source': data_source, 'notified_entity_id': user.id } data_source.disconnect()
def test_data_source_for_entity(): data_source = MongoDataSource(User) assert data_source.for_entity(User()) == True assert data_source.for_entity(Handin()) == False