-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_insert_quote.py
More file actions
130 lines (102 loc) · 5.05 KB
/
Copy pathtest_insert_quote.py
File metadata and controls
130 lines (102 loc) · 5.05 KB
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
#!/usr/bin/env python3
import tempfile
import time
import unittest
import yaml
from insert_quote import yaml_insert
FIXTURE = [
{"time": "08:00", "time_name": "eight o'clock", "source": "Book A", "author": "Author A", "quote": "Quote at eight."},
{"time": "12:00", "time_name": "noon", "source": "Book B", "author": "Author B", "quote": "Quote at noon."},
{"time": "18:00", "time_name": "six o'clock", "source": "Book C", "author": "Author C", "quote": "Quote at six."},
]
def make_temp_yaml(content):
f = tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False)
f.write(yaml.dump(content, allow_unicode=True, sort_keys=True))
f.close()
return f.name
def load_yaml(path):
with open(path) as f:
return yaml.safe_load(f)
def t(hhmm):
return time.strptime(hhmm, "%H:%M")
class TestInsertionPosition(unittest.TestCase):
def test_insert_in_middle(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("10:00"), "ten o'clock", "Book D", "Author D", "Quote at ten.", filepath=path)
result = load_yaml(path)
times = [e["time"] for e in result]
self.assertEqual(times, ["08:00", "10:00", "12:00", "18:00"])
def test_insert_before_first(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("06:00"), "six in the morning", "Book E", "Author E", "Early quote.", filepath=path)
result = load_yaml(path)
self.assertEqual(result[0]["time"], "06:00")
self.assertEqual(len(result), 4)
def test_insert_after_last(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("23:00"), "eleven at night", "Book F", "Author F", "Late quote.", filepath=path)
result = load_yaml(path)
self.assertEqual(result[-1]["time"], "23:00")
self.assertEqual(len(result), 4)
def test_insert_at_same_time_as_existing(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("12:00"), "noon", "Book G", "Author G", "Another noon quote.", filepath=path)
result = load_yaml(path)
noon_entries = [e for e in result if e["time"] == "12:00"]
self.assertEqual(len(noon_entries), 2)
class TestDataIntegrity(unittest.TestCase):
def test_all_fields_written(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("10:00"), "ten o'clock", "My Book", "My Author", "My quote.", filepath=path)
result = load_yaml(path)
entry = next(e for e in result if e["source"] == "My Book")
self.assertEqual(entry["time"], "10:00")
self.assertEqual(entry["time_name"], "ten o'clock")
self.assertEqual(entry["source"], "My Book")
self.assertEqual(entry["author"], "My Author")
self.assertEqual(entry["quote"], "My quote.")
def test_existing_entries_preserved(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("10:00"), "ten o'clock", "Book D", "Author D", "New quote.", filepath=path)
result = load_yaml(path)
sources = [e["source"] for e in result]
self.assertIn("Book A", sources)
self.assertIn("Book B", sources)
self.assertIn("Book C", sources)
def test_output_is_valid_yaml(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("10:00"), "ten o'clock", "Book D", "Author D", "New quote.", filepath=path)
result = load_yaml(path)
self.assertIsInstance(result, list)
self.assertTrue(all(isinstance(e, dict) for e in result))
class TestSpecialCharacters(unittest.TestCase):
def test_double_quotes_in_quote(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("10:00"), "ten o'clock", "Ulysses", "James Joyce",
'"But wait till I tell you," he said.', filepath=path)
result = load_yaml(path)
entry = next(e for e in result if e["source"] == "Ulysses")
self.assertEqual(entry["quote"], '"But wait till I tell you," he said.')
def test_single_quotes_in_quote(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("10:00"), "ten o'clock", "Some Book", "Some Author",
"It's a fine day, isn't it?", filepath=path)
result = load_yaml(path)
entry = next(e for e in result if e["source"] == "Some Book")
self.assertEqual(entry["quote"], "It's a fine day, isn't it?")
def test_em_dash_and_unicode(self):
path = make_temp_yaml(FIXTURE)
yaml_insert(t("10:00"), "ten o'clock", "Some Book", "Some Author",
"Time\u2014the great healer.", filepath=path)
result = load_yaml(path)
entry = next(e for e in result if e["author"] == "Some Author")
self.assertEqual(entry["quote"], "Time\u2014the great healer.")
def test_mixed_quotes_round_trip(self):
path = make_temp_yaml(FIXTURE)
quote = "\"It's over,\" she said\u2014and meant it."
yaml_insert(t("10:00"), "ten o'clock", "Novel", "Writer", quote, filepath=path)
result = load_yaml(path)
entry = next(e for e in result if e["source"] == "Novel")
self.assertEqual(entry["quote"], quote)
if __name__ == "__main__":
unittest.main()