Skip to content

Commit feeffd5

Browse files
committed
Add tests for exercise file generation during publishing.
Only automatically write attached files to perseus file for raw perseus questions.
1 parent c3ad53f commit feeffd5

4 files changed

Lines changed: 1113 additions & 45 deletions

File tree

contentcuration/contentcuration/tests/testdata.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
import pytest
1313
from django.core.files.storage import default_storage
14+
from le_utils.constants import exercises
1415
from le_utils.constants import format_presets
16+
from PIL import Image
1517

1618
from contentcuration import models as cc
1719
from contentcuration.tests.utils import mixer
@@ -385,27 +387,31 @@ def create_test_file(filebytes, ext="pdf"):
385387
]
386388

387389

388-
def fileobj_exercise_image():
390+
def fileobj_exercise_image(size=(100, 100), color="red"):
389391
"""
390392
Create a generic exercise image file in storage and return a File model pointing to it.
391393
"""
392-
filecontents = "".join(random.sample(string.printable, 20))
394+
image = Image.new("RGB", size, color=color)
395+
buffer = BytesIO()
396+
image.save(buffer, "JPEG")
393397
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"
395399
)
396400
return temp_file_dict["db_file"]
397401

398402

399-
def fileobj_exercise_graphie():
403+
def fileobj_exercise_graphie(original_filename=None):
400404
"""
401405
Create an graphi exercise image file in storage and return a File model pointing to it.
402406
"""
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
404410
temp_file_dict = create_studio_file(
405411
filecontents,
406412
preset=format_presets.EXERCISE_GRAPHIE,
407413
ext="graphie",
408-
original_filename="theoriginalfilename",
414+
original_filename=original_filename or "theoriginalfilename",
409415
)
410416
return temp_file_dict["db_file"]
411417

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"answerArea": {
3+
"calculator": false,
4+
"chi2Table": false,
5+
"periodicTable": false,
6+
"tTable": false,
7+
"zTable": false
8+
},
9+
"hints": [
10+
{
11+
"content": "The bottom bar lines up to $\\purpleD{6}$. \n\n![](web+graphie://cdn.kastatic.org/ka-perseus-graphie/d855aefe9a722f9a794b0883ebcdb8c37b4ba0c7)\n\nWhich type of fruit has $\\purpleD{6}$ in Luigi's home?",
12+
"images": {
13+
"web+graphie://cdn.kastatic.org/ka-perseus-graphie/d855aefe9a722f9a794b0883ebcdb8c37b4ba0c7": {
14+
"height": 330,
15+
"width": 404
16+
}
17+
},
18+
"replace": false,
19+
"widgets": {}
20+
},
21+
{
22+
"content": "Kind of fruit | Number\n:- | :-: \nOranges | $\\purpleD{6}$ \n\nLuigi has $\\purpleD{6}$ oranges. So, the bottom bar should be labeled $\\purpleD{\\text{Oranges}}$.",
23+
"images": {},
24+
"replace": false,
25+
"widgets": {}
26+
},
27+
{
28+
"content": "Now let's label the other bars to match the table.",
29+
"images": {},
30+
"replace": false,
31+
"widgets": {}
32+
},
33+
{
34+
"content": "Here is the completed graph:\n\n![](web+graphie://cdn.kastatic.org/ka-perseus-graphie/95262ebaf42bdd1929e5d6d1e2853d3eb0a5cc74)",
35+
"images": {
36+
"web+graphie://cdn.kastatic.org/ka-perseus-graphie/95262ebaf42bdd1929e5d6d1e2853d3eb0a5cc74": {
37+
"height": 330,
38+
"width": 404
39+
}
40+
},
41+
"replace": false,
42+
"widgets": {}
43+
}
44+
],
45+
"itemDataVersion": {
46+
"major": 0,
47+
"minor": 1
48+
},
49+
"question": {
50+
"content": "Luigi created a chart and a bar graph to show how many of each type of fruit were in his home.\n\nKind of fruit | Number \n:- | :-: \nApple | $7$ \nStrawberries | $3$ \nOranges | $6$ \nBananas| $2$ \n\n**Label each bar on the bar graph.**\n\n[[☃ label-image 1]]\n",
51+
"images": {},
52+
"widgets": {
53+
"label-image 1": {
54+
"alignment": "default",
55+
"graded": true,
56+
"options": {
57+
"choices": [
58+
"Apple",
59+
"Strawberries",
60+
"Oranges",
61+
"Bananas"
62+
],
63+
"hideChoicesFromInstructions": true,
64+
"imageAlt": "",
65+
"imageHeight": 330,
66+
"imageUrl": "web+graphie://cdn.kastatic.org/ka-perseus-graphie/ab207c6f38c887130b68c078e6158a87aab60c45",
67+
"imageWidth": 404,
68+
"markers": [
69+
{
70+
"answers": [
71+
"Strawberries"
72+
],
73+
"label": "",
74+
"x": 24.1,
75+
"y": 17.7
76+
},
77+
{
78+
"answers": [
79+
"Bananas"
80+
],
81+
"label": "",
82+
"x": 24.4,
83+
"y": 35.7
84+
},
85+
{
86+
"answers": [
87+
"Apple"
88+
],
89+
"label": "",
90+
"x": 23.8,
91+
"y": 52.9
92+
},
93+
{
94+
"answers": [
95+
"Oranges"
96+
],
97+
"label": "",
98+
"x": 24.1,
99+
"y": 70.9
100+
}
101+
],
102+
"multipleAnswers": false,
103+
"static": false
104+
},
105+
"static": false,
106+
"type": "label-image",
107+
"version": {
108+
"major": 0,
109+
"minor": 0
110+
}
111+
}
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)