From c24abe8f79c77e5a119a9965e11dd31de08989eb Mon Sep 17 00:00:00 2001 From: stuart-madetech <75611506+stuart-madetech@users.noreply.github.com> Date: Thu, 25 Jan 2024 12:53:10 +0000 Subject: [PATCH] EYB import trade associations management command update to cater for non sequential Ids (#2854) * Changing to delete before import as ids cannot be trustd * Adding test coverage to eyb trade association mgmt command --- .../commands/eyb_import_trade_associations.py | 18 ++++----- .../commands/test_import_eyb_data.py | 40 ++++++++++++++++++- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/core/management/commands/eyb_import_trade_associations.py b/core/management/commands/eyb_import_trade_associations.py index 02f281dcc8..a0f066c6cd 100644 --- a/core/management/commands/eyb_import_trade_associations.py +++ b/core/management/commands/eyb_import_trade_associations.py @@ -16,14 +16,14 @@ def handle(self, *args, **options): bucket_name=settings.AWS_STORAGE_BUCKET_NAME_DATA_SCIENCE, ) data = tablib.import_set(file, format='csv', headers=True) + TradeAssociation.objects.all().delete() for item in data: - if not TradeAssociation.objects.filter(trade_association_id=item[0]).exists(): - TradeAssociation.objects.create( - trade_association_id=item[0], - sector_grouping=item[1], - association_name=item[2], - website_link=item[3], - sector=item[4], - brief_description=item[5], - ) + TradeAssociation.objects.create( + trade_association_id=item[0], + sector_grouping=item[1], + association_name=item[2], + website_link=item[3], + sector=item[4], + brief_description=item[5], + ) self.stdout.write(self.style.SUCCESS('All done with trade associations, bye!')) diff --git a/tests/unit/core/management/commands/test_import_eyb_data.py b/tests/unit/core/management/commands/test_import_eyb_data.py index d85460d686..65612be034 100644 --- a/tests/unit/core/management/commands/test_import_eyb_data.py +++ b/tests/unit/core/management/commands/test_import_eyb_data.py @@ -10,8 +10,44 @@ @pytest.mark.django_db def test_import_eyb_data(mgm_cmd): with patch('core.helpers.get_s3_file_stream') as mock_work_function: - call_command(mgm_cmd, stdout=StringIO()) - assert mock_work_function.called + with patch('tablib.import_set') as mock_import_set: + mock_import_set.return_value = tablib.Dataset( + [ + 'TRADE_ASSOCIATION_0001', + 'Manufacturing, Energy and Infrastructure 1', + 'UK H2 Mobility 1', + 'http://www.ukh2mobility.co.uk/', + 'Energy', + 'Some test description here', + ], + [ + 'TRADE_ASSOCIATION_0002', + 'Manufacturing, Food and Infrastructure 2', + 'UK H2 Mobility 2', + 'http://www.ukh2mobility.co.uk/', + 'Food and Drink', + 'Some test description here', + ], + [ + 'TRADE_ASSOCIATION_0003', + 'Manufacturing, Technology and Infrastructure 3', + 'UK H2 Mobility 3', + 'http://www.ukh2mobility.co.uk/', + 'Technology', + 'Some test description here', + ], + headers=[ + 'trade_assocation_id', + 'sector_grouping', + 'association_name', + 'website_link', + 'sector', + 'brief_description', + ], + ) + mock_work_function.return_value = None + call_command(mgm_cmd, stdout=StringIO()) + assert mock_work_function.called @pytest.mark.parametrize('mgm_cmd', [('eyb_import_salary_data')])