Skip to content

Commit

Permalink
gcloud.datastore.helpers: 81% -> 100% coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Sep 16, 2014
1 parent f346593 commit e5a4124
Showing 1 changed file with 134 additions and 47 deletions.
181 changes: 134 additions & 47 deletions gcloud/datastore/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,136 @@
import calendar
from datetime import datetime
import time

import unittest2

from gcloud.datastore import helpers
from gcloud.datastore.key import Key


class TestHelpers(unittest2.TestCase):

def test_get_protobuf_attribute(self):
mapping = (
(str(), 'string_value'),
(unicode(), 'string_value'),
(int(), 'integer_value'),
(long(), 'integer_value'),
(float(), 'double_value'),
(bool(), 'boolean_value'),
(datetime.now(), 'timestamp_microseconds_value'),
(Key(), 'key_value'),
)

for test_value, expected_name in mapping:
actual_name, _ = helpers.get_protobuf_attribute_and_value(test_value)
self.assertEqual(expected_name, actual_name,
'Test value "%s" expected %s, got %s' % (
test_value, expected_name, actual_name))

def test_get_protobuf_value(self):
now = datetime.utcnow()

mapping = (
(str('string'), 'string'),
(unicode('string'), 'string'),
(int(), int()),
(long(), int()),
(float(), float()),
(bool(), bool()),
(now, long(calendar.timegm(now.timetuple()) * 1e6 + now.microsecond)),
(Key(), Key().to_protobuf()),
)

for test_value, expected_value in mapping:
_, actual_value = helpers.get_protobuf_attribute_and_value(test_value)
self.assertEqual(expected_value, actual_value,
'Test value "%s" expected %s, got %s.' % (
test_value, expected_value, actual_value))

class Test_get_protobuf_attribute_and_value(unittest2.TestCase):

def _callFUT(self, val):

This comment has been minimized.

Copy link
@jgeewax

jgeewax Oct 3, 2014

Contributor

@tseaver : What is "FUT"? Mind if I open a ticket to rename this method... (make it follow PEP8 style and all that) ?

This comment has been minimized.

Copy link
@tseaver

tseaver Oct 3, 2014

Author Contributor

FUT is "function-under-test". Frankly, I think it would be a waste of time, but I wouldn't veto it.

This comment has been minimized.

Copy link
@dhermes

dhermes Oct 3, 2014

Contributor

I agree it is a waste of time. The tests are fairly consistent in using MUT (module) and FUT. It was also new to me but @tseaver pointed out some references.

I suppose we could add a section to the README explaining what it means / stands for?

This comment has been minimized.

Copy link
@tseaver

tseaver Oct 3, 2014

Author Contributor

Maybe add a HACKING.rst file which documents the development conventions:

  • Coding style (deltas to PEP8, etc., plus how to check locally via tools)
  • Unit testing patterns / strategy (tox, nose, unittest2, etc.). We really didn't document them for #105
  • Functional testing patterns strategy
  • Any notes about preferences for third-party PRs (single focus, based against current master, etc.)

This comment has been minimized.

Copy link
@silvolu

silvolu Oct 3, 2014

Contributor

What about expanding the content of CONTRIB.rst (and maybe rename it to CONTRIBUTING.rst), as we do in gcloud-node? I'll do it if it sounds good.

This comment has been minimized.

Copy link
@jgeewax

jgeewax via email Oct 3, 2014

Contributor

This comment has been minimized.

Copy link
@dhermes

dhermes Oct 3, 2014

Contributor

@tseaver Are you going to mention FUT/MUT in #219?

from gcloud.datastore.helpers import get_protobuf_attribute_and_value
return get_protobuf_attribute_and_value(val)

def test_datetime_naive(self):
import calendar
import datetime
import pytz
naive = datetime.datetime(2014, 9, 16, 10, 19, 32, 4375) # no zone
utc = datetime.datetime(2014, 9, 16, 10, 19, 32, 4375, pytz.utc)
name, value = self._callFUT(naive)
self.assertEqual(name, 'timestamp_microseconds_value')
self.assertEqual(value / 1000000, calendar.timegm(utc.timetuple()))
self.assertEqual(value % 1000000, 4375)

def test_datetime_w_zone(self):
import calendar
import datetime
import pytz
utc = datetime.datetime(2014, 9, 16, 10, 19, 32, 4375, pytz.utc)
name, value = self._callFUT(utc)
self.assertEqual(name, 'timestamp_microseconds_value')
self.assertEqual(value / 1000000, calendar.timegm(utc.timetuple()))
self.assertEqual(value % 1000000, 4375)

def test_key(self):
from gcloud.datastore.dataset import Dataset
from gcloud.datastore.key import Key
_DATASET = 'DATASET'
_KIND = 'KIND'
_ID = 1234
_PATH = [{'kind': _KIND, 'id': _ID}]
key = Key(dataset=Dataset(_DATASET), path=_PATH)
name, value = self._callFUT(key)
self.assertEqual(name, 'key_value')
self.assertEqual(value, key.to_protobuf())

def test_bool(self):
name, value = self._callFUT(False)
self.assertEqual(name, 'boolean_value')
self.assertEqual(value, False)

def test_float(self):
name, value = self._callFUT(3.1415926)
self.assertEqual(name, 'double_value')
self.assertEqual(value, 3.1415926)

def test_int(self):
name, value = self._callFUT(42)
self.assertEqual(name, 'integer_value')
self.assertEqual(value, 42)

def test_long(self):
must_be_long = 1 << 63
name, value = self._callFUT(must_be_long)
self.assertEqual(name, 'integer_value')
self.assertEqual(value, must_be_long)

def test_native_str(self):
name, value = self._callFUT('str')
self.assertEqual(name, 'string_value')
self.assertEqual(value, 'str')

def test_unicode(self):
name, value = self._callFUT(u'str')
self.assertEqual(name, 'string_value')
self.assertEqual(value, u'str')


class Test_get_value_from_protobuf(unittest2.TestCase):

def _callFUT(self, pb):
from gcloud.datastore.helpers import get_value_from_protobuf
return get_value_from_protobuf(pb)

def _makePB(self, attr_name, value):
from gcloud.datastore.datastore_v1_pb2 import Property
prop = Property()
setattr(prop.value, attr_name, value)
return prop

def test_datetime(self):
import calendar
import datetime
import pytz
naive = datetime.datetime(2014, 9, 16, 10, 19, 32, 4375) # no zone
utc = datetime.datetime(2014, 9, 16, 10, 19, 32, 4375, pytz.utc)
micros = (calendar.timegm(utc.timetuple()) * 1000000) + 4375
pb = self._makePB('timestamp_microseconds_value', micros)
# self.assertEqual(self._callFUT(pb), utc) XXX
# see https://github.com/GoogleCloudPlatform/gcloud-python/issues/131
self.assertEqual(self._callFUT(pb), naive)

def test_key(self):
from gcloud.datastore.datastore_v1_pb2 import Property
from gcloud.datastore.dataset import Dataset
from gcloud.datastore.key import Key
_DATASET = 'DATASET'
_KIND = 'KIND'
_ID = 1234
_PATH = [{'kind': _KIND, 'id': _ID}]
pb = Property()
expected = Key(dataset=Dataset(_DATASET), path=_PATH).to_protobuf()
pb.value.key_value.CopyFrom(expected)
found = self._callFUT(pb)
self.assertEqual(found.to_protobuf(), expected)

def test_bool(self):
pb = self._makePB('boolean_value', False)
self.assertEqual(self._callFUT(pb), False)

def test_float(self):
pb = self._makePB('double_value', 3.1415926)
self.assertEqual(self._callFUT(pb), 3.1415926)

def test_int(self):
pb = self._makePB('integer_value', 42)
self.assertEqual(self._callFUT(pb), 42)

def test_native_str(self):
pb = self._makePB('string_value', 'str')
self.assertEqual(self._callFUT(pb), 'str')

def test_unicode(self):
pb = self._makePB('string_value', u'str')
self.assertEqual(self._callFUT(pb), u'str')

def test_unknown(self):
from gcloud.datastore.datastore_v1_pb2 import Property
pb = Property()
self.assertEqual(self._callFUT(pb), None) # XXX desirable?

0 comments on commit e5a4124

Please sign in to comment.