-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathtest_storage.py
More file actions
48 lines (40 loc) · 1.6 KB
/
Copy pathtest_storage.py
File metadata and controls
48 lines (40 loc) · 1.6 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
import tempfile
import unittest
from pathlib import Path
from mini_contacts import storage
class StorageTests(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.tmp_path = Path(tmp.name)
def test_round_trip(self):
path = self.tmp_path / "contacts.csv"
storage.add_contact(
str(path), "Alice", "alice@example.com", "555-1234"
)
expected = {
"name": "Alice",
"email": "alice@example.com",
"phone": "555-1234",
}
self.assertEqual(storage.read_contacts(str(path)), [expected])
def test_header_written_once(self):
path = self.tmp_path / "contacts.csv"
storage.add_contact(
str(path), "Alice", "alice@example.com", "555-1234"
)
storage.add_contact(str(path), "Bob", "bob@example.com", "555-5678")
self.assertEqual(path.read_text().count("name,email,phone"), 1)
def test_read_missing_file_returns_empty(self):
missing = self.tmp_path / "missing.csv"
self.assertEqual(storage.read_contacts(str(missing)), [])
def test_short_row_filled_with_blanks(self):
path = self.tmp_path / "contacts.csv"
path.write_text("name,email,phone\nAlice,alice@example.com\n")
contacts = storage.read_contacts(str(path))
self.assertEqual(contacts[0]["phone"], "")
def test_add_blank_field_raises(self):
with self.assertRaises(ValueError):
storage.add_contact(
str(self.tmp_path / "c.csv"), "Alice", "", "555-1234"
)