Skip to content

Commit a53cde7

Browse files
Stop attempting to extract marketing data from nested key (#5851)
1 parent 0d4a621 commit a53cde7

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

datahub/investment_lead/tasks/ingest_eyb_marketing.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
22

3+
from django.db.models import Q
4+
35
from datahub.investment_lead.models import EYBLead
46
from datahub.investment_lead.serializers import CreateEYBLeadMarketingSerializer
57
from datahub.investment_lead.tasks.ingest_eyb_common import (
@@ -54,7 +56,27 @@ def _get_hashed_uuid(self, obj):
5456
return None if obj is None else obj.get('hashed_uuid', None)
5557

5658
def _record_has_no_changes(self, record):
57-
hashed_uuid = self._get_hashed_uuid(record.get('object'))
59+
hashed_uuid = self._get_hashed_uuid(record)
5860
if hashed_uuid and EYBLead.objects.filter(marketing_hashed_uuid=hashed_uuid).exists():
5961
return True
6062
return False
63+
64+
def json_to_model(self, jsn):
65+
serializer = self.serializer_class(data=jsn)
66+
hashed_uuid = self._get_hashed_uuid(jsn)
67+
if serializer.is_valid():
68+
queryset = EYBLead.objects.filter(
69+
Q(user_hashed_uuid=hashed_uuid)
70+
| Q(triage_hashed_uuid=hashed_uuid)
71+
| Q(marketing_hashed_uuid=hashed_uuid),
72+
)
73+
instance, created = queryset.update_or_create(defaults=serializer.validated_data)
74+
if created:
75+
self.created_hashed_uuids.append(hashed_uuid)
76+
else:
77+
self.updated_hashed_uuids.append(hashed_uuid)
78+
else:
79+
self.errors.append({
80+
'index': hashed_uuid,
81+
'errors': serializer.errors,
82+
})

datahub/investment_lead/test/test_tasks/test_ingest_eyb_marketing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_marketing_file_is_ingested(self, caplog, test_marketing_file_path):
141141
"""
142142
initial_eyb_lead_count = EYBLead.objects.count()
143143
initial_ingested_count = IngestedFile.objects.count()
144-
file_contents = file_contents_faker(default_faker='marketing')
144+
file_contents = file_contents_faker(default_faker='marketing', nested=False)
145145
setup_s3_bucket(BUCKET, [test_marketing_file_path], [file_contents])
146146
with caplog.at_level(logging.INFO):
147147
ingest_eyb_marketing_data(BUCKET, test_marketing_file_path)
@@ -164,7 +164,7 @@ def test_marketing_data_ingestion_does_not_update_existing(self, test_marketing_
164164
'content': updated_value,
165165
}),
166166
]
167-
file_contents = file_contents_faker(records)
167+
file_contents = file_contents_faker(records, nested=False)
168168
setup_s3_bucket(BUCKET, [test_marketing_file_path], [file_contents])
169169
ingest_eyb_marketing_data(BUCKET, test_marketing_file_path)
170170
assert EYBLead.objects.count() == 1
@@ -182,7 +182,7 @@ def test_marketing_data_ingestion_does_not_fail_with_empty_records(
182182
EYBLeadFactory(marketing_hashed_uuid=hashed_uuid, utm_content=initial_value)
183183
assert EYBLead.objects.count() == 1
184184
records = []
185-
file_contents = file_contents_faker(records)
185+
file_contents = file_contents_faker(records, nested=False)
186186
setup_s3_bucket(BUCKET, [test_marketing_file_path], [file_contents])
187187
ingest_eyb_marketing_data(BUCKET, test_marketing_file_path)
188188
assert EYBLead.objects.count() == 1
@@ -223,7 +223,7 @@ def test_incoming_marketing_records_trigger_correct_logging(self, caplog):
223223
]
224224

225225
file_path = f'{MARKETING_PREFIX}1.jsonl.gz'
226-
file_contents = file_contents_faker(records)
226+
file_contents = file_contents_faker(records, nested=False)
227227
setup_s3_bucket(BUCKET, [file_path], [file_contents])
228228
with caplog.at_level(logging.INFO):
229229
ingest_eyb_marketing_data(BUCKET, file_path)

datahub/investment_lead/test/test_tasks/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ def setup_s3_client():
1818
return boto3.client('s3', REGION)
1919

2020

21-
def file_contents_faker(records: list[dict] = None, default_faker: str = 'triage') -> bytes:
21+
def file_contents_faker(
22+
records: list[dict] = None, default_faker: str = 'triage', nested: bool = True,
23+
) -> bytes:
2224
"""Create fake file contents with the specified list of records.
2325
2426
Returns compressed lines of JSON encoded with UTF-8.
@@ -35,6 +37,7 @@ def file_contents_faker(records: list[dict] = None, default_faker: str = 'triage
3537
records = [eyb_lead_marketing_record_faker()]
3638
json_lines = [
3739
json.dumps({'object': record}, default=str)
40+
if nested else json.dumps(record, default=str)
3841
for record in records
3942
]
4043
compressed_content = gzip.compress('\n'.join(json_lines).encode('utf-8'))

0 commit comments

Comments
 (0)