def test_write(self, mock_get_duties, mock_get_section_details, mock_prepend_introduction): mock_get_duties.return_value = None mock_get_section_details.return_value = None mock_prepend_introduction.return_value = None file_contents = 'XML' application = Application(CLASSIFICATION) db_chapter = ChapterFactory(id=1, description='Test') chapter = ClassificationChapter(application, db_chapter) actual_remote_file_name = chapter.write(file_contents) assert mock_prepend_introduction.called is True db_chapter.refresh_from_db() assert db_chapter.classification_document.name == actual_remote_file_name with zipfile.ZipFile(db_chapter.classification_document) as fh: actual_files = [f.filename for f in fh.filelist] assert set(actual_files) == { 'word/fontTable.xml', 'word/numbering.xml', 'word/header1.xml', 'docProps/core.xml', 'word/_rels/document.xml.rels', 'customXml/_rels/item1.xml.rels', 'customXml/itemProps1.xml', 'word/theme/theme1.xml', 'word/header2.xml', 'customXml/item1.xml', 'docProps/app.xml', 'word/endnotes.xml', 'word/footer1.xml', 'word/webSettings.xml', 'word/styles.xml', '_rels/.rels', 'word/settings.xml', 'word/footnotes.xml', 'word/document.xml', '[Content_Types].xml' } actual_document_xml = fh.read('word/document.xml') assert actual_document_xml == bytes(file_contents, 'utf-8')
def test_write(self): file_contents = 'XML' real_application = Application(SCHEDULE) application = mock.MagicMock(document_type=SCHEDULE, OUTPUT_DIR='/tmp', MODEL_DIR=real_application.MODEL_DIR) db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) actual_remote_file_name = chapter.write(file_contents) db_chapter.refresh_from_db() assert db_chapter.schedule_document.name == actual_remote_file_name with zipfile.ZipFile(db_chapter.schedule_document) as fh: actual_files = [f.filename for f in fh.filelist] assert set(actual_files) == { 'word/fontTable.xml', 'word/numbering.xml', 'word/header1.xml', 'docProps/core.xml', 'word/_rels/document.xml.rels', 'customXml/_rels/item1.xml.rels', 'customXml/itemProps1.xml', 'word/theme/theme1.xml', 'word/header2.xml', 'customXml/item1.xml', 'docProps/app.xml', 'word/endnotes.xml', 'word/footer1.xml', 'word/webSettings.xml', 'word/styles.xml', '_rels/.rels', 'word/settings.xml', 'word/footnotes.xml', 'word/document.xml', '[Content_Types].xml' } actual_document_xml = fh.read('word/document.xml') assert actual_document_xml == bytes(file_contents, 'utf-8')
def test_upload_generic_document_to_s3(): chapter = ChapterFactory() fake_file_content = b'information' with tempfile.NamedTemporaryFile() as f: f.write(fake_file_content) f.seek(0) actual_remote_file_name = upload_generic_document_to_s3( chapter, 'schedule_document', f.name, '1.docx') chapter.refresh_from_db() assert chapter.schedule_document.read() == fake_file_content assert actual_remote_file_name.endswith('docx') assert chapter.schedule_document_created_at == timezone.now()
def test_assign_inherited_duty_to_commodity(self, combined_duty, expected_child_combined_duty): application = mock.MagicMock( document_type=SCHEDULE, OUTPUT_DIR='/tmp', ) commodity_1 = Commodity(application, product_line_suffix='80', commodity_code='1200000000', indents=0) commodity_1.combined_duty = combined_duty commodity_2 = Commodity( application, product_line_suffix='82', commodity_code='1211000000', indents=3, ) commodity_3 = Commodity(application, product_line_suffix='80', commodity_code='1211110000', indents=10) db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) actual_max_indent = chapter.assign_inherited_duty_to_commodity( [commodity_1, commodity_2, commodity_3]) assert actual_max_indent == 10 assert commodity_2.combined_duty == expected_child_combined_duty assert commodity_3.combined_duty == expected_child_combined_duty
def test_assign_authorised_use_commodities(self): SeasonalQuotaFactory(quota_order_number_id='1234567891') SeasonalQuotaFactory(quota_order_number_id='1234567892') application = mock.MagicMock( document_type=SCHEDULE, OUTPUT_DIR='/tmp', authorised_use_list=['1234567891', '1234567892'], ) commodity_1 = Commodity(application, product_line_suffix='80', commodity_code='1234567891') commodity_2 = Commodity(application, product_line_suffix='82', commodity_code='1234567892') commodity_3 = Commodity(application, product_line_suffix='80', commodity_code='1234567893') db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) assert chapter.contains_authorised_use is False assert chapter.seasonal_records == 0 chapter.assign_authorised_use_commodities( [commodity_1, commodity_2, commodity_3]) assert chapter.contains_authorised_use is True assert chapter.seasonal_records == 1
def test_unsuppress_selected_commodities(self): application = mock.MagicMock( document_type=SCHEDULE, OUTPUT_DIR='/tmp', ) commodity_1 = Commodity( application, product_line_suffix='80', commodity_code='1200000000', indents=0, ) commodity_2 = Commodity( application, product_line_suffix='82', commodity_code='8708701080', indents=3, ) commodity_3 = Commodity(application, product_line_suffix='80', commodity_code='1211110000', indents=10) commodity_1.suppress_duty = True commodity_2.suppress_duty = True commodity_3.suppress_duty = True db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) chapter.unsuppress_selected_commodities( [commodity_1, commodity_2, commodity_3]) assert commodity_1.suppress_duty is True assert commodity_2.suppress_duty is False assert commodity_3.suppress_duty is True
def test_create_document(mock_create_document, force, add_chapters, expected_result): mock_create_document.return_value = None if add_chapters: ChapterFactory() mfn_document = MFNDocumentFactory(document_type=SCHEDULE) app = Application(SCHEDULE, force=force) app.create_document(mfn_document) assert mock_create_document.called is expected_result
def test_process_schedule_chapter(self, mock_init, mock_format_chapter): mock_format_chapter.return_value = None mock_init.return_value = None application = mock.MagicMock(document_type=CLASSIFICATION, OUTPUT_DIR='/tmp') db_chapter = ChapterFactory(id=1, description='Test') process_chapter(application, db_chapter.id) assert mock_init.called assert mock_format_chapter.called
def test_initialise(self): application = mock.MagicMock(document_type=SCHEDULE, OUTPUT_DIR='/tmp') db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) assert chapter.footnote_list == [] assert chapter.duty_list == [] assert chapter.supplementary_unit_list == [] assert chapter.seasonal_records == 0 assert chapter.contains_authorised_use is False assert chapter.document_title == 'UK Goods Schedule'
def test_process_schedule_chapter(self, mock_init, mock_format_chapter, chapter_id, create_chapter, should_chapter_be_processed): mock_format_chapter.return_value = None mock_init.return_value = None application = mock.MagicMock(document_type=SCHEDULE, OUTPUT_DIR='/tmp') if create_chapter: ChapterFactory(id=chapter_id, description='Test') process_chapter(application, chapter_id) assert mock_init.called is should_chapter_be_processed assert mock_format_chapter.called is should_chapter_be_processed
def test_get_document_content(self, chapter_id, contains_authorised_use, expected_document_content): application = mock.MagicMock( document_type=SCHEDULE, OUTPUT_DIR='/tmp', ) db_chapter = ChapterFactory(id=chapter_id, description='Test') chapter = ScheduleChapter(application, db_chapter) chapter.contains_authorised_use = contains_authorised_use actual_document_content = chapter.get_document_content() assert expected_document_content == actual_document_content
def test_format_chapter(self, mock_create_document): mock_create_document.return_value = None application = mock.MagicMock( document_type=CLASSIFICATION, OUTPUT_DIR='/tmp', ) db_chapter = ChapterFactory(id=1, description='Test') chapter = ClassificationChapter(application, db_chapter) chapter.format_chapter() assert mock_create_document.called assert set(mock_create_document.call_args_list[0][0][0].keys()) == { 'WIDTH_CLASSIFICATION', 'WIDTH_DUTY', 'CHAPTER_NOTE_DOCUMENT', 'commodity_list', 'WIDTH_DESCRIPTION', 'WIDTH_NOTES' }
def test_format_heading(self, display_section_heading, expected_heading_dict): application = mock.MagicMock( document_type=SCHEDULE, OUTPUT_DIR='/tmp', ) db_chapter = ChapterFactory( id=1, description='Test', display_section_heading=display_section_heading) chapter = ScheduleChapter(application, db_chapter) chapter.section_numeral = 'I' chapter.section_title = 'Test Section' actual_heading_dict = chapter.format_heading() assert expected_heading_dict == actual_heading_dict
def test_format_table(self): application = mock.MagicMock( document_type=SCHEDULE, OUTPUT_DIR='/tmp', ) commodity_1 = Commodity( application, product_line_suffix='80', commodity_code='1200000000', indents=0, ) commodity_2 = Commodity( application, product_line_suffix='80', commodity_code='1211000000', indents=3, ) commodity_2.notes_list = ['2 notes'] commodity_3 = Commodity(application, product_line_suffix='80', commodity_code='1211110000', indents=10) commodity_3.notes_list = ['3 notes'] db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) actual_table = chapter.format_table_content( [commodity_1, commodity_2, commodity_3]) assert 'commodity_list' in actual_table assert len(actual_table['commodity_list']) == 3 assert set(actual_table['commodity_list'][0].keys()) == { 'DUTY', 'DESCRIPTION', 'INDENT', 'COMMODITY', 'NOTES' } assert actual_table['commodity_list'][0]['COMMODITY'] == '1200' assert actual_table['commodity_list'][1]['COMMODITY'] == '1211' assert actual_table['commodity_list'][2]['COMMODITY'] == '1211 11' assert 'w:left="0"' in actual_table['commodity_list'][0]['INDENT'] assert 'w:left="340"' in actual_table['commodity_list'][1]['INDENT'] assert 'w:left="1134"' in actual_table['commodity_list'][2]['INDENT'] assert actual_table['commodity_list'][0][ 'NOTES'] == '<w:r><w:t></w:t></w:r>' assert actual_table['commodity_list'][1][ 'NOTES'] == '<w:r><w:t>2 notes</w:t></w:r>' assert actual_table['commodity_list'][2][ 'NOTES'] == '<w:r><w:t>3 notes</w:t></w:r>'
def test_get_chapter_note_content(self, chapter_has_note): application = mock.MagicMock( document_type=CLASSIFICATION, OUTPUT_DIR='/tmp', ) db_chapter = ChapterFactory(id=1, description='Test') expected_note_content = {'CHAPTER_NOTE_DOCUMENT': ''} if chapter_has_note: db_chapter_note = ChapterNoteWithDocumentFactory( chapter=db_chapter) expected_note_content = { 'CHAPTER_NOTE_DOCUMENT': db_chapter_note.document_check_sum } chapter = ClassificationChapter(application, db_chapter) actual_chapter_note_content = chapter.get_chapter_note_content() assert actual_chapter_note_content == expected_note_content
def test_create_document( self, mock_render_to_string, mock_write, mock_get_duties, mock_get_section_details, context, force, expected_template, expected_document_xml, expected_change, raise_write_exception, ): fake_file_name = 'fake_file.txt' mock_get_duties.return_value = None mock_get_section_details.return_value = None mock_write.return_value = fake_file_name if raise_write_exception: mock_write.side_effect = EndpointConnectionError(endpoint_url='') mock_render_to_string.return_value = expected_document_xml application = Application(CLASSIFICATION, force=force) db_chapter = ChapterFactory(id=1, description='Test') chapter = ClassificationChapter(application, db_chapter) chapter.create_document(context) if expected_document_xml: mock_render_to_string.assert_called_with(expected_template, context) mock_write.asssert_called_with(expected_document_xml) else: assert mock_render_to_string.called is False assert mock_write.called is False if expected_change: document_history = ChapterDocumentHistory.objects.get( chapter=db_chapter) assert document_history.forced is force assert document_history.data == context assert document_history.change == expected_change assert document_history.remote_file_name == fake_file_name else: assert ChapterDocumentHistory.objects.filter( chapter=db_chapter).exists() is False
def test_suppress_child_duty(self): application = mock.MagicMock( document_type=SCHEDULE, OUTPUT_DIR='/tmp', ) commodity_1 = Commodity( application, product_line_suffix='80', commodity_code='1200000000', indents=0, ) commodity_1.suppress_row = False commodity_2 = Commodity( application, product_line_suffix='82', commodity_code='1211000000', indents=1, ) commodity_2.suppress_row = False commodity_3 = Commodity(application, product_line_suffix='80', commodity_code='1211110000', indents=2) commodity_3.suppress_row = False commodity_4 = Commodity(application, product_line_suffix='80', commodity_code='1211111100', indents=3) commodity_4.suppress_row = False commodity_5 = Commodity(application, product_line_suffix='80', commodity_code='1211111111', indents=4) commodity_5.suppress_row = False db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) chapter.suppress_child_duty( [commodity_1, commodity_2, commodity_3, commodity_4, commodity_5], 4) assert commodity_1.suppress_duty is True assert commodity_2.suppress_duty is True assert commodity_3.suppress_duty is True assert commodity_4.suppress_duty is True assert commodity_5.suppress_duty is False
def test_admin_views( authenticated_admin_client, url, include_kwargs, post, ): if 'agreementdocumenthistory' in url: factory = AgreementDocumentHistoryFactory() elif '_agreement_' in url: factory = AgreementFactory() elif 'chapterdocumenthistory' in url: factory = ChapterDocumentHistoryFactory() elif 'latinterm' in url: factory = LatinTermFactory() elif 'specialnote' in url: factory = SpecialNoteFactory() elif 'chapternote' in url: factory = ChapterNoteFactory() elif '_chapter_' in url: factory = ChapterFactory() elif '_extendedquota_' in url: factory = ExtendedQuotaFactory() elif 'mfntableofcontent' in url: factory = MFNTableOfContentFactory() else: factory = MFNDocumentHistoryFactory() kwargs = {} if include_kwargs: kwargs = {'object_id': factory.pk} uri = reverse(url, kwargs=kwargs) response = authenticated_admin_client.get(uri) assert response.status_code == status.HTTP_200_OK if post: post_data = get_factory_dict(factory) post_data.update(post) post_response = authenticated_admin_client.post(uri, data=post_data, follow=True) assert post_response.status_code == status.HTTP_200_OK assert b'errorlist' not in post_response.content, str(post_response.content) factory.refresh_from_db() for key, value in post.items(): assert getattr(factory, key) == value
def test_get_duties(self, mock_get_section_details): mock_get_section_details.return_value = None component_1 = MeasureComponentFactory(measure_sid=1, duty_amount=1, duty_expression_id='15', monetary_unit_code='EUR') SimpleCurrentMeasureFactory( measure_sid=component_1.measure_sid, measure_type_id='103', goods_nomenclature_item_id='0123456781', validity_start_date=datetime(2019, 11, 1, 0, 0), ) component_2 = MeasureComponentFactory(measure_sid=2, duty_amount=2) SimpleCurrentMeasureFactory( measure_sid=component_2.measure_sid, measure_type_id='100', goods_nomenclature_item_id='012345672', validity_start_date=datetime(2019, 11, 1, 0, 0), ) component_3 = MeasureComponentFactory(measure_sid=3, duty_amount=3) SimpleCurrentMeasureFactory( measure_sid=component_3.measure_sid, measure_type_id='103', goods_nomenclature_item_id='0123456783', validity_start_date=datetime(2018, 11, 1, 0, 0), ) component_4 = MeasureComponentFactory(measure_sid=4, duty_amount=4) SimpleCurrentMeasureFactory( measure_sid=component_4.measure_sid, measure_type_id='103', goods_nomenclature_item_id='021111111', validity_start_date=datetime(2019, 11, 1, 0, 0), ) application = Application(SCHEDULE) db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) actual_duties = chapter.get_duties() assert len(actual_duties) == 1 assert actual_duties[0].duty_amount == 1 assert actual_duties[0].duty_string == 'MIN 1.000 €' assert actual_duties[0].commodity_code == '0123456781' assert actual_duties[0].monetary_unit_code == '€' assert actual_duties[0].measurement_unit_qualifier_code == ''
def test_assign_duties_to_commodities(self): application = mock.MagicMock( document_type=SCHEDULE, OUTPUT_DIR='/tmp', ) commodity_1 = Commodity(application, product_line_suffix='80', commodity_code='1234567891') commodity_2 = Commodity(application, product_line_suffix='82', commodity_code='1234567892') Commodity(application, product_line_suffix='80', commodity_code='1234567893') duty_1 = Duty(commodity_code=commodity_1.commodity_code) Duty(commodity_code=commodity_2.commodity_code) db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) chapter.duty_list = [duty_1] chapter.assign_duties_to_commodities([commodity_1]) assert commodity_1.assigned is True assert commodity_1.duty_list == [duty_1] assert commodity_1.commodity_code_formatted == '1234 56 78 91'
def test_get_change_dict(document_type, with_toc, expected_checksum): chapter = ChapterFactory() chapter_with_checksum = ChapterFactory( id=2, schedule_document_check_sum=schedule_checksum, classification_document_check_sum=classification_checksum, ) expected_change_dict = { chapter.get_document_name(document_type): None, chapter_with_checksum.get_document_name(document_type): expected_checksum, } if with_toc: MFNTableOfContentFactory( document_check_sum=toc_checksum, document_type=document_type, ) expected_change_dict['toc'] = toc_checksum app = Application(document_type) actual_change_dict = app.get_change_dict() assert actual_change_dict == expected_change_dict
def get_object(self): return ChapterFactory()
def test_format_schedule_chapter_with_empty_commodity_list(self): application = mock.MagicMock(document_type=SCHEDULE, OUTPUT_DIR='/tmp') db_chapter = ChapterFactory(id=1, description='Test') chapter = ScheduleChapter(application, db_chapter) result = chapter.format_schedule_chapter([]) assert result == {'commodity_list': []}
def test_chapter_document_history_model(): chapter = ChapterFactory() document_history = ChapterDocumentHistoryFactory(chapter=chapter) assert str(document_history) == f'schedule {chapter.chapter_string} - Doc History - 2019-02-01 02:00:00+00:00'
def test_chapter_note(): chapter = ChapterFactory(description='Chapter description') chapter_note = ChapterNoteFactory( chapter=chapter, ) assert str(chapter_note) == 'Chapter Note - Chapter description'
def test_chapter_model(): chapter = ChapterFactory(description='Chapter description') assert str(chapter) == '01 - Chapter description' assert chapter.chapter_string == '01' assert chapter.get_document_name(SCHEDULE) == 'schedule01.docx'