class TestFilings: def test_bad_filing_type(self): with pytest.raises(FilingTypeError): filings(cik_lookup='aapl', filing_type='10-k') def test_filing_type_plus_entry_filter_filters_both(self): pass def test_count_not_implemented(self): with pytest.raises(NotImplementedError): filings(start_date=date(2010, 1, 1), end_date=date(2020, 1, 1), count=10) def test_no_end_date_no_cik_lookp_returns_daily_filings(self): day = date(2020, 1, 1) f = filings(start_date=day, end_date=date(2020, 1, 1)) assert isinstance(f, DailyFilings) assert f.date == day assert f.year == day.year assert f.quarter == day.month // 3 + 1 def test_exact_quarter_returns_quarterly_filings(self): f = filings(start_date=date(2020, 1, 1), end_date=date(2020, 3, 31)) assert isinstance(f, QuarterlyFilings) assert f.year == 2020 assert f.quarter == 1 def test_mismatch_of_quarters_returns_combo_filings(self): start_date = date(2020, 1, 1) end_date = date(2020, 5, 1) f = filings(start_date=start_date, end_date=end_date) assert isinstance(f, ComboFilings) assert f.start_date == start_date assert f.end_date == end_date @pytest.mark.parametrize( "kwargs,error", [ (dict(), ValueError), (dict(end_date=date(2020, 1, 1)), ValueError), (dict(client=NetworkClient()), ValueError), (dict(entry_filter=lambda f: f.form_type == '10-k'), ValueError), # end_date assumed to be today - giving count tries to # make daily or quarterly filing, but will not be recognized (dict(count=10, client=NetworkClient()), NotImplementedError), (dict(count=10), NotImplementedError), ]) def test_bad_args_combination_raises_error(self, kwargs, error): with pytest.raises(error): filings(**kwargs)
def test_rate_limit_requests_per_second(self, tmp_data_directory, rate_limit, mock_filing_response): client = NetworkClient(rate_limit=rate_limit) min_seconds = 3 num_requests = rate_limit * min_seconds inputs = [("https://google.com", os.path.join(tmp_data_directory, str(i))) for i in range(num_requests)] loop = asyncio.get_event_loop() start = time.time() loop.run_until_complete(client.wait_for_download_async(inputs)) end = time.time() assert num_requests / math.ceil(end - start) <= rate_limit
def __init__(self, cik_lookup, filing_type=None, start_date=None, end_date=date.today(), client=None, count=None, ownership="include", match_format="ALL", **kwargs): # Leave params before other setters self._params = { "action": "getcompany", "output": "xml", "owner": ownership, "start": 0, } self.start_date = start_date self.end_date = end_date self.filing_type = filing_type self.count = count self.match_format = match_format # Make default client NetworkClient and pass in kwargs self._client = client if client is not None else NetworkClient( **kwargs) # make CIKLookup object for users if not given self.cik_lookup = cik_lookup
def test_client_passed_to_objects(self, mock_user_agent): client = NetworkClient(user_agent=mock_user_agent) combo = ComboFilings(start_date=date(2020, 1, 1), end_date=date(2020, 12, 31), client=client) assert combo.quarterly.client == client assert combo.daily.client == client
def __init__(self, cik_lookup, filing_type=None, user_agent=None, start_date=None, end_date=date.today(), client=None, count=None, ownership="include", match_format="ALL", **kwargs): self._params = { "action": "getcompany", "output": "xml", "start": 0 } self.start_date = start_date self.end_date = end_date self.filing_type = filing_type self.count = count self.match_format = match_format # Make default client NetworkClient and pass in kwargs self._client = client or NetworkClient(user_agent=user_agent, **kwargs) # make CIKLookup object for users if not given self.cik_lookup = cik_lookup self.ownership = ownership
def test_filing_returns_correct_number_of_urls(self, count, mock_cik_validator_get_multiple_ciks, mock_single_cik_filing): # Uses same response for filing links (will all be filings for aapl) f = Filing(cik_lookup=['aapl', 'msft', 'amzn'], filing_type=FilingType.FILING_10Q, count=count, client=NetworkClient(batch_size=10)) assert all(len(f.get_urls().get(key)) == count for key in f.get_urls().keys())
def __init__(self, user_agent=None, client=None, entry_filter=None, **kwargs): super().__init__() self._client = client or NetworkClient(user_agent=user_agent, **kwargs) self._listings_directory = None self._master_idx_file = None self._filings_dict = None self._paths = [] self._urls = {} self.entry_filter = entry_filter
def __init__(self, client=None, **kwargs): super().__init__() self._client = client if client is not None else NetworkClient( **kwargs) self._listings_directory = None self._master_idx_file = None self._filings_dict = None self._paths = [] self._urls = []
def test_filing_returns_correct_number_of_urls(self, monkeypatch, count): monkeypatch.setattr(_CIKValidator, "get_ciks", MockCIKValidatorMultipleCIKs.get_ciks) # Use same response for each request monkeypatch.setattr(NetworkClient, "get_response", MockSingleCIKFiling) f = Filing(cik_lookup=['aapl', 'msft', 'amzn'], filing_type=FilingType.FILING_10Q, count=count, client=NetworkClient(batch_size=10)) assert all( len(f.get_urls().get(key)) == count for key in f.get_urls().keys())
def __init__(self, lookups, client, **kwargs): if lookups and isinstance(lookups, str): self._lookups = [lookups] # make single string into list else: # Check that iterable only contains strings and is not empty if not (lookups and all(type(o) is str for o in lookups)): raise TypeError("CIKs must be given as string or iterable.") self._lookups = lookups self._params = {} self._client = client or NetworkClient(**kwargs) self._lookup_dict = None self._ciks = None
def __init__(self, start_date: date, end_date: date, user_agent: Union[str, None] = None, client=None, entry_filter=lambda _: True, balancing_point=30, **kwargs): self.entry_filter = entry_filter self.start_date = start_date self.end_date = end_date self._client = client or NetworkClient(user_agent=user_agent, **kwargs) self._balancing_point = balancing_point
def __init__(self, date, client=None): if not isinstance(date, datetime.datetime): raise TypeError( "Date must be given as datetime object. Was given type {type}." .format(type=type(date))) self._date = date self._client = client if client is not None else NetworkClient() # Caches for responses self._quarterly_directory = None self._master_idx_file = None self._filings_dict = None self._paths = [] self._urls = []
def test_client_passed_to_objects(self, mock_user_agent): client = NetworkClient(user_agent=mock_user_agent) company = filings(cik_lookup="aapl", filing_type=FilingType.FILING_10Q, client=client) daily = filings(start_date=date(2021, 1, 1), end_date=date(2021, 1, 1), client=client) quarterly = filings(start_date=date(2020, 1, 1), end_date=date(2020, 3, 31), client=client) assert company.client == client assert daily.client == client assert quarterly.client == client
def test_filing_raises_warning_when_less_filings_than_count(self, recwarn, count, raises_error, tmp_data_directory, mock_cik_validator_get_single_cik, mock_single_cik_filing_limited_responses): # noqa:E501 f = Filing(cik_lookup=['aapl', 'msft', 'amzn'], filing_type=FilingType.FILING_10Q, count=count, client=NetworkClient(batch_size=10)) f.save(tmp_data_directory) if raises_error: w = recwarn.pop(UserWarning) assert issubclass(w.category, UserWarning) else: try: w = recwarn.pop(UserWarning) pytest.fail("Expected no UserWarning, but received one.") # Should raise assertion error since no UserWarning should be found except AssertionError: pass
def test_filing_raises_warning_when_less_filings_than_count( self, monkeypatch, recwarn, count, raises_error, tmp_data_directory): monkeypatch.setattr(_CIKValidator, "get_ciks", MockCIKValidatorGetCIKs.get_ciks) monkeypatch.setattr(NetworkClient, "get_response", MockSingleCIKFilingLimitedResponses(10)) f = Filing(cik_lookup=['aapl', 'msft', 'amzn'], filing_type=FilingType.FILING_10Q, count=count, client=NetworkClient(batch_size=10)) f.save(tmp_data_directory) if raises_error: w = recwarn.pop(UserWarning) assert issubclass(w.category, UserWarning) else: try: w = recwarn.pop(UserWarning) pytest.fail("Expected no UserWarning, but received one.") # Should raise assertion error since no UserWarning should be found except AssertionError: pass
def __init__(self, cik_lookup, filing_type, start_date=None, end_date=datetime.datetime.today(), client=None, count=None, **kwargs): # Leave params before other setters self._params = { 'action': 'getcompany', 'output': 'xml', 'owner': 'include', 'start': 0, } self.start_date = start_date self.end_date = end_date self.filing_type = filing_type # make CIKLookup object for users if not given self.cik_lookup = cik_lookup self.count = count # Make default client NetworkClient and pass in kwargs self._client = client if client is not None else NetworkClient( **kwargs)
def client(): return NetworkClient(pause=0.01)
def test_set_good_client(self, mock_user_agent): client = NetworkClient(user_agent=mock_user_agent) daily = DailyFilings(date=date(2021, 1, 1), client=client) assert daily.client == client
def client(): return NetworkClient()
def test_good_backoff_factor_setter(self, good_backoff_factor): client = NetworkClient() client.backoff_factor = good_backoff_factor assert client.backoff_factor == good_backoff_factor
def test_bad_backoff_factor_setter(self, bad_backoff_factor): with pytest.raises(TypeError): _ = NetworkClient(backoff_factor=bad_backoff_factor)
def test_client_bad_response_raises_error(self, client): no_cik_response = MockResponse(datapath_args=["CIK", "cik_not_found.html"]) with pytest.raises(EDGARQueryError): NetworkClient()._validate_response(no_cik_response)
def client(mock_user_agent): return NetworkClient(user_agent=mock_user_agent)
def test_good_backoff_factor_setter(self, mock_user_agent, good_backoff_factor): client = NetworkClient(user_agent=mock_user_agent) client.backoff_factor = good_backoff_factor assert client.backoff_factor == good_backoff_factor
def test_client_property_set(self, mock_user_agent): aapl = CompanyFilings( cik_lookup="aapl", filing_type=FilingType.FILING_10Q, client=NetworkClient(user_agent=mock_user_agent)) assert aapl.client.user_agent == mock_user_agent
def test_client_bad_user_agent(self, user_agent): with pytest.raises(TypeError): _ = NetworkClient(user_agent=user_agent)
def test_bad_backoff_factor_setter(self, bad_backoff_factor, mock_user_agent): with pytest.raises(TypeError): _ = NetworkClient(user_agent=mock_user_agent, backoff_factor=bad_backoff_factor)
def mock_client_cik_lookup(mock_user_agent): return NetworkClient(user_agent=mock_user_agent)
def real_test_client(): """``NetworkClient`` to use when running live smoke tests.""" return NetworkClient(user_agent="sec_edgar_testing")