aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_obfuscator.py
blob: cc7d2c1281f2e6ec5da2180a55d4fedd232080c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from obfuscator.obfuscate import obfuscate

# Check if the function does what its supposed to and can obfuscate 
# valid PII fields in a list of dictionaries
def test_obfuscate_data_with_valid_pii_fields():
    data = [
        {
            "student_id": "1234",
            "name": "John Smith",
            "course": "Software",
            "email_address": "j.smith@email.com",
        },
        {
            "student_id": "5678",
            "name": "Jane Doe",
            "course": "Data Science",
            "email_address": "j.doe@email.com",
        },
    ]
    pii_fields = ["name", "email_address"]
    expected = [
        {
            "student_id": "1234",
            "name": "***",
            "course": "Software",
            "email_address": "***",
        },
        {
            "student_id": "5678",
            "name": "***",
            "course": "Data Science",
            "email_address": "***",
        },
    ]

    result = obfuscate(data, pii_fields)
    assert result == expected

# Check if the function can obfuscate data even when some PII
# fields are missing from some of the data, returning a list of dictionaries
# but with the missing PII fields obfuscated and the rest of the data intact
def test_obfuscate_data_with_missing_pii_field():
    data = [
        {"student_id": "1234", "name": "John Smith", "course": "Software"},
        {
            "student_id": "5678",
            "name": "Jane Doe",
            "course": "Data Science",
            "email_address": "j.doe@email.com",
        },
    ]
    pii_fields = ["name", "email_address"]
    expected = [
        {"student_id": "1234", "name": "***", "course": "Software"},
        {
            "student_id": "5678",
            "name": "***",
            "course": "Data Science",
            "email_address": "***",
        },
    ]

    result = obfuscate(data, pii_fields)
    assert result == expected

# Check if the function can handle an empty list of data, returning an empty list
def test_obfuscate_data_with_no_data():
    data = []
    pii_fields = ["name", "email_address"]
    expected = []

    result = obfuscate(data, pii_fields)
    assert result == expected

# Check if the function can handle an empty list of PII fields, returning the data as is
# without mutating it
def test_obfuscate_data_with_empty_pii_fields():
    data = [
        {
            "student_id": "1234",
            "name": "John Smith",
            "course": "Software",
            "email_address": "j.smith@email.com",
        }
    ]
    pii_fields = []
    expected = data.copy()

    result = obfuscate(data, pii_fields)
    assert result == expected
git.ajschof.me — hosted by ajschofield — powered by cgit