Skip to content

Commit

Permalink
Merge pull request #6 from paulhtremblay/development
Browse files Browse the repository at this point in the history
fixing bug; updating docs
  • Loading branch information
paulhtremblay authored May 25, 2023
2 parents 3c05ee6 + f0ba470 commit 0b5461b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 5 deletions.
13 changes: 13 additions & 0 deletions check_dist.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion data_mock/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.0.2"
__version__ = "0.0.3"
__author__ = 'Henry Tremblay'

7 changes: 5 additions & 2 deletions data_mock/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions data_mock/mock_helpers/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import data_mock.exceptions
from data_mock.google.cloud import bigquery
#from bigquery import SchemaField
import types

class QueryResultsFromList:

Expand Down Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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',
author='Henry Tremblay',
author_email='[email protected]',
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',
Expand Down
48 changes: 48 additions & 0 deletions unittests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 0b5461b

Please sign in to comment.