Esempio n. 1
0
    def test_image(self):
        obj = WebcamTestModel()
        obj.photo = SimpleUploadedFile("assignment.jpg", PICTURE)
        obj.save()

        self.assertEqual(obj.photo.dimension, (320, 200))

        self.assertEqual(obj.photo.width, 320)
        self.assertEqual(obj.photo.height, 200)
        self.assertEqual(obj.photo.image.format, 'JPEG')
Esempio n. 2
0
    def test_image(self):
        obj = WebcamTestModel()
        obj.photo = SimpleUploadedFile("assignment.jpg", PICTURE)
        obj.save()

        self.assertEqual(obj.photo.dimension, (320, 200))

        self.assertEqual(obj.photo.width, 320)
        self.assertEqual(obj.photo.height, 200)
        self.assertEqual(obj.photo.image.format, 'JPEG')
Esempio n. 3
0
    def test_form_edit_no_data(self):
        obj = WebcamTestModel(photo=SimpleUploadedFile('-1', PICTURE))
        obj.save()
        prev_name = obj.photo
        Form = modelform_factory(WebcamTestModel)
        form = Form({}, instance=obj)
        assert form.is_valid(), form._errors
        obj = form.save()

        self.assertTrue(os.path.isfile(temp_storage.path(obj.photo)), '`{0.photo.name}` is not a file'.format(obj))
        self.assertEqual(prev_name, obj.photo)
Esempio n. 4
0
    def test_form_edit_no_data(self):
        obj = WebcamTestModel(photo=SimpleUploadedFile('-1', PICTURE))
        obj.save()
        prev_name = obj.photo
        Form = modelform_factory(WebcamTestModel)
        form = Form({}, instance=obj)
        assert form.is_valid(), form._errors
        obj = form.save()

        self.assertTrue(os.path.isfile(temp_storage.path(obj.photo)),
                        '`{0.photo.name}` is not a file'.format(obj))
        self.assertEqual(prev_name, obj.photo)
Esempio n. 5
0
    def test_edit_no_changes(self):

        base64_picture_mime = b64.encodestring(PICTURE)
        obj = WebcamTestModel(photo=PictureUploadedFile(get_picture_name(), PICTURE))
        obj.save()
        obj = WebcamTestModel.objects.get(pk=obj.pk)
        prev_name = obj.photo
        Form = modelform_factory(WebcamTestModel)
        form = Form({'data_photo': base64_picture_mime}, instance=obj)
        obj = form.save()

        self.assertTrue(os.path.isfile(temp_storage.path(obj.photo.name)), '`{0.photo.name}` is not a file'.format(obj))
        self.assertEqual(prev_name, obj.photo)
Esempio n. 6
0
    def test_files(self):
        temp_storage.save('tests/default.jpg', ContentFile(PICTURE))
        # Attempting to access a FileField from the class raises a descriptive
        # error
        self.assertRaises(AttributeError, lambda: WebcamTestModel.photo)

        # An object without a file has limited functionality.
        obj1 = WebcamTestModel()
        self.assertEqual(obj1.photo.name, None)
        self.assertRaises(ValueError, lambda: obj1.photo.size)

        # Saving a file enables full functionality.
        # self.assertRaises(ValueError, obj1.photo.save, "django_test.txt", ContentFile("content"))

        obj1.photo.save(None, ContentFile(PICTURE))
        filename = os.path.basename(obj1.photo.name)

        basename, suffix = filename.split('.')
        self.assertTrue(temp_storage.exists(obj1.photo.name), filename)
        self.assertTrue(uuid_rex.match(basename))
        self.assertEqual(obj1.photo.size, 53472)
        self.assertTrue(lambda: obj1.photo.is_valid())
        obj1.photo.close()

        # File objects can be assigned to FileField attributes, but shouldn't
        # get committed until the model it's attached to is saved.
        obj1.photo = SimpleUploadedFile("assignment.jpg", PICTURE)
        dirs, files = temp_storage.listdir("tests")
        self.assertEqual(dirs, [])
        # self.assertEqual(sorted(files), sorted(["default.jpg", filename]))

        obj1.save()
        self.assertEqual(str(obj1.photo), "tests/assignment.jpg")
        dirs, files = temp_storage.listdir("tests")
        self.assertEqual(sorted(files),
                         sorted(["assignment.jpg", "default.jpg", filename]))

        # Files can be read in a little at a time, if necessary.
        obj1.photo.open()
        self.assertEqual(obj1.photo.read(3), PICTURE[:3])
        obj1.photo.close()

        obj2 = WebcamTestModel()
        obj2.photo.save("assignment.txt", ContentFile(PICTURE))

        # Push the objects into the cache to make sure they pickle properly
        cache.set("obj1", obj1)
        cache.set("obj2", obj2)

        self.assertEqual(cache.get("obj2").photo.name, "tests/assignment.txt")

        # Deleting an object does not delete the file it uses.
        obj2.delete()
        obj2.photo.save("django_test.txt", ContentFile(PICTURE))
        self.assertEqual(obj2.photo.name, "tests/django_test.txt")
Esempio n. 7
0
    def test_edit_no_changes(self):

        base64_picture_mime = b64.encodestring(PICTURE)
        obj = WebcamTestModel(
            photo=PictureUploadedFile(get_picture_name(), PICTURE))
        obj.save()
        obj = WebcamTestModel.objects.get(pk=obj.pk)
        prev_name = obj.photo
        Form = modelform_factory(WebcamTestModel)
        form = Form({'data_photo': base64_picture_mime}, instance=obj)
        obj = form.save()

        self.assertTrue(os.path.isfile(temp_storage.path(obj.photo.name)),
                        '`{0.photo.name}` is not a file'.format(obj))
        self.assertEqual(prev_name, obj.photo)
Esempio n. 8
0
    def test_files(self):
        temp_storage.save('tests/default.jpg', ContentFile(PICTURE))
        # Attempting to access a FileField from the class raises a descriptive
        # error
        self.assertRaises(AttributeError, lambda: WebcamTestModel.photo)

        # An object without a file has limited functionality.
        obj1 = WebcamTestModel()
        self.assertEqual(obj1.photo.name, None)
        self.assertRaises(ValueError, lambda: obj1.photo.size)

        # Saving a file enables full functionality.
        # self.assertRaises(ValueError, obj1.photo.save, "django_test.txt", ContentFile("content"))

        obj1.photo.save(None, ContentFile(PICTURE))
        filename = os.path.basename(obj1.photo.name)

        basename, suffix = filename.split('.')
        self.assertTrue(temp_storage.exists(obj1.photo.name), filename)
        self.assertTrue(uuid_rex.match(basename))
        self.assertEqual(obj1.photo.size, 53472)
        self.assertTrue(lambda: obj1.photo.is_valid())
        obj1.photo.close()

        # File objects can be assigned to FileField attributes, but shouldn't
        # get committed until the model it's attached to is saved.
        obj1.photo = SimpleUploadedFile("assignment.jpg", PICTURE)
        dirs, files = temp_storage.listdir("tests")
        self.assertEqual(dirs, [])
        # self.assertEqual(sorted(files), sorted(["default.jpg", filename]))

        obj1.save()
        self.assertEqual(str(obj1.photo), "tests/assignment.jpg")
        dirs, files = temp_storage.listdir("tests")
        self.assertEqual(
            sorted(files), sorted(["assignment.jpg", "default.jpg", filename])
        )

        # Files can be read in a little at a time, if necessary.
        obj1.photo.open()
        self.assertEqual(obj1.photo.read(3), PICTURE[:3])
        obj1.photo.close()

        obj2 = WebcamTestModel()
        obj2.photo.save("assignment.txt", ContentFile(PICTURE))

        # Push the objects into the cache to make sure they pickle properly
        cache.set("obj1", obj1)
        cache.set("obj2", obj2)

        self.assertEqual(cache.get("obj2").photo.name, "tests/assignment.txt")

        # Deleting an object does not delete the file it uses.
        obj2.delete()
        obj2.photo.save("django_test.txt", ContentFile(PICTURE))
        self.assertEqual(obj2.photo.name, "tests/django_test.txt")