diff --git a/check_dist.sh b/check_dist.sh new file mode 100644 index 0000000..782dc13 --- /dev/null +++ b/check_dist.sh @@ -0,0 +1,13 @@ +set -e +#rmvirtualenv temp +#mkvirtualenv temp +bash create_dist.sh +pip install . +cd ~/Downloads +rm -Rf py_mock_data_test_dist +mkdir py_mock_data_test_dist +cd py_mock_data_test_dist +cp ~/projects/py-data-mock/requirements_test_dist.txt . +pip install -r requirements_test_dist.txt +cp -R $PROJECTS_DIR/py-data-mock/unittests . +bash unittests/test_all.sh diff --git a/data_mock/__init__.py b/data_mock/__init__.py index 6e040bf..5b6c1a1 100644 --- a/data_mock/__init__.py +++ b/data_mock/__init__.py @@ -1,3 +1,3 @@ -__version__ = "0.0.2" +__version__ = "0.0.3" __author__ = 'Henry Tremblay' diff --git a/data_mock/google/cloud/bigquery/client.py b/data_mock/google/cloud/bigquery/client.py index 6de8fdb..bba47a3 100644 --- a/data_mock/google/cloud/bigquery/client.py +++ b/data_mock/google/cloud/bigquery/client.py @@ -1,9 +1,9 @@ -from . import exceptions from . import table as _table from .job import query as job_query from . import retry as retries from . import dataset as _dataset import data_mock.mock_helpers.provider as provider +import data_mock.exceptions as exceptions from typing import Union, Optional @@ -49,7 +49,10 @@ def query(self, query, if not data: raise exceptions.InvalidMockData(f'{key} not found in registered_data') else: - data, m = self.data_provider.get_data('default') + try: + data, m = self.data_provider.get_data('default') + except TypeError: + raise exceptions.InvalidMockData(f'bad class') return _table.RowIterator(data = data, m = m) def create_table(self, diff --git a/data_mock/mock_helpers/provider.py b/data_mock/mock_helpers/provider.py index 86c6906..28c6b9a 100644 --- a/data_mock/mock_helpers/provider.py +++ b/data_mock/mock_helpers/provider.py @@ -3,6 +3,7 @@ import data_mock.exceptions from data_mock.google.cloud import bigquery #from bigquery import SchemaField +import types class QueryResultsFromList: @@ -54,6 +55,10 @@ def get_data(self, key:str) -> Union[None, types.GeneratorType]: return None, None if not hasattr(self.dict[key], 'query_results'): raise data_mock.exceptions.InvalidMockData('object does not have query_results') + if not isinstance(self.dict[key].query_results, types.MethodType): + msg = '"query_results" is not a method; did you pass the class rather than class()?' + raise data_mock.exceptions.InvalidMockData(msg) + return self.dict[key].query_results() diff --git a/setup.py b/setup.py index 5594128..08255f8 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='py-data-mock', - version='0.0.2', + version='0.0.3', scripts=['data_mock/scripts/mkmock.py'], description='Mock Data', url='https://github.com/paulhtremblay/py-data-mock', @@ -10,7 +10,7 @@ author_email='paulhtremblay@gmail.com', license='GNU GENERAL PUBLIC LICENSE', packages=['data_mock/google/cloud/bigquery/job', 'data_mock/google/cloud/bigquery', - 'data_mock/google/cloud/storage','data_mock/mock_helpers', + 'data_mock/google/cloud/storage','data_mock/mock_helpers', 'data_mock', ], classifiers=[ 'Programming Language :: Python :: 3', diff --git a/unittests/test_client.py b/unittests/test_client.py index 16f1f35..b7c1750 100644 --- a/unittests/test_client.py +++ b/unittests/test_client.py @@ -116,6 +116,13 @@ def query_results(self): else: return self.gen_func1(), {'total_rows':10} +class BadClass1: + def query_results(self): + return None + + + + class TestResults(unittest.TestCase): def setUp(self): @@ -206,6 +213,47 @@ def test_not_a_list_in_list_data_raises_InvalidData(self): data_mock.exceptions.InvalidMockData, bigquery.Client, mock_data = data ) + def test_data_not_a_class_raises_InvalidData(self): + class Client(bigquery.Client): + + def register_initial_mock_data(self): + self.data_provider.add_data(data =ProviderData1, tag = 'default') + client = Client() + self.assertRaises( + data_mock.exceptions.InvalidMockData, + client.query, query = '' + ) + + def test_data_not_a_class2_raises_InvalidData(self): + class Client(bigquery.Client): + + def register_initial_mock_data(self): + self.data_provider.add_data(data =1, tag = 'default') + client = Client() + self.assertRaises( + data_mock.exceptions.InvalidMockData, + client.query, query = '' + ) + + def test_data_not_a_class3_raises_InvalidData(self): + class Client(bigquery.Client): + def query_results(self): + return None + + def register_initial_mock_data(self): + self.data_provider.add_data(data =BadClass1(), tag = 'default') + + c = Client() + self.assertRaises(data_mock.exceptions.InvalidMockData,c.query, query = '') + + def test_data_not_registered_raises_InvalidData(self): + client = bigquery.Client() + sql = """ + + py-bigquery-mock-register: data1 + """ + self.assertRaises(data_mock.exceptions.InvalidMockData,client.query, query = sql) + def test_create_table_succeeds(self): table_id = 'project.dataset_id.tabele_id'