| 375 | |
| 376 | @require_pil |
| 377 | def test_image_field(self): |
| 378 | class TestImage(Document): |
| 379 | image = ImageField() |
| 380 | |
| 381 | TestImage.drop_collection() |
| 382 | |
| 383 | with tempfile.TemporaryFile() as f: |
| 384 | f.write(b"Hello World!") |
| 385 | f.flush() |
| 386 | |
| 387 | t = TestImage() |
| 388 | try: |
| 389 | t.image.put(f) |
| 390 | self.fail("Should have raised an invalidation error") |
| 391 | except ValidationError as e: |
| 392 | assert "%s" % e == "Invalid image: cannot identify image file %s" % f |
| 393 | |
| 394 | t = TestImage() |
| 395 | t.image.put(get_file(TEST_IMAGE_PATH)) |
| 396 | t.save() |
| 397 | |
| 398 | t = TestImage.objects.first() |
| 399 | |
| 400 | assert t.image.format == "PNG" |
| 401 | |
| 402 | w, h = t.image.size |
| 403 | assert w == 371 |
| 404 | assert h == 76 |
| 405 | |
| 406 | t.image.delete() |
| 407 | |
| 408 | @require_pil |
| 409 | def test_image_field_reassigning(self): |