本文整理汇总了Python中mock.Mock.query方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.query方法的具体用法?Python Mock.query怎么用?Python Mock.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.Mock
的用法示例。
在下文中一共展示了Mock.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_retrieve_report_metadata_double_tumor_ref
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_retrieve_report_metadata_double_tumor_ref(self, mock_sqlalchemy):
mock_sqlalchemy.or_ = Mock()
mock_session = Mock()
mock_session.query = Mock()
mock_session.query().filter().all = Mock(side_effect=[["dummy"], ["dummy1", "dummy2"]])
self.assertRaises(ValueError, metadata.retrieve_report_metadata, "03098121", "03098849", mock_session, self.id2addresses)
示例2: test_retrieve_report_metadata_missing_blood_ref
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_retrieve_report_metadata_missing_blood_ref(self, mock_sqlalchemy):
mock_sqlalchemy.or_ = Mock()
mock_session = Mock()
mock_session.query = Mock()
mock_session.query().filter().all = Mock(side_effect=[[], []])
self.assertRaises(ValueError, metadata.retrieve_report_metadata, "03098121", "03098849", mock_session, self.id2addresses)
示例3: test_query_database_no_barcode_attributes
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_query_database_no_barcode_attributes(self, mock_sqlalchemy):
mock_sqlalchemy.or_ = Mock()
mock_referral_type = Mock()
mock_session = Mock()
mock_session.query = Mock()
mock_session.query().filter().all = Mock(side_effect=[["dummy"]])
self.assertRaises(ValueError, metadata.query_database, "03098121", mock_referral_type, mock_session)
示例4: test_query_database_one_hit
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_query_database_one_hit(self, mock_sqlalchemy):
mock_sqlalchemy.or_ = Mock()
mock_referral_type = Mock()
mock_referral_type.barcode1 = "03098121"
mock_session = Mock()
mock_session.query = Mock()
mock_session.query().filter().all = Mock(side_effect=[["dummy"]])
output = metadata.query_database("03098121", mock_referral_type, mock_session)
self.assertEqual(output, "dummy")
示例5: test_get_df_handles_dttm_col
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_get_df_handles_dttm_col(self):
form_data = {'dummy': 123}
query_obj = {'granularity': 'day'}
results = Mock()
results.query = Mock()
results.status = Mock()
results.error_message = Mock()
datasource = Mock()
datasource.type = 'table'
datasource.query = Mock(return_value=results)
mock_dttm_col = Mock()
datasource.get_col = Mock(return_value=mock_dttm_col)
test_viz = viz.BaseViz(datasource, form_data)
test_viz.df_metrics_to_num = Mock()
test_viz.get_fillna_for_columns = Mock(return_value=0)
results.df = pd.DataFrame(data={DTTM_ALIAS: ['1960-01-01 05:00:00']})
datasource.offset = 0
mock_dttm_col = Mock()
datasource.get_col = Mock(return_value=mock_dttm_col)
mock_dttm_col.python_date_format = 'epoch_ms'
result = test_viz.get_df(query_obj)
print(result)
import logging
logging.info(result)
pd.testing.assert_series_equal(
result[DTTM_ALIAS],
pd.Series([datetime(1960, 1, 1, 5, 0)], name=DTTM_ALIAS),
)
mock_dttm_col.python_date_format = None
result = test_viz.get_df(query_obj)
pd.testing.assert_series_equal(
result[DTTM_ALIAS],
pd.Series([datetime(1960, 1, 1, 5, 0)], name=DTTM_ALIAS),
)
datasource.offset = 1
result = test_viz.get_df(query_obj)
pd.testing.assert_series_equal(
result[DTTM_ALIAS],
pd.Series([datetime(1960, 1, 1, 6, 0)], name=DTTM_ALIAS),
)
datasource.offset = 0
results.df = pd.DataFrame(data={DTTM_ALIAS: ['1960-01-01']})
mock_dttm_col.python_date_format = '%Y-%m-%d'
result = test_viz.get_df(query_obj)
pd.testing.assert_series_equal(
result[DTTM_ALIAS],
pd.Series([datetime(1960, 1, 1, 0, 0)], name=DTTM_ALIAS),
)
示例6: test_retrieve_report_metadata_mismatching_ids
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_retrieve_report_metadata_mismatching_ids(self, mock_sqlalchemy):
mock_sqlalchemy.or_ = Mock()
mock_session = Mock()
mock_session.query = Mock()
mock_blood_ref = Mock()
mock_blood_ref.pnr = "1212121212"
mock_tissue_ref = Mock()
mock_tissue_ref.pnr = "1212121213"
mock_session.query().filter().all = Mock(side_effect=[[mock_blood_ref], [mock_tissue_ref]])
self.assertRaises(ValueError, metadata.retrieve_report_metadata, "03098121", "03098849", mock_session, self.id2addresses)
示例7: test_report_customers_by_products
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_report_customers_by_products(self):
""" Unit test to customers grouped by product report.
"""
# Mock sale model and products models
salesList = []
keys = [2, 1, 3, 2, 1]
for x in keys:
sale = Mock(spec_set=saleModel.SaleModel())
sale.product = Mock(spec_set=productModel.ProductModel())
sale.product.key = Mock(spec_set=ndb.Key('ProductModel', x))
sale.product.key.id = Mock(return_value=x)
sale.product.key.get = Mock(return_value=sale.product)
salesList.append(sale)
# Mock customers models
keys = [{'id': '1', 'name': 'Test1'}, {'id': '2', 'name': 'Test2'}]
for sale in salesList:
sale.customer = Mock(spec_set=customerModel.CustomerModel())
customer = keys[randint(0, 1)]
sale.customer.key = Mock(
spec_set=ndb.Key('CustomerModel', customer['id']))
sale.customer.key.id = Mock(return_value=customer['id'])
sale.customer.key.get = Mock(return_value=customer)
sale.customer.name = PropertyMock(return_value=customer['name'])
# Mock fetch method
mockSale = Mock(spec_set=saleModel.SaleModel())
mockSale.query = Mock(spec=mockSale.query)
mockSale.query().fetch = MagicMock(return_value=salesList)
# Set get_sales_query to return query mocked
saleModel.get_sales_query = MagicMock(return_value=mockSale.query())
# Call report_customers_by_product method
result = saleModel.report_customers_by_products()
# Must have lenght == 3
self.assertEqual(len(result), 3)
# The result must be:
# Product 1 => 2 Customers
# Product 2 => 2 Customers
# Product 3 => 1 Customer
self.assertEqual(len(result[0]['customers']), 2)
self.assertEqual(len(result[1]['customers']), 2)
self.assertEqual(len(result[2]['customers']), 1)
# And the products must be ordered
self.assertEqual(result[0]['product'].key.id(), 1)
self.assertEqual(result[1]['product'].key.id(), 2)
self.assertEqual(result[2]['product'].key.id(), 3)
示例8: test_report_products_by_customers
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_report_products_by_customers(self):
""" Unit test to products grouped by customers report.
"""
# Mock sale model and products models
salesList = []
keys = [2, 1, 3, 2, 1]
for x in keys:
sale = Mock(spec_set=saleModel.SaleModel())
sale.customer = Mock(spec_set=customerModel.CustomerModel())
sale.customer.key = Mock(spec_set=ndb.Key('CustomerModel', x))
sale.customer.key.id = Mock(return_value=x)
sale.customer.key.get = Mock(return_value=sale.customer)
salesList.append(sale)
# Mock products models
keys = [2, 3, 1]
for sale in salesList:
sale.product = Mock(spec_set=productModel.ProductModel())
key_id = keys[randint(0, 2)]
sale.product.key = Mock(
spec_set=ndb.Key('ProductModel', key_id))
sale.product.key.id = Mock(return_value=key_id)
sale.product.key.get = Mock(return_value=sale.product)
# Mock fetch method
mockSale = Mock(spec_set=saleModel.SaleModel())
mockSale.query = Mock(spec=mockSale.query)
mockSale.query().fetch = MagicMock(return_value=salesList)
# Set get_sales_query to return query mocked
saleModel.get_sales_query = MagicMock(return_value=mockSale.query())
# Call report_customers_by_product method
result = saleModel.report_products_by_customers()
# Must have lenght == 3
self.assertEqual(len(result), 3)
# The result must be:
# Customer 1 => 2 Customers
# Customer 2 => 2 Customers
# Customer 3 => 1 Customer
self.assertEqual(len(result[0]['products']), 2)
self.assertEqual(len(result[1]['products']), 2)
self.assertEqual(len(result[2]['products']), 1)
# And the customers must be ordered
self.assertEqual(result[0]['customer'].key.id(), 1)
self.assertEqual(result[1]['customer'].key.id(), 2)
self.assertEqual(result[2]['customer'].key.id(), 3)
示例9: test_get_df_handles_dttm_col
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_get_df_handles_dttm_col(self):
datasource = Mock()
datasource.type = 'table'
datasource.offset = 1
mock_dttm_col = Mock()
mock_dttm_col.python_date_format = 'epoch_ms'
datasource.get_col = Mock(return_value=mock_dttm_col)
form_data = {'dummy': 123}
query_obj = {'granularity': 'day'}
results = Mock()
results.query = Mock()
results.status = Mock()
results.error_message = Mock()
df = Mock()
df.columns = [DTTM_ALIAS]
f_datetime = datetime(1960, 1, 1, 5, 0)
df.__getitem__ = Mock(return_value=pd.Series([f_datetime]))
df.__setitem__ = Mock()
df.replace = Mock()
df.fillna = Mock()
results.df = df
results.df.empty = False
datasource.query = Mock(return_value=results)
test_viz = viz.BaseViz(datasource, form_data)
test_viz.df_metrics_to_num = Mock()
test_viz.get_fillna_for_columns = Mock(return_value=0)
test_viz.get_df(query_obj)
mock_call = df.__setitem__.mock_calls[0]
self.assertEqual(mock_call[1][0], DTTM_ALIAS)
self.assertFalse(mock_call[1][1].empty)
self.assertEqual(mock_call[1][1][0], f_datetime)
mock_call = df.__setitem__.mock_calls[1]
self.assertEqual(mock_call[1][0], DTTM_ALIAS)
self.assertEqual(mock_call[1][1][0].hour, 6)
self.assertEqual(mock_call[1][1].dtype, 'datetime64[ns]')
mock_dttm_col.python_date_format = 'utc'
test_viz.get_df(query_obj)
mock_call = df.__setitem__.mock_calls[2]
self.assertEqual(mock_call[1][0], DTTM_ALIAS)
self.assertFalse(mock_call[1][1].empty)
self.assertEqual(mock_call[1][1][0].hour, 7)
mock_call = df.__setitem__.mock_calls[3]
self.assertEqual(mock_call[1][0], DTTM_ALIAS)
self.assertEqual(mock_call[1][1][0].hour, 6)
self.assertEqual(mock_call[1][1].dtype, 'datetime64[ns]')
mock_call = df.__setitem__.mock_calls[4]
self.assertEqual(mock_call[1][0], DTTM_ALIAS)
self.assertEqual(mock_call[1][1][0].hour, 7)
self.assertEqual(mock_call[1][1].dtype, 'datetime64[ns]')
示例10: test_get_df_returns_empty_df
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_get_df_returns_empty_df(self):
datasource = Mock()
datasource.type = 'table'
form_data = {'dummy': 123}
query_obj = {'granularity': 'day'}
results = Mock()
results.query = Mock()
results.status = Mock()
results.error_message = None
results.df = pd.DataFrame()
datasource.query = Mock(return_value=results)
test_viz = viz.BaseViz(datasource, form_data)
result = test_viz.get_df(query_obj)
self.assertEqual(type(result), pd.DataFrame)
self.assertTrue(result.empty)
示例11: get_datasource_mock
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def get_datasource_mock(self):
datasource = Mock()
results = Mock()
results.query = Mock()
results.status = Mock()
results.error_message = None
results.df = pd.DataFrame()
datasource.type = 'table'
datasource.query = Mock(return_value=results)
mock_dttm_col = Mock()
datasource.get_col = Mock(return_value=mock_dttm_col)
datasource.query = Mock(return_value=results)
datasource.database = Mock()
datasource.database.db_engine_spec = Mock()
datasource.database.db_engine_spec.mutate_expression_label = lambda x: x
return datasource
示例12: test_old_query_params_except_offset
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_old_query_params_except_offset(self):
uri = Mock()
uri.query = "foo=bar&spam=ham"
result = []
helpers._get_old_query_params_except_offset(result, uri)
self.assertEqual(result, ["foo=bar", "spam=ham"])
示例13: test_run
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_run(self, *mocks):
eddaclient = Mock()
eddaclient._since = 500
def ret_list(args):
return [
{'imageId': 'ami-1', 'instanceId': 'a', 'launchTime': '500',
'tags': [{'key': 'service_name', 'value': 'conversion'}, {'key': 'started_by', 'value': 'john'}]},
{'imageId': 'ami-1', 'instanceId': 'b', 'launchTime': '2000',
'tags': [{'key': 'service_name', 'value': 'router'}]},
{'imageId': 'ami-2', 'instanceId': 'c', 'launchTime': '400'}]
m = Mock()
m.query = Mock(side_effect=ret_list)
eddaclient.clean = Mock(return_value=m)
self.plugin.init(eddaclient, self.config, {'first_seen': {"ami-1": 1000, "ami-2": 400}})
real = self.plugin.run()
expected = [
{'id': 'ami-1', 'plugin_name': 'ami', 'details': [
('a', 500, [{'service_name': 'conversion'}, {'started_by': 'john'}]),
('b', 2000, [{'service_name': 'router'}])]}
]
self.assertEqual(expected, real)
m.query.assert_has_calls([call('/api/v2/view/instances;_expand')])
self.assertEqual(self.plugin.status, {'first_seen': {'ami-1': 500, 'ami-2': 400}})
示例14: test_run
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_run(self, *mocks):
instance_enricher = InstanceEnricher(Mock())
eddaclient = Mock()
eddaclient._since = 500
def ret_list(args):
return [
{'imageId': 'ami-1', 'instanceId': 'a', 'launchTime': 400, "tags": [{"key": "Name", "value": "tag1"}]},
{'imageId': 'ami-2', 'instanceId': 'b', 'launchTime': 600,
"tags": [{"key": "service_name", "value": "foo"}]},
{'imageId': 'ami-3', 'instanceId': 'c', 'launchTime': 800, "tags":
[{"key": "Name", "value": "tag1"}, {"key": "service_name", "value": "foo"}]},
]
m = Mock()
m.query = Mock(side_effect=ret_list)
eddaclient.clean = Mock(return_value=m)
self.plugin.init(eddaclient, Mock(), {}, instance_enricher)
# run the tested method
result = self.plugin.run()
self.assertEqual(1, len(result))
result = result[0]
self.assertEqual("foo", result["id"])
self.assertEqual(2, len(result["details"]))
self.assertIn("b", [d["instanceId"] for d in result["details"]])
self.assertIn("c", [d["instanceId"] for d in result["details"]])
m.query.assert_has_calls([call('/api/v2/view/instances;_expand')])
示例15: test_old_query_params_except_offset_with_equals
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import query [as 别名]
def test_old_query_params_except_offset_with_equals(self):
uri = Mock()
uri.query = "foo=spam=ham&offset=bar"
result = []
helpers._get_old_query_params_except_offset(result, uri)
self.assertEqual(result, ["foo=spam%3Dham"])