def test_bool_field(self): self.assertEqual(Bool(1, value=True).enc_value, 1) self.assertEqual(Bool(3).decode('true'), True) for val in [True, False, 1, 56, 0, '', 'true']: self.assertEqual( bool(val), Bool(1).decode(Bool(1, value=val).enc_value), )
class RamlQueryParameter(Model): name = String() description = String() example = Or(String(),Int(),Float()) displayName = String() type = String() enum = List(Or(String(),Float(),Int())) pattern = String() minLength = Int() maxLength = Int() repeat = Bool() required = Bool() default = Or(String(),Int(),Float()) minimum = Or(Int(),Float()) maximum = Or(Int(),Float())
class RamlNamedParameters(Model): """ http://raml.org/spec.html#named-parameters """ displayName = String() description = String() type = Choice(default='string', choices=NAMED_PARAMETER_TYPES) name = String() example = Or(String(), Int(), Float()) enum = List(Or(String(), Float(), Int())) pattern = String() minLength = Int() maxLength = Int() repeat = Bool() required = Bool() default = Or(String(), Int(), Float()) minimum = Or(Int(), Float()) maximum = Or(Int(), Float())
class RamlMethod(Model): notNull = Bool() description = String() body = Reference(RamlBody) responses = Map(Int(), Reference(RamlBody)) #responses = Map(Int(), Reference(RamlResponse)) queryParameters = Map(String(), Reference(RamlQueryParameter))
class RamlResponse(Model): schema = String() example = String() notNull = Bool() description = String() headers = Map(String(), Reference(RamlHeader)) body = Reference("pyraml.entities.RamlBody")
class RamlBody(Model): """ A method's body is defined in the body property as a hashmap, in which the key MUST be a valid media type. """ schema = Or(JSONData(), XMLData(), String()) example = String() notNull = Bool() formParameters = RamlNamedParametersMap()
class RamlBody(Model): schema = String() example = String() notNull = Bool() formParameters = Map(String(), Reference(RamlHeader)) headers = Map(String(), Reference(RamlHeader)) body = Map(String(), Reference("pyraml.entities.RamlBody")) is_ = List(String(), field_name="is")
def test_encoder(self): fields_to_encode = [ Int(3, value=1), Int(8, value=9), Int(5, value=3), ] coder = BitsCoder(fields_to_encode) enc = coder.encode() proper_value = 0b001_00001001_00011 self.assertEqual(enc, proper_value.to_bytes(2, 'big')) coder = BitsCoder(fields_to_encode, byteorder='little') enc = coder.encode() self.assertEqual(enc, proper_value.to_bytes(2, 'little')) fields_to_encode = [ Float(7, 1, value=-1.3), Bool(1, value=True), Int(8, value=-3), ] coder = BitsCoder(fields_to_encode) enc = coder.encode() proper_value = 0b1110011_1_0000000011111101.to_bytes(3, 'big') self.assertEqual(enc, proper_value) # empty fill fields_to_encode = [ Ufloat(5, 1, value=1.3), Uint(8, value=9), Int(4, value=-3), ] coder = BitsCoder(fields_to_encode) enc = coder.encode() proper_value = 0b01101_00001001_1101_0000000.to_bytes(3, 'big') self.assertEqual(enc, proper_value) coder = BitsCoder([ Int(6, name='temperature', value=21), Bool(1, name='is_nice', value=True), Float(18, 3, name='lat', value=78.234), Float(18, 3, name='lon', value=-33.111) ]) enc = coder.encode() proper_value = 0b010101_1_010011000110011010_110111111010101001_00000 self.assertEqual(enc, proper_value.to_bytes(6, 'big'))
class RamlResponse(Model): """ Responses MUST be a map of one or more HTTP status codes, where each status code itself is a map that describes that status code. """ notNull = Bool() description = String() headers = RamlNamedParametersMap() body = Map(String(), Reference("pyraml.entities.RamlBody"))
class RamlMethod(TraitedEntity, SecuredEntity, Model): """ http://raml.org/spec.html#methods """ notNull = Bool() description = String() body = Map(String(), Reference(RamlBody)) responses = Map(Int(), Reference(RamlResponse)) queryParameters = RamlNamedParametersMap() baseUriParameters = RamlNamedParametersMap() headers = RamlNamedParametersMap() protocols = List( Choice(field_name='protocols', choices=RAML_VALID_PROTOCOLS))
def test_decoder(self): proper_map = {'a': -12, 'b': 2.11, 'c': True, 'd': 12, '___1': 0} pld = 'e9a7003000' fields = [ Int(7, name='a'), Ufloat(8, 2, name='b'), Bool(1, name='c'), Int(6, name='d') ] coder = BitsCoder(fields) coder.decode(pld) self.assertEqual(proper_map, coder.map)
class RamlHeader(Model): type = String() required = Bool()
class RamlMethod(Model): notNull = Bool() description = String() body = Reference(RamlBody) responses = Map(Int(), Reference(RamlBody))