Skip to content

Commit

Permalink
EYB import trade associations management command update to cater for …
Browse files Browse the repository at this point in the history
…non sequential Ids (#2854)

* Changing to delete before import as ids cannot be trustd

* Adding test coverage to eyb trade association mgmt command
  • Loading branch information
stuart-mindt authored Jan 25, 2024
1 parent 3814d61 commit c24abe8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
18 changes: 9 additions & 9 deletions core/management/commands/eyb_import_trade_associations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!'))
40 changes: 38 additions & 2 deletions tests/unit/core/management/commands/test_import_eyb_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')])
Expand Down

0 comments on commit c24abe8

Please sign in to comment.