Python protorpc.messages.StringField() Examples

The following are 30 code examples of protorpc.messages.StringField(). 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 protorpc.messages , or try the search function .
Example #1
Source File: protourlencode_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testParameterPrefix(self):
    """Test using the 'prefix' parameter to encode_message."""
    class MyMessage(messages.Message):
      number = messages.IntegerField(1)
      names = messages.StringField(2, repeated=True)

    message = MyMessage()
    message.number = 10
    message.names = [u'Fred', u'Lisa']

    encoded_message = protourlencode.encode_message(message, prefix='prefix-')
    self.assertEquals({'prefix-number': ['10'],
                       'prefix-names-0': ['Fred'],
                       'prefix-names-1': ['Lisa'],
                      },
                      cgi.parse_qs(encoded_message))

    self.assertEquals(message, protourlencode.decode_message(MyMessage,
                                                             encoded_message,
                                                             prefix='prefix-')) 
Example #2
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def testNestedMessageField(self):
    class Inner(messages.Message):
      count = messages.IntegerField(1)
      greet = messages.MessageField(Greeting, 2)

    class Outer(messages.Message):
      inner = messages.MessageField(Inner, 1)
      extra = messages.StringField(2)

    class Store(model.Model):
      outer = msgprop.MessageProperty(Outer,
                                      indexed_fields=['inner.greet.text'])
    greet = Greeting(text='abc', when=123)
    inner = Inner(count=42, greet=greet)
    outer = Outer(inner=inner)
    st = Store(outer=outer)
    st.put()
    res = Store.query(Store.outer.inner.greet.text == 'abc').fetch()
    self.assertEqual(res, [st]) 
Example #3
Source File: descriptor_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testDefinitionWithFields(self):
    class MessageWithFields(messages.Message):
      field1 = messages.IntegerField(10)
      field2 = messages.StringField(30)
      field3 = messages.IntegerField(20)

    expected = descriptor.MessageDescriptor()
    expected.name = 'MessageWithFields'

    expected.fields = [
      descriptor.describe_field(MessageWithFields.field_by_name('field1')),
      descriptor.describe_field(MessageWithFields.field_by_name('field3')),
      descriptor.describe_field(MessageWithFields.field_by_name('field2')),
    ]

    described = descriptor.describe_message(MessageWithFields)
    described.check_initialized()
    self.assertEquals(expected, described) 
Example #4
Source File: descriptor_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testDefault(self):
    for field_class, default, expected_default in (
        (messages.IntegerField, 200, '200'),
        (messages.FloatField, 1.5, '1.5'),
        (messages.FloatField, 1e6, '1000000.0'),
        (messages.BooleanField, True, 'true'),
        (messages.BooleanField, False, 'false'),
        (messages.BytesField, 'ab\xF1', 'ab\\xf1'),
        (messages.StringField, RUSSIA, RUSSIA),
        ):
      field = field_class(10, default=default)
      field.name = u'a_field'

      expected = descriptor.FieldDescriptor()
      expected.name = u'a_field'
      expected.number = 10
      expected.label = descriptor.FieldDescriptor.Label.OPTIONAL
      expected.variant = field_class.DEFAULT_VARIANT
      expected.default_value = expected_default

      described = descriptor.describe_field(field)
      described.check_initialized()
      self.assertEquals(expected, described) 
Example #5
Source File: messages_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testDefaultFields_Single(self):
    """Test default field is correct type (single)."""
    defaults = {messages.IntegerField: 10,
                messages.FloatField: 1.5,
                messages.BooleanField: False,
                messages.BytesField: b'abc',
                messages.StringField: u'abc',
               }

    def action(field_class):
      field_class(1, default=defaults[field_class])
    self.ActionOnAllFieldClasses(action)

    # Run defaults test again checking for str/unicode compatiblity.
    defaults[messages.StringField] = 'abc'
    self.ActionOnAllFieldClasses(action) 
Example #6
Source File: messages_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testGetAssignedValue(self):
    """Test getting the assigned value of a field."""
    class SomeMessage(messages.Message):
      a_value = messages.StringField(1, default=u'a default')

    message = SomeMessage()
    self.assertEquals(None, message.get_assigned_value('a_value'))

    message.a_value = u'a string'
    self.assertEquals(u'a string', message.get_assigned_value('a_value'))

    message.a_value = u'a default'
    self.assertEquals(u'a default', message.get_assigned_value('a_value'))

    self.assertRaisesWithRegexpMatch(
        AttributeError,
        'Message SomeMessage has no field no_such_field',
        message.get_assigned_value,
        'no_such_field') 
Example #7
Source File: definition_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testDefineField_Required(self):
    """Test defining a required field instance from a method descriptor."""
    field_descriptor = descriptor.FieldDescriptor()

    field_descriptor.name = 'a_field'
    field_descriptor.number = 1
    field_descriptor.variant = descriptor.FieldDescriptor.Variant.STRING
    field_descriptor.label = descriptor.FieldDescriptor.Label.REQUIRED

    field = definition.define_field(field_descriptor)

    # Name will not be set from the original descriptor.
    self.assertFalse(hasattr(field, 'name'))

    self.assertTrue(isinstance(field, messages.StringField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.STRING, field.variant)
    self.assertTrue(field.required)
    self.assertFalse(field.repeated) 
Example #8
Source File: definition_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testDefineField_Default_Str(self):
    """Test defining a default value for a str."""
    field_descriptor = descriptor.FieldDescriptor()

    field_descriptor.name = 'a_field'
    field_descriptor.number = 1
    field_descriptor.variant = descriptor.FieldDescriptor.Variant.STRING
    field_descriptor.default_value = u'Test'

    field = definition.define_field(field_descriptor)

    # Name will not be set from the original descriptor.
    self.assertFalse(hasattr(field, 'name'))

    self.assertTrue(isinstance(field, messages.StringField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.STRING, field.variant)
    self.assertFalse(field.required)
    self.assertFalse(field.repeated)
    self.assertEqual(field.default, u'Test') 
Example #9
Source File: definition_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testDefineMessageFieldsOnly(self):
    """Test definition a message with only fields."""

    class AMessage(messages.Message):

      field1 = messages.IntegerField(1)
      field2 = messages.StringField(2)

    message_descriptor = descriptor.describe_message(AMessage)

    message_class = definition.define_message(message_descriptor, '__main__')

    self.assertEquals('AMessage', message_class.__name__)
    self.assertEquals('__main__', message_class.__module__)

    self.assertEquals(message_descriptor,
                      descriptor.describe_message(message_class)) 
Example #10
Source File: definition_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testDefineMessage(self):
    """Test defining Message class from descriptor."""

    class AMessage(messages.Message):
      class NestedEnum(messages.Enum):
        pass

      field1 = messages.IntegerField(1)
      field2 = messages.StringField(2)

    message_descriptor = descriptor.describe_message(AMessage)

    message_class = definition.define_message(message_descriptor, '__main__')

    self.assertEquals('AMessage', message_class.__name__)
    self.assertEquals('__main__', message_class.__module__)

    self.assertEquals(message_descriptor,
                      descriptor.describe_message(message_class)) 
Example #11
Source File: definition_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def setUp(self):
    """Set up mock and request classes."""
    self.module = types.ModuleType('stocks')

    class GetQuoteRequest(messages.Message):
      __module__ = 'stocks'

      symbols = messages.StringField(1, repeated=True)

    class GetQuoteResponse(messages.Message):
      __module__ = 'stocks'

      prices = messages.IntegerField(1, repeated=True)

    self.module.GetQuoteRequest = GetQuoteRequest
    self.module.GetQuoteResponse = GetQuoteResponse 
Example #12
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def CreateMessageClass(self):
    """Creates a simple message class with 3 fields.

    Fields are defined in alphabetical order but with conflicting numeric
    order.
    """
    class ComplexMessage(messages.Message):
      a3 = messages.IntegerField(3)
      b1 = messages.StringField(1)
      c2 = messages.StringField(2)

    return ComplexMessage 
Example #13
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testValidateElement(self):
    """Test validation of valid values."""
    values = {messages.IntegerField: (10, -1, 0),
              messages.FloatField: (1.5, -1.5, 3),  # for json it is all a number
              messages.BooleanField: (True, False),
              messages.BytesField: (b'abc',),
              messages.StringField: (u'abc',),
             }
    def action(field_class):
      # Optional.
      field = field_class(1)
      for value in values[field_class]:
        field.validate_element(value)

      # Required.
      field = field_class(1, required=True)
      for value in values[field_class]:
        field.validate_element(value)

      # Repeated.
      field = field_class(1, repeated=True)
      self.assertRaises(messages.ValidationError,
                        field.validate_element,
                        [])
      self.assertRaises(messages.ValidationError,
                        field.validate_element,
                        ())
      for value in values[field_class]:
        field.validate_element(value)

      # Right value, but repeated.
      self.assertRaises(messages.ValidationError,
                        field.validate_element,
                        list(values[field_class]))  # testing list
      self.assertRaises(messages.ValidationError,
                        field.validate_element,
                        values[field_class])  # testing tuple
    self.ActionOnAllFieldClasses(action) 
Example #14
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testMessageDefinition(self):
    """Test that message definition is set on fields."""
    class MyMessage(messages.Message):

      my_field = messages.StringField(1)

    self.assertEquals(MyMessage,
                      MyMessage.field_by_name('my_field').message_definition()) 
Example #15
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testNoneAssignment(self):
    """Test that assigning None does not change comparison."""
    class MyMessage(messages.Message):

      my_field = messages.StringField(1)

    m1 = MyMessage()
    m2 = MyMessage()
    m2.my_field = None
    self.assertEquals(m1, m2) 
Example #16
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testNonAsciiStr(self):
    """Test validation fails for non-ascii StringField values."""
    class Thing(messages.Message):
      string_field = messages.StringField(2)

    thing = Thing()
    self.assertRaisesWithRegexpMatch(
      messages.ValidationError,
      'Field string_field encountered non-ASCII string',
      setattr, thing, 'string_field', test_util.BINARY) 
Example #17
Source File: protobuf_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testDecodeWrongWireFormat(self):
    """Test what happens when wrong wire format found in protobuf."""
    class ExpectedProto(messages.Message):
      value = messages.StringField(1)

    class WrongVariant(messages.Message):
      value = messages.IntegerField(1)

    original = WrongVariant()
    original.value = 10
    self.assertErrorIs(messages.DecodeError,
                       'Expected wire type STRING but found NUMERIC',
                       protobuf.decode_message,
                       ExpectedProto,
                       protobuf.encode_message(original)) 
Example #18
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testReset(self):
    """Test resetting a field value."""
    class SomeMessage(messages.Message):
      a_value = messages.StringField(1, default=u'a default')
      repeated = messages.IntegerField(2, repeated=True)

    message = SomeMessage()

    self.assertRaises(AttributeError, message.reset, 'unknown')

    self.assertEquals(u'a default', message.a_value)
    message.reset('a_value')
    self.assertEquals(u'a default', message.a_value)

    message.a_value = u'a new value'
    self.assertEquals(u'a new value', message.a_value)
    message.reset('a_value')
    self.assertEquals(u'a default', message.a_value)

    message.repeated = [1, 2, 3]
    self.assertEquals([1, 2, 3], message.repeated)
    saved = message.repeated
    message.reset('repeated')
    self.assertEquals([], message.repeated)
    self.assertIsInstance(message.repeated, messages.FieldList)
    self.assertEquals([1, 2, 3], saved) 
Example #19
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testRepr(self):
    """Test represtation of Message object."""
    class MyMessage(messages.Message):
      integer_value = messages.IntegerField(1)
      string_value = messages.StringField(2)
      unassigned = messages.StringField(3)
      unassigned_with_default = messages.StringField(4, default=u'a default')

    my_message = MyMessage()
    my_message.integer_value = 42
    my_message.string_value = u'A string'

    pat = re.compile(r"<MyMessage\n integer_value: 42\n"
                      " string_value: [u]?'A string'>")
    self.assertTrue(pat.match(repr(my_message)) is not None) 
Example #20
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testConstructorKwargs(self):
    """Test kwargs via constructor."""
    class SomeMessage(messages.Message):
      name = messages.StringField(1)
      number = messages.IntegerField(2)

    expected = SomeMessage()
    expected.name = 'my name'
    expected.number = 200
    self.assertEquals(expected, SomeMessage(name='my name', number=200)) 
Example #21
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    super(MsgPropTests, self).setUp()
    global Greeting

    class Greeting(messages.Message):
      text = messages.StringField(1, required=True)
      when = messages.IntegerField(2)
      color = messages.EnumField(Color, 3) 
Example #22
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testValidate_Valid(self):
    """Test validation of valid values."""
    values = {messages.IntegerField: 10,
              messages.FloatField: 1.5,
              messages.BooleanField: False,
              messages.BytesField: b'abc',
              messages.StringField: u'abc',
             }
    def action(field_class):
      # Optional.
      field = field_class(1)
      field.validate(values[field_class])

      # Required.
      field = field_class(1, required=True)
      field.validate(values[field_class])

      # Repeated.
      field = field_class(1, repeated=True)
      field.validate([])
      field.validate(())
      field.validate([values[field_class]])
      field.validate((values[field_class],))

      # Right value, but not repeated.
      self.assertRaises(messages.ValidationError,
                        field.validate,
                        values[field_class])
      self.assertRaises(messages.ValidationError,
                        field.validate,
                        values[field_class])

    self.ActionOnAllFieldClasses(action) 
Example #23
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testDefaultFields_InvalidRepeated(self):
    """Test default field does not accept defaults."""
    self.assertRaisesWithRegexpMatch(
      messages.FieldDefinitionError,
      'Repeated fields may not have defaults',
      messages.StringField, 1, repeated=True, default=[1, 2, 3]) 
Example #24
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testStringField_BadUnicodeInDefault(self):
    """Test binary values in string field."""
    self.assertRaisesWithRegexpMatch(
      messages.InvalidDefaultError,
      r"Invalid default value for StringField:.*: "
      r"Field encountered non-ASCII string .*: "
      r"'ascii' codec can't decode byte 0x89 in position 0: "
      r"ordinal not in range",
      messages.StringField, 1, default=b'\x89') 
Example #25
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def ActionOnAllFieldClasses(self, action):
    """Test all field classes except Message and Enum.

    Message and Enum require separate tests.

    Args:
      action: Callable that takes the field class as a parameter.
    """
    for field_class in (messages.IntegerField,
                        messages.FloatField,
                        messages.BooleanField,
                        messages.BytesField,
                        messages.StringField,
                       ):
      action(field_class) 
Example #26
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 5 votes vote down vote up
def testPropertyNameConflict(self):
    class MyMsg(messages.Message):
      blob_ = messages.StringField(1)
    msgprop.MessageProperty(MyMsg)  # Should be okay
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      MyMsg, indexed_fields=['blob_']) 
Example #27
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 5 votes vote down vote up
def testRepeatedIndexedFieldInRepeatedMessageProperty(self):
    class AltGreeting(messages.Message):
      lines = messages.StringField(1, repeated=True)
      when = messages.IntegerField(2)
    self.assertRaises(TypeError, msgprop.MessageProperty,
                      AltGreeting, indexed_fields=['lines'], repeated=True) 
Example #28
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 5 votes vote down vote up
def testRepeatedIndexedField(self):
    class AltGreeting(messages.Message):
      lines = messages.StringField(1, repeated=True)
      when = messages.IntegerField(2)

    class Store(model.Model):
      altg = msgprop.MessageProperty(AltGreeting, indexed_fields=['lines'])
    s1 = Store(altg=AltGreeting(lines=['foo', 'bar'], when=123))
    s1.put()
    s2 = Store(altg=AltGreeting(lines=['baz', 'bletch'], when=456))
    s2.put()
    res = Store.query(Store.altg.lines == 'foo').fetch()
    self.assertEqual(res, [s1]) 
Example #29
Source File: msgprop.py    From python-compat-runtime with Apache License 2.0 4 votes vote down vote up
def _make_model_class(message_type, indexed_fields, **props):
  """Construct a Model subclass corresponding to a Message subclass.

  Args:
    message_type: A Message subclass.
    indexed_fields: A list of dotted and undotted field names.
    **props: Additional properties with which to seed the class.

  Returns:
    A Model subclass whose properties correspond to those fields of
    message_type whose field name is listed in indexed_fields, plus
    the properties specified by the **props arguments.  For dotted
    field names, a StructuredProperty is generated using a Model
    subclass created by a recursive call.

  Raises:
    Whatever _analyze_indexed_fields() raises.
    ValueError if a field name conflicts with a name in **props.
    ValueError if a field name is not valid field of message_type.
    ValueError if an undotted field name designates a MessageField.
  """
  analyzed = _analyze_indexed_fields(indexed_fields)
  for field_name, sub_fields in analyzed.iteritems():
    if field_name in props:
      raise ValueError('field name %s is reserved' % field_name)
    try:
      field = message_type.field_by_name(field_name)
    except KeyError:
      raise ValueError('Message type %s has no field named %s' %
                       (message_type.__name__, field_name))
    if isinstance(field, messages.MessageField):
      if not sub_fields:
        raise ValueError(
            'MessageField %s cannot be indexed, only sub-fields' % field_name)
      sub_model_class = _make_model_class(field.type, sub_fields)
      prop = model.StructuredProperty(sub_model_class, field_name,
                                      repeated=field.repeated)
    else:
      if sub_fields is not None:
        raise ValueError(
            'Unstructured field %s cannot have indexed sub-fields' % field_name)
      if isinstance(field, messages.EnumField):
        prop = EnumProperty(field.type, field_name, repeated=field.repeated)
      elif isinstance(field, messages.BytesField):
        prop = model.BlobProperty(field_name,
                                  repeated=field.repeated, indexed=True)
      else:
        # IntegerField, FloatField, BooleanField, StringField.
        prop = model.GenericProperty(field_name, repeated=field.repeated)
    props[field_name] = prop
  return model.MetaModel('_%s__Model' % message_type.__name__,
                         (model.Model,), props) 
Example #30
Source File: discovery.py    From luci-py with Apache License 2.0 4 votes vote down vote up
def _get_type_format(field):
  """Returns the schema type and format for the given message type.

  Args:
    field: The protorpc.messages.Field to get schema type and format for.

  Returns:
    (type, format) for use in the "schemas" section of a discovery document.
  """
  if isinstance(field, messages.BooleanField):
    return ('boolean', None)

  if isinstance(field, messages.BytesField):
    return ('string', 'byte')

  if isinstance(field, message_types.DateTimeField):
    return ('string', 'date-time')

  if isinstance(field, messages.EnumField):
    return ('string', None)

  if isinstance(field, messages.FloatField):
    if field.variant == messages.Variant.DOUBLE:
      return ('number', 'double')
    return ('number', 'float')

  if isinstance(field, messages.IntegerField):
    if field.variant in (messages.Variant.INT32, messages.Variant.SINT32):
      return ('integer', 'int32')

    if field.variant in (messages.Variant.INT64, messages.Variant.SINT64):
      # If the type requires int64 or uint64, specify string or JavaScript will
      # convert them to 32-bit.
      return ('string', 'int64')

    if field.variant == messages.Variant.UINT32:
      return ('integer', 'uint32')

    if field.variant == messages.Variant.UINT64:
      return ('string', 'uint64')

    # Despite the warning about JavaScript, Endpoints v2's discovery document
    # generator uses integer, int64 as the default here. Follow their choice.
    return ('integer', 'int64')

  if isinstance(field, messages.StringField):
    return ('string', None)

  return (None, None)