forked from cityofaustin/knackpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
184 lines (159 loc) · 4.55 KB
/
test_api.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
import os
import random
import time
import knackpy
import pytest
APP_ID = os.environ["KNACK_APP_ID"]
API_KEY = os.environ["KNACK_API_KEY"]
OBJ = "object_3" # "all_fields_test"
UPDATE_KEY = "field_25" # rating field type
FILTERS = {
"match": "or",
"rules": [
# field_125 is name: "id", type: short text
{"field": "field_125", "operator": "is", "value": "1"},
],
}
UPLOAD_CONFIG = {
"path": "tests/plaid.jpg",
"obj": OBJ,
"file_field": "field_17",
"image_field": "field_18",
}
# we're trying to randomize sleep time across three+ concurrent tests of this package
# via github workflows
SLEEP_TIME = random.random() * 10
@pytest.fixture
def records():
time.sleep(SLEEP_TIME)
return knackpy.api.get(app_id=APP_ID, api_key=API_KEY, obj=OBJ, record_limit=1)
def test_random_pause():
knackpy.api._random_pause()
assert True
def test_upload_file_create_update_delete_record():
"""
Yes, this is three tests in one. Create a record with a new file. Update the
record with another file (ok, well, technically the same file), delete that
record.
"""
path = UPLOAD_CONFIG["path"]
field = UPLOAD_CONFIG["file_field"]
obj = UPLOAD_CONFIG["obj"]
# create
record1 = knackpy.api.upload(
app_id=APP_ID,
api_key=API_KEY,
obj=obj,
asset_type="file",
path=path,
field=field,
)
assert record1
time.sleep(SLEEP_TIME)
# update
record2 = knackpy.api.upload(
app_id=APP_ID,
api_key=API_KEY,
record_id=record1["id"],
obj=obj,
asset_type="file",
path=path,
field=field,
)
# verify a new record was not created
assert record1["id"] == record2["id"]
time.sleep(SLEEP_TIME)
# delete
response = knackpy.api.record(
method="delete",
app_id=APP_ID,
api_key=API_KEY,
data={"id": record2["id"]},
obj=OBJ,
)
assert response["delete"]
def test_upload_image_create_update_delete_record():
"""
Yes, this is three tests in one. Create a record with a new image. Update the
record with another image (ok, well, technically the same image), delete that
record.
"""
path = UPLOAD_CONFIG["path"]
field = UPLOAD_CONFIG["image_field"]
obj = UPLOAD_CONFIG["obj"]
# create
record1 = knackpy.api.upload(
app_id=APP_ID,
api_key=API_KEY,
obj=obj,
asset_type="image",
path=path,
field=field,
)
assert record1
time.sleep(SLEEP_TIME)
# update
record2 = knackpy.api.upload(
app_id=APP_ID,
api_key=API_KEY,
record_id=record1["id"],
obj=obj,
asset_type="image",
path=path,
field=field,
)
assert record1["id"] == record2["id"]
time.sleep(SLEEP_TIME)
# delete
response = knackpy.api.record(
method="delete",
app_id=APP_ID,
api_key=API_KEY,
data={"id": record2["id"]},
obj=OBJ,
)
assert response["delete"]
def test_get_limit(records):
assert len(records) == 1
def test_get_no_limit():
time.sleep(SLEEP_TIME)
records = knackpy.api.get(app_id=APP_ID, api_key=API_KEY, obj=OBJ)
assert len(records) > 1
def test_get_filters():
time.sleep(SLEEP_TIME)
records = knackpy.api.get(app_id=APP_ID, api_key=API_KEY, obj=OBJ, filters=FILTERS)
assert len(records) == 1
def test_record_create_delete():
# yes, two tests in one :/
time.sleep(SLEEP_TIME)
new_record = knackpy.api.record(
method="create", app_id=APP_ID, api_key=API_KEY, data={}, obj=OBJ
)
assert new_record
time.sleep(SLEEP_TIME)
response = knackpy.api.record(
method="delete",
app_id=APP_ID,
api_key=API_KEY,
data={"id": new_record["id"]},
obj=OBJ,
)
assert response["delete"]
def test_record_update(records):
"""
Given the first record in our static data, update one value and validate the
updated data returned in the response.
"""
record = records[0]
update_value = "0.00" if record[UPDATE_KEY] != "0.00" else "1.00"
data = {"id": record["id"], UPDATE_KEY: update_value}
record_updated = knackpy.api.record(
method="update", app_id=APP_ID, api_key=API_KEY, data=data, obj=OBJ
)
assert record_updated[UPDATE_KEY] == update_value
def test_get_metadata():
metadata = knackpy.api.get_metadata(app_id=APP_ID)
assert metadata["application"]
def test_slug_param():
time.sleep(SLEEP_TIME)
assert knackpy.api.get_metadata(app_id=APP_ID, slug="atd")