Count duplicates

This commit is contained in:
Matt Hughes
2019-11-15 16:05:43 -05:00
committed by Matt Hughes
parent ce28ccb497
commit 66b970c11d
2 changed files with 26 additions and 2 deletions

View File

@@ -48,7 +48,7 @@ class Command(BaseCommand):
uid_mappings = json.load(f)
slug = options['saml_provider_slug']
email_map = { m['email']: {'uid': m['student_key'], 'updated': False} for m in uid_mappings }
email_map = { m['email']: {'uid': m['student_key'], 'updated': False, 'counted': False } for m in uid_mappings }
user_queryset = User.objects.prefetch_related('social_auth').filter(social_auth__uid__startswith=slug + ':')
users = [u for u in user_queryset]
@@ -70,8 +70,16 @@ class Command(BaseCommand):
updated += 1
not_previously_linked = 0
updated = 0
duplicated_in_mapping = 0
for mapping in uid_mappings:
not_previously_linked += not email_map[mapping['email']]['updated']
info_for_email = email_map[mapping['email']]
if not info_for_email['counted']:
not_previously_linked += not info_for_email['updated']
updated += info_for_email['updated']
info_for_email['counted'] = True
else:
duplicated_in_mapping += 1
log.info(
'Number of users with {slug} UserSocialAuth records for which there was no mapping in the provided file: {missed}'.format(
@@ -83,5 +91,8 @@ class Command(BaseCommand):
slug=slug,
not_previously_linked=not_previously_linked
))
log.info('Number of mappings in the mapping file where the identified user has already been processed: {duplicated_in_mapping}'.format(
duplicated_in_mapping=duplicated_in_mapping
))

View File

@@ -149,3 +149,16 @@ class TestMigrateSamlUids(TestCase):
for ind, auth in enumerate(auths):
auth.refresh_from_db()
assert auth.uid == self._format_slug_urn_pair(self.provider_slug, new_urn + six.text_type(ind))
@patch(_COMMAND_PATH + '.log')
def test_learner_duplicated_in_mapping(self, mock_log):
auth = UserSocialAuthFactory()
email = auth.user.email
new_urn = '9001'
mock_info = mock_log.info
self._call_command('[{}]'.format(
','.join([self._format_email_uid_pair(email, new_urn) for _ in range(5)])
))
mock_info.assert_any_call('Number of mappings in the mapping file where the identified user has already been processed: 4')