def setUp(self): if self.contexts is None: self.contexts = [] o = patch.object(options.mockable(), 'databases', {"default": "sqlite:///", "odoo": "sqlite:///", "data_image_match": "sqlite:///", "etl_info": "sqlite:///", "image_match": "sqlite:///", "redshift": "sqlite:///" }) self.contexts.append(o) # 对外请求mock掉,在单元测试里特殊指定响应 o = patch.object(options.mockable(), 'purchase_url', "http://") self.contexts.append(o) o = patch.object(options.mockable(), 'odoo_url', "http://") self.contexts.append(o) for context in self.contexts: context.__enter__() super(EngineTest, self).setUp() session = ModelBase.get_session() for key, engine in session.shards.items(): self.assertEqual(engine.driver, "pysqlite") ModelBase.metadata.create_all(engine)
def test_parse_option_as_dict(mocker: MockerFixture) -> None: define("opt1") define("opt2") mocker.patch.object(options.mockable(), "opt1", "k1:v1,k2:v2") mocker.patch.object(options.mockable(), "opt2", "a:b:c") parse_option_as_dict.cache_clear() # Compare as a list to verify insertion order assert list(parse_option_as_dict("opt1").items()) == [ ("k1", "v1"), ("k2", "v2"), ] # Case with multiple colons in the value assert parse_option_as_dict("opt2") == { "a": "b:c", }
def test_proxy(self): o = patch.object(options.mockable(), 'cache_options', {"max_size": 2}) o.__enter__() self.contexts.append(o) o = patch.object(options.mockable(), 'cache_engine', "apps.core.cache.memory.MemoryCache") o.__enter__() self.contexts.append(o) yield cache_proxy.set("somekey", 1, 1) yield cache_proxy.set("somekey2", 2, 2) yield sleep(2) self.assertNotIn("somekey", cache_proxy._cache) self.assertNotIn("somekey", cache_proxy)
def setUp(self): if self.contexts is None: self.contexts = [] o = patch.object(options.mockable(), 'cache_engine', "apps.core.cache.memory.MemoryCache") self.contexts.append(o) super(MemoryCacheTestCase, self).setUp()
def setUp(self): self.database_name = 'test_motorblog' sync_client = pymongo.mongo_client.MongoClient() sync_db = sync_client[self.database_name] for collection_name in [ 'events', 'fs.chunks', 'fs.files', 'posts', 'categories']: sync_db.drop_collection(collection_name) self.patchers = [] patcher = mock.patch.multiple( tornado_options.mockable(), host='localhost', blog_name='My Test Blog', base_url='test-blog', author_display_name='Test J. Author', author_email='*****@*****.**', twitter_handle='test_twitter_handle', description='test description', google_analytics_id='TEST GA ID', user='******', password='******', cookie_secret='test-cookie-secret', debug=True, ) patcher.start() self.patchers.append(patcher) # Sets self.__port, and sets self.app = self.get_app(). super(MotorBlogTest, self).setUp()
def test_allow_origin(self): response = self.fetch('/') assert 'Access-Control-Allow-Origin' not in response.headers with mock.patch.object(options.mockable(), 'debug', True): response = self.fetch('/') assert response.headers['Access-Control-Allow-Origin'] == '*'
def test_allowed_hosts(self, mock_app): """Allowed host option should be passed from the command line to the application.""" with patch.object(options.mockable(), 'allowed_hosts', ['example.com', ]): with patch('shoestring.__main__.parse_command_line') as mock_parse: main(self.io_loop) mock_parse.assert_called_with() expected = self.get_app_defaults() expected['allowed_hosts'] = ['example.com', ] mock_app.assert_called_with(**expected)
def test_backed(self, mock_app): """Backend option should be passed from the command line to the application.""" with patch.object(options.mockable(), 'backend', __name__): with patch('shoestring.__main__.parse_command_line') as mock_parse: main(self.io_loop) mock_parse.assert_called_with() expected = self.get_app_defaults() expected['backend'] = __name__ mock_app.assert_called_with(**expected)
def test_debug(self, mock_app): """Debug option should be passed from the command line to the application.""" with patch.object(options.mockable(), 'debug', True): with patch('shoestring.__main__.parse_command_line') as mock_parse: main(self.io_loop) mock_parse.assert_called_with() expected = self.get_app_defaults() expected['debug'] = True mock_app.assert_called_with(**expected)
def get_app(self): # At the point Tornado patcher = mock.patch.object( tornado_options.mockable(), 'port', self.get_http_port()) patcher.start() self.patchers.append(patcher) db = self.get_db() return application.get_application('..', db, tornado.options.options)
def get_app(self): # At the point Tornado patcher = mock.patch.object(tornado_options.mockable(), 'port', self.get_http_port()) patcher.start() self.patchers.append(patcher) db = self.get_db() return application.get_application('..', db, tornado.options.options)
def setUp(self): if self.contexts is None: self.contexts = [] o = patch.object(options.mockable(), 'base_url', b"/") self.contexts.append(o) super(BaseTestCase, self).setUp() engine = _get_master_engine() self.assertEqual(engine.driver, "pysqlite") ModelBase.metadata.create_all(engine)
def test_allowed_hosts(self, mock_app): """Allowed host option should be passed from the command line to the application.""" with patch.object(options.mockable(), 'allowed_hosts', [ 'example.com', ]): with patch('shoestring.__main__.parse_command_line') as mock_parse: main(self.io_loop) mock_parse.assert_called_with() expected = self.get_app_defaults() expected['allowed_hosts'] = [ 'example.com', ] mock_app.assert_called_with(**expected)
def setUp(self): if self.contexts is None: self.contexts = [] o = patch.object(options.mockable(), 'sql_connection', b"sqlite:///") self.contexts.append(o) for context in self.contexts: context.__enter__() super(EngineTest, self).setUp() engine = _get_master_engine() self.assertEqual(str(engine.url), options.test_db) self.assertEqual(engine.driver, "pysqlite") engine = _get_slave_engine() self.assertEqual(str(engine.url), options.test_db) self.assertEqual(engine.driver, "pysqlite")
def mock_option(self, name, value): return mock.patch.object(options.mockable(), name, value)
def set_option(self, name, value): patcher = mock.patch.object(tornado_options.mockable(), name, value) patcher.start() # So we can reverse it in tearDown. self.patchers.append(patcher)
def test_get_mypy_versions(mocker: MockerFixture) -> None: mocker.patch.object(options.mockable(), "mypy_versions", "mypy 0.790:0.790") get_mypy_versions.cache_clear() assert get_mypy_versions() == [("mypy 0.790", "0.790")]