You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
4.4 KiB
Python
62 lines
4.4 KiB
Python
|
1 month ago
|
import json
|
||
|
|
import unittest
|
||
|
|
from align_training_answer_fields import plan_row, APPROVED_CONFLICTS
|
||
|
|
|
||
|
|
|
||
|
|
def record(task, columns, **extra):
|
||
|
|
return dict(id='student-answer', task_key=task, **{'step%d_answer' % n: json.dumps(v) for n, v in columns.items()}, **extra)
|
||
|
|
|
||
|
|
|
||
|
|
class FieldAlignmentTest(unittest.TestCase):
|
||
|
|
def test_profile_charts_and_dynamic_fifth_step_move_without_losing_content(self):
|
||
|
|
row = record('target-user-profile', {1: {'excelUrl': '/file/data.xlsx', 'excelName': 'data.xlsx'},
|
||
|
|
2: {'charts': [{'type': 'segment-share', 'url': 'http://120.79.161.177:8999/file/chart.png', 'name': 'chart.png'}]},
|
||
|
|
3: {'insights': [{'dimension': '消费能力', 'analysis': '188元'}]}, 4: {'records': [{'code': 'U01', 'user': '白领'}]}},
|
||
|
|
dynamic_step_answers=json.dumps({'5': {'segments': [{'market': '白领'}], 'marketSelection': '优先白领'}, 'custom-id': {'content': '保留'}}))
|
||
|
|
changes, moves = plan_row(row, preview={'columns': ['R'], 'rows': [['5']]})
|
||
|
|
self.assertEqual(json.loads(changes['step2_answer'])['rfmCharts']['segment-share']['url'], '/file/chart.png')
|
||
|
|
self.assertEqual(json.loads(changes['step5_answer'])['marketSelection'], '优先白领')
|
||
|
|
self.assertEqual(json.loads(changes['dynamic_step_answers']), {'custom-id': {'content': '保留'}})
|
||
|
|
self.assertEqual(json.loads(changes['step1_answer'])['profilePreview']['rows'], [['5']])
|
||
|
|
self.assertEqual(moves['http://120.79.161.177:8999/file/chart.png'], '/file/chart.png')
|
||
|
|
self.assertFalse(plan_row({**row, **changes})[0])
|
||
|
|
|
||
|
|
def test_questionnaire_and_report_fields_preserve_all_images_and_rows(self):
|
||
|
|
row = record('consumer-behavior', {1: {'hypotheses': [{'targetGroup': '白领'}]}, 2: {'needs': [{'feature': '照明'}]},
|
||
|
|
3: {'questions': [{'title': '年龄?'}], 'importedDocumentUrl': '/file/q.docx', 'questionnaireFileName': '问卷.docx'},
|
||
|
|
4: {'rows': [{'title': str(n), 'content': '内容'} for n in range(8)], 'chartFiles': {'均值对比': {'url': '/file/chart.png'}}, 'dataExcelFileUrl': '/file/data.xlsx'}})
|
||
|
|
changes, _ = plan_row(row, questionnaire_text='问卷<标题>\n年龄?')
|
||
|
|
self.assertEqual(json.loads(changes['step3_answer'])['questionnaireImport']['name'], '问卷.docx')
|
||
|
|
self.assertIn('<标题>', json.loads(changes['step3_answer'])['questionnaireText'])
|
||
|
|
self.assertEqual(len(json.loads(changes['step4_answer'])['surveyReportRows']), 8)
|
||
|
|
self.assertEqual(json.loads(changes['step4_answer'])['chartFiles']['均值对比']['url'], '/file/chart.png')
|
||
|
|
|
||
|
|
def test_conflicting_aggregate_requires_explicit_authorization(self):
|
||
|
|
row = record('feature-conversion', {1: {'productName': '实际产品', 'rows': [1], 'scoreRows': [2], 'priorityRows': [3], 'priorityDecision': {'p0': '实际决策'}},
|
||
|
|
2: {'rows': ['旧示例']}, 3: {'rows': ['旧示例']}})
|
||
|
|
row['id'] = '700f22bf-9254-4038-9c54-b70b86a22e84'
|
||
|
|
self.assertFalse(plan_row(row)[0])
|
||
|
|
changes, _ = plan_row(row, approved=True)
|
||
|
|
self.assertNotIn('scoreRows', json.loads(changes['step1_answer']))
|
||
|
|
self.assertEqual(json.loads(changes['step2_answer']), {'scoreRows': [2]})
|
||
|
|
self.assertEqual(json.loads(changes['step3_answer'])['priorityDecision']['p0'], '实际决策')
|
||
|
|
|
||
|
|
def test_alias_collision_aborts_instead_of_overwriting(self):
|
||
|
|
row = record('consumer-behavior', {1: {'hypotheses': [1], 'targetHypotheses': [2]}})
|
||
|
|
with self.assertRaises(ValueError): plan_row(row)
|
||
|
|
|
||
|
|
def test_existing_fields_and_remote_urls_are_unchanged(self):
|
||
|
|
row = record('target-user-profile', {1: {'profileFileUrl': 'https://school.example.edu/file/data.xlsx', 'profilePreview': {'rows': [1]}},
|
||
|
|
2: {'rfmCharts': {'segment-share': {'url': '/file/chart.png', 'name': '图'}}}})
|
||
|
|
self.assertFalse(plan_row(row)[0])
|
||
|
|
|
||
|
|
def test_observed_file_url_aliases_create_current_attachment_objects(self):
|
||
|
|
for task, step, old, new in [('market-opportunity-selection', 4, 'visualizationUrl', 'visualization'),
|
||
|
|
('hardware-cost', 4, 'smallBatchChartUrl', 'smallBatchChart'),
|
||
|
|
('purchase-demand-forecast', 2, 'forecastChartUrl', 'demandForecastChart')]:
|
||
|
|
changes, _ = plan_row(record(task, {step: {old: '/file/chart.png'}}))
|
||
|
|
self.assertEqual(json.loads(changes['step%d_answer' % step])[new]['url'], '/file/chart.png')
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__': unittest.main()
|