def test_get_quota_measures():
    measure = get_mfn_siv_product(1,
                                  geographical_area_id='1011',
                                  measure_type_id='143')
    current_measure = CurrentMeasureFactory(
        measure_sid=measure.measure_sid,
        geographical_area_id=measure.geographical_area_id,
        measure_type_id=measure.measure_type_id,
        validity_start_date=measure.validity_start_date,
        validity_end_date=measure.validity_end_date,
        ordernumber=measure.quota_order_number_id,
        goods_nomenclature_item_id=measure.goods_nomenclature_item_id,
        reduction_indicator=measure.reduction_indicator,
    )
    AgreementFactory(country_name='Espana',
                     slug='spain',
                     country_codes=['1011'])
    application = Application(country_profile='spain')
    document = Document(application)
    document.get_quota_measures()
    actual_measure_list = document.measure_list
    assert len(actual_measure_list) == 1, 'Not the correct things to assert'
    actual_measure = actual_measure_list[0]
    assert actual_measure.commodity_code == str(
        current_measure.goods_nomenclature_item_id)
    assert actual_measure.measure_sid == current_measure.measure_sid
    assert actual_measure.quota_order_number_id == str(
        current_measure.ordernumber)
    assert actual_measure.validity_start_date == current_measure.validity_start_date
    assert actual_measure.validity_end_date == current_measure.validity_end_date
    assert actual_measure.geographical_area_id == current_measure.geographical_area_id
    assert actual_measure.reduction_indicator == current_measure.reduction_indicator
def test_add_licensed_quotas():
    agreement = AgreementFactory(country_name='Espana',
                                 slug='spain',
                                 country_codes=['1011'])
    application = Application(country_profile='spain')
    licensed_quota = ExtendedQuotaFactory(
        quota_type=ExtendedQuota.LICENSED,
        agreement=application.agreement,
    )
    document = Document(application)
    document.add_licensed_quotas()
    assert licensed_quota.quota_order_number_id in document.balance_dict
    quota_balance = document.balance_dict[licensed_quota.quota_order_number_id]
    assert quota_balance.y1_balance == 0
    assert quota_balance.yx_balance == licensed_quota.opening_balance
    assert quota_balance.country == agreement.slug
    assert quota_balance.method == dict(
        ExtendedQuota.QUOTA_CHOICES)[ExtendedQuota.LICENSED]
    assert quota_balance.quota_order_number_id == licensed_quota.quota_order_number_id
    assert quota_balance.origin_quota == 'Yes'
    assert quota_balance.scope == licensed_quota.scope
    assert quota_balance.addendum == licensed_quota.addendum
    assert quota_balance.measurement_unit_code == 'KGM'
    assert quota_balance.validity_start_date_2019 == datetime(2018, 1, 1, 0, 0)
    assert quota_balance.validity_end_date_2019 == datetime(2019, 12, 31, 0, 0)
def test_get_commodities_for_local_sivs(measures,
                                        expected_local_sivs_commodities_only):
    AgreementFactory(country_name='Espana',
                     slug='spain',
                     country_codes=['1011'])
    application = Application(country_profile='spain')
    for measure_properties in measures:
        measure = get_mfn_siv_product(
            measure_properties.pop('goods_nomenclature_item_id'),
            geographical_area_id='1011',
            measure_type_id='142',
            measure_quota_number='99999',
            **measure_properties)
        CurrentMeasureFactory(
            measure_sid=measure.measure_sid,
            geographical_area_id=measure.geographical_area_id,
            measure_type_id=measure.measure_type_id,
            validity_start_date=measure.validity_start_date,
            validity_end_date=measure.validity_end_date,
            ordernumber=measure.quota_order_number_id,
            goods_nomenclature_item_id=measure.goods_nomenclature_item_id,
            reduction_indicator=measure.reduction_indicator,
        )

    document = Document(application)
    actual_local_sivs, actual_local_sivs_commodities_only = document.get_commodities_for_local_sivs(
    )
    assert actual_local_sivs_commodities_only == expected_local_sivs_commodities_only
    assert len(actual_local_sivs) == len(expected_local_sivs_commodities_only)
    for local_siv in actual_local_sivs:
        assert local_siv.goods_nomenclature_item_id in expected_local_sivs_commodities_only
Пример #4
0
def test_agreement_raises_error_when_no_country_codes_are_associated():
    AgreementFactory(
        slug='israel',
        country_name='israel',
        version='2.0',
        country_codes=[],
        agreement_date='2019-01-01',
    )

    with pytest.raises(CountryProfileError) as error:
        Application('israel')
    assert str(error.value) == 'Country profile has no country codes'
def test_add_first_come_serve_quotas(
    has_extended_info,
    expected_origin_quota,
    expected_addendum,
    expected_scope,
    expected_measurement_unit_code,
):
    geographical_area_id = '1011'
    country_profile = 'spain'
    quota_order_number = QuotaOrderNumberFactory()

    quota_definition = QuotaDefinitionFactory(
        quota_order_number=quota_order_number,
        initial_volume=2000,
        volume=3000,
        measurement_unit_code='FS')
    QuotaOrderNumberOriginFactory(
        quota_order_number_sid=quota_order_number.quota_order_number_sid,
        geographical_area_id=geographical_area_id,
    )
    agreement = AgreementFactory(country_name=country_profile.capitalize(),
                                 slug=country_profile,
                                 country_codes=[geographical_area_id])
    if has_extended_info:
        ExtendedQuotaFactory(
            quota_type=ExtendedQuota.FIRST_COME_FIRST_SERVED,
            agreement=agreement,
            scope='FCFS Scope',
            addendum='FCFS Addendum',
            measurement_unit_code='KGM',
            is_origin_quota=True,
            quota_order_number_id=quota_order_number.quota_order_number_id)

    application = Application(country_profile=country_profile)
    document = Document(application)
    document.add_first_come_first_serve_quotas()
    assert quota_order_number.quota_order_number_id in document.balance_dict
    quota_balance = document.balance_dict[
        quota_order_number.quota_order_number_id]
    assert quota_balance.quota_order_number_id == quota_order_number.quota_order_number_id
    assert quota_balance.y1_balance == quota_definition.initial_volume
    assert quota_balance.yx_balance == quota_definition.volume
    assert quota_balance.country == agreement.slug
    assert quota_balance.method == dict(
        ExtendedQuota.QUOTA_CHOICES)[ExtendedQuota.FIRST_COME_FIRST_SERVED]
    assert quota_balance.validity_start_date_2019 == datetime(2018, 1, 1, 0, 0)
    assert quota_balance.validity_end_date_2019 == datetime(2019, 12, 31, 0, 0)

    assert quota_balance.origin_quota == expected_origin_quota
    assert quota_balance.scope == expected_scope
    assert quota_balance.addendum == expected_addendum
    assert quota_balance.measurement_unit_code == expected_measurement_unit_code
def test_check_for_quotas_is_true():
    SimpleCurrentMeasureFactory(
        measure_type_id='143',
        geographical_area_id='1234',
        ordernumber=1,
        validity_start_date=datetime.today(),
    )
    AgreementFactory(country_name='Espana',
                     slug='spain',
                     country_codes=['1234'])
    application = Application(country_profile='spain')
    document = Document(application)
    assert document.check_for_quotas() is True
Пример #7
0
def test_agreement_properties_set():
    AgreementFactory(
        slug='israel',
        country_name='israel',
        version='2.0',
        country_codes=['IS', '2334'],
        agreement_date='2019-01-01',
    )
    application = Application('israel')
    assert application.agreement.slug == 'israel'
    assert application.agreement.geo_ids == "'IS', '2334'"
    assert application.agreement.agreement_date_short == '01/01/2019'
    assert application.agreement.agreement_date_long == '1 January 2019'
Пример #8
0
def test_main_with_no_quotas(mock_create_document):
    mock_create_document.return_value = None
    agreement = AgreementFactory(
        slug='israel',
        country_name='israel',
        version='2.0',
        country_codes=['IS', '2334'],
        agreement_date='2019-01-01',
        document_status=DocumentStatus.UNAVAILABLE
    )
    application = Application('israel')
    application.main()
    agreement.refresh_from_db()
    assert agreement.document_status == DocumentStatus.AVAILABLE
    assert mock_create_document.called is True
    expected_context = {
        'AGREEMENT_NAME': agreement.agreement_name,
        'VERSION': '2.0',
        'AGREEMENT_DATE': '1 January 2019',
        'AGREEMENT_DATE_SHORT': '01/01/2019',
        'COUNTRY_NAME': 'israel',
        'TARIFF_WIDTH_CLASSIFICATION': '400',
        'TARIFF_WIDTH_DUTY': '1450',
        'TARIFF_TABLE_ROWS': [],
        'WIDTH_QUOTA_NUMBER': '8',
        'WIDTH_ORIGIN_QUOTA': '7',
        'WIDTH_COMMODITY_CODE': '11',
        'WIDTH_PREFERENTIAL_QUOTA_DUTY_RATE': '22',
        'WIDTH_QUOTA_VOLUME': '16',
        'WIDTH_QUOTA_OPEN_DATE': '10',
        'WIDTH_QUOTA_CLOSE_DATE': '10',
        'WIDTH_2019_QUOTA_VOLUME': '16',
        'QUOTA_TABLE_ROWS': [],
        'HAS_QUOTAS': False
    }

    mock_create_document.assert_called_with(expected_context)
def test_get_measure_conditions():
    duty_amount = 200
    measure = get_mfn_siv_product(1, duty_amount=duty_amount)
    AgreementFactory(country_name='Espana',
                     slug='spain',
                     country_codes=['1011'])
    application = Application(country_profile='spain')
    document = Document(application)
    actual_result = document.get_measure_conditions("'103'")
    assert len(actual_result) == 1

    assert measure.measure_sid in actual_result
    actual_measure_condition = actual_result[measure.measure_sid]
    assert actual_measure_condition.condition_duty_amount == duty_amount
    assert actual_measure_condition.measure_sid == measure.measure_sid
    assert actual_measure_condition.measure_condition_sid == 0
    assert actual_measure_condition.component_sequence_number == 1
    assert actual_measure_condition.condition_code == 'V'
def test__get_duties():
    measure = get_mfn_siv_product(1,
                                  geographical_area_id='1011',
                                  measure_type_id='143')
    current_measure = CurrentMeasureFactory(
        measure_sid=measure.measure_sid,
        geographical_area_id=measure.geographical_area_id,
        measure_type_id=measure.measure_type_id,
        validity_start_date=measure.validity_start_date,
        validity_end_date=measure.validity_end_date,
        ordernumber=measure.quota_order_number_id,
        goods_nomenclature_item_id=measure.goods_nomenclature_item_id,
        reduction_indicator=measure.reduction_indicator,
    )
    AgreementFactory(country_name='Espana',
                     slug='spain',
                     country_codes=['1011'])
    application = Application(country_profile='spain')

    GoodsNomenclatureFactory(
        goods_nomenclature_item_id=current_measure.goods_nomenclature_item_id)
    document = Document(application)
    actual_duties = list(document._get_duties("'143', '146'"))
    assert len(actual_duties) == 1
    actual_duty = actual_duties[0]

    expected_duty = {
        'additional_code_id': None,
        'additional_code_type_id': None,
        'duty_amount': None,
        'duty_expression_id': None,
        'geographical_area_id': '1011',
        'goods_nomenclature_item_id': '1',
        'measure_sid': current_measure.measure_sid,
        'measure_type_id': '143',
        'measurement_unit_code': None,
        'measurement_unit_qualifier_code': None,
        'monetary_unit_code': None,
        'ordernumber': '10',
        'reduction_indicator': 5,
        'validity_end_date': datetime(2019, 4, 2, 1, 0, tzinfo=timezone.utc),
        'validity_start_date': datetime(2019, 5, 1, 1, 0, tzinfo=timezone.utc)
    }
    assert actual_duty == expected_duty
def test_get_quota_order_numbers(quota_order_numbers, has_quotas, expected_q):
    AgreementFactory(country_name='Espana',
                     slug='spain',
                     country_codes=['1011'])
    application = Application(country_profile='spain')
    ignored_measure = get_mfn_siv_product(1,
                                          geographical_area_id='1011',
                                          measure_type_id='142',
                                          measure_quota_number='99999')
    CurrentMeasureFactory(
        measure_sid=ignored_measure.measure_sid,
        geographical_area_id=ignored_measure.geographical_area_id,
        measure_type_id=ignored_measure.measure_type_id,
        validity_start_date=ignored_measure.validity_start_date,
        validity_end_date=ignored_measure.validity_end_date,
        ordernumber=ignored_measure.quota_order_number_id,
        goods_nomenclature_item_id=ignored_measure.goods_nomenclature_item_id,
        reduction_indicator=ignored_measure.reduction_indicator,
    )

    for quota_order_number in quota_order_numbers:
        measure = get_mfn_siv_product(1,
                                      geographical_area_id='1011',
                                      measure_type_id='143',
                                      measure_quota_number=quota_order_number)
        CurrentMeasureFactory(
            measure_sid=measure.measure_sid,
            geographical_area_id=measure.geographical_area_id,
            measure_type_id=measure.measure_type_id,
            validity_start_date=measure.validity_start_date,
            validity_end_date=measure.validity_end_date,
            ordernumber=measure.quota_order_number_id,
            goods_nomenclature_item_id=measure.goods_nomenclature_item_id,
            reduction_indicator=measure.reduction_indicator,
        )
    document = Document(application)
    document.get_quota_order_numbers()
    assert document.has_quotas is has_quotas
    assert document.q == expected_q
    assert len(document.quota_order_number_list) == len(expected_q)
    for qon in document.quota_order_number_list:
        assert qon.quota_order_number_id in expected_q
def test_get_duties(instrument_type, measure_type_id, expected_duty_list,
                    expected_measure_list, expected_commodity_list,
                    expected_quota_order_number_list):
    measure = get_mfn_siv_product(1,
                                  geographical_area_id='1011',
                                  measure_type_id=measure_type_id)
    current_measure = CurrentMeasureFactory(
        measure_sid=measure.measure_sid,
        geographical_area_id=measure.geographical_area_id,
        measure_type_id=measure.measure_type_id,
        validity_start_date=measure.validity_start_date,
        validity_end_date=measure.validity_end_date,
        ordernumber=measure.quota_order_number_id,
        goods_nomenclature_item_id=measure.goods_nomenclature_item_id,
        reduction_indicator=measure.reduction_indicator,
    )
    GoodsNomenclatureFactory(
        goods_nomenclature_item_id=current_measure.goods_nomenclature_item_id)
    AgreementFactory(country_name='Espana',
                     slug='spain',
                     country_codes=['1011'])
    application = Application(country_profile='spain')
    document = Document(application)
    document.get_duties(instrument_type)

    assert len(document.duty_list) == len(expected_duty_list)
    if document.duty_list:
        assert_object(document.duty_list[0], expected_duty_list[0])

    assert len(document.measure_list) == len(expected_measure_list)
    if document.measure_list:
        assert_object(document.measure_list[0], expected_measure_list[0])

    assert len(document.commodity_list) == len(expected_commodity_list)
    if document.commodity_list:
        assert_object(document.commodity_list[0], expected_commodity_list[0])

    assert len(document.quota_order_number_list) == len(
        expected_quota_order_number_list)
    if document.quota_order_number_list:
        assert_object(document.quota_order_number_list[0],
                      expected_quota_order_number_list[0])
def test_get_quota_definitions(quota_order_number_id, add_balance,
                               expected_quota_order_number_sid,
                               expected_definition):
    geographical_area_id = '1011'
    country_profile = 'spain'

    quota_order_number = QuotaOrderNumberFactory(
        id=quota_order_number_id,
        quota_order_number_sid=str(quota_order_number_id))
    if not quota_order_number_id.startswith('094'):
        QuotaDefinitionFactory(
            quota_order_number_id=quota_order_number.id,
            quota_order_number_sid=quota_order_number.quota_order_number_sid,
            initial_volume=2000,
            volume=3000,
            measurement_unit_code='')

    AgreementFactory(country_name=country_profile.capitalize(),
                     slug=country_profile,
                     country_codes=[geographical_area_id])
    application = Application(country_profile=country_profile)

    document = Document(application)
    document.has_quotas = True

    if add_balance:
        qb = get_quota_balance(
            quota_order_number_id=quota_order_number.quota_order_number_id,
            y1_balance=6000,
        )
        document.balance_dict[quota_order_number.quota_order_number_id] = qb

    document.q = [quota_order_number.quota_order_number_id]
    qon = QuotaOrderNumber(quota_order_number.quota_order_number_id)
    assert document.quota_definition_list == []
    document.quota_order_number_list = [qon]
    document.get_quota_definitions()
    assert len(qon.quota_definition_list) == 1
    assert qon.quota_definition_list[0].quota_order_number_id == str(
        quota_order_number_id)
    assert len(document.quota_definition_list) == 1
    assert_object(document.quota_definition_list[0], expected_definition)
def test_create_document(
    mock_render_to_string,
    mock_write,
    context,
    force,
    expected_template,
    expected_document_xml,
    expected_change,
    raise_write_exception,
):
    fake_file_name = 'fake_file.txt'

    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
    agreement = AgreementFactory(country_name='Espana',
                                 slug='spain',
                                 country_codes=['1011'])
    application = Application(country_profile='spain',
                              force_document_generation=force)
    document = Document(application)
    document.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 = AgreementDocumentHistory.objects.get(
            agreement=agreement)
        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 AgreementDocumentHistory.objects.filter(
            agreement=agreement).exists() is False
def test_write():
    file_contents = 'XML'
    agreement = AgreementFactory(country_name='Espana',
                                 slug='spain',
                                 country_codes=['1011'])
    application = Application(country_profile='spain')
    document = Document(application)
    actual_remote_file_name = document.write(file_contents)
    agreement.refresh_from_db()
    assert agreement.document.name == actual_remote_file_name
    with zipfile.ZipFile(agreement.document) as fh:
        actual_files = [f.filename for f in fh.filelist]
        assert set(actual_files) == {
            '[Content_Types].xml', '_rels/.rels', 'word/webSettings.xml',
            'word/footer2.xml', 'word/settings.xml', 'word/footnotes.xml',
            'word/footer1.xml', 'word/fontTable.xml', 'word/header1.xml',
            'word/document.xml', 'word/endnotes.xml', 'word/styles.xml',
            'word/numbering.xml', 'word/_rels/document.xml.rels',
            'word/theme/theme1.xml', 'customXml/item1.xml',
            'customXml/itemProps1.xml', 'customXml/_rels/item1.xml.rels'
        }
        actual_document_xml = fh.read('word/document.xml')
        assert actual_document_xml == bytes(file_contents, 'utf-8')
Пример #16
0
def test_combine_duties():
    measure = get_measure(
        validity_start_date=datetime.now(),
    )
    duties = [
        get_duty_object(
            measure_sid=1,
            validity_start_date=datetime(2019, 1, 1),
            validity_end_date=datetime(2019, 8, 31),
            duty_expression_id='12',
        ),
        get_duty_object(
            measure_sid=2,
            validity_start_date=datetime(2019, 9, 2),
            validity_end_date=datetime(2019, 12, 1),
            duty_amount=2,
            duty_expression_id='12',
        ),
    ]
    measure.duty_list = duties
    AgreementFactory(country_name='Espana', slug='spain', country_codes=['1011'])
    application = Application(country_profile='spain')
    measure.combine_duties(application)
    assert measure.combined_duty == 'CAD - + (AC + AC) 100%'
Пример #17
0
def test_unknown_country_profile_raises_exception():
    with pytest.raises(CountryProfileError) as error:
        Application('israel')
    assert str(error.value) == 'Country profile does not exist'
Пример #18
0
def get_application(country_profile):
    AgreementFactory(country_name=country_profile, slug=country_profile)
    return Application(country_profile)
Пример #19
0
def generate_fta_document(country_profile, force=False):
    app = FTAApplication(
        country_profile=country_profile,
        force_document_generation=force,
    )
    app.main()