def test_safety_level_str(self): photo = PhotoFactory(safety_level=0) self.assertEqual(photo.safety_level_str, 'None') photo.safety_level = 2 self.assertEqual(photo.safety_level_str, 'Moderate') photo.safety_level = 99 self.assertIsNone(photo.safety_level_str)
def test_public_manager_recent(self): "The public manager ONLY includes public photos." public_photo = PhotoFactory(is_private=False) private_photo = PhotoFactory(is_private=True) photos = Photo.public_objects.all() self.assertEqual(len(photos), 1) self.assertEqual(photos[0], public_photo)
def test_public_photos(self): "public_photos() should only return public photos." public_photo = PhotoFactory(is_private=False) private_photo = PhotoFactory(is_private=True) photoset = PhotosetFactory() photoset.photos.add(public_photo, private_photo) self.assertEqual(len(photoset.public_photos()), 1) self.assertEqual(photoset.public_photos()[0], public_photo)
def createDogPhoto(self): "Creates a photo tagged with 'dog' and 'mammal'." self.dog_photo = PhotoFactory(title='Dog') mammal_tag = TagFactory(slug='mammal') dog_tag = TagFactory(slug='dog') taggedphoto_5 = TaggedPhotoFactory(content_object=self.dog_photo, tag=mammal_tag) taggedphoto_6 = TaggedPhotoFactory(content_object=self.dog_photo, tag=dog_tag)
def test_user_detail_privacy(self): "It doesn't show private photos." user = UserFactory() public_photo = PhotoFactory(user=user) private_photo = PhotoFactory(user=user, is_private=True) response = self.client.get( reverse('flickr:user_detail', kwargs={'nsid': user.nsid})) self.assertIn('photo_list', response.context) self.assertEqual(len(response.context['photo_list']), 1) self.assertIn(public_photo, response.context['photo_list'])
def test_photo_ordering(self): "Should return photos in the correct order." photo_1 = PhotoFactory() photo_2 = PhotoFactory() photo_3 = PhotoFactory() photoset = PhotosetFactory() photoset.photos.add(photo_2, photo_1, photo_3) photos = photoset.photos.all() self.assertEqual(photos[0], photo_2) self.assertEqual(photos[1], photo_1) self.assertEqual(photos[2], photo_3)
def setUp(self): user_1 = UserFactory(nsid='1234567890@N01') user_2 = UserFactory(nsid='9876543210@N01') account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) self.photos_1 = PhotoFactory.create_batch(2, user=user_1) self.photos_2 = PhotoFactory.create_batch(3, user=user_2) self.private_photo = PhotoFactory(user=user_1, is_private=True) # For comparing with the results: self.public_pks = [p.pk for p in self.photos_1] + \ [p.pk for p in self.photos_2]
def test_public_photos_manager(self): "Returns ONLY public photos, ONLY by an Account." user = UserFactory() account = AccountFactory(user=user) public_photo = PhotoFactory(is_private=False) public_photo_by_account = PhotoFactory(is_private=False, user=user) private_photo = PhotoFactory(is_private=True) private_photo_by_account = PhotoFactory(is_private=True, user=user) photos = Photo.public_photo_objects.all() self.assertEqual(len(photos), 1) self.assertEqual(photos[0], public_photo_by_account)
def test_photos_manager(self): "Returns public AND private photos ONLY by an Account." user = UserFactory() account = AccountFactory(user=user) public_photo = PhotoFactory(is_private=False) public_photo_by_account = PhotoFactory(is_private=False, user=user) private_photo = PhotoFactory(is_private=True) private_photo_by_account = PhotoFactory(is_private=True, user=user) photos = Photo.photo_objects.all() self.assertEqual(len(photos), 2) self.assertIn(public_photo_by_account, photos) self.assertIn(private_photo_by_account, photos)
def test_home_ordering_posted(self): "By default, photos should be in reverse-post_time order." dt = datetime_now() ac = AccountFactory() photo_1 = PhotoFactory(user=ac.user, post_time=dt - timedelta(days=1)) photo_3 = PhotoFactory(user=ac.user, post_time=dt - timedelta(days=3)) photo_2 = PhotoFactory(user=ac.user, post_time=dt - timedelta(days=2)) response = self.client.get(reverse('flickr:home')) pl = response.context['photo_list'] self.assertEqual(pl[0].pk, photo_1.pk) self.assertEqual(pl[1].pk, photo_2.pk) self.assertEqual(pl[2].pk, photo_3.pk)
def test_data(self): "Check format of a single data element." user = FlickrUserFactory(nsid='35034346050@N01') PhotoFactory.create_batch(3, user=user, post_time=make_datetime('2018-01-01 12:00:00')) result = FlickrGenerator(nsid='35034346050@N01').get_photos_per_year() self.assertIn('data', result) self.assertEqual(len(result['data']), 1) self.assertEqual(result['data'][0]['label'], '2018') self.assertEqual(result['data'][0]['value'], 3)
def test_user_detail_ordering_posted(self): "By default, photos should be in reverse-post_time order." dt = datetime_now() user = UserFactory() ac = AccountFactory(user=user) photo_1 = PhotoFactory(user=user, post_time=dt - timedelta(days=1)) photo_3 = PhotoFactory(user=user, post_time=dt - timedelta(days=3)) photo_2 = PhotoFactory(user=user, post_time=dt - timedelta(days=2)) response = self.client.get( reverse('flickr:user_detail', kwargs={'nsid': user.nsid})) pl = response.context['photo_list'] self.assertEqual(pl[0].pk, photo_1.pk) self.assertEqual(pl[1].pk, photo_2.pk) self.assertEqual(pl[2].pk, photo_3.pk)
def test_ordering(self): "Latest photo (uploaded) should come first by default." photo_1 = PhotoFactory( title='Earliest', post_time=datetime.datetime.strptime( '2016-04-07 12:00:00', '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.utc)) photo_2 = PhotoFactory( title='Latest', post_time=datetime.datetime.strptime( '2016-04-08 12:00:00', '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.utc)) photos = Photo.objects.all() self.assertEqual(photos[0].title, 'Latest') self.assertEqual(photos[1].title, 'Earliest')
def test_home_context(self): "The Flickr home page sends the correct data to templates" accounts = AccountFactory.create_batch(3) photos_1 = PhotoFactory.create_batch(2, user=accounts[0].user) photos_2 = PhotoFactory.create_batch(2, user=accounts[1].user) response = self.client.get(reverse('flickr:home')) self.assertIn('account_list', response.context) self.assertIn('photo_list', response.context) self.assertEqual(len(response.context['photo_list']), 4) self.assertIn('order', response.context) self.assertEqual(response.context['order'], 'uploaded') # Three accounts, only two of which have Photos: self.assertEqual( [account.pk for account in response.context['account_list']], [1, 2, 3])
def test_home_ordering_taken(self): """With ?order=taken, photos should be in reverse-taken_time order. And Photos with taken_unknown=True should not appear.""" dt = datetime_now() ac = AccountFactory() photo_1 = PhotoFactory(user=ac.user, taken_time=dt - timedelta(days=1)) photo_3 = PhotoFactory(user=ac.user, taken_time=dt - timedelta(days=3)) photo_2 = PhotoFactory(user=ac.user, taken_time=dt - timedelta(days=2)) photo_4 = PhotoFactory(user=ac.user, taken_unknown=True) response = self.client.get(reverse('flickr:home') + '?order=taken') pl = response.context['photo_list'] self.assertEqual(len(pl), 3) self.assertEqual(pl[0].pk, photo_1.pk) self.assertEqual(pl[1].pk, photo_2.pk) self.assertEqual(pl[2].pk, photo_3.pk)
def test_syncs_tags(self): """If a photo already has some tags, and we save_tags, the final set of tags should match the new set of tags, deleting unused relationships.""" photo_info_data = self.load_fixture('photos.getInfo')['photo'] user_1 = UserFactory(nsid="35034346050@N01") user_2 = UserFactory(nsid="12345678901@N01") photo = PhotoFactory(user=user_1) # Create an initial tag on the photo. tag = Tag.objects.create(slug='initial', name='Initial') tagged_photo = Photo.tags.through( flickr_id='12345', author=user_1, content_object=photo, tag=tag) tagged_photo.save() tag_slugs = photo.tags.slugs() # Check starting state: self.assertIn('initial', tag_slugs) self.assertEqual(len(tag_slugs), 1) # Save new tags: PhotoSaver()._save_tags(photo, photo_info_data['tags']['tag']) tag_slugs = photo.tags.slugs() # Check that first tag has gone and the rest are there: self.assertNotIn('initial', tag_slugs) self.assertEqual(len(tag_slugs), 7)
def test_creates_tags(self): """Saving tags should create all the tags and tagged_photos' data.""" photo_info_data = self.load_fixture('photos.getInfo')['photo'] user_1 = UserFactory(nsid="35034346050@N01") user_2 = UserFactory(nsid="12345678901@N01") photo = PhotoFactory(user=user_1) PhotoSaver()._save_tags(photo, photo_info_data['tags']['tag']) tags = photo.tags.all() tagged_photos = TaggedPhoto.objects.filter(content_object=photo) # Check we've saved both tags and the through model objects. self.assertEqual(len(tags), 7) self.assertEqual(len(tagged_photos), 7) # Check we've saved all the stuff on the through model correctly. for tp in tagged_photos: if tp.tag.slug == 'abbeydore': self.assertEqual(tp.tag.name, 'Abbey Dore') self.assertFalse(tp.machine_tag) self.assertEqual(tp.author, user_2) self.assertEqual(tp.flickr_id, '5827-26069027966-1200699') else: self.assertEqual(tp.author, user_1) if tp.tag.slug == 'tester:foo=bar': self.assertTrue(tp.machine_tag)
def test_location_str(self): photo = PhotoFactory(locality_name='Abbey Dore', county_name='', region_name='England', country_name='United Kingdom') self.assertEqual(photo.location_str, 'Abbey Dore, England, United Kingdom')
def test_home_context(self): "The Flickr home page sends the correct data to templates" accounts = AccountFactory.create_batch(3) photos_1 = PhotoFactory.create_batch(2, user=accounts[0].user) photos_2 = PhotoFactory.create_batch(2, user=accounts[1].user) response = self.client.get(reverse('flickr:home')) self.assertIn('account_list', response.context) self.assertIn('photo_list', response.context) self.assertEqual(len(response.context['photo_list']), 4) self.assertIn('order', response.context) self.assertEqual(response.context['order'], 'uploaded') # Three accounts, only two of which have Photos: self.assertEqual( [account.pk for account in response.context['account_list']], [1,2,3] )
def test_medium_url(self): "Has a different format to most other image sizes." photo = PhotoFactory(farm=3, server='1234', flickr_id=4567, secret='9876') self.assertEqual(photo.medium_url, 'https://farm3.static.flickr.com/1234/4567_9876.jpg')
def test_photo_detail_privacy(self): "It does not show private Photos" user = UserFactory() photo = PhotoFactory(is_private=True) response = self.client.get(reverse('flickr:photo_detail', kwargs={'nsid': user.nsid, 'flickr_id': photo.flickr_id})) self.assertEqual(response.status_code, 404)
def test_adds_primary_photo(self): "If the primary photo is in the DB, it's set." photo = PhotoFactory(flickr_id=24990464004) photoset_data = self.make_photoset_data() photoset = self.make_photoset_object( photoset_data ) self.assertEqual(photoset.primary_photo, photo)
def createDogPhoto(self): "Creates a photo tagged with 'dog' and 'mammal'." self.dog_photo = PhotoFactory(title='Dog') mammal_tag = TagFactory(slug='mammal') dog_tag = TagFactory(slug='dog') taggedphoto_5 = TaggedPhotoFactory( content_object=self.dog_photo, tag=mammal_tag) taggedphoto_6 = TaggedPhotoFactory( content_object=self.dog_photo, tag=dog_tag)
def setUp(self): user_1 = UserFactory(nsid='1234567890@N01') user_2 = UserFactory(nsid='9876543210@N01') account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) self.photos_1 = PhotoFactory.create_batch(2, user=user_1) self.photos_2 = PhotoFactory.create_batch(3, user=user_2) post_time = datetime.datetime(2015, 3, 18, 12, 0, 0).replace(tzinfo=pytz.utc) self.photos_1[0].post_time = post_time self.photos_1[0].save() self.photos_2[1].post_time = post_time + datetime.timedelta(hours=1) self.photos_2[1].save() self.private_photo = PhotoFactory(user=user_1, is_private=True, post_time=post_time)
def test_photo_detail_404(self): "Should 404 with mis-matched user and photo IDs" response = self.client.get( reverse('flickr:photo_detail', kwargs={ 'nsid': UserFactory().nsid, 'flickr_id': PhotoFactory().flickr_id })) self.assertEqual(response.status_code, 404)
def test_home_privacy(self): "Only public Photos should appear." # We only display Photos from Accounts, so add some. user_1 = UserFactory() user_2 = UserFactory() account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) public_photo_1 = PhotoFactory(user=user_1) private_photo_1 = PhotoFactory(user=user_1, is_private=True) public_photo_2 = PhotoFactory(user=user_2) private_photo_2 = PhotoFactory(user=user_2, is_private=True) response = self.client.get(reverse('flickr:home')) photos = response.context['photo_list'] self.assertEqual(len(photos), 2) self.assertEqual(photos[0].pk, public_photo_1.pk) self.assertEqual(photos[1].pk, public_photo_2.pk)
def setUp(self): self.user_1 = UserFactory(nsid='1234567890@N01') self.user_2 = UserFactory(nsid='9876543210@N01') AccountFactory(user=self.user_1) AccountFactory(user=self.user_2) # Photos taken in 2015 and 2016 for user 1: PhotoFactory.create_batch(3, taken_time=datetime_from_str('2015-01-01 12:00:00'), user=self.user_1) PhotoFactory.create_batch(2, taken_time=datetime_from_str('2016-01-01 12:00:00'), user=self.user_1) # And one for user_2 taken in 2015: PhotoFactory(user=self.user_2, taken_time=datetime_from_str('2015-01-01 12:00:00')) # And one private photo for user_1 taken in 2015: PhotoFactory(user=self.user_1, is_private=True, taken_time=datetime_from_str('2015-01-01 12:00:00'))
def test_empty_years(self): "It should include years for which there are no photos." # Add a photo in 2018, leaving a gap for 2017: PhotoFactory(taken_time=datetime_from_str('2018-01-01 12:00:00'), user=self.user_1) photos = ditto_flickr.annual_photo_counts(count_by='taken_time') self.assertEqual(len(photos), 4) self.assertEqual(photos[2]['year'], 2017) self.assertEqual(photos[2]['count'], 0)
class PhotoNextPrevTestCase(TestCase): def setUp(self): user = UserFactory() account = AccountFactory(user=user) self.photo_1 = PhotoFactory( user=user, post_time=datetime.datetime.strptime( '2016-04-08 12:00:00', '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.utc)) self.private_photo = PhotoFactory( user=user, is_private=True, post_time=datetime.datetime.strptime( '2016-04-09 12:00:00', '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.utc)) # Photo by a different user: user_2 = UserFactory() account_2 = AccountFactory(user=user_2) self.other_photo = PhotoFactory( user=user_2, post_time=datetime.datetime.strptime( '2016-04-10 12:00:00', '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.utc)) self.photo_2 = PhotoFactory( user=user, post_time=datetime.datetime.strptime( '2016-04-11 12:00:00', '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.utc)) def test_next_public_by_post_time(self): self.assertEqual(self.photo_1.get_next_public_by_post_time(), self.photo_2) def test_next_public_by_post_time_none(self): self.assertIsNone(self.photo_2.get_next_public_by_post_time()) def test_previous_public_by_post_time(self): self.assertEqual(self.photo_2.get_previous_public_by_post_time(), self.photo_1) def test_previous_public_by_post_time_none(self): self.assertIsNone(self.photo_1.get_previous_public_by_post_time()) def test_next(self): self.assertEqual(self.photo_1.get_next(), self.photo_2) def test_next_none(self): self.assertIsNone(self.photo_2.get_next()) def test_previous(self): self.assertEqual(self.photo_2.get_previous(), self.photo_1) def test_previous_none(self): self.assertIsNone(self.photo_1.get_previous())
def setUp(self): """ Creates two photos sharing three tags: 1 has 'fish' and 'carp' tags. 2 has 'fish' and 'cod' tags. """ self.carp_photo = PhotoFactory(title='Carp') self.cod_photo = PhotoFactory(title='Cod') fish_tag = TagFactory(slug='fish', name='Fish') carp_tag = TagFactory(slug='carp', name='Carp') cod_tag = TagFactory(slug='cod', name='Cod') taggedphoto_1 = TaggedPhotoFactory(content_object=self.carp_photo, tag=fish_tag) taggedphoto_2 = TaggedPhotoFactory(content_object=self.carp_photo, tag=carp_tag) taggedphoto_3 = TaggedPhotoFactory(content_object=self.cod_photo, tag=fish_tag) taggedphoto_4 = TaggedPhotoFactory(content_object=self.cod_photo, tag=cod_tag)
def setUp(self): """ Creates two photos sharing three tags: 1 has 'fish' and 'carp' tags. 2 has 'fish' and 'cod' tags. """ self.carp_photo = PhotoFactory(title='Carp') self.cod_photo = PhotoFactory(title='Cod') fish_tag = TagFactory(slug='fish', name='Fish') carp_tag = TagFactory(slug='carp', name='Carp') cod_tag = TagFactory(slug='cod', name='Cod') taggedphoto_1 = TaggedPhotoFactory( content_object=self.carp_photo, tag=fish_tag) taggedphoto_2 = TaggedPhotoFactory( content_object=self.carp_photo, tag=carp_tag) taggedphoto_3 = TaggedPhotoFactory( content_object=self.cod_photo, tag=fish_tag) taggedphoto_4 = TaggedPhotoFactory( content_object=self.cod_photo, tag=cod_tag)
def test_image_urls(self): """Test all but the Original and Medium image URL properties.""" photo = PhotoFactory(farm=3, server='1234', flickr_id=4567, secret='9876') for size, prop in self.photo_sizes.items(): self.assertEqual( getattr(photo, prop), 'https://farm3.static.flickr.com/1234/4567_9876_%s.jpg' % size)
def setUp(self): "Set the value of this setting that's used in ditto.flickr.models." super().setUp() self.default_use_local = app_settings.DITTO_FLICKR_USE_LOCAL_MEDIA app_settings.DITTO_FLICKR_USE_LOCAL_MEDIA = True self.photo = PhotoFactory( user=UserFactory(nsid='123456@N01'), taken_time=datetime.datetime.strptime( '2015-08-14 12:00:00', '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.utc))
def test_flickr_photos_several(self): "It should return all Photos from each day." user = UserFactory(nsid='11111111111@N01') account = FlickrAccountFactory(user=user) day_1_photos = PhotoFactory.create_batch(4, user=user, is_private=False, post_time=make_datetime('2017-05-01 12:30:00')) day_2_photos = PhotoFactory.create_batch(3, user=user, is_private=False, post_time=make_datetime('2017-06-01 12:30:00')) r = RecentObjects( (('flickr_photos', user.nsid,),) ) objects = r.get_objects() self.assertEqual(len(objects), 2) self.assertEqual(len(objects[0]['objects']), 3) self.assertEqual(len(objects[1]['objects']), 4)
def test_remote_original_url(self): "Should still return remote URL when we're using local photos." photo = PhotoFactory(farm=3, server='1234', flickr_id=4567, secret='9876', original_secret='7777', original_format='gif') self.assertEqual( photo.remote_original_url, 'https://farm3.static.flickr.com/1234/4567_7777_o.gif')
def test_remote_original_url(self): "By default, same as photo.original_url." photo = PhotoFactory(farm=3, server='1234', flickr_id=4567, secret='9876', original_secret='7777', original_format='gif') self.assertEqual( photo.remote_original_url, 'https://farm3.static.flickr.com/1234/4567_7777_o.gif')
def test_photo_detail_templates(self): "Uses the correct templates" account = AccountFactory() photos = PhotoFactory.create_batch(3, user=account.user) response = self.client.get(reverse('flickr:photo_detail', kwargs={'nsid': account.user.nsid, 'flickr_id': photos[1].flickr_id})) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'flickr/photo_detail.html') self.assertTemplateUsed(response, 'flickr/base.html') self.assertTemplateUsed(response, 'ditto/base.html')
def setUp(self): user_1 = UserFactory(nsid='1234567890@N01') user_2 = UserFactory(nsid='9876543210@N01') account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) self.photos_1 = PhotoFactory.create_batch(2, user=user_1) self.photos_2 = PhotoFactory.create_batch(3, user=user_2) taken_time = datetime.datetime(2014, 3, 18, 12, 0, 0).replace( tzinfo=pytz.utc) post_time = datetime.datetime(2015, 3, 18, 12, 0, 0).replace( tzinfo=pytz.utc) self.photos_1[0].taken_time = taken_time self.photos_1[0].post_time = post_time self.photos_1[0].save() self.photos_2[1].taken_time = taken_time + datetime.timedelta(hours=1) self.photos_2[1].post_time = post_time + datetime.timedelta(hours=1) self.photos_2[1].save() self.private_photo = PhotoFactory(user=user_1, is_private=True, post_time = post_time)
def test_flickr_photos(self): "It only returns Public Photos from the correct User" u1 = UserFactory(nsid='11111111111@N01') u2 = UserFactory(nsid='22222222222@N01') FlickrAccountFactory(user=u1) FlickrAccountFactory(user=u2) PhotoFactory.create_batch(3, user=u1, is_private=False) # These shouldn't be returned: PhotoFactory(user=u1, is_private=True) PhotoFactory(user=u2, is_private=False) r = RecentObjects( (('flickr_photos', u1.nsid,),) ) objects = r.get_objects() self.assertEqual(len(objects), 1) for obj in objects: self.assertEqual(obj['objects'][0].user, u1) self.assertFalse(obj['objects'][0].is_private)
def test_years(self): "Should include all intermediate years." user = FlickrUserFactory(nsid='35034346050@N01') PhotoFactory(user=user, post_time=make_datetime('2014-01-01 12:00:00')) PhotoFactory.create_batch(3, user=user, post_time=make_datetime('2016-01-01 12:00:00')) PhotoFactory(user=user, post_time=make_datetime('2018-01-01 12:00:00')) result = FlickrGenerator(nsid='35034346050@N01').get_photos_per_year() self.assertIn('data', result) self.assertEqual(result['data'], [ {'label': '2014', 'value': 1}, {'label': '2015', 'value': 0}, {'label': '2016', 'value': 3}, {'label': '2017', 'value': 0}, {'label': '2018', 'value': 1}, ])
def test_photo_detail_context(self): "Sends the correct data to templates" account = AccountFactory() photos = PhotoFactory.create_batch(3, user=account.user) response = self.client.get(reverse('flickr:photo_detail', kwargs={'nsid': account.user.nsid, 'flickr_id': photos[1].flickr_id})) self.assertIn('account', response.context) self.assertEqual(account.pk, response.context['account'].pk) self.assertIn('flickr_user', response.context) self.assertEqual(account.user.pk, response.context['flickr_user'].pk) self.assertIn('photo', response.context) self.assertEqual(photos[1].pk, response.context['photo'].pk)
def test_photo_detail_context_no_account(self): "Sends correct data to templates when showing a photo with no account" user = UserFactory() photos = PhotoFactory.create_batch(3, user=user) response = self.client.get(reverse('flickr:photo_detail', kwargs={'nsid': user.nsid, 'flickr_id': photos[1].flickr_id})) self.assertIn('account', response.context) self.assertIsNone(response.context['account']) self.assertIn('flickr_user', response.context) self.assertEqual(user.pk, response.context['flickr_user'].pk) self.assertIn('photo', response.context) self.assertEqual(photos[1].pk, response.context['photo'].pk)
def test_user_detail_context_no_account(self): "Sends correct data to templates for a User with no Account." user = UserFactory() users_photos = PhotoFactory.create_batch(2, user=user) other_photos = PhotoFactory.create_batch(2) response = self.client.get(reverse('flickr:user_detail', kwargs={'nsid': user.nsid})) self.assertIn('account', response.context) self.assertIsNone(response.context['account']) self.assertIn('flickr_user', response.context) self.assertEqual(user.pk, response.context['flickr_user'].pk) self.assertIn('order', response.context) self.assertEqual(response.context['order'], 'uploaded') self.assertIn('photo_list', response.context) self.assertEqual(len(response.context['photo_list']), 2) # ie, only user's photos. self.assertIn(users_photos[0], response.context['photo_list']) self.assertIn(users_photos[1], response.context['photo_list'])
def setUp(self): self.user_1 = UserFactory(nsid='1234567890@N01') self.account_1 = AccountFactory(user=self.user_1) # Three photos, one of which is private. self.photos_1 = PhotoFactory.create_batch(3, user=self.user_1) self.photos_1[0].is_private = True self.photos_1[0].save() self.user_2 = UserFactory(nsid='9876543210@N01') self.account_2 = AccountFactory(user=self.user_2) self.photos_2 = PhotoFactory.create_batch(3, user=self.user_2) # Has three photos, one of them private: self.photoset_1 = PhotosetFactory(user=self.user_1, flickr_id=123456) self.photoset_1.photos.add(*self.photos_1) # Should have two of user_2's three photos: self.photoset_2a = PhotosetFactory(user=self.user_2, flickr_id=98765) self.photoset_2a.photos.add(self.photos_2[0], self.photos_2[1]) # Has all three of user_2's photos: self.photoset_2b = PhotosetFactory(user=self.user_2, flickr_id=55555) self.photoset_2b.photos.add(*self.photos_2)
class TagViewTests(TestCase): """Have a bit more set up than the other tests, so may as well share it.""" def setUp(self): """ Creates two photos sharing three tags: 1 has 'fish' and 'carp' tags. 2 has 'fish' and 'cod' tags. """ self.carp_photo = PhotoFactory(title='Carp') self.cod_photo = PhotoFactory(title='Cod') fish_tag = TagFactory(slug='fish', name='Fish') carp_tag = TagFactory(slug='carp', name='Carp') cod_tag = TagFactory(slug='cod', name='Cod') taggedphoto_1 = TaggedPhotoFactory( content_object=self.carp_photo, tag=fish_tag) taggedphoto_2 = TaggedPhotoFactory( content_object=self.carp_photo, tag=carp_tag) taggedphoto_3 = TaggedPhotoFactory( content_object=self.cod_photo, tag=fish_tag) taggedphoto_4 = TaggedPhotoFactory( content_object=self.cod_photo, tag=cod_tag) def createDogPhoto(self): "Creates a photo tagged with 'dog' and 'mammal'." self.dog_photo = PhotoFactory(title='Dog') mammal_tag = TagFactory(slug='mammal') dog_tag = TagFactory(slug='dog') taggedphoto_5 = TaggedPhotoFactory( content_object=self.dog_photo, tag=mammal_tag) taggedphoto_6 = TaggedPhotoFactory( content_object=self.dog_photo, tag=dog_tag) # TAG LIST def test_tag_list_templates(self): "Uses the correct templates" # Shouldn't need any photos to exist. response = self.client.get(reverse('flickr:tag_list')) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'flickr/tag_list.html') self.assertTemplateUsed(response, 'flickr/base.html') self.assertTemplateUsed(response, 'ditto/base.html') def test_tag_list_context(self): "Sends the correct data to templates" response = self.client.get(reverse('flickr:tag_list')) self.assertIn('tag_list', response.context) self.assertIn('account_list', response.context) self.assertEqual(len(response.context['tag_list']), 3) def test_tag_list_privacy(self): "Should only show tags from public Photos" self.carp_photo.is_private = True self.carp_photo.save() response = self.client.get(reverse('flickr:tag_list')) self.assertIn('tag_list', response.context) self.assertEqual(len(response.context['tag_list']), 2) # TAG DETAIL def test_tag_detail_templates(self): "Uses the correct templates" response = self.client.get(reverse('flickr:tag_detail', kwargs={'slug': 'fish'})) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'flickr/tag_detail.html') self.assertTemplateUsed(response, 'flickr/base.html') self.assertTemplateUsed(response, 'ditto/base.html') def test_tag_detail_context(self): "Sends the correct data to the templates" accounts = AccountFactory.create_batch(2) # The 'fish' tag page shouldn't include this dog photo: self.createDogPhoto() response = self.client.get(reverse('flickr:tag_detail', kwargs={'slug': 'fish'})) self.assertIn('account_list', response.context) self.assertEqual( [account.pk for account in response.context['account_list']], [1,2] ) self.assertIn('tag', response.context) self.assertEqual(response.context['tag'].slug, 'fish') self.assertIn('order', response.context) self.assertEqual(response.context['order'], 'uploaded') self.assertIn('photo_list', response.context) self.assertEqual(len(response.context['photo_list']), 2) self.assertEqual(response.context['photo_list'][0].title, 'Carp') self.assertEqual(response.context['photo_list'][1].title, 'Cod') def test_tag_detail_ordering_posted(self): "By default, photos should be in reverse-post_time order." dt = datetime_now() self.carp_photo.post_time = dt - timedelta(days=2) self.carp_photo.save() self.cod_photo.post_time = dt - timedelta(days=1) self.cod_photo.save() response = self.client.get(reverse('flickr:tag_detail', kwargs={'slug': 'fish'})) pl = response.context['photo_list'] self.assertEqual(pl[0].pk, self.cod_photo.pk) self.assertEqual(pl[1].pk, self.carp_photo.pk) def test_tag_detail_ordering_taken(self): """With ?order=taken, photos should be in reverse-taken_time order. And Photos with taken_unknown=True should not appear.""" dt = datetime_now() self.carp_photo.taken_time = dt - timedelta(days=2) self.carp_photo.save() self.cod_photo.taken_time = dt - timedelta(days=1) self.cod_photo.save() response = self.client.get( reverse('flickr:tag_detail', kwargs={'slug': 'fish'}) + \ '?order=taken') pl = response.context['photo_list'] self.assertEqual(pl[0].pk, self.cod_photo.pk) self.assertEqual(pl[1].pk, self.carp_photo.pk) def test_tag_detail_privacy(self): "Does not display private Photos" self.carp_photo.is_private = True self.carp_photo.save() response = self.client.get(reverse('flickr:tag_detail', kwargs={'slug': 'carp'})) self.assertEquals(response.status_code, 404) def test_tag_detail_fails(self): "Returns a 404 if a non-existent tag's page is requested" response = self.client.get(reverse('flickr:tag_detail', kwargs={'slug': 'bob'})) self.assertEquals(response.status_code, 404) ## USER TAG DETAIL def test_user_tag_detail_templates(self): "Uses the correct templates" response = self.client.get(reverse('flickr:user_tag_detail', kwargs={'nsid': self.carp_photo.user.nsid, 'tag_slug': 'fish'})) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'flickr/user_tag_detail.html') self.assertTemplateUsed(response, 'flickr/base.html') self.assertTemplateUsed(response, 'ditto/base.html') def test_user_tag_detail_context(self): "Sends the correct data to templates" self.createDogPhoto() # Ensure the cod, carp and dog photos are all owned by the same user. # Only the carp and cod pics should show up on the user's 'fish' page. self.cod_photo.user = self.carp_photo.user self.cod_photo.save() self.dog_photo.user = self.carp_photo.user self.dog_photo.save() response = self.client.get(reverse('flickr:user_tag_detail', kwargs={'nsid': self.carp_photo.user.nsid, 'tag_slug': 'fish'})) self.assertIn('tag', response.context) self.assertEqual(response.context['tag'].name, 'Fish') self.assertIn('order', response.context) self.assertEqual(response.context['order'], 'uploaded') self.assertIn('photo_list', response.context) self.assertEqual(len(response.context['photo_list']), 2) self.assertEqual( [photo.pk for photo in response.context['photo_list']], [1,2] ) def test_user_tag_detail_ordering_posted(self): "By default, photos should be in reverse-post_time order." dt = datetime_now() # Need two photos by same user to test ordering: self.carp_photo.user = self.cod_photo.user self.carp_photo.post_time = dt - timedelta(days=2) self.carp_photo.save() self.cod_photo.post_time = dt - timedelta(days=1) self.cod_photo.save() response = self.client.get(reverse('flickr:user_tag_detail', kwargs={'nsid': self.carp_photo.user.nsid, 'tag_slug': 'fish'})) pl = response.context['photo_list'] self.assertEqual(pl[0].pk, self.cod_photo.pk) self.assertEqual(pl[1].pk, self.carp_photo.pk) def test_user_tag_detail_ordering_taken(self): """With ?order=taken, photos should be in reverse-taken_time order. And Photos with taken_unknown=True should not appear.""" dt = datetime_now() # Need two photos by same user to test ordering: self.carp_photo.user = self.cod_photo.user self.carp_photo.taken_time = dt - timedelta(days=2) self.carp_photo.save() self.cod_photo.taken_time = dt - timedelta(days=1) self.cod_photo.save() response = self.client.get(reverse('flickr:user_tag_detail', kwargs={'nsid': self.carp_photo.user.nsid, 'tag_slug': 'fish'}) + \ '?order=taken') pl = response.context['photo_list'] self.assertEqual(pl[0].pk, self.cod_photo.pk) self.assertEqual(pl[1].pk, self.carp_photo.pk) def test_user_tag_detail_privacy(self): "Does not display private photos" self.carp_photo.is_private = True self.carp_photo.save() response = self.client.get(reverse('flickr:user_tag_detail', kwargs={'nsid': self.carp_photo.user.nsid, 'tag_slug': 'carp'})) self.assertEquals(response.status_code, 404) def test_user_tag_detail_fails_1(self): "Returns a 404 if a non-existent user is requested" response = self.client.get(reverse('flickr:user_tag_detail', kwargs={'nsid': '99999999999@N99', 'tag_slug': 'fish'})) self.assertEquals(response.status_code, 404) def test_user_tag_detail_fails_2(self): "Returns a 404 if a non-existent tag is requested" response = self.client.get(reverse('flickr:user_tag_detail', kwargs={'nsid': self.carp_photo.user.nsid, 'tag_slug': 'mammal'})) self.assertEquals(response.status_code, 404)