Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Coding Style

Exceptions to PEP8:

- Many unit tests use a helper method, ``_callFUT`` ("FUT" is short for
- Many unit tests use a helper method, ``_call_fut`` ("FUT" is short for
"Function-Under-Test"), which is PEP8-incompliant, but more readable.
Some also use a local variable, ``MUT`` (short for "Module-Under-Test").

Expand Down
120 changes: 62 additions & 58 deletions bigquery/unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,177 +17,177 @@

class Test_not_null(unittest.TestCase):

def _callFUT(self, value, field):
def _call_fut(self, value, field):
from google.cloud.bigquery._helpers import _not_null
return _not_null(value, field)

def test_w_none_nullable(self):
self.assertFalse(self._callFUT(None, _Field('NULLABLE')))
self.assertFalse(self._call_fut(None, _Field('NULLABLE')))

def test_w_none_required(self):
self.assertTrue(self._callFUT(None, _Field('REQUIRED')))
self.assertTrue(self._call_fut(None, _Field('REQUIRED')))

def test_w_value(self):
self.assertTrue(self._callFUT(object(), object()))
self.assertTrue(self._call_fut(object(), object()))


class Test_int_from_json(unittest.TestCase):

def _callFUT(self, value, field):
def _call_fut(self, value, field):
from google.cloud.bigquery._helpers import _int_from_json
return _int_from_json(value, field)

def test_w_none_nullable(self):
self.assertIsNone(self._callFUT(None, _Field('NULLABLE')))
self.assertIsNone(self._call_fut(None, _Field('NULLABLE')))

def test_w_none_required(self):
with self.assertRaises(TypeError):
self._callFUT(None, _Field('REQUIRED'))
self._call_fut(None, _Field('REQUIRED'))

def test_w_string_value(self):
coerced = self._callFUT('42', object())
coerced = self._call_fut('42', object())
self.assertEqual(coerced, 42)

def test_w_float_value(self):
coerced = self._callFUT(42, object())
coerced = self._call_fut(42, object())
self.assertEqual(coerced, 42)


class Test_float_from_json(unittest.TestCase):

def _callFUT(self, value, field):
def _call_fut(self, value, field):
from google.cloud.bigquery._helpers import _float_from_json
return _float_from_json(value, field)

def test_w_none_nullable(self):
self.assertIsNone(self._callFUT(None, _Field('NULLABLE')))
self.assertIsNone(self._call_fut(None, _Field('NULLABLE')))

def test_w_none_required(self):
with self.assertRaises(TypeError):
self._callFUT(None, _Field('REQUIRED'))
self._call_fut(None, _Field('REQUIRED'))

def test_w_string_value(self):
coerced = self._callFUT('3.1415', object())
coerced = self._call_fut('3.1415', object())
self.assertEqual(coerced, 3.1415)

def test_w_float_value(self):
coerced = self._callFUT(3.1415, object())
coerced = self._call_fut(3.1415, object())
self.assertEqual(coerced, 3.1415)


class Test_bool_from_json(unittest.TestCase):

def _callFUT(self, value, field):
def _call_fut(self, value, field):
from google.cloud.bigquery._helpers import _bool_from_json
return _bool_from_json(value, field)

def test_w_none_nullable(self):
self.assertIsNone(self._callFUT(None, _Field('NULLABLE')))
self.assertIsNone(self._call_fut(None, _Field('NULLABLE')))

def test_w_none_required(self):
with self.assertRaises(AttributeError):
self._callFUT(None, _Field('REQUIRED'))
self._call_fut(None, _Field('REQUIRED'))

def test_w_value_t(self):
coerced = self._callFUT('T', object())
coerced = self._call_fut('T', object())
self.assertTrue(coerced)

def test_w_value_true(self):
coerced = self._callFUT('True', object())
coerced = self._call_fut('True', object())
self.assertTrue(coerced)

def test_w_value_1(self):
coerced = self._callFUT('1', object())
coerced = self._call_fut('1', object())
self.assertTrue(coerced)

def test_w_value_other(self):
coerced = self._callFUT('f', object())
coerced = self._call_fut('f', object())
self.assertFalse(coerced)


class Test_datetime_from_json(unittest.TestCase):

def _callFUT(self, value, field):
def _call_fut(self, value, field):
from google.cloud.bigquery._helpers import _datetime_from_json
return _datetime_from_json(value, field)

def test_w_none_nullable(self):
self.assertIsNone(self._callFUT(None, _Field('NULLABLE')))
self.assertIsNone(self._call_fut(None, _Field('NULLABLE')))

def test_w_none_required(self):
with self.assertRaises(TypeError):
self._callFUT(None, _Field('REQUIRED'))
self._call_fut(None, _Field('REQUIRED'))

def test_w_string_value(self):
import datetime
from google.cloud._helpers import _EPOCH
coerced = self._callFUT('1.234567', object())
coerced = self._call_fut('1.234567', object())
self.assertEqual(
coerced,
_EPOCH + datetime.timedelta(seconds=1, microseconds=234567))

def test_w_float_value(self):
import datetime
from google.cloud._helpers import _EPOCH
coerced = self._callFUT(1.234567, object())
coerced = self._call_fut(1.234567, object())
self.assertEqual(
coerced,
_EPOCH + datetime.timedelta(seconds=1, microseconds=234567))


class Test_date_from_json(unittest.TestCase):

def _callFUT(self, value, field):
def _call_fut(self, value, field):
from google.cloud.bigquery._helpers import _date_from_json
return _date_from_json(value, field)

def test_w_none_nullable(self):
self.assertIsNone(self._callFUT(None, _Field('NULLABLE')))
self.assertIsNone(self._call_fut(None, _Field('NULLABLE')))

def test_w_none_required(self):
with self.assertRaises(TypeError):
self._callFUT(None, _Field('REQUIRED'))
self._call_fut(None, _Field('REQUIRED'))

def test_w_string_value(self):
import datetime
coerced = self._callFUT('1987-09-22', object())
coerced = self._call_fut('1987-09-22', object())
self.assertEqual(
coerced,
datetime.date(1987, 9, 22))


class Test_record_from_json(unittest.TestCase):

def _callFUT(self, value, field):
def _call_fut(self, value, field):
from google.cloud.bigquery._helpers import _record_from_json
return _record_from_json(value, field)

def test_w_none_nullable(self):
self.assertIsNone(self._callFUT(None, _Field('NULLABLE')))
self.assertIsNone(self._call_fut(None, _Field('NULLABLE')))

def test_w_none_required(self):
with self.assertRaises(TypeError):
self._callFUT(None, _Field('REQUIRED'))
self._call_fut(None, _Field('REQUIRED'))

def test_w_nullable_subfield_none(self):
subfield = _Field('NULLABLE', 'age', 'INTEGER')
field = _Field('REQUIRED', fields=[subfield])
value = {'f': [{'v': None}]}
coerced = self._callFUT(value, field)
coerced = self._call_fut(value, field)
self.assertEqual(coerced, {'age': None})

def test_w_scalar_subfield(self):
subfield = _Field('REQUIRED', 'age', 'INTEGER')
field = _Field('REQUIRED', fields=[subfield])
value = {'f': [{'v': 42}]}
coerced = self._callFUT(value, field)
coerced = self._call_fut(value, field)
self.assertEqual(coerced, {'age': 42})

def test_w_repeated_subfield(self):
subfield = _Field('REPEATED', 'color', 'STRING')
field = _Field('REQUIRED', fields=[subfield])
value = {'f': [{'v': ['red', 'yellow', 'blue']}]}
coerced = self._callFUT(value, field)
coerced = self._call_fut(value, field)
self.assertEqual(coerced, {'color': ['red', 'yellow', 'blue']})

def test_w_record_subfield(self):
Expand All @@ -213,30 +213,30 @@ def test_w_record_subfield(self):
'rank': 1,
}
}
coerced = self._callFUT(value, person)
coerced = self._call_fut(value, person)
self.assertEqual(coerced, expected)


class Test_string_from_json(unittest.TestCase):

def _callFUT(self, value, field):
def _call_fut(self, value, field):
from google.cloud.bigquery._helpers import _string_from_json
return _string_from_json(value, field)

def test_w_none_nullable(self):
self.assertIsNone(self._callFUT(None, _Field('NULLABLE')))
self.assertIsNone(self._call_fut(None, _Field('NULLABLE')))

def test_w_none_required(self):
self.assertIsNone(self._callFUT(None, _Field('RECORD')))
self.assertIsNone(self._call_fut(None, _Field('RECORD')))

def test_w_string_value(self):
coerced = self._callFUT('Wonderful!', object())
coerced = self._call_fut('Wonderful!', object())
self.assertEqual(coerced, 'Wonderful!')


class Test_rows_from_json(unittest.TestCase):

def _callFUT(self, value, field):
def _call_fut(self, value, field):
from google.cloud.bigquery._helpers import _rows_from_json
return _rows_from_json(value, field)

Expand Down Expand Up @@ -281,7 +281,7 @@ def test_w_record_subfield(self):
('Bharney Rhubble', bharney_phone, ['brown']),
('Wylma Phlyntstone', None, []),
]
coerced = self._callFUT(rows, schema)
coerced = self._call_fut(rows, schema)
self.assertEqual(coerced, expected)

def test_w_int64_float64(self):
Expand Down Expand Up @@ -312,26 +312,27 @@ def test_w_int64_float64(self):
('Bharney Rhubble', 4, 0.125),
('Wylma Phlyntstone', 20, 0.625),
]
coerced = self._callFUT(rows, schema)
coerced = self._call_fut(rows, schema)
self.assertEqual(coerced, expected)


class Test_ConfigurationProperty(unittest.TestCase):

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.bigquery._helpers import _ConfigurationProperty
return _ConfigurationProperty

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_it(self):

class Configuration(object):
_attr = None

class Wrapper(object):
attr = self._makeOne('attr')
attr = self._make_one('attr')

def __init__(self):
self._configuration = Configuration()
Expand All @@ -353,20 +354,21 @@ def __init__(self):

class Test_TypedProperty(unittest.TestCase):

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.bigquery._helpers import _TypedProperty
return _TypedProperty

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_it(self):

class Configuration(object):
_attr = None

class Wrapper(object):
attr = self._makeOne('attr', int)
attr = self._make_one('attr', int)

def __init__(self):
self._configuration = Configuration()
Expand All @@ -386,13 +388,14 @@ def __init__(self):

class Test_EnumProperty(unittest.TestCase):

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.bigquery._helpers import _EnumProperty
return _EnumProperty

def test_it(self):

class Sub(self._getTargetClass()):
class Sub(self._get_target_class()):
ALLOWED = ('FOO', 'BAR', 'BAZ')

class Configuration(object):
Expand All @@ -419,15 +422,16 @@ def __init__(self):

class Test_UDFResourcesProperty(unittest.TestCase):

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.bigquery._helpers import UDFResourcesProperty
return UDFResourcesProperty

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def _descriptor_and_klass(self):
descriptor = self._makeOne()
descriptor = self._make_one()

class _Test(object):
_udf_resources = ()
Expand Down
11 changes: 6 additions & 5 deletions bigquery/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

class TestConnection(unittest.TestCase):

def _getTargetClass(self):
@staticmethod
def _get_target_class():
from google.cloud.bigquery._http import Connection
return Connection

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_build_api_url_no_extra_query_params(self):
conn = self._makeOne()
conn = self._make_one()
URI = '/'.join([
conn.API_BASE_URL,
'bigquery',
Expand All @@ -37,7 +38,7 @@ def test_build_api_url_no_extra_query_params(self):
def test_build_api_url_w_extra_query_params(self):
from six.moves.urllib.parse import parse_qsl
from six.moves.urllib.parse import urlsplit
conn = self._makeOne()
conn = self._make_one()
uri = conn.build_api_url('/foo', {'bar': 'baz'})
scheme, netloc, path, qs, _ = urlsplit(uri)
self.assertEqual('%s://%s' % (scheme, netloc), conn.API_BASE_URL)
Expand Down
Loading