class TestPhoto(TestCase): def setUp(self): self.p = Photo() def tearDown(self): """ if the picture exists I should delete to avoid leftover files """ if self.p.id: self.p.delete() """ Tests the picture part of the blog. """ def test_image_creation_and_deletion(self): """ """ f = File(open("core/tests/test_images/test.jpg", "r")) p = self.p p.title = "test" p.image.save("testfile.jpg", f) p.save() #I save the images path full_pic = p.image.path small_pic = p.get_path(thumb=False) thumb_pic = p.get_path(thumb=True) #they should exist self.assertTrue(os.path.isfile(full_pic)) self.assertTrue(os.path.isfile(small_pic)) self.assertTrue(os.path.isfile(thumb_pic)) p.delete() # and they should not exist anymore self.assertFalse(os.path.isfile(full_pic)) self.assertFalse(os.path.isfile(small_pic)) self.assertFalse(os.path.isfile(thumb_pic)) def test_image_properties(self): """ See if associated props are there. Those are important for the admin and templates """ f = File(open("core/tests/test_images/test.jpg", "r")) p = self.p p.title = "test" p.image.save("NEW_testfile.jpg", f) p.save() self.assertTrue("NEW_testfile_t.jpg" in p.thumb) self.assertTrue("NEW_testfile_s.jpg" in p.small) self.assertTrue("NEW_testfile_s.jpg" in p.full_url) self.assertTrue(MEDIA_URL in p.full_url) self.assertTrue(SITE_URL in p.wave) self.assertTrue("photowave" in p.wave) self.assertTrue(str(p.id) in p.wave)
def check_custom_image_filter_for_modelAdmin(self, modelAdmin): """ The conditions for the image to be on the query are pics with the is_used=False(default) I am stubbing the resquest and the db_field """ request = {} class foo(object): name = "pic" def formfield(self, **kwargs): self.args = kwargs db_field = foo() modelAdmin.formfield_for_foreignkey(db_field, request) # So far so good queryset = db_field.args["queryset"] #no images found self.assertEqual(queryset.count(),0) # Now I create 3 files to the 3 conditions to see if the query finds only two f = File(open("core/tests/test_images/test.jpg", "r")) p1 = Photo() p1.title = "test1" p1.image.save("testfile1.jpg", f) p1.save() f = File(open("core/tests/test_images/test.jpg", "r")) p2 = Photo() p2.title = "test2" p2.image.save("testfile2.jpg", f) # this should not apear p2.used = True p2.save() f = File(open("core/tests/test_images/test.jpg", "r")) p3 = Photo() p3.title = "test3" p3.image.save("testfile3.jpg", f) p3.used = True p3.save() Post.objects.create(title="foo",slug="foo",text="foo",pic=p3) #Finding only p1 and p3 self.assertEqual(queryset.count(),2) p1.delete() p2.delete() p3.delete()
def setUp(self): self.p = Photo()