Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions polygon_cli/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ def get_statements_list(self):
for lang, files_raw in statements_raw.items():
encoding = files_raw.get('encoding', None)
for name, content in files_raw.items():
# The API response also contains statement metadata. In
# particular, `encoding` is a string and newer Polygon
# versions include boolean review flags. None of those fields
# represent downloadable statement sections.
if name == 'encoding' or not isinstance(content, str):
continue

file = polygon_file.PolygonFile()
file.type = 'statement'
file.name = '%s/%s.tex' % (lang, name)
Expand Down
59 changes: 59 additions & 0 deletions tests/test_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import unittest

from polygon_cli.problem import ProblemSession


class GetStatementsListTest(unittest.TestCase):
def setUp(self):
self.session = ProblemSession('main', 123, None, verbose=False)

def test_ignores_encoding_and_boolean_metadata(self):
response = {
'english': {
'name': 'Example',
'legend': 'Solve it.',
'input': 'An integer.',
'output': 'Its answer.',
'notes': '',
'tutorial': 'Use math.',
'encoding': 'UTF-8',
'showCautionsAndGrammaticalFixes': False,
'showInReview': True,
},
}
self.session.send_api_request = lambda *_args, **_kwargs: response

files = self.session.get_statements_list()

self.assertEqual(
[file.name for file in files],
[
'english/name.tex',
'english/legend.tex',
'english/input.tex',
'english/output.tex',
'english/notes.tex',
'english/tutorial.tex',
],
)
self.assertEqual(files[0].content, b'Example')
self.assertTrue(all(file.type == 'statement' for file in files))

def test_uses_the_language_encoding_for_statement_content(self):
response = {
'spanish': {
'name': 'Números ocultos',
'encoding': 'iso-8859-1',
},
}
self.session.send_api_request = lambda *_args, **_kwargs: response

files = self.session.get_statements_list()

self.assertEqual(len(files), 1)
self.assertEqual(files[0].name, 'spanish/name.tex')
self.assertEqual(files[0].content, 'Números ocultos'.encode('iso-8859-1'))


if __name__ == '__main__':
unittest.main()