-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_demo.py
87 lines (64 loc) · 2.3 KB
/
test_demo.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
from collections import defaultdict
from snapshottest.file import FileSnapshot
def api_client_get(url):
return {
"url": url,
}
def test_me_endpoint(snapshot):
"""Testing the API for /me"""
my_api_response = api_client_get("/me")
snapshot.assert_match(my_api_response)
def test_unicode(snapshot):
"""Simple test with unicode"""
expect = "pépère"
snapshot.assert_match(expect)
class SomeObject(object):
def __init__(self, value):
self.value = value
def __repr__(self):
return "SomeObject({})".format(repr(self.value))
def test_object(snapshot):
"""
Test a snapshot with a custom object. The object will be represented in the
snapshot using `snapshottest.GenericRepr`. The snapshot will only match if the
object's repr remains the same.
"""
test_value = SomeObject(3)
snapshot.assert_match(test_value)
def test_file(snapshot, tmpdir):
"""
Test a file snapshot. The file contents will be saved in a sub-folder of the
snapshots folder. Useful for large files (e.g. media files) that aren't suitable
for storage as text inside the snap_***.py file.
"""
temp_file = tmpdir.join("example.txt")
temp_file.write("Hello, world!")
snapshot.assert_match(FileSnapshot(str(temp_file)))
def test_multiple_files(snapshot, tmpdir):
"""
Each file is stored separately with the snapshot's name inside the module's file
snapshots folder.
"""
temp_file1 = tmpdir.join("example1.txt")
temp_file1.write("Hello, world 1!")
snapshot.assert_match(FileSnapshot(str(temp_file1)))
temp_file1 = tmpdir.join("example2.txt")
temp_file1.write("Hello, world 2!")
snapshot.assert_match(FileSnapshot(str(temp_file1)))
class ObjectWithBadRepr(object):
def __repr__(self):
return "#"
def test_nested_objects(snapshot):
obj = ObjectWithBadRepr()
dict_ = {"key": obj}
defaultdict_ = defaultdict(list, [("key", [obj])])
list_ = [obj]
tuple_ = (obj,)
set_ = set((obj,))
frozenset_ = frozenset((obj,))
snapshot.assert_match(dict_, "dict")
snapshot.assert_match(defaultdict_, "defaultdict")
snapshot.assert_match(list_, "list")
snapshot.assert_match(tuple_, "tuple")
snapshot.assert_match(set_, "set")
snapshot.assert_match(frozenset_, "frozenset")