forked from gtalarico/pyairtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_orm_model.py
199 lines (151 loc) · 5.18 KB
/
test_orm_model.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from functools import partial
from unittest import mock
import pytest
from pyairtable.orm import Model
from pyairtable.orm import fields as f
from pyairtable.testing import fake_id, fake_meta, fake_record
@pytest.fixture(autouse=True)
def no_requests(requests_mock):
"""
Fail if any tests in this module try to make network calls.
"""
def test_model_missing_meta():
"""
Test that we throw an exception if Meta is missing.
"""
with pytest.raises(AttributeError):
class Address(Model):
street = f.TextField("Street")
def test_model_missing_meta_attribute():
"""
Test that we throw an exception if Meta is missing a required attribute.
"""
with pytest.raises(ValueError):
class Address(Model):
street = f.TextField("Street")
class Meta:
base_id = "required"
table_name = "required"
# api_key = "required"
def test_model_empty_meta():
"""
Test that we throw an exception when a required Meta attribute is None.
"""
with pytest.raises(ValueError):
class Address(Model):
Meta = fake_meta(api_key=None)
street = f.TextField("Street")
@pytest.mark.parametrize("name", ("exists", "id"))
def test_model_overlapping(name):
"""
Test that we raise ValueError when a subclass of Model defines
a field with the same name as one of Model's properties or methods.
"""
with pytest.raises(ValueError):
type(
"Address",
(Model,),
{
"Meta": fake_meta(),
name: f.TextField(name),
},
)
class FakeModel(Model):
Meta = fake_meta()
one = f.TextField("one")
two = f.TextField("two")
def test_repr():
record = fake_record()
assert repr(FakeModel.from_record(record)) == f"<FakeModel id='{record['id']}'>"
assert repr(FakeModel()) == "<unsaved FakeModel>"
def test_delete():
obj = FakeModel.from_record(record := fake_record())
with mock.patch("pyairtable.Table.delete") as mock_delete:
obj.delete()
mock_delete.assert_called_once_with(record["id"])
def test_delete__unsaved():
obj = FakeModel()
with pytest.raises(ValueError):
obj.delete()
def test_fetch():
obj = FakeModel(id=fake_id())
assert not obj.one
assert not obj.two
with mock.patch("pyairtable.Table.get") as mock_get:
mock_get.return_value = fake_record(one=1, two=2)
obj.fetch()
assert obj.one == 1
assert obj.two == 2
def test_fetch__unsaved():
obj = FakeModel()
with pytest.raises(ValueError):
obj.fetch()
@pytest.mark.parametrize(
"method,args",
[
("comments", []),
("add_comment", ["Hello!"]),
],
)
def test_model_comment_method(method, args):
"""
Test that comments() and add_comment() are passthroughs to Table.
"""
record_id = fake_id()
instance = FakeModel.from_id(record_id, fetch=False)
with mock.patch(f"pyairtable.Table.{method}") as mock_method:
result = getattr(instance, method)(*args)
assert result == mock_method.return_value
mock_method.assert_called_once_with(record_id, *args)
@mock.patch("pyairtable.Table.get")
def test_from_id(mock_get):
class Contact(Model):
Meta = fake_meta()
name = f.TextField("Name")
fake_contact = fake_record(Name="Alice")
mock_get.return_value = fake_contact
contact = Contact.from_id(fake_contact["id"])
assert contact.id == fake_contact["id"]
assert contact.name == "Alice"
@mock.patch("pyairtable.Table.all")
def test_from_ids(mock_all):
fake_records = [fake_record() for _ in range(10)]
mock_all.return_value = fake_records
fake_ids = [record["id"] for record in fake_records]
contacts = FakeModel.from_ids(fake_ids)
mock_all.assert_called_once()
assert len(contacts) == len(fake_records)
assert {c.id for c in contacts} == {r["id"] for r in fake_records}
# Should raise KeyError because of the invalid ID
mock_all.reset_mock()
with pytest.raises(KeyError):
FakeModel.from_ids(fake_ids + ["recDefinitelyNotValid"])
mock_all.assert_called_once()
@mock.patch("pyairtable.Table.all")
def test_from_ids__no_fetch(mock_all):
fake_ids = [fake_id() for _ in range(10)]
contacts = FakeModel.from_ids(fake_ids, fetch=False)
assert mock_all.call_count == 0
assert len(contacts) == 10
assert set(contact.id for contact in contacts) == set(fake_ids)
def test_dynamic_model_meta():
"""
Test that we can provide callables in our Meta class to provide
the access token, base ID, and table name at runtime.
"""
data = {
"api_key": "FakeApiKey",
"base_id": "appFakeBaseId",
"table_name": "tblFakeTableName",
}
class Fake(Model):
class Meta:
api_key = lambda: data["api_key"] # noqa
base_id = partial(data.get, "base_id")
@staticmethod
def table_name():
return data["table_name"]
f = Fake()
assert f._get_meta("api_key") == data["api_key"]
assert f._get_meta("base_id") == data["base_id"]
assert f._get_meta("table_name") == data["table_name"]