-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_pydocket.py
More file actions
215 lines (167 loc) · 7.95 KB
/
Copy pathtest_pydocket.py
File metadata and controls
215 lines (167 loc) · 7.95 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""Test the pydocket module."""
import json
from pytest import fixture
import vcr
from pydocket import RegulationDocument, RegulationDocket, \
RegulationDocumentSearch
DOCKET_ID = 'FDA-2017-N-4515'
DOCUMENT_ID = 'FDA-2017-N-4515-3417'
# print(json.dumps(response, sort_keys=True, indent=4, separators=(',', ': ')))
@fixture
def docket_keys():
"""A list of Regulations.gov docket keys."""
return ['agency', 'agencyAcronym', 'docketId',
'field2', 'generic', 'keywords', 'numberOfComments',
'parentAgency', 'parentAgencyAcronym', 'program', 'shortTitle',
'title', 'type']
@fixture
def document_keys():
"""A list of Regulations.gov document keys."""
return ['agencyAcronym', 'agencyName', 'allowLateComment',
'attachmentCount', 'comment', 'commentCategory', 'commentDueDate',
'commentOnDoc', 'commentStartDate', 'docSubType', 'docketId',
'docketTitle', 'docketType', 'documentId', 'documentType',
'numItemsRecieved', 'openForComment', 'postedDate', 'receivedDate',
'status', 'title', 'trackingNumber']
@fixture
def document_search_keys():
"""A list of Regulations.gov document search keys."""
return ['documents', 'totalNumRecords']
@vcr.use_cassette('tests/vcr_cassettes/docket-get.yml')
def test_docket_get(docket_keys):
"""Test an API call to get a Regulations.gov docket's data."""
docket = RegulationDocket(DOCKET_ID)
response = docket.get()
assert isinstance(response, dict)
assert response['docketId'] == DOCKET_ID, \
'The ID should be in the response'
assert set(docket_keys).issubset(response.keys()), \
'All keys should be in the response'
@vcr.use_cassette('tests/vcr_cassettes/document-get.yml')
def test_document_get(document_keys):
"""Test an API call to get a Regulations.gov document's data."""
document = RegulationDocument(DOCKET_ID, DOCUMENT_ID)
response = document.get()
assert isinstance(response, dict)
assert response['documentId']['value'] == DOCUMENT_ID, \
'The ID should be in the response'
assert set(document_keys).issubset(response.keys()), \
'All keys should be in the response'
@vcr.use_cassette('tests/vcr_cassettes/search-number_of_records.yml')
def test_search_number_of_records():
"""Test API call to search for the total number of records by docket ID."""
response = RegulationDocumentSearch.number_of_records(DOCKET_ID)
assert isinstance(response, int), 'Response should be an integer'
assert response == 6585, 'Response should be 6585'
@vcr.use_cassette('tests/vcr_cassettes/search-number_of_records-w_dct.yml')
def test_search_number_of_records_w_dct():
"""
Test API call to search for the total number of records by docket ID.
Includes document type parameter (dct). Valid values for dct are
[ 'N': Notice, 'PR': Proposed Rule, 'FR': Rule, 'O': Other,
'SR': Supporting & Related Material, 'PS': Public Submission ].
"""
parameters = {'docket_id': DOCKET_ID, 'document_type': 'N'}
response = RegulationDocumentSearch.number_of_records(**parameters)
assert isinstance(response, int), 'Response should be an integer'
assert response == 2, 'Response should be 2'
@vcr.use_cassette('tests/vcr_cassettes/search-by_dktid.yml')
def test_search_by_dktid(document_search_keys):
"""Test API call to search for Regulations.gov documents by docket ID."""
response = RegulationDocumentSearch.by_docket_id(DOCKET_ID)
assert isinstance(response, dict), 'Response should be a dictionary'
assert set(document_search_keys).issubset(response.keys()), \
'All keys should be in the response'
assert len(response['documents']) == 10, \
'Query should return 10 documents by default'
@vcr.use_cassette('tests/vcr_cassettes/search-by_dktid_dct.yml')
def test_search_by_dktid_w_dct():
"""
Test API call to search for Regulations.gov documents by docket ID.
Includes document type parameter (dct). Valid values for dct are
[ 'N': Notice, 'PR': Proposed Rule, 'FR': Rule, 'O': Other,
'SR': Supporting & Related Material, 'PS': Public Submission ].
"""
parameters = {'docket_id': DOCKET_ID, 'document_type': 'N'}
response = RegulationDocumentSearch.by_docket_id(**parameters)
assert len(response['documents']) == 2, \
'Query should return 2 documents'
assert response['documents'][0]['documentType'] == 'Notice', \
'Response document type should be Notice'
@vcr.use_cassette('tests/vcr_cassettes/search-by_dktid-rpp.yml')
def test_search_by_dktid_w_rpp():
"""
Test API call to search for Regulations.gov documents by docket ID.
Includes results per page parameter (rpp). Valid values for rpp
are [ 10, 25, 100, 500, 1000 ].
"""
parameters = {'docket_id': DOCKET_ID, 'results_per_page': 25}
response = RegulationDocumentSearch.by_docket_id(**parameters)
assert len(response['documents']) == 25, 'Query should return 25 documents'
@vcr.use_cassette('tests/vcr_cassettes/search-by_dktid-rpp-po.yml')
def test_search_by_dktid_w_rpp_po():
"""
Test API call to search for Regulations.gov documents by docket ID.
Includes results per page parameter (rpp) and page offset (po).
Valid values for rpp are [ 10, 25, 100, 500, 1000 ]. Page offset
begins at 0 and generally should increment by results per page.
"""
parameters = {
'docket_id': DOCKET_ID,
'results_per_page': 25,
'offset': 6575
}
response = RegulationDocumentSearch.by_docket_id(**parameters)
assert len(response['documents']) == 10, 'Query should return 10 documents'
@vcr.use_cassette('tests/vcr_cassettes/search-by_dktid-sort_asc.yml')
def test_search_by_dktid_w_sort_asc():
"""
Test API call to search for Regulations.gov documents by docket ID.
Includes sort by (sb) posted date and sort order (so) ascending.
Valid values for sb are [ "docketId", "docId", "title", "postedDate",
"agency", "documentType", "submitterName", "organization" ]. Valid values
for so are [ "ASC", "DESC" ].
"""
parameters = {
'docket_id': DOCKET_ID,
'results_per_page': 100,
'offset': 0,
'sort_by': 'postedDate',
'sort_order': 'ASC'
}
response = RegulationDocumentSearch.by_docket_id(**parameters)
first_doc_date = response['documents'][0]['postedDate']
second_doc_date = response['documents'][99]['postedDate']
assert first_doc_date <= second_doc_date, \
'Response documents should be in ascending sort order'
@vcr.use_cassette('tests/vcr_cassettes/search-by_dktid-sort_desc.yml')
def test_search_by_dktid_w_sort_desc():
"""
Test API call to search for Regulations.gov documents by docket ID.
Includes sort by (sb) posted date and sort order (so) descending.
Valid values for sb are [ "docketId", "docId", "title", "postedDate",
"agency", "documentType", "submitterName", "organization" ]. Valid values
for so are [ "ASC", "DESC" ].
"""
parameters = {
'docket_id': DOCKET_ID,
'results_per_page': 100,
'offset': 0,
'sort_by': 'postedDate',
'sort_order': 'DESC'
}
response = RegulationDocumentSearch.by_docket_id(**parameters)
first_doc_date = response['documents'][0]['postedDate']
second_doc_date = response['documents'][99]['postedDate']
assert first_doc_date >= second_doc_date, \
'Response documents should be in descending sort order'
def test_search_all_comments():
"""Test API call to retrieve all Regulations.gov comments by docket ID."""
comments = RegulationDocumentSearch.all_comments_by_docket_id(DOCKET_ID)
assert isinstance(comments, list), 'Response should be a list'
assert len(comments) == 6581, \
'Query should return 6581 documents by default'
first_doc_date = comments[0]['postedDate']
last_doc_date = comments[-1]['postedDate']
assert first_doc_date <= last_doc_date, \
'Comments should be in ascending sort order by default'