|
11 | 11 |
|
12 | 12 | import pytest |
13 | 13 | from django.core.files.storage import default_storage |
| 14 | +from le_utils.constants import exercises |
14 | 15 | from le_utils.constants import format_presets |
| 16 | +from PIL import Image |
15 | 17 |
|
16 | 18 | from contentcuration import models as cc |
17 | 19 | from contentcuration.tests.utils import mixer |
@@ -385,27 +387,31 @@ def create_test_file(filebytes, ext="pdf"): |
385 | 387 | ] |
386 | 388 |
|
387 | 389 |
|
388 | | -def fileobj_exercise_image(): |
| 390 | +def fileobj_exercise_image(size=(100, 100), color="red"): |
389 | 391 | """ |
390 | 392 | Create a generic exercise image file in storage and return a File model pointing to it. |
391 | 393 | """ |
392 | | - filecontents = "".join(random.sample(string.printable, 20)) |
| 394 | + image = Image.new("RGB", size, color=color) |
| 395 | + buffer = BytesIO() |
| 396 | + image.save(buffer, "JPEG") |
393 | 397 | temp_file_dict = create_studio_file( |
394 | | - filecontents, preset=format_presets.EXERCISE_IMAGE, ext="jpg" |
| 398 | + buffer.getvalue(), preset=format_presets.EXERCISE_IMAGE, ext="jpg" |
395 | 399 | ) |
396 | 400 | return temp_file_dict["db_file"] |
397 | 401 |
|
398 | 402 |
|
399 | | -def fileobj_exercise_graphie(): |
| 403 | +def fileobj_exercise_graphie(original_filename=None): |
400 | 404 | """ |
401 | 405 | Create an graphi exercise image file in storage and return a File model pointing to it. |
402 | 406 | """ |
403 | | - filecontents = "".join(random.sample(string.printable, 20)) |
| 407 | + svg_content = f"<svg><circle cx='50' cy='50' r='40' />{original_filename or ''.join(random.sample(string.printable, 20))}</svg>" |
| 408 | + json_content = '{"version": {"major": 0, "minor": 0}}' |
| 409 | + filecontents = svg_content + exercises.GRAPHIE_DELIMITER + json_content |
404 | 410 | temp_file_dict = create_studio_file( |
405 | 411 | filecontents, |
406 | 412 | preset=format_presets.EXERCISE_GRAPHIE, |
407 | 413 | ext="graphie", |
408 | | - original_filename="theoriginalfilename", |
| 414 | + original_filename=original_filename or "theoriginalfilename", |
409 | 415 | ) |
410 | 416 | return temp_file_dict["db_file"] |
411 | 417 |
|
|
0 commit comments