def test_int_filed(self): self.assertEqual(Int(3, value=2).enc_value, 2) self.assertEqual(Int(5).decode(19), -13) for n, val in enumerate(range(-2, 1, -11)): self.assertEqual( val, Int(2).decode(Int(n + 3, value=val).enc_value), )
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 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 RamlTrait(Model): """ A trait is a partial method definition that, like a method, can provide method-level properties such as description, headers, query string parameters, and responses. """ usage = String() description = String() headers = RamlNamedParametersMap() queryParameters = RamlNamedParametersMap() responses = Map(Int(), Reference(RamlResponse))
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))
class RamlSecuritySchemeDescription(Model): """ The describedBy attribute MAY be used to apply a trait-like structure to a security scheme mechanism so as to extend the mechanism, such as specifying response codes, HTTP headers or custom documentation. """ description = String() body = Map(String(), Reference(RamlBody)) headers = RamlNamedParametersMap() queryParameters = RamlNamedParametersMap() responses = Map(Int(), Reference(RamlResponse)) baseUriParameters = RamlNamedParametersMap() protocols = List( Choice(field_name='protocols', choices=RAML_VALID_PROTOCOLS))
class RamlRoot(SecuredEntity, Model): """ http://raml.org/spec.html#root-section """ raml_version = String(required=True) title = String(required=True) version = Or(String(), Int(), Float()) baseUri = String(required=True) protocols = List( Choice(field_name='protocols', choices=RAML_VALID_PROTOCOLS)) mediaType = String() documentation = List(Reference(RamlDocumentation)) traits = Map(String(), Reference(RamlTrait)) resources = Map(String(), Reference(RamlResource)) resourceTypes = Map(String(), Reference(RamlResourceType)) schemas = Map(String(), Or(JSONData(), XMLData(), String())) baseUriParameters = RamlNamedParametersMap() securitySchemes = Map(String(), Reference(RamlSecurityScheme))
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())
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 RamlTrait(Model): """ traits: - secured: usage: Apply this to any method that needs to be secured description: Some requests require authentication. queryParameters: access_token: description: Access Token type: string example: ACCESS_TOKEN required: true """ name = String() usage = String() description = String() displayName = String() responses = Map(Int(), Reference(RamlResponse)) method = String() queryParameters = Map(String(), Reference(RamlQueryParameter)) body = Reference(RamlBody) # Reference to another RamlTrait is_ = List(String(), field_name="is")
class RamlMethod(Model): notNull = Bool() description = String() body = Reference(RamlBody) responses = Map(Int(), Reference(RamlBody))