fbi-data-api 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ .venv/
2
+ __pycache__/
3
+ .pytest_cache/
4
+ .vscode/
5
+ .env
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ted Jesus C. Chua
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,54 @@
1
+ Metadata-Version: 2.4
2
+ Name: fbi-data-api
3
+ Version: 0.1.0
4
+ Summary: Interface for extracting data assets from the FBI Crime Data API.
5
+ Project-URL: Homepage, https://github.com/teddythepooh/fbi_api
6
+ Project-URL: Issues, https://github.com/teddythepooh/fbi_api/issues
7
+ Author-email: "Ted Jesus C. Chua" <chuatedjesusc@gmail.com>
8
+ License: MIT License
9
+
10
+ Copyright (c) 2026 Ted Jesus C. Chua
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ of this software and associated documentation files (the "Software"), to deal
14
+ in the Software without restriction, including without limitation the rights
15
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ copies of the Software, and to permit persons to whom the Software is
17
+ furnished to do so, subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ SOFTWARE.
29
+ License-File: LICENSE
30
+ Requires-Python: >=3.12
31
+ Requires-Dist: pandas>=3.0.1
32
+ Requires-Dist: python-dotenv>=1.2.2
33
+ Requires-Dist: pyyaml>=6.0.3
34
+ Requires-Dist: requests>=2.32.5
35
+ Description-Content-Type: text/markdown
36
+
37
+ # FBI Crime Data API
38
+ This is a wrapper around the FBI Crime Data API, the first of its kind as of March 2026.
39
+
40
+ ## Example
41
+ After signing up for an API key in https://api.data.gov/signup/,
42
+
43
+ ```python
44
+ from fbi_api import FBI
45
+
46
+ # If no api_key is passed, your environment variable FBI_API_KEY is automatically invoked.
47
+ api = FBI(api_key = your_api_key)
48
+
49
+ metadata = api.get_metadata(state_abbr = "all")
50
+
51
+ # ORI stands for Originating Agency Identifer (ORI), uniquely identifying the law enforcement agencies that report
52
+ # to the FBI. The ORIs in a state can be extracted from api.get_metadata().
53
+ crime_statistics = api.get_crime_statistics(ori = "ILCPD0000", year = 2024, offense = "Violent Crimes")
54
+ ```
@@ -0,0 +1,18 @@
1
+ # FBI Crime Data API
2
+ This is a wrapper around the FBI Crime Data API, the first of its kind as of March 2026.
3
+
4
+ ## Example
5
+ After signing up for an API key in https://api.data.gov/signup/,
6
+
7
+ ```python
8
+ from fbi_api import FBI
9
+
10
+ # If no api_key is passed, your environment variable FBI_API_KEY is automatically invoked.
11
+ api = FBI(api_key = your_api_key)
12
+
13
+ metadata = api.get_metadata(state_abbr = "all")
14
+
15
+ # ORI stands for Originating Agency Identifer (ORI), uniquely identifying the law enforcement agencies that report
16
+ # to the FBI. The ORIs in a state can be extracted from api.get_metadata().
17
+ crime_statistics = api.get_crime_statistics(ori = "ILCPD0000", year = 2024, offense = "Violent Crimes")
18
+ ```
@@ -0,0 +1,35 @@
1
+ [build-system]
2
+ requires = ["hatchling >= 1.26"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [tool.hatch.build.targets.wheel]
6
+ packages = ["src/fbi_api"]
7
+
8
+ [project]
9
+ name = "fbi-data-api"
10
+ version = "0.1.0"
11
+ authors = [
12
+ { name="Ted Jesus C. Chua", email="chuatedjesusc@gmail.com" },
13
+ ]
14
+ description = "Interface for extracting data assets from the FBI Crime Data API."
15
+ readme = "README.md"
16
+ requires-python = ">=3.12"
17
+ dependencies = [
18
+ "pandas>=3.0.1",
19
+ "python-dotenv>=1.2.2",
20
+ "pyyaml>=6.0.3",
21
+ "requests>=2.32.5",
22
+ ]
23
+ license = {file = "LICENSE"}
24
+ license-files = ["LICEN[CS]E*"]
25
+
26
+ [dependency-groups]
27
+ dev = [
28
+ "ipykernel>=7.2.0",
29
+ "pytest>=9.0.2",
30
+ "pytest-mock>=3.15.1",
31
+ ]
32
+
33
+ [project.urls]
34
+ Homepage = "https://github.com/teddythepooh/fbi_api"
35
+ Issues = "https://github.com/teddythepooh/fbi_api/issues"
@@ -0,0 +1 @@
1
+ from .core import FBI
@@ -0,0 +1,64 @@
1
+ state_abbrs:
2
+ - AL
3
+ - AK
4
+ - AZ
5
+ - AR
6
+ - CA
7
+ - CO
8
+ - CT
9
+ - DE
10
+ - FL
11
+ - GA
12
+ - HI
13
+ - ID
14
+ - IL
15
+ - IN
16
+ - IA
17
+ - KS
18
+ - KY
19
+ - LA
20
+ - ME
21
+ - MD
22
+ - MA
23
+ - MI
24
+ - MN
25
+ - MS
26
+ - MO
27
+ - MT
28
+ - NE
29
+ - NV
30
+ - NH
31
+ - NJ
32
+ - NM
33
+ - NY
34
+ - NC
35
+ - ND
36
+ - OH
37
+ - OK
38
+ - OR
39
+ - PA
40
+ - RI
41
+ - SC
42
+ - SD
43
+ - TN
44
+ - TX
45
+ - UT
46
+ - VT
47
+ - VA
48
+ - WA
49
+ - WV
50
+ - WI
51
+ - WY
52
+ - DC
53
+
54
+ offenses:
55
+ Violent Crimes: V
56
+ Aggravated Assault: ASS
57
+ Burglary: BUR
58
+ Larceny: LAR
59
+ Motor Vehicle Theft: MVT
60
+ Homicide: HOM
61
+ Rape: RPE
62
+ Robbery: ROB
63
+ Arson: ARS
64
+ All Property Crimes: P
@@ -0,0 +1,107 @@
1
+ import os
2
+ import requests
3
+ import pandas as pd
4
+ from pathlib import Path
5
+ from .utils import load_yaml
6
+
7
+ class FBI:
8
+ base_url = "https://api.usa.gov/crime/fbi/cde"
9
+ config = load_yaml(Path(__file__).parent.joinpath("config.yml"))
10
+
11
+ def __init__(self, api_key: str = None):
12
+ '''
13
+ If no api_key is passed, it is invoked from environment variable "FBI_API_KEY."
14
+ '''
15
+ self.api_key = api_key
16
+
17
+ @staticmethod
18
+ def _get_state_abbrs() -> list:
19
+ return FBI.config["state_abbrs"]
20
+
21
+ @staticmethod
22
+ def _get_offenses() -> dict:
23
+ return FBI.config["offenses"]
24
+
25
+ def _get_api_key(self) -> str:
26
+ return os.getenv("FBI_API_KEY", default = self.api_key)
27
+
28
+ def _add_key_to_call(self, api_call: str) -> str:
29
+ prefix = "&" if "?" in api_call else "?"
30
+
31
+ auth_str = f"{prefix}API_KEY={self._get_api_key()}"
32
+
33
+ if not api_call.endswith(auth_str):
34
+ api_call = f"{api_call}{auth_str}"
35
+
36
+ return api_call
37
+
38
+ def get(self, api_call: str, timeout_limit: int = 10) -> dict:
39
+ api_call = self._add_key_to_call(api_call)
40
+ response = requests.get(api_call, timeout = timeout_limit)
41
+
42
+ return response.json() if response.status_code == 200 else None
43
+
44
+ def _oris_by_state(self, state_abbr: str) -> pd.DataFrame:
45
+ nested = self.get(f"{FBI.base_url}/agency/byStateAbbr/{state_abbr}")
46
+
47
+ flattened = []
48
+
49
+ for agencies in nested.values():
50
+ for agency in agencies:
51
+ flattened.append(agency)
52
+
53
+ return pd.DataFrame(flattened)
54
+
55
+ def get_metadata(self, state_abbr: str) -> pd.DataFrame:
56
+ '''
57
+ state_abbr: State abbreviation of desired state.
58
+
59
+ Extracts the metadata for a state, namely all law enforcement agencies that have provided data to the
60
+ Uniform Crime Reporting (UCR) program. If metadata for all states are desired, set state_abbr to "all."
61
+ '''
62
+ if state_abbr == "all":
63
+ results = []
64
+
65
+ for state in FBI._get_state_abbrs():
66
+ print(f"Extracting metadata for {state}...")
67
+ results.append(self._oris_by_state(state))
68
+
69
+ return pd.concat(results, ignore_index = True)
70
+ else:
71
+ return self._oris_by_state(state_abbr)
72
+
73
+ def get_crime_statistics(self, ori: str, year: int, offense: str) -> pd.DataFrame:
74
+ '''
75
+ ori: The originating agency identifier (ORI). Invoke get_metadata() to extract all ORIs in a state.
76
+ year: The year.
77
+
78
+ Extracts the monthly crime statistics reported by an agency.
79
+ '''
80
+ try:
81
+ offense_mapping = FBI._get_offenses()[offense]
82
+ api_call = self.get(
83
+ f"{FBI.base_url}/summarized/agency/{ori}/{offense_mapping}?from=01-{year}&to=12-{year}"
84
+ )
85
+ except KeyError:
86
+ raise KeyError(f"Valid offenses to pass are {", ".join(list(FBI._get_offenses().keys()))}.")
87
+
88
+ last_refresh_date = api_call["cde_properties"]["last_refresh_date"]["UCR"]
89
+
90
+ _, first_value = next(iter(api_call["offenses"]["actuals"].items()))
91
+
92
+ crime_statistics = pd.Series(first_value, name = "count").rename_axis("date").reset_index()
93
+
94
+ month_year_columns = ["month", "year"]
95
+ crime_statistics[month_year_columns] = crime_statistics["date"].str.split("-", expand = True)
96
+ crime_statistics.drop(columns = "date", inplace = True)
97
+
98
+ crime_statistics["ori"] = ori
99
+ crime_statistics["offense"] = offense
100
+ crime_statistics["last_refresh_date"] = last_refresh_date
101
+
102
+ for col in month_year_columns:
103
+ crime_statistics[col] = crime_statistics[col].astype(int)
104
+
105
+ crime_statistics.sort_values(by = month_year_columns, inplace = True)
106
+
107
+ return crime_statistics[["ori", "month", "year", "offense", "count", "last_refresh_date"]]
@@ -0,0 +1,9 @@
1
+ import yaml
2
+ from pathlib import Path
3
+
4
+ def load_yaml(file: Path) -> dict:
5
+ try:
6
+ with open(file, "r") as file:
7
+ return yaml.safe_load(file)
8
+ except FileNotFoundError:
9
+ raise FileNotFoundError(f"{file} not found.")
@@ -0,0 +1,254 @@
1
+ {
2
+ "offenses": {
3
+ "rates": {
4
+ "Illinois Offenses": {
5
+ "01-2024": 20.9,
6
+ "02-2024": 21.92,
7
+ "03-2024": 23.74,
8
+ "04-2024": 24.57,
9
+ "05-2024": 27.94,
10
+ "06-2024": 27.63,
11
+ "07-2024": 28.6,
12
+ "08-2024": 26.73,
13
+ "09-2024": 25.08,
14
+ "10-2024": 23.7,
15
+ "11-2024": 22.51,
16
+ "12-2024": 20.95
17
+ },
18
+ "Illinois Clearances": {
19
+ "01-2024": 5.35,
20
+ "02-2024": 5.98,
21
+ "03-2024": 6.29,
22
+ "04-2024": 6.24,
23
+ "05-2024": 7.11,
24
+ "06-2024": 7.28,
25
+ "07-2024": 6.97,
26
+ "08-2024": 6.44,
27
+ "09-2024": 6.41,
28
+ "10-2024": 6.72,
29
+ "11-2024": 5.84,
30
+ "12-2024": 6.01
31
+ },
32
+ "United States Offenses": {
33
+ "01-2024": 27.87,
34
+ "02-2024": 26.74,
35
+ "03-2024": 29.35,
36
+ "04-2024": 30.06,
37
+ "05-2024": 33.28,
38
+ "06-2024": 32.68,
39
+ "07-2024": 33.04,
40
+ "08-2024": 31.94,
41
+ "09-2024": 31.31,
42
+ "10-2024": 30.54,
43
+ "11-2024": 27.56,
44
+ "12-2024": 30.26
45
+ },
46
+ "United States Clearances": {
47
+ "01-2024": 12.52,
48
+ "02-2024": 12.43,
49
+ "03-2024": 13.46,
50
+ "04-2024": 13.44,
51
+ "05-2024": 14.48,
52
+ "06-2024": 13.77,
53
+ "07-2024": 14.49,
54
+ "08-2024": 13.96,
55
+ "09-2024": 13.41,
56
+ "10-2024": 13.55,
57
+ "11-2024": 12.18,
58
+ "12-2024": 12.06
59
+ },
60
+ "Chicago Police Department Offenses": {
61
+ "01-2024": 40.47,
62
+ "02-2024": 39.6,
63
+ "03-2024": 42.29,
64
+ "04-2024": 42.52,
65
+ "05-2024": 49.19,
66
+ "06-2024": 51.69,
67
+ "07-2024": 54.88,
68
+ "08-2024": 48.09,
69
+ "09-2024": 47.37,
70
+ "10-2024": 43.54,
71
+ "11-2024": 41.31,
72
+ "12-2024": 38.88
73
+ },
74
+ "Chicago Police Department Clearances": {
75
+ "01-2024": 3.52,
76
+ "02-2024": 3.49,
77
+ "03-2024": 4.09,
78
+ "04-2024": 4.17,
79
+ "05-2024": 4.43,
80
+ "06-2024": 5.72,
81
+ "07-2024": 5.38,
82
+ "08-2024": 3.6,
83
+ "09-2024": 3.98,
84
+ "10-2024": 4.51,
85
+ "11-2024": 4.36,
86
+ "12-2024": 4.06
87
+ }
88
+ },
89
+ "actuals": {
90
+ "Chicago Police Department Offenses": {
91
+ "01-2024": 1068,
92
+ "02-2024": 1045,
93
+ "03-2024": 1116,
94
+ "04-2024": 1122,
95
+ "05-2024": 1298,
96
+ "06-2024": 1364,
97
+ "07-2024": 1448,
98
+ "08-2024": 1269,
99
+ "09-2024": 1250,
100
+ "10-2024": 1149,
101
+ "11-2024": 1090,
102
+ "12-2024": 1026
103
+ },
104
+ "Chicago Police Department Clearances": {
105
+ "01-2024": 93,
106
+ "02-2024": 92,
107
+ "03-2024": 108,
108
+ "04-2024": 110,
109
+ "05-2024": 117,
110
+ "06-2024": 151,
111
+ "07-2024": 142,
112
+ "08-2024": 95,
113
+ "09-2024": 105,
114
+ "10-2024": 119,
115
+ "11-2024": 115,
116
+ "12-2024": 107
117
+ }
118
+ }
119
+ },
120
+ "tooltips": {
121
+ "leftYAxisHeaders": {
122
+ "yAxisHeaderRates": "Offenses per 100,000 people",
123
+ "yAxisHeaderActual": "Offenses"
124
+ },
125
+ "Percent of Population Coverage": {
126
+ "Illinois": {
127
+ "01-2024": 93.46,
128
+ "02-2024": 93.49,
129
+ "03-2024": 93.31,
130
+ "04-2024": 93.28,
131
+ "05-2024": 93.63,
132
+ "06-2024": 94.32,
133
+ "07-2024": 94.23,
134
+ "08-2024": 94.23,
135
+ "09-2024": 94.32,
136
+ "10-2024": 94.29,
137
+ "11-2024": 94.31,
138
+ "12-2024": 94.17
139
+ },
140
+ "United States": {
141
+ "01-2024": 95.8,
142
+ "02-2024": 95.72,
143
+ "03-2024": 95.61,
144
+ "04-2024": 95.67,
145
+ "05-2024": 95.53,
146
+ "06-2024": 95.73,
147
+ "07-2024": 95.69,
148
+ "08-2024": 95.69,
149
+ "09-2024": 95.62,
150
+ "10-2024": 95.63,
151
+ "11-2024": 95.43,
152
+ "12-2024": 95.47
153
+ }
154
+ }
155
+ },
156
+ "populations": {
157
+ "population": {
158
+ "Illinois": {
159
+ "01-2024": 12710158,
160
+ "02-2024": 12710158,
161
+ "03-2024": 12710158,
162
+ "04-2024": 12710158,
163
+ "05-2024": 12710158,
164
+ "06-2024": 12710158,
165
+ "07-2024": 12710158,
166
+ "08-2024": 12710158,
167
+ "09-2024": 12710158,
168
+ "10-2024": 12710158,
169
+ "11-2024": 12710158,
170
+ "12-2024": 12710158
171
+ },
172
+ "United States": {
173
+ "01-2024": 343551384,
174
+ "02-2024": 343551384,
175
+ "03-2024": 343551384,
176
+ "04-2024": 343551384,
177
+ "05-2024": 343551384,
178
+ "06-2024": 343551384,
179
+ "07-2024": 343551384,
180
+ "08-2024": 343551384,
181
+ "09-2024": 343551384,
182
+ "10-2024": 343551384,
183
+ "11-2024": 343551384,
184
+ "12-2024": 343551384
185
+ },
186
+ "Chicago Police Department": {
187
+ "01-2024": 2638698,
188
+ "02-2024": 2638698,
189
+ "03-2024": 2638698,
190
+ "04-2024": 2638698,
191
+ "05-2024": 2638698,
192
+ "06-2024": 2638698,
193
+ "07-2024": 2638698,
194
+ "08-2024": 2638698,
195
+ "09-2024": 2638698,
196
+ "10-2024": 2638698,
197
+ "11-2024": 2638698,
198
+ "12-2024": 2638698
199
+ }
200
+ },
201
+ "participated_population": {
202
+ "Illinois": {
203
+ "01-2024": 11879491,
204
+ "02-2024": 11882231,
205
+ "03-2024": 11859958,
206
+ "04-2024": 11855607,
207
+ "05-2024": 11901138,
208
+ "06-2024": 11988464,
209
+ "07-2024": 11976528,
210
+ "08-2024": 11976668,
211
+ "09-2024": 11987763,
212
+ "10-2024": 11984867,
213
+ "11-2024": 11987090,
214
+ "12-2024": 11969280
215
+ },
216
+ "United States": {
217
+ "01-2024": 329131831,
218
+ "02-2024": 328851436,
219
+ "03-2024": 328468916,
220
+ "04-2024": 328689276,
221
+ "05-2024": 328179474,
222
+ "06-2024": 328886364,
223
+ "07-2024": 328753597,
224
+ "08-2024": 328739752,
225
+ "09-2024": 328501661,
226
+ "10-2024": 328543145,
227
+ "11-2024": 327838575,
228
+ "12-2024": 327975212
229
+ },
230
+ "Chicago Police Department": {
231
+ "01-2024": 2638698,
232
+ "02-2024": 2638698,
233
+ "03-2024": 2638698,
234
+ "04-2024": 2638698,
235
+ "05-2024": 2638698,
236
+ "06-2024": 2638698,
237
+ "07-2024": 2638698,
238
+ "08-2024": 2638698,
239
+ "09-2024": 2638698,
240
+ "10-2024": 2638698,
241
+ "11-2024": 2638698,
242
+ "12-2024": 2638698
243
+ }
244
+ }
245
+ },
246
+ "cde_properties": {
247
+ "max_data_date": {
248
+ "UCR": "02/2026"
249
+ },
250
+ "last_refresh_date": {
251
+ "UCR": "02/15/2026"
252
+ }
253
+ }
254
+ }
@@ -0,0 +1,54 @@
1
+ {
2
+ "LEE": [
3
+ {
4
+ "ori": "IL0520200",
5
+ "counties": "LEE",
6
+ "is_nibrs": true,
7
+ "latitude": 41.713726,
8
+ "longitude": -89.33026,
9
+ "state_abbr": "IL",
10
+ "state_name": "Illinois",
11
+ "agency_name": "Amboy Police Department",
12
+ "agency_type_name": "City",
13
+ "nibrs_start_date": "2022-12-01"
14
+ },
15
+ {
16
+ "ori": "IL0520400",
17
+ "counties": "LEE",
18
+ "is_nibrs": false,
19
+ "latitude": 41.747442,
20
+ "longitude": -89.299351,
21
+ "state_abbr": "IL",
22
+ "state_name": "Illinois",
23
+ "agency_name": "Franklin Grove Police Department",
24
+ "agency_type_name": "City",
25
+ "nibrs_start_date": null
26
+ }
27
+ ],
28
+ "BOND": [
29
+ {
30
+ "ori": "IL0030300",
31
+ "counties": "BOND",
32
+ "is_nibrs": false,
33
+ "latitude": 38.885924,
34
+ "longitude": -89.436592,
35
+ "state_abbr": "IL",
36
+ "state_name": "Illinois",
37
+ "agency_name": "Sorento Police Department",
38
+ "agency_type_name": "City",
39
+ "nibrs_start_date": null
40
+ },
41
+ {
42
+ "ori": "IL0030100",
43
+ "counties": "BOND",
44
+ "is_nibrs": true,
45
+ "latitude": 38.889313,
46
+ "longitude": -89.41342,
47
+ "state_abbr": "IL",
48
+ "state_name": "Illinois",
49
+ "agency_name": "Greenville Police Department",
50
+ "agency_type_name": "City",
51
+ "nibrs_start_date": "2022-01-01"
52
+ }
53
+ ]
54
+ }
@@ -0,0 +1,59 @@
1
+ '''
2
+ The underlying API calls in get_metadata() and get_crime_statistics() return nested JSON objects. The tests below verify
3
+ that they are correctly parsed to a tabular format.
4
+ '''
5
+ import json
6
+ import pytest
7
+ from pathlib import Path
8
+
9
+ from fbi_api import FBI
10
+
11
+ def load_mock_json(file: str):
12
+ try:
13
+ with open(Path(__file__).parent.joinpath("data").joinpath(file), "r") as file:
14
+ return json.load(file)
15
+ except FileNotFoundError:
16
+ raise FileNotFoundError(f"{file} not found.")
17
+
18
+ MOCK_RESPONSE_GET_METADATA = load_mock_json("mock_response_get_metadata.json")
19
+ MOCK_RESPONSE_GET_CRIME_STATISTICS = load_mock_json("mock_response_get_crime_statistics.json")
20
+
21
+ @pytest.fixture(scope = "module")
22
+ def fbi_client():
23
+ return FBI()
24
+
25
+ def test_get_metadata(fbi_client, mocker):
26
+ mocker.patch.object(fbi_client, "get", return_value = MOCK_RESPONSE_GET_METADATA)
27
+
28
+ metadata = fbi_client.get_metadata("IL")
29
+
30
+ assert set(metadata.columns) == {
31
+ "ori",
32
+ "counties",
33
+ "is_nibrs",
34
+ "latitude",
35
+ "longitude",
36
+ "state_abbr",
37
+ "state_name",
38
+ "agency_name",
39
+ "agency_type_name",
40
+ "nibrs_start_date"
41
+ }
42
+
43
+ assert len(metadata) > 0
44
+
45
+ def test_get_crime_statistics(fbi_client, mocker):
46
+ mocker.patch.object(fbi_client, "get", return_value = MOCK_RESPONSE_GET_CRIME_STATISTICS)
47
+
48
+ crime_statistics = fbi_client.get_crime_statistics(ori = "ILCPD0000", year = 2024, offense = "Violent Crimes")
49
+
50
+ assert set(crime_statistics.columns) == {
51
+ "ori",
52
+ "month",
53
+ "year",
54
+ "offense",
55
+ "count",
56
+ "last_refresh_date"
57
+ }
58
+
59
+ assert len(crime_statistics) > 0