Projet

Général

Profil

0002-csv_import-ignore-empty-values-when-checking-uniquen.patch

Benjamin Dauvergne, 14 juillet 2020 18:24

Télécharger (3,5 ko)

Voir les différences:

Subject: [PATCH 2/4] csv_import: ignore empty values when checking uniqueness
 (#44805)

 src/authentic2/csv_import.py |  5 ++++
 tests/test_csv_import.py     | 45 ++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
src/authentic2/csv_import.py
576 576
            if header.name == SOURCE_ID:
577 577
                unique_key = (SOURCE_ID, row[SOURCE_NAME].value, cell.value)
578 578
            elif header.key or header.globally_unique or header.unique:
579
                if not cell.value:
580
                    # empty values are not checked
581
                    continue
579 582
                unique_key = (header.name, cell.value)
580 583
            else:
581 584
                continue
......
595 598
        for cell in row:
596 599
            if (not cell.header.globally_unique and not cell.header.unique) or (user and not cell.header.update):
597 600
                continue
601
            if not cell.value:
602
                continue
598 603
            qs = ou_users
599 604
            if cell.header.globally_unique:
600 605
                qs = users
tests/test_csv_import.py
518 518
    assert importer.run()
519 519
    assert importer.has_errors
520 520
    assert importer.rows[0].cells[-1].errors[0].code == 'data-error'
521

  
522

  
523
def test_check_empty_value_for_uniqueness(profile, user_csv_importer_factory):
524
    ou = get_default_ou()
525
    ou.username_is_unique = True
526
    ou.email_is_unique = True
527
    ou.save()
528

  
529
    Attribute.objects.update(required=False)
530
    User.objects.create(username='john.doe', email='john.doe@gmail.com')
531

  
532
    content = '''_source_name,_source_id,username,email,first_name,last_name
533
remote,1,,john.doe1@gmail.com,John,Doe1
534
remote,2,john.doe2,,John,Doe2
535
remote,3,,,John,Doe3
536
remote,4,,,John,Doe4
537
remote,5,,john.doe1@gmail.com,,
538
remote,6,john.doe2,,,
539
remote,7,,john.doe@gmail.com,,
540
remote,8,john.doe,,,
541
'''
542
    for i, line in enumerate(content.splitlines()):
543
        print(i, line.count(','))
544
    importer = user_csv_importer_factory(content)
545
    importer.run()
546
    assert importer.has_errors
547
    assert not importer.errors
548
    assert importer.headers_by_name['username'].unique
549
    assert importer.headers_by_name['username'].globally_unique
550
    assert importer.headers_by_name['email'].unique
551
    assert not importer.headers_by_name['email'].globally_unique
552
    row_errors = {row.line: [error.description for error in row.errors] for row in importer.rows if row.errors}
553
    assert row_errors == {
554
        6: ['Unique constraint on column "email" failed: value already appear on line 2',
555
            'Unique constraint on column "email" failed'],
556
        7: ['Unique constraint on column "username" failed: value already appear on line 3'],
557
        9: ['Unique constraint on column "username" failed']
558
    }
559
    cell_errors = {
560
        row.line: {
561
            cell.header.name: [error for error in cell.errors] for cell in row.cells if cell.errors
562
        }
563
        for row in importer.rows if any(cell.errors for cell in row.cells)
564
    }
565
    assert cell_errors == {}
521
-