kensho-kfinance 1.0.0__py3-none-any.whl

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.

Potentially problematic release.


This version of kensho-kfinance might be problematic. Click here for more details.

@@ -0,0 +1,241 @@
1
+ from unittest import TestCase
2
+ from unittest.mock import Mock
3
+
4
+ from kfinance.fetch import KFinanceApiClient
5
+
6
+
7
+ class TestFetchItem(TestCase):
8
+ def setUp(self):
9
+ """Create a KFinanceApiClient with mocked-out fetch function."""
10
+ self.kfinance_api_client = KFinanceApiClient(refresh_token="fake_refresh_token")
11
+ self.kfinance_api_client.fetch = Mock()
12
+
13
+ def test_fetch_id_triple(self) -> None:
14
+ identifier = "SPGI"
15
+ exchange_code = "NYSE"
16
+ expected_fetch_url = (
17
+ self.kfinance_api_client.url_base + f"id/{identifier}/exchange_code/{exchange_code}"
18
+ )
19
+ self.kfinance_api_client.fetch_id_triple(identifier=identifier, exchange_code=exchange_code)
20
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
21
+
22
+ def test_fetch_isin(self) -> None:
23
+ security_id = 2629107
24
+ expected_fetch_url = self.kfinance_api_client.url_base + f"isin/{security_id}"
25
+ self.kfinance_api_client.fetch_isin(security_id=security_id)
26
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
27
+
28
+ def test_fetch_cusip(self) -> None:
29
+ security_id = 2629107
30
+ expected_fetch_url = self.kfinance_api_client.url_base + f"cusip/{security_id}"
31
+ self.kfinance_api_client.fetch_cusip(security_id=security_id)
32
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
33
+
34
+ def test_fetch_history_without_dates(self) -> None:
35
+ trading_item_id = 2629108
36
+ expected_fetch_url = (
37
+ f"{self.kfinance_api_client.url_base}pricing/{trading_item_id}/none/none/none/adjusted"
38
+ )
39
+ self.kfinance_api_client.fetch_history(trading_item_id=trading_item_id)
40
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
41
+
42
+ start_date = "2025-01-01"
43
+ end_date = "2025-01-31"
44
+ is_adjusted = False
45
+ periodicity = "day"
46
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}pricing/{trading_item_id}/{start_date}/{end_date}/{periodicity}/unadjusted"
47
+ self.kfinance_api_client.fetch_history(
48
+ trading_item_id=trading_item_id,
49
+ is_adjusted=is_adjusted,
50
+ start_date=start_date,
51
+ end_date=end_date,
52
+ periodicity=periodicity,
53
+ )
54
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
55
+
56
+ def test_fetch_history_metadata(self) -> None:
57
+ trading_item_id = 2629108
58
+ expected_fetch_url = (
59
+ f"{self.kfinance_api_client.url_base}pricing/{trading_item_id}/metadata"
60
+ )
61
+ self.kfinance_api_client.fetch_history_metadata(trading_item_id=trading_item_id)
62
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
63
+
64
+ def test_fetch_statement(self) -> None:
65
+ company_id = 21719
66
+ statement_type = "BS"
67
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}statements/{company_id}/{statement_type}/none/none/none/none/none"
68
+ self.kfinance_api_client.fetch_statement(
69
+ company_id=company_id, statement_type=statement_type
70
+ )
71
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
72
+ period_type = "quarterly"
73
+ start_year = 2024
74
+ end_year = 2024
75
+ start_quarter = 1
76
+ end_quarter = 4
77
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}statements/{company_id}/{statement_type}/{period_type}/{start_year}/{end_year}/{start_quarter}/{end_quarter}"
78
+ self.kfinance_api_client.fetch_statement(
79
+ company_id=company_id,
80
+ statement_type=statement_type,
81
+ period_type=period_type,
82
+ start_year=start_year,
83
+ end_year=end_year,
84
+ start_quarter=start_quarter,
85
+ end_quarter=end_quarter,
86
+ )
87
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
88
+
89
+ def test_fetch_line_item(self) -> None:
90
+ company_id = 21719
91
+ line_item = "cash"
92
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}line_item/{company_id}/{line_item}/none/none/none/none/none"
93
+ self.kfinance_api_client.fetch_line_item(company_id=company_id, line_item=line_item)
94
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
95
+ period_type = "quarterly"
96
+ start_year = 2024
97
+ end_year = 2024
98
+ start_quarter = 1
99
+ end_quarter = 4
100
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}line_item/{company_id}/{line_item}/{period_type}/{start_year}/{end_year}/{start_quarter}/{end_quarter}"
101
+ self.kfinance_api_client.fetch_line_item(
102
+ company_id=company_id,
103
+ line_item=line_item,
104
+ period_type=period_type,
105
+ start_year=start_year,
106
+ end_year=end_year,
107
+ start_quarter=start_quarter,
108
+ end_quarter=end_quarter,
109
+ )
110
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
111
+
112
+ def test_fetch_info(self) -> None:
113
+ company_id = 21719
114
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}info/{company_id}"
115
+ self.kfinance_api_client.fetch_info(company_id=company_id)
116
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
117
+
118
+ def test_fetch_earnings_dates(self) -> None:
119
+ company_id = 21719
120
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}earnings/{company_id}/dates"
121
+ self.kfinance_api_client.fetch_earnings_dates(company_id=company_id)
122
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
123
+
124
+ def test_fetch_ticker_geography_groups(self) -> None:
125
+ country_iso_code = "USA"
126
+ expected_fetch_url = (
127
+ f"{self.kfinance_api_client.url_base}ticker_groups/geo/country/{country_iso_code}"
128
+ )
129
+ self.kfinance_api_client.fetch_ticker_geography_groups(country_iso_code=country_iso_code)
130
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
131
+ state_iso_code = "FL"
132
+ expected_fetch_url = expected_fetch_url + f"/{state_iso_code}"
133
+ self.kfinance_api_client.fetch_ticker_geography_groups(
134
+ country_iso_code=country_iso_code, state_iso_code=state_iso_code
135
+ )
136
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
137
+
138
+ def test_fetch_company_geography_groups(self) -> None:
139
+ country_iso_code = "USA"
140
+ expected_fetch_url = (
141
+ f"{self.kfinance_api_client.url_base}company_groups/geo/country/{country_iso_code}"
142
+ )
143
+ self.kfinance_api_client.fetch_company_geography_groups(country_iso_code=country_iso_code)
144
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
145
+ state_iso_code = "FL"
146
+ expected_fetch_url = expected_fetch_url + f"/{state_iso_code}"
147
+ self.kfinance_api_client.fetch_company_geography_groups(
148
+ country_iso_code=country_iso_code, state_iso_code=state_iso_code
149
+ )
150
+ self.kfinance_api_client.fetch.assert_called_with(expected_fetch_url)
151
+
152
+ def test_fetch_ticker_industry_simple_groups(self) -> None:
153
+ simple_industry = "media"
154
+ expected_fetch_url = (
155
+ f"{self.kfinance_api_client.url_base}ticker_groups/industry/simple/{simple_industry}"
156
+ )
157
+ self.kfinance_api_client.fetch_ticker_simple_industry_groups(
158
+ simple_industry=simple_industry
159
+ )
160
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
161
+
162
+ def test_fetch_company_industry_simple_groups(self) -> None:
163
+ simple_industry = "media"
164
+ expected_fetch_url = (
165
+ f"{self.kfinance_api_client.url_base}company_groups/industry/simple/{simple_industry}"
166
+ )
167
+ self.kfinance_api_client.fetch_company_simple_industry_groups(
168
+ simple_industry=simple_industry
169
+ )
170
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
171
+
172
+ def test_fetch_ticker_exchange_groups(self) -> None:
173
+ exchange_code = "NYSE"
174
+ expected_fetch_url = (
175
+ f"{self.kfinance_api_client.url_base}ticker_groups/exchange/{exchange_code}"
176
+ )
177
+ self.kfinance_api_client.fetch_ticker_exchange_groups(exchange_code=exchange_code)
178
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
179
+
180
+ def test_fetch_trading_item_exchange_groups(self) -> None:
181
+ exchange_code = "NYSE"
182
+ expected_fetch_url = (
183
+ f"{self.kfinance_api_client.url_base}trading_item_groups/exchange/{exchange_code}"
184
+ )
185
+ self.kfinance_api_client.fetch_trading_item_exchange_groups(exchange_code=exchange_code)
186
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
187
+
188
+ def test_fetch_ticker_combined_no_parameter_exception(self) -> None:
189
+ with self.assertRaises(
190
+ RuntimeError, msg="Invalid parameters: No parameters provided or all set to none"
191
+ ):
192
+ self.kfinance_api_client.fetch_ticker_combined()
193
+
194
+ def test_fetch_ticker_combined_state_no_country_exception(self) -> None:
195
+ state_iso_code = "FL"
196
+ with self.assertRaises(
197
+ RuntimeError,
198
+ msg="Invalid parameters: state_iso_code must be provided with a country_iso_code value",
199
+ ):
200
+ self.kfinance_api_client.fetch_ticker_combined(state_iso_code=state_iso_code)
201
+
202
+ def test_fetch_ticker_combined_only_country(self) -> None:
203
+ country_iso_code = "USA"
204
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}ticker_groups/filters/geo/{country_iso_code.lower()}/none/simple/none/exchange/none"
205
+ self.kfinance_api_client.fetch_ticker_combined(country_iso_code=country_iso_code)
206
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
207
+
208
+ def test_fetch_ticker_combined_country_and_state(self) -> None:
209
+ country_iso_code = "USA"
210
+ state_iso_code = "FL"
211
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}ticker_groups/filters/geo/{country_iso_code.lower()}/{state_iso_code.lower()}/simple/none/exchange/none"
212
+ self.kfinance_api_client.fetch_ticker_combined(
213
+ country_iso_code=country_iso_code, state_iso_code=state_iso_code
214
+ )
215
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
216
+
217
+ def test_fetch_ticker_combined_only_simple_industry(self) -> None:
218
+ simple_industry = "Media"
219
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}ticker_groups/filters/geo/none/none/simple/{simple_industry.lower()}/exchange/none"
220
+ self.kfinance_api_client.fetch_ticker_combined(simple_industry=simple_industry)
221
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
222
+
223
+ def test_fetch_ticker_combined_only_exchange(self) -> None:
224
+ exchange_code = "NYSE"
225
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}ticker_groups/filters/geo/none/none/simple/none/exchange/{exchange_code.lower()}"
226
+ self.kfinance_api_client.fetch_ticker_combined(exchange_code=exchange_code)
227
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)
228
+
229
+ def test_fetch_ticker_combined_all(self) -> None:
230
+ country_iso_code = "USA"
231
+ state_iso_code = "FL"
232
+ simple_industry = "Media"
233
+ exchange_code = "NYSE"
234
+ expected_fetch_url = f"{self.kfinance_api_client.url_base}ticker_groups/filters/geo/{country_iso_code.lower()}/{state_iso_code.lower()}/simple/{simple_industry.lower()}/exchange/{exchange_code.lower()}"
235
+ self.kfinance_api_client.fetch_ticker_combined(
236
+ country_iso_code=country_iso_code,
237
+ state_iso_code=state_iso_code,
238
+ simple_industry=simple_industry,
239
+ exchange_code=exchange_code,
240
+ )
241
+ self.kfinance_api_client.fetch.assert_called_once_with(expected_fetch_url)