def test_utils_load_cached(path): _load = utils.load with mock.patch( 'fake_useragent.utils.load', side_effect=_load, ) as mocked: data = utils.load_cached(path, use_cache_server=False) mocked.assert_called() expected = { 'randomize': mock.ANY, 'browsers': { 'chrome': mock.ANY, 'firefox': mock.ANY, 'opera': mock.ANY, 'safari': mock.ANY, 'internetexplorer': mock.ANY, }, } assert expected == data expected = data with mock.patch('fake_useragent.utils.load') as mocked: data = utils.load_cached(path, use_cache_server=False) mocked.assert_not_called() assert expected == data
def test_load_cached(): data = utils.load_cached() check_dict(data) clear() data = utils.load_cached() check_dict(data)
def test_load_cached(): data = utils.load_cached(settings.DB) check_dict(data) clear(settings.DB) data = utils.load_cached(settings.DB) check_dict(data)
def load(self): try: with self.load.lock: if self.cache: self.data = load_cached( self.path, use_cache_server=self.use_cache_server, verify_ssl=self.verify_ssl, ) else: self.data = load( use_cache_server=self.use_cache_server, verify_ssl=self.verify_ssl, ) # TODO: change source file format # version 0.1.4+ migration tool self.data_randomize = list(self.data['randomize'].values()) self.data_browsers = self.data['browsers'] except FakeUserAgentError: if self.fallback is None: raise else: logger.warning( 'Error occurred during fetching data, ' 'but was suppressed with fallback.', )
def test_utils_load_no_use_cache_server(path): denied_urls = [ 'https://www.w3schools.com/browsers/browsers_stats.asp', 'http://useragentstring.com/pages/useragentstring.php', ] with mock.patch( 'fake_useragent.utils.Request', side_effect=partial(_request, denied_urls=denied_urls), ): with pytest.raises(errors.FakeUserAgentError): utils.load(use_cache_server=False) with pytest.raises(errors.FakeUserAgentError): utils.load_cached(path, use_cache_server=False) with pytest.raises(errors.FakeUserAgentError): utils.update(path, use_cache_server=False)
def __init__(self, cache=True): super(UserAgent, self).__init__() if cache: self.data = load_cached() else: self.data = load() self.cache = cache
def load(self): try: with self.load.lock: if self.cache: self.data = load_cached(self.path) else: self.data = load() # TODO: change source file format # version 0.1.4- migration tool self.data_randomize = list(self.data['randomize'].values()) self.data_browsers = self.data['browsers'] except FakeUserAgentError: if self.fallback is None: logger.error('Error occurred during fetching data...', ) raise else: logger.warning( 'Error occurred during fetching data but was suppressed with fallback.', # noqa )
def load(self): try: with self.load.lock: if self.cache: self.data = load_cached( self.path, use_cache_server=self.use_cache_server, verify_ssl=self.verify_ssl, ) else: self.data = load( use_cache_server=self.use_cache_server, verify_ssl=self.verify_ssl, ) # TODO: change source file format # version 0.1.4+ migration tool self.data_randomize = list(self.data['randomize'].values()) self.data_browsers = self.data['browsers'] for family in self.data_browsers: for browser in self.data_browsers[family]: if re.search('(Mobile|Android)', browser): if family not in self.data_browsers_mobile: self.data_browsers_mobile.update({family: []}) self.data_browsers_mobile[family].append(browser) else: if family not in self.data_browsers_desktop: self.data_browsers_desktop.update({family: []}) self.data_browsers_desktop[family].append(browser) except FakeUserAgentError: if self.fallback is None: raise else: logger.warning( 'Error occurred during fetching data, ' 'but was suppressed with fallback.', )
def load(self): if self.cache: self.data = load_cached() else: self.data = load()