def test_constructor(self) -> None: ENUM1 = 1 ENUM2 = 2 ENUM3 = 3 ENUMS = (ENUM1, ENUM2, ENUM3) field = gateaux.EnumField(members=ENUMS) self.assertEqual(field.members, ENUMS) with self.assertRaises(gateaux.errors.FieldError): gateaux.EnumField()
class EventLog(gateaux.Structure): # Enum members used in the value[1] field TYPE_UPLOAD = 0 TYPE_DOWNLOAD = 1 TYPE_MEMBERS = ( TYPE_UPLOAD, TYPE_DOWNLOAD, ) # Keys are a tuple of fields key = ( gateaux.DateTimeField( name='date', help_text='Date and time of the event', ), gateaux.IPv4AddressField( name='ip address', help_text='IPv4 address of the client that triggered the event', ) ) # Values are also a tuple of fields value = ( gateaux.IntegerField( name='bytes' min_value=0, help_text='Bytes transferred during the event', ), gateaux.EnumField( name='event type', members=TYPE_MEMBERS, help_text='Type of the event', ) )
def test_validation(self) -> None: field = gateaux.EnumField(members=(1, 2, 3)) self.assertEqual(field.pack(1), 1) self.assertEqual(field.pack(2), 2) self.assertEqual(field.pack(3), 3) with self.assertRaises(gateaux.errors.ValidationError): field.pack(0) with self.assertRaises(gateaux.errors.ValidationError): field.pack(4)
def test_unpack(self) -> None: field = gateaux.EnumField(members=(1, 2, 3)) with self.assertRaises(gateaux.errors.ValidationError): field.unpack('not int') # type: ignore self.assertEqual(field.unpack(1), 1)