aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_write.py
blob: eceac28926b4936a15bd08bd831a813ec8b6d4ce (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
import io
import csv
from obfuscator.csv_writer import create_byte_stream


def csv_bytes_to_list(csv_bytes: bytes):
    csv_string = csv_bytes.decode("utf-8")
    f = io.StringIO(csv_string)
    reader = csv.DictReader(f)
    return [dict(row) for row in reader]


def test_create_byte_stream_valid_data():
    data = [
        {"student_id": "1234", "name": "Student 1", "course": "Course 1"},
        {"student_id": "5678", "name": "Student 2", "course": "Course 2"},
    ]
    csv_bytes = create_byte_stream(data)
    result = csv_bytes_to_list(csv_bytes)
    assert result == data


def test_create_byte_stream_empty_data():
    csv_bytes = create_byte_stream([])
    assert csv_bytes == b""


def test_create_byte_stream_handles_quoted_fields():
    data = [
        {"student_id": "1234", "name": 'Student "One"', "course": "Course, A"},
        {"student_id": "5678", "name": 'Student "Two"', "course": "Course, B"},
    ]
    csv_bytes = create_byte_stream(data)
    result = csv_bytes_to_list(csv_bytes)
    assert result == data


def test_create_byte_stream_consistent_header_order():
    data = [
        {"student_id": "1234", "name": "Alice", "course": "Math"},
        {"student_id": "5678", "name": "Bob", "course": "Science"},
    ]
    csv_bytes = create_byte_stream(data)
    csv_string = csv_bytes.decode("utf-8")
    header_line = csv_string.splitlines()[0]
    expected_header = ",".join(data[0].keys())
    assert header_line == expected_header


def test_create_byte_stream_special_characters():
    data = [
        {"student_id": "1234", "name": "Student 1", "course": "Line1\nLine2"},
        {"student_id": "5678", "name": "Student 2", "course": "Value with, comma"},
    ]
    csv_bytes = create_byte_stream(data)
    result = csv_bytes_to_list(csv_bytes)
    assert result == data
git.ajschof.me — hosted by ajschofield — powered by cgit