示例#1
0
  def testDefineField_Default_Bool(self):
    """Test defining a default value for a bool."""
    field_descriptor = descriptor.FieldDescriptor()

    field_descriptor.name = 'a_field'
    field_descriptor.number = 1
    field_descriptor.variant = descriptor.FieldDescriptor.Variant.BOOL
    field_descriptor.default_value = u'true'

    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.BooleanField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.BOOL, field.variant)
    self.assertFalse(field.required)
    self.assertFalse(field.repeated)
    self.assertEqual(field.default, True)

    field_descriptor.default_value = u'false'

    field = definition.define_field(field_descriptor)

    self.assertEqual(field.default, False)
  def testDefineField_Default_Invalid(self):
    """Test defining a default value that is not valid."""
    field_descriptor = descriptor.FieldDescriptor()

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

    # Verify that the string is passed to the Constructor.
    mock = mox.Mox()
    mock.StubOutWithMock(messages.IntegerField, '__init__')
    messages.IntegerField.__init__(
        default=u'Test',
        number=1,
        variant=messages.Variant.INT64
        ).AndRaise(messages.InvalidDefaultError)

    mock.ReplayAll()
    with self.assertRaises(messages.InvalidDefaultError):
      _ = definition.define_field(field_descriptor)
    mock.VerifyAll()

    mock.ResetAll()
    mock.UnsetStubs()
示例#3
0
  def testDefineField_Optional(self):
    """Test defining an optional 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.INT32
    field_descriptor.label = descriptor.FieldDescriptor.Label.OPTIONAL

    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.IntegerField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.INT32, field.variant)
    self.assertFalse(field.required)
    self.assertFalse(field.repeated)
示例#4
0
  def testDefineField_Repeated(self):
    """Test defining a repeated 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.DOUBLE
    field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED

    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.FloatField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.DOUBLE, field.variant)
    self.assertFalse(field.required)
    self.assertTrue(field.repeated)
示例#5
0
  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')
示例#6
0
  def testDefineField_Default_Float(self):
    """Test defining a default value for a float."""
    field_descriptor = descriptor.FieldDescriptor()

    field_descriptor.name = 'a_field'
    field_descriptor.number = 1
    field_descriptor.variant = descriptor.FieldDescriptor.Variant.FLOAT
    field_descriptor.default_value = u'34.567'

    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.FloatField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.FLOAT, field.variant)
    self.assertFalse(field.required)
    self.assertFalse(field.repeated)
    self.assertEqual(field.default, 34.567)
示例#7
0
  def testDefineField_DateTime(self):
    """Test defining a date time field."""
    field_descriptor = descriptor.FieldDescriptor()

    field_descriptor.name = 'a_timestamp'
    field_descriptor.number = 1
    field_descriptor.variant = descriptor.FieldDescriptor.Variant.MESSAGE
    field_descriptor.type_name = 'protorpc.message_types.DateTimeMessage'
    field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED

    field = definition.define_field(field_descriptor)

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

    self.assertTrue(isinstance(field, message_types.DateTimeField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.MESSAGE, field.variant)
    self.assertFalse(field.required)
    self.assertTrue(field.repeated)
示例#8
0
    def testDefineField_Optional(self):
        """Test defining an optional 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.INT32
        field_descriptor.label = descriptor.FieldDescriptor.Label.OPTIONAL

        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.IntegerField))
        self.assertEquals(1, field.number)
        self.assertEquals(descriptor.FieldDescriptor.Variant.INT32,
                          field.variant)
        self.assertFalse(field.required)
        self.assertFalse(field.repeated)
示例#9
0
    def testDefineField_Repeated(self):
        """Test defining a repeated 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.DOUBLE
        field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED

        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.FloatField))
        self.assertEquals(1, field.number)
        self.assertEquals(descriptor.FieldDescriptor.Variant.DOUBLE,
                          field.variant)
        self.assertFalse(field.required)
        self.assertTrue(field.repeated)
示例#10
0
    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')
示例#11
0
    def testDefineField_Default_Float(self):
        """Test defining a default value for a float."""
        field_descriptor = descriptor.FieldDescriptor()

        field_descriptor.name = 'a_field'
        field_descriptor.number = 1
        field_descriptor.variant = descriptor.FieldDescriptor.Variant.FLOAT
        field_descriptor.default_value = u'34.567'

        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.FloatField))
        self.assertEquals(1, field.number)
        self.assertEquals(descriptor.FieldDescriptor.Variant.FLOAT,
                          field.variant)
        self.assertFalse(field.required)
        self.assertFalse(field.repeated)
        self.assertEqual(field.default, 34.567)
示例#12
0
    def testDefineField_DateTime(self):
        """Test defining a date time field."""
        field_descriptor = descriptor.FieldDescriptor()

        field_descriptor.name = 'a_timestamp'
        field_descriptor.number = 1
        field_descriptor.variant = descriptor.FieldDescriptor.Variant.MESSAGE
        field_descriptor.type_name = 'protorpc.message_types.DateTimeMessage'
        field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED

        field = definition.define_field(field_descriptor)

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

        self.assertTrue(isinstance(field, message_types.DateTimeField))
        self.assertEquals(1, field.number)
        self.assertEquals(descriptor.FieldDescriptor.Variant.MESSAGE,
                          field.variant)
        self.assertFalse(field.required)
        self.assertTrue(field.repeated)
示例#13
0
  def testDefineField_Enum(self):
    """Test defining an enum field."""
    field_descriptor = descriptor.FieldDescriptor()

    field_descriptor.name = 'a_field'
    field_descriptor.number = 1
    field_descriptor.variant = descriptor.FieldDescriptor.Variant.ENUM
    field_descriptor.type_name = 'something.yet.to.be.Defined'
    field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED

    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.EnumField))
    self.assertEquals(1, field.number)
    self.assertEquals(descriptor.FieldDescriptor.Variant.ENUM, field.variant)
    self.assertFalse(field.required)
    self.assertTrue(field.repeated)
    self.assertRaisesWithRegexpMatch(messages.DefinitionNotFoundError,
                                     'Could not find definition for '
                                     'something.yet.to.be.Defined',
                                     getattr, field, 'type')
示例#14
0
    def testDefineField_Enum(self):
        """Test defining an enum field."""
        field_descriptor = descriptor.FieldDescriptor()

        field_descriptor.name = 'a_field'
        field_descriptor.number = 1
        field_descriptor.variant = descriptor.FieldDescriptor.Variant.ENUM
        field_descriptor.type_name = 'something.yet.to.be.Defined'
        field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED

        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.EnumField))
        self.assertEquals(1, field.number)
        self.assertEquals(descriptor.FieldDescriptor.Variant.ENUM,
                          field.variant)
        self.assertFalse(field.required)
        self.assertTrue(field.repeated)
        self.assertRaisesWithRegexpMatch(
            messages.DefinitionNotFoundError, 'Could not find definition for '
            'something.yet.to.be.Defined', getattr, field, 'type')