def test_sync_account_recipes_from_api_recipe_edited_rating_and_other_fields( mock_categories, user): pa = PaprikaAccountFactory() r1 = RecipeFactory(paprika_account=pa, rating=0) new_rating = 5 new_notes = 'These notes are different' r1.rating = new_rating r1.notes = new_notes r1.hash = str(uuid4()) recipes_api_dict = recipes_to_api_dict([r1]) recipe_api_dict = recipe_to_api_dict(r1) pa.start_sync_recipes() with mock.patch('paprika_sync.core.models.PaprikaAccount.get_recipes', return_value=recipes_api_dict), mock.patch( 'paprika_sync.core.models.PaprikaAccount.get_recipe', return_value=recipe_api_dict): pa.sync_recipes() r1, r2 = Recipe.objects.all().order_by('id') assert NewsItem.objects.all().count() == 2 ni1, ni2 = NewsItem.objects.all().order_by('id') assert ni1.type == NewsItem.TYPE_RECIPE_RATED assert ni2.type == NewsItem.TYPE_RECIPE_EDITED assert ni2.payload['fields_changed'] == ['notes']
def test_sync_account_recipes_from_api_recipe_edited(mock_categories, user): pa = PaprikaAccountFactory() r1 = RecipeFactory(paprika_account=pa) new_name = 'New name' r1.name = new_name r1.hash = str(uuid4()) recipes_api_dict = recipes_to_api_dict([r1]) recipe_api_dict = recipe_to_api_dict(r1) pa.start_sync_recipes() with mock.patch('paprika_sync.core.models.PaprikaAccount.get_recipes', return_value=recipes_api_dict), mock.patch( 'paprika_sync.core.models.PaprikaAccount.get_recipe', return_value=recipe_api_dict): pa.sync_recipes() assert Recipe.objects.all().count() == 2 r1, r2 = Recipe.objects.all().order_by('id') assert r1.date_ended assert r2.date_ended is None assert r1.uid == r2.uid assert r1.name != r2.name assert r2.name == new_name assert NewsItem.objects.all().count() == 1 ni = NewsItem.objects.get() assert ni.type == NewsItem.TYPE_RECIPE_EDITED assert set(ni.payload['fields_changed']) == {'name'} assert ni.payload['recipe'] == r2.id assert ni.payload['previous_recipe'] == r1.id
def test_sync_account_recipes_from_api_no_change(mock_categories, user): pa = PaprikaAccountFactory() r1 = RecipeFactory(paprika_account=pa) recipes_api_dict = recipes_to_api_dict(pa.recipes.all()) recipe_api_dict = recipe_to_api_dict(r1) pa.start_sync_recipes() with mock.patch('paprika_sync.core.models.PaprikaAccount.get_recipes', return_value=recipes_api_dict), mock.patch( 'paprika_sync.core.models.PaprikaAccount.get_recipe', return_value=recipe_api_dict): pa.sync_recipes() assert Recipe.objects.all().count() == 1 assert NewsItem.objects.all().count() == 0
def test_sync_account_recipes_from_api_dont_notify_added_trashed_recipe( mock_categories, user): pa = PaprikaAccountFactory() r1 = RecipeFactory.build(paprika_account=None, in_trash=True) recipes_api_dict = recipes_to_api_dict([r1]) recipe_api_dict = recipe_to_api_dict(r1) pa.start_sync_recipes() with mock.patch('paprika_sync.core.models.PaprikaAccount.get_recipes', return_value=recipes_api_dict), mock.patch( 'paprika_sync.core.models.PaprikaAccount.get_recipe', return_value=recipe_api_dict): pa.sync_recipes() assert Recipe.objects.all().count() == 1 assert Recipe.objects.get().in_trash assert NewsItem.objects.all().count() == 0
def test_sync_account_recipes_from_api_recipe_added(mock_categories, user): pa = PaprikaAccountFactory() r1 = RecipeFactory(paprika_account=pa) r2 = RecipeFactory.build(paprika_account=None) recipes_api_dict = recipes_to_api_dict([r1, r2]) recipe_api_dict = recipe_to_api_dict(r2) pa.start_sync_recipes() with mock.patch('paprika_sync.core.models.PaprikaAccount.get_recipes', return_value=recipes_api_dict), mock.patch( 'paprika_sync.core.models.PaprikaAccount.get_recipe', return_value=recipe_api_dict): pa.sync_recipes() assert Recipe.objects.all().count() == 2 assert Recipe.objects.filter(date_ended__isnull=True).count() == 2 assert NewsItem.objects.all().count() == 1 assert NewsItem.objects.get().type == NewsItem.TYPE_RECIPE_ADDED
def test_sync_account_recipes_from_api_recipe_trashed(mock_categories, user): pa = PaprikaAccountFactory() r1 = RecipeFactory(paprika_account=pa) r1.in_trash = True r1.hash = str(uuid4()) recipes_api_dict = recipes_to_api_dict([r1]) recipe_api_dict = recipe_to_api_dict(r1) pa.start_sync_recipes() with mock.patch('paprika_sync.core.models.PaprikaAccount.get_recipes', return_value=recipes_api_dict), mock.patch( 'paprika_sync.core.models.PaprikaAccount.get_recipe', return_value=recipe_api_dict): pa.sync_recipes() assert Recipe.objects.all().count() == 2 assert Recipe.objects.earliest('id').date_ended is not None assert not Recipe.objects.earliest('id').in_trash assert Recipe.objects.latest('id').date_ended is None assert Recipe.objects.latest('id').in_trash assert NewsItem.objects.all().count() == 1 assert NewsItem.objects.get().type == NewsItem.TYPE_RECIPE_DELETED
def test_sync_account_recipes_from_api_recipe_edited_rating_only( mock_categories, user): pa = PaprikaAccountFactory() r1 = RecipeFactory(paprika_account=pa, rating=0) new_rating = 5 r1.rating = new_rating r1.hash = str(uuid4()) recipes_api_dict = recipes_to_api_dict([r1]) recipe_api_dict = recipe_to_api_dict(r1) pa.start_sync_recipes() with mock.patch('paprika_sync.core.models.PaprikaAccount.get_recipes', return_value=recipes_api_dict), mock.patch( 'paprika_sync.core.models.PaprikaAccount.get_recipe', return_value=recipe_api_dict): pa.sync_recipes() r1, r2 = Recipe.objects.all().order_by('id') assert NewsItem.objects.all().count() == 1 ni = NewsItem.objects.get() assert ni.type == NewsItem.TYPE_RECIPE_RATED assert r1.rating == 0 assert r2.rating == new_rating
def test_sync_recipes_from_api(mock_categories, user): pa = PaprikaAccountFactory(import_sync_status=PaprikaAccount.SYNC_SUCCESS, last_synced=timezone.now() - timezone.timedelta(days=2)) r1 = RecipeFactory(paprika_account=pa) # NOQA r2 = RecipeFactory(paprika_account=pa) r3 = RecipeFactory.build(paprika_account=None) r2.name = 'New name' r2.hash = str(uuid4()) recipes_api_dict = recipes_to_api_dict([r2, r3]) assert Recipe.objects.all().count() == 2 with mock.patch('paprika_sync.core.models.PaprikaAccount.get_recipes', return_value=recipes_api_dict), \ mock.patch('paprika_sync.core.models.PaprikaAccount.get_recipe', side_effect=[recipe_to_api_dict(r2), recipe_to_api_dict(r3)]): call_command('sync_recipes_from_api') assert Recipe.objects.all().count() == 4 # r1 deleted, r2 edited (renamed), r3 added assert NewsItem.objects.all().count() == 3