def test_constructor(self) -> None: field = gateaux.IntegerField() self.assertEqual(field.min_value, None) self.assertEqual(field.max_value, None) field = gateaux.IntegerField(min_value=1234, max_value=5678) self.assertEqual(field.min_value, 1234) self.assertEqual(field.max_value, 5678) with self.assertRaises(gateaux.errors.FieldError): gateaux.IntegerField(min_value=5678, max_value=1234)
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.IntegerField() self.assertEqual(field.pack(1), 1) field = gateaux.IntegerField(min_value=123) self.assertEqual(field.min_value, 123) self.assertEqual(field.pack(123), 123) self.assertEqual(field.pack(124), 124) with self.assertRaises(gateaux.errors.ValidationError): field.pack(122) field = gateaux.IntegerField(max_value=456) self.assertEqual(field.max_value, 456) self.assertEqual(field.pack(456), 456) self.assertEqual(field.pack(455), 455) with self.assertRaises(gateaux.errors.ValidationError): field.pack(457) field = gateaux.IntegerField(min_value=123, max_value=456) self.assertEqual(field.pack(123), 123) self.assertEqual(field.pack(456), 456) self.assertEqual(field.pack(333), 333) with self.assertRaises(gateaux.errors.ValidationError): field.pack(122) with self.assertRaises(gateaux.errors.ValidationError): field.pack(457)
class TemperatureReading(gateaux.Structure): key = (gateaux.IntegerField(name='year', ), gateaux.IntegerField(name='day', )) value = (gateaux.IntegerField(name='degrees', ), )
class ClassAvailability(gateaux.Structure): '''Stores the number of available seats for a class''' key = (gateaux.StringField(name='name', help_text='Name of the class'), ) value = (gateaux.IntegerField(name='seats', help_text='Number of available seats'), )
def test_unpack(self) -> None: field = gateaux.IntegerField() with self.assertRaises(gateaux.errors.ValidationError): field.unpack('not int') # type: ignore self.assertEqual(field.unpack(123), 123)