/
test_input_output.py
154 lines (119 loc) · 5.59 KB
/
test_input_output.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pytest
import pathlib as pl
import csv
import itertools
from .testsuite import get_module_name
from .. import prepare_magic_file
import sys
from io import StringIO
import contextlib
def get_data(name: str, data_dir:str="data") -> pl.Path:
current_module = sys.modules[__name__]
return (pl.Path(current_module.__file__).parent / f"{data_dir}/{name}").resolve()
def reference_solution_find_all_files(f: pl.Path) -> "list[pl.Path]":
return list(f.parent.glob(f.name))
def test_find_all_files(function_to_test):
f = pl.Path("data/")
assert function_to_test(f) == reference_solution_find_all_files(f)
def reference_solution_count_parents(f: pl.Path) -> int:
return len([ob for ob in f.parent.glob("*") if ob.is_dir()])
def test_count_parents(function_to_test):
f = pl.Path(sys.modules[__name__].__file__).parent.parent
print(f)
assert function_to_test(f) == reference_solution_count_parents(f)
def reference_solution_read_file(f: pl.Path) -> "list[str]":
with open(f) as lines:
return [l for l in lines.readlines()]
def test_read_file(function_to_test):
for fp in ["lines.txt", "example.csv"]:
data = get_data(fp)
assert function_to_test(data) == reference_solution_read_file(data)
def reference_solution_write_file(f: pl.Path, lines: "list[str]") -> None:
with open(f, "w") as f:
f.writelines(lines)
def test_write_file(function_to_test):
lines = ["python tutorial 2023"]
f = pl.Path("test.txt")
function_to_test(f)
with open(f) as input_file:
assert input_file.readlines() == lines
def reference_solution_exercise1(f: pl.Path) -> "dict[str, list[int]]":
with open(f) as lines:
reader = csv.reader(lines)
headers = next(reader)
transposed = {k:list(v) for k,v in zip(headers, itertools.zip_longest(*(l for l in reader)))}
return transposed
def reference_solution_print_odd(n: int) -> None:
for i in range(n):
if i % 2 != 0:
print(i)
@pytest.mark.parametrize("n", [1, 2, 3, 4, 5])
def test_print_odd(function_to_test, n:int):
#redirect stdout of function_to_test to a string
solution_stdout = StringIO()
with contextlib.redirect_stdout(solution_stdout):
function_to_test(n)
reference_stdout = StringIO()
with contextlib.redirect_stdout(reference_stdout):
reference_solution_print_odd(n)
assert reference_stdout.getvalue() == solution_stdout.getvalue()
def reference_solution_print_salutation() -> None:
name = input()
print(f"Hello {name}")
def test_print_salutation(function_to_test):
#redirect stdout of function_to_test to a string
solution_stdout = StringIO()
solution_stdin = StringIO()
with contextlib.redirect_stdout(solution_stdout), contextlib.redirect_stdin(solution_stdin):
function_to_test()
reference_stdout = StringIO()
reference_stdin = StringIO(solution_stdin.getvalue())
with contextlib.redirect_stdout(reference_stdout), contextlib.redirect_stdin(reference_stdin):
reference_solution_print_salutation()
assert reference_stdout.getvalue() == solution_stdout.getvalue()
def reference_solution_read_write_file(input_file: pl.Path, output_file: pl.Path) -> None:
with open(input_file) as read_file, open(output_file, "w") as write_file:
for line in read_file.readlines():
write_file.write(f"{line}, {len(line)}")
def test_read_write_file(function_to_test):
input_file = get_data("lines.txt")
output_file = pl.Path("output.txt")
test_output_file = pl.Path("test_output.txt")
function_to_test(input_file, output_file)
reference_solution_read_write_file(input_file, test_output_file)
with open(output_file) as output_file, open(test_output_file) as tes:
assert output_file.readlines() == tes.readlines()
def test_exercise1(function_to_test):
f = get_data("example.csv")
assert function_to_test(f) == reference_solution_exercise1(f)
def reference_solution_exercise2(f: pl.Path)-> int:
with open(f) as lines:
return len(list(itertools.chain.from_iterable([l.split() for l in lines.readlines()])))
def test_exercise2(function_to_test):
f = get_data("lines.txt")
assert function_to_test(f) == reference_solution_exercise2(f)
def reference_solution_exercise3(f: pl.Path) -> "dict[str, int]":
with open(f) as lines:
res = {k: len(list(v)) for k,v in itertools.groupby(sorted([l for l in itertools.chain(*itertools.chain(lines.readlines())) if l.isalpha()]))}
return res
def test_exercise3(function_to_test):
f = get_data("lines.txt")
assert function_to_test(f) == reference_solution_exercise3(f)
def reference_solution_exercise4(english: pl.Path, dictionary: pl.Path) -> "list[(str, str)]":
with open(english) as english_file:
english_reader = csv.reader(english_file)
english_words = [w for w, *rest in english_reader]
with open(dictionary) as dict_file:
dict_reader = csv.reader(dict_file)
next(dict_reader)
translations = {en:it for index,it, en,*rest in dict_reader}
return [(e, translations[e]) for e in english_words if e in translations.keys()]
def test_exercise4(function_to_test):
words = get_data("english.csv")
dictionary = get_data("dict.csv")
assert function_to_test(words, dictionary) == reference_solution_exercise4(words, dictionary)
def reference_solution_exercise5(secret_file: pl.Path) -> str:
return prepare_magic_file.decode_secret_message(secret_file)
def test_exercise5(function_to_test):
message = get_data("secret_message.dat")
assert function_to_test(message) == reference_solution_exercise5(message)