Python google.appengine.ext.ndb.IntegerProperty() Examples

The following are 30 code examples of google.appengine.ext.ndb.IntegerProperty(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.appengine.ext.ndb , or try the search function .
Example #1
Source File: monotonic_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_store_new_version_extra(self):
    # Includes an unrelated entity in the PUT. It must be in the same entity
    # group.
    cls = monotonic.get_versioned_root_model('fidoula')
    parent = ndb.Key(cls, 'foo')
    class Unrelated(ndb.Model):
      b = ndb.IntegerProperty()
    unrelated = Unrelated(id='bar', parent=parent, b=42)
    actual = monotonic.store_new_version(
        EntityX(a=1, parent=parent), cls, extra=[unrelated])
    self.assertEqual(
        ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID), actual)
    actual = monotonic.store_new_version(EntityX(a=2, parent=parent), cls)
    self.assertEqual(
        ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID - 1), actual)
    self.assertEqual({'b': 42}, unrelated.key.get().to_dict()) 
Example #2
Source File: serializable_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_bad_type_for_repeated_property(self):
    """Trying to deserialize repeated property not from a list -> ValueError."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop = ndb.IntegerProperty(repeated=True)

    # A list, tuple or nothing should work.
    Entity.from_serializable_dict({'prop': [1]})
    Entity.from_serializable_dict({'prop': (1,)})
    Entity.from_serializable_dict({})

    # A single item shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': 1})
    # 'None' shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': None})
    # Dict shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': {}}) 
Example #3
Source File: snippets.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def query_purchase_with_ancestor_key():
    # [START purchase_with_ancestor_key_models]
    class Customer(ndb.Model):
        name = ndb.StringProperty()

    class Purchase(ndb.Model):
        price = ndb.IntegerProperty()
    # [END purchase_with_ancestor_key_models]

    def create_purchase_for_customer_with_ancestor(customer_entity):
        purchase = Purchase(parent=customer_entity.key)
        return purchase

    def query_for_purchases_of_customer_with_ancestor(customer_entity):
        purchases = Purchase.query(ancestor=customer_entity.key).fetch()
        return purchases

    return (Customer, Purchase,
            create_purchase_for_customer_with_ancestor,
            query_for_purchases_of_customer_with_ancestor) 
Example #4
Source File: snippets.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def query_purchase_with_customer_key():
    # [START purchase_with_customer_key_models]
    class Customer(ndb.Model):
        name = ndb.StringProperty()

    class Purchase(ndb.Model):
        customer = ndb.KeyProperty(kind=Customer)
        price = ndb.IntegerProperty()
    # [END purchase_with_customer_key_models]

    def query_purchases_for_customer_via_key(customer_entity):
        purchases = Purchase.query(
            Purchase.customer == customer_entity.key).fetch()
        return purchases

    return Customer, Purchase, query_purchases_for_customer_via_key 
Example #5
Source File: serializable_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _test_repeated_structured_properties_class(self, structured_cls):
    """Common testing for StructuredProperty and LocalStructuredProperty."""
    class Inner(ndb.Model):
      a = ndb.IntegerProperty()

    class Outter(ndb.Model, serializable.SerializableModelMixin):
      inner = structured_cls(Inner, repeated=True)

    # Repeated structured property -> list of dicts.
    entity = Outter()
    entity.inner.extend([Inner(a=1), Inner(a=2)])
    self.assertEqual(
        {'inner': [{'a': 1}, {'a': 2}]},
        entity.to_serializable_dict())

    # Reverse also works.
    self.assertEqual(
        entity,
        Outter.from_serializable_dict({'inner': [{'a': 1}, {'a': 2}]})) 
Example #6
Source File: serializable_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _test_repeated_structured_properties_class(self, structured_cls):
    """Common testing for StructuredProperty and LocalStructuredProperty."""
    class Inner(ndb.Model):
      a = ndb.IntegerProperty()

    class Outter(ndb.Model, serializable.SerializableModelMixin):
      inner = structured_cls(Inner, repeated=True)

    # Repeated structured property -> list of dicts.
    entity = Outter()
    entity.inner.extend([Inner(a=1), Inner(a=2)])
    self.assertEqual(
        {'inner': [{'a': 1}, {'a': 2}]},
        entity.to_serializable_dict())

    # Reverse also works.
    self.assertEqual(
        entity,
        Outter.from_serializable_dict({'inner': [{'a': 1}, {'a': 2}]})) 
Example #7
Source File: monotonic_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_store_new_version_extra(self):
    # Includes an unrelated entity in the PUT. It must be in the same entity
    # group.
    cls = monotonic.get_versioned_root_model('fidoula')
    parent = ndb.Key(cls, 'foo')
    class Unrelated(ndb.Model):
      b = ndb.IntegerProperty()
    unrelated = Unrelated(id='bar', parent=parent, b=42)
    actual = monotonic.store_new_version(
        EntityX(a=1, parent=parent), cls, extra=[unrelated])
    self.assertEqual(
        ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID), actual)
    actual = monotonic.store_new_version(EntityX(a=2, parent=parent), cls)
    self.assertEqual(
        ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID - 1), actual)
    self.assertEqual({'b': 42}, unrelated.key.get().to_dict()) 
Example #8
Source File: monotonic_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_store_new_version_extra(self):
    # Includes an unrelated entity in the PUT. It must be in the same entity
    # group.
    cls = monotonic.get_versioned_root_model('fidoula')
    parent = ndb.Key(cls, 'foo')
    class Unrelated(ndb.Model):
      b = ndb.IntegerProperty()
    unrelated = Unrelated(id='bar', parent=parent, b=42)
    actual = monotonic.store_new_version(
        EntityX(a=1, parent=parent), cls, extra=[unrelated])
    self.assertEqual(
        ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID), actual)
    actual = monotonic.store_new_version(EntityX(a=2, parent=parent), cls)
    self.assertEqual(
        ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID - 1), actual)
    self.assertEqual({'b': 42}, unrelated.key.get().to_dict()) 
Example #9
Source File: serializable_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_bad_type_for_repeated_property(self):
    """Trying to deserialize repeated property not from a list -> ValueError."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop = ndb.IntegerProperty(repeated=True)

    # A list, tuple or nothing should work.
    Entity.from_serializable_dict({'prop': [1]})
    Entity.from_serializable_dict({'prop': (1,)})
    Entity.from_serializable_dict({})

    # A single item shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': 1})
    # 'None' shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': None})
    # Dict shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': {}}) 
Example #10
Source File: serializable_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_bad_type_for_repeated_property(self):
    """Trying to deserialize repeated property not from a list -> ValueError."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop = ndb.IntegerProperty(repeated=True)

    # A list, tuple or nothing should work.
    Entity.from_serializable_dict({'prop': [1]})
    Entity.from_serializable_dict({'prop': (1,)})
    Entity.from_serializable_dict({})

    # A single item shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': 1})
    # 'None' shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': None})
    # Dict shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': {}}) 
Example #11
Source File: serializable_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_bad_type_for_repeated_property(self):
    """Trying to deserialize repeated property not from a list -> ValueError."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop = ndb.IntegerProperty(repeated=True)

    # A list, tuple or nothing should work.
    Entity.from_serializable_dict({'prop': [1]})
    Entity.from_serializable_dict({'prop': (1,)})
    Entity.from_serializable_dict({})

    # A single item shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': 1})
    # 'None' shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': None})
    # Dict shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': {}}) 
Example #12
Source File: serializable_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _test_repeated_structured_properties_class(self, structured_cls):
    """Common testing for StructuredProperty and LocalStructuredProperty."""
    class Inner(ndb.Model):
      a = ndb.IntegerProperty()

    class Outter(ndb.Model, serializable.SerializableModelMixin):
      inner = structured_cls(Inner, repeated=True)

    # Repeated structured property -> list of dicts.
    entity = Outter()
    entity.inner.extend([Inner(a=1), Inner(a=2)])
    self.assertEqual(
        {'inner': [{'a': 1}, {'a': 2}]},
        entity.to_serializable_dict())

    # Reverse also works.
    self.assertEqual(
        entity,
        Outter.from_serializable_dict({'inner': [{'a': 1}, {'a': 2}]})) 
Example #13
Source File: serializable_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_bad_type_for_repeated_property(self):
    """Trying to deserialize repeated property not from a list -> ValueError."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop = ndb.IntegerProperty(repeated=True)

    # A list, tuple or nothing should work.
    Entity.from_serializable_dict({'prop': [1]})
    Entity.from_serializable_dict({'prop': (1,)})
    Entity.from_serializable_dict({})

    # A single item shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': 1})
    # 'None' shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': None})
    # Dict shouldn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': {}}) 
Example #14
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_bad_type_for_simple_property(self):
    """Trying to deserialize non-number into IntegerProperty -> ValueError."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop = ndb.IntegerProperty()

    # Works.
    Entity.from_serializable_dict({'prop': 123})
    # Doesn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': 'abc'}) 
Example #15
Source File: monotonic.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_versioned_root_model(model_name):
  """Returns a root model that can be used for versioned entities.

  Using this entity for get_versioned_most_recent(),
  get_versioned_most_recent_with_root() and store_new_version() is optional. Any
  entity with cls.current as an ndb.IntegerProperty will do.
  """
  assert isinstance(model_name, str), model_name
  class _Root(Root):
    @classmethod
    def _get_kind(cls):
      return model_name

  return _Root 
Example #16
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_from_serializable_dict_kwargs_work(self):
    """Keyword arguments in from_serializable_dict are passed to constructor."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop = ndb.IntegerProperty()

    # Pass entity key via keyword parameters.
    entity = Entity.from_serializable_dict(
        {'prop': 123}, id='my id', parent=ndb.Key('Fake', 'parent'))
    self.assertEqual(123, entity.prop)
    self.assertEqual(ndb.Key('Fake', 'parent', 'Entity', 'my id'), entity.key) 
Example #17
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_repeated_properties(self):
    """Test that properties with repeated=True are handled."""
    class IntsEntity(ndb.Model, serializable.SerializableModelMixin):
      ints = ndb.IntegerProperty(repeated=True)
    class DatesEntity(ndb.Model, serializable.SerializableModelMixin):
      dates = ndb.DateTimeProperty(repeated=True)

    # Same point in time as datetime and as timestamp.
    dt = datetime.datetime(2012, 1, 2, 3, 4, 5)
    ts = 1325473445000000

    # Repeated properties that are not set are converted to empty lists.
    self.assertEqual({'ints': []}, IntsEntity().to_serializable_dict())
    self.assertEqual({'dates': []}, DatesEntity().to_serializable_dict())

    # List of ints works (as an example of simple repeated property).
    self.assertEqual(
        {'ints': [1, 2]},
        IntsEntity(ints=[1, 2]).to_serializable_dict())
    self.assertEqual(
        {'ints': [1, 2]},
        IntsEntity.convert_serializable_dict({'ints': [1, 2]}))

    # List of datetimes works (as an example of not-so-simple property).
    self.assertEqual(
        {'dates': [ts, ts]},
        DatesEntity(dates=[dt, dt]).to_serializable_dict())
    self.assertEqual(
        {'dates': [dt, dt]},
        DatesEntity.convert_serializable_dict({'dates': [ts, ts]})) 
Example #18
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_repeated_properties(self):
    """Test that properties with repeated=True are handled."""
    class IntsEntity(ndb.Model, serializable.SerializableModelMixin):
      ints = ndb.IntegerProperty(repeated=True)
    class DatesEntity(ndb.Model, serializable.SerializableModelMixin):
      dates = ndb.DateTimeProperty(repeated=True)

    # Same point in time as datetime and as timestamp.
    dt = datetime.datetime(2012, 1, 2, 3, 4, 5)
    ts = 1325473445000000

    # Repeated properties that are not set are converted to empty lists.
    self.assertEqual({'ints': []}, IntsEntity().to_serializable_dict())
    self.assertEqual({'dates': []}, DatesEntity().to_serializable_dict())

    # List of ints works (as an example of simple repeated property).
    self.assertEqual(
        {'ints': [1, 2]},
        IntsEntity(ints=[1, 2]).to_serializable_dict())
    self.assertEqual(
        {'ints': [1, 2]},
        IntsEntity.convert_serializable_dict({'ints': [1, 2]}))

    # List of datetimes works (as an example of not-so-simple property).
    self.assertEqual(
        {'dates': [ts, ts]},
        DatesEntity(dates=[dt, dt]).to_serializable_dict())
    self.assertEqual(
        {'dates': [dt, dt]},
        DatesEntity.convert_serializable_dict({'dates': [ts, ts]})) 
Example #19
Source File: monotonic.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def get_versioned_root_model(model_name):
  """Returns a root model that can be used for versioned entities.

  Using this entity for get_versioned_most_recent(),
  get_versioned_most_recent_with_root() and store_new_version() is optional. Any
  entity with cls.current as an ndb.IntegerProperty will do.
  """
  assert isinstance(model_name, str), model_name
  class _Root(Root):
    @classmethod
    def _get_kind(cls):
      return model_name

  return _Root 
Example #20
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_exclude_works(self):
    """|exclude| argument of to_serializable_dict() is respected."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop1 = ndb.IntegerProperty()
      prop2 = ndb.IntegerProperty()
      prop3 = ndb.IntegerProperty()

    entity = Entity(prop1=1, prop2=2, prop3=3)
    self.assertEqual(
        {'prop1': 1, 'prop3': 3},
        entity.to_serializable_dict(exclude=['prop2'])) 
Example #21
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_bad_type_for_simple_property(self):
    """Trying to deserialize non-number into IntegerProperty -> ValueError."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop = ndb.IntegerProperty()

    # Works.
    Entity.from_serializable_dict({'prop': 123})
    # Doesn't.
    with self.assertRaises(ValueError):
      Entity.from_serializable_dict({'prop': 'abc'}) 
Example #22
Source File: manager.py    From tekton with MIT License 5 votes vote down vote up
def parse_property(p):
    name, type_alias = p.split(':')
    types = {'string': 'ndb.StringProperty(required=True)',
             'date': 'ndb.DateProperty(required=True)',
             'datetime': 'ndb.DateTimeProperty(required=True)',
             'int': 'ndb.IntegerProperty(required=True)',
             'float': 'ndb.FloatProperty(required=True)',
             'decimal': 'property.SimpleDecimal(required=True)',
             'currency': 'property.SimpleCurrency(required=True)',
             'bool': 'ndb.BooleanProperty(required=True)'}
    return '    %s = %s' % (name, types[type_alias]) 
Example #23
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_from_serializable_dict_kwargs_work(self):
    """Keyword arguments in from_serializable_dict are passed to constructor."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop = ndb.IntegerProperty()

    # Pass entity key via keyword parameters.
    entity = Entity.from_serializable_dict(
        {'prop': 123}, id='my id', parent=ndb.Key('Fake', 'parent'))
    self.assertEqual(123, entity.prop)
    self.assertEqual(ndb.Key('Fake', 'parent', 'Entity', 'my id'), entity.key) 
Example #24
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_exclude_works(self):
    """|exclude| argument of to_serializable_dict() is respected."""
    class Entity(ndb.Model, serializable.SerializableModelMixin):
      prop1 = ndb.IntegerProperty()
      prop2 = ndb.IntegerProperty()
      prop3 = ndb.IntegerProperty()

    entity = Entity(prop1=1, prop2=2, prop3=3)
    self.assertEqual(
        {'prop1': 1, 'prop3': 3},
        entity.to_serializable_dict(exclude=['prop2'])) 
Example #25
Source File: manager.py    From tekton with MIT License 5 votes vote down vote up
def _to_default_model_value(descriptor, name, index):
    if isinstance(descriptor, (StringProperty, TextProperty)):
        return "'%s_string'" % name
    if isinstance(descriptor, DateProperty):
        return "date(2014, 1, %s)" % (index + 1)
    if isinstance(descriptor, DateTimeProperty):
        return "datetime(2014, 1, 1, 1, %s, 0)" % (index + 1)
    if isinstance(descriptor, (SimpleCurrency, SimpleDecimal)):
        return "Decimal('1.%s')" % (index + 1 if index >= 9 else '0%s' % (index + 1))
    if isinstance(descriptor, IntegerProperty):
        return "%s" % (index + 1)
    if isinstance(descriptor, FloatProperty):
        return "1.%s" % (index + 1)
    if isinstance(descriptor, BooleanProperty):
        return "True" 
Example #26
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _test_structured_properties_class(self, structured_cls):
    """Common testing for StructuredProperty and LocalStructuredProperty."""
    # Plain ndb.Model.
    class InnerSimple(ndb.Model):
      a = ndb.IntegerProperty()

    # With SerializableModelMixin.
    class InnerSmart(ndb.Model, serializable.SerializableModelMixin):
      serializable_properties = {
        'a': serializable.READABLE | serializable.WRITABLE,
      }
      a = ndb.IntegerProperty()
      b = ndb.IntegerProperty()

    class Outter(ndb.Model, serializable.SerializableModelMixin):
      simple = structured_cls(InnerSimple)
      smart = structured_cls(InnerSmart)

    # InnerSimple gets serialized entirely, while only readable fields
    # on InnerSmart are serialized.
    entity = Outter()
    entity.simple = InnerSimple(a=1)
    entity.smart = InnerSmart(a=2, b=3)
    self.assertEqual(
        {'simple': {'a': 1}, 'smart': {'a': 2}},
        entity.to_serializable_dict())

    # Works backwards as well. Note that 'convert_serializable_dict' returns
    # a dictionary that can be fed to entity's 'populate' or constructor. Entity
    # by itself is smart enough to transform subdicts into structured
    # properties.
    self.assertEqual(
        Outter(simple=InnerSimple(a=1), smart=InnerSmart(a=2)),
        Outter.from_serializable_dict({'simple': {'a': 1}, 'smart': {'a': 2}})) 
Example #27
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_repeated_properties(self):
    """Test that properties with repeated=True are handled."""
    class IntsEntity(ndb.Model, serializable.SerializableModelMixin):
      ints = ndb.IntegerProperty(repeated=True)
    class DatesEntity(ndb.Model, serializable.SerializableModelMixin):
      dates = ndb.DateTimeProperty(repeated=True)

    # Same point in time as datetime and as timestamp.
    dt = datetime.datetime(2012, 1, 2, 3, 4, 5)
    ts = 1325473445000000

    # Repeated properties that are not set are converted to empty lists.
    self.assertEqual({'ints': []}, IntsEntity().to_serializable_dict())
    self.assertEqual({'dates': []}, DatesEntity().to_serializable_dict())

    # List of ints works (as an example of simple repeated property).
    self.assertEqual(
        {'ints': [1, 2]},
        IntsEntity(ints=[1, 2]).to_serializable_dict())
    self.assertEqual(
        {'ints': [1, 2]},
        IntsEntity.convert_serializable_dict({'ints': [1, 2]}))

    # List of datetimes works (as an example of not-so-simple property).
    self.assertEqual(
        {'dates': [ts, ts]},
        DatesEntity(dates=[dt, dt]).to_serializable_dict())
    self.assertEqual(
        {'dates': [dt, dt]},
        DatesEntity.convert_serializable_dict({'dates': [ts, ts]})) 
Example #28
Source File: manager.py    From tekton with MIT License 5 votes vote down vote up
def _to_default_reques_value(descriptor, name, index):
    if isinstance(descriptor, (StringProperty, TextProperty)):
        return "'%s_string'" % name
    if isinstance(descriptor, DateProperty):
        return "'1/%s/2014'" % (index + 1)
    if isinstance(descriptor, DateTimeProperty):
        return "'1/1/2014 01:%s:0'" % (index + 1)
    if isinstance(descriptor, (SimpleCurrency, SimpleDecimal)):
        return "'1.%s'" % (index + 1 if index >= 9 else '0%s' % (index + 1))
    if isinstance(descriptor, IntegerProperty):
        return "'%s'" % (index + 1)
    if isinstance(descriptor, FloatProperty):
        return "'1.%s'" % (index + 1)
    if isinstance(descriptor, BooleanProperty):
        return "'True'" 
Example #29
Source File: bigquery_test.py    From loaner with Apache License 2.0 5 votes vote down vote up
def test_generate_entity_schema(self):

    class NestedTestModel(ndb.Model):
      nested_string_attribute = ndb.StringProperty()

    class TestModel(ndb.Model):
      string_attribute = ndb.StringProperty()
      integer_attribute = ndb.IntegerProperty()
      boolean_attribute = ndb.BooleanProperty()
      nested_attribute = ndb.StructuredProperty(NestedTestModel)

    schema = bigquery._generate_entity_schema(TestModel())
    expected_schema_names = _populate_schema_names(self.entity_schema)
    schema_names = _populate_schema_names(schema)
    self.assertCountEqual(expected_schema_names, schema_names) 
Example #30
Source File: handler_utils.py    From upvote with Apache License 2.0 5 votes vote down vote up
def _CoerceQueryParam(field, query_param):
  """Attempts to coerce `query_param` to match the ndb type of `field`.

  Args:
    field: The ndb field being queried.
    query_param: The query term to be coerced.

  Returns:
    The query param coerced if a coercion was possible.

  Raises:
    QueryTypeError: If there is an error with the type conversion.
  """
  if isinstance(field, ndb.IntegerProperty):
    try:
      return int(query_param)
    except ValueError:
      raise QueryTypeError(
          'Query param "%s" could not be converted to integer' % query_param)
  elif isinstance(field, ndb.BooleanProperty):
    if query_param.lower() == 'true':
      return True
    elif query_param.lower() == 'false':
      return False
    else:
      raise QueryTypeError(
          'Query param "%s" could not be converted to boolean' % query_param)
  elif isinstance(field, ndb.KeyProperty):
    key = datastore_utils.GetKeyFromUrlsafe(query_param)
    if not key:
      raise QueryTypeError(
          'Query param "%s" could not be converted to ndb.Key' % query_param)
    return key
  else:
    return query_param