def test_quote_unquote(): with TemporaryDirectory() as dir: fr = FileResource(dir, schema=s.bytes(), extension=".bin") body = b"body" id = "resource%identifier" assert fr.create(id, body)["id"] == id fr.delete(id)
def _static(fs_path): content = fs_path.read_bytes() content_type, encoding = guess_type(fs_path.name) if content_type is None or encoding is not None: content_type = "application/octet-stream" schema = s.bytes(format="binary", content_type=content_type) return StaticResource(content, schema, fs_path.name, "Static resource.", security)
class TestResource(Resource): @operation(params={"_body": s.bytes(format="binary")}, returns=s.dict({"id": s.str()})) def create(self, _body): if _body != b"hello_body": raise BadRequest("_body not hello_body") return {"id": "foo"} @operation(type="action", params={"a": s.int(), "b": s.str()}, returns=s.str()) def foo(self, a, b): return "hoot"
class DC: id: s.uuid() str: s.str(nullable=True) dict: s.dict({"a": s.int()}, nullable=True) list: s.list(s.int(), nullable=True) _set: s.set(s.str(), nullable=True) int: s.int(nullable=True) float: s.float(nullable=True) bool: s.bool(nullable=True) bytes: s.bytes(format="byte", nullable=True) date: s.date(nullable=True) datetime: s.datetime(nullable=True)
def test( self, a: s.list(s.str()), b: s.set(s.str()), c: s.int(), d: s.float(), e: s.bool(), f: s.bytes(), g: s.datetime(), h: s.uuid(), ): pass
def test_crud_bytes(self): with TemporaryDirectory() as dir: frs = FileResource(dir, schema=s.bytes(), extension=".bin") body = b"\x00\x0e\0x01\0x01\0x00" id = "binary" self.assertEqual(id, frs.create(id, body)["id"]) self.assertEqual(frs.list(), [id]) self.assertEqual(body, frs.read(id)) body = bytes((1,2,3,4,5)) frs.update(id, body) self.assertEqual(body, frs.read(id)) frs.delete(id) self.assertEqual(frs.list(), [])
def test_crud_bytes(): with TemporaryDirectory() as dir: frs = FileResource(dir, schema=s.bytes(), extension=".bin") body = b"\x00\x0e\0x01\0x01\0x00" id = "binary" assert frs.create(id, body)["id"] == id assert frs.list() == [id] assert frs.read(id) == body body = bytes((1, 2, 3, 4, 5)) frs.update(id, body) assert frs.read(id) == body frs.delete(id) assert frs.list() == []
def test_invalid_directory(): with TemporaryDirectory() as dir: fr = FileResource(dir, schema=s.bytes(), extension=".bin") # directory should now be deleted underneath the resource body = b"body" id = "resource%identifier" with pytest.raises(InternalServerError): fr.create(id, body) with pytest.raises(NotFound): fr.read(id) with pytest.raises(NotFound): fr.update(id, body) with pytest.raises(NotFound): fr.delete(id) with pytest.raises(InternalServerError): fr.list()
class User: """User who owns/administers teams, tasks and/or stations.""" id: s.uuid(description="Identifies the user.") email: s.str(description="Email address of the user.") password: s.bytes( description="Hash of the password for user to authenticate with server." ) name: s.str(description="Full name of the user.") call_sign: s.str(description="Call sign of the user.") status: s.str(enum={"active", "suspended"}, description="Status of the user.") created: s.datetime(description="Date and time user record was created.") failures: s.list( items=s.datetime(), description="Date and time of recent consecutive login failures.", ) _required = "email name status created"
def test_bytes_json_decode_error(self): self._error(s.bytes().json_decode, "this_is_not_a_bytes_object_either")
def test_bytes_json_decode_success(self): val = bytes([7, 8, 9]) self.assertEqual(s.bytes().json_decode(b64encode(val).decode()), val)
def test_bytes_json_encode_error(self): self._error(s.bytes().json_encode, "definitely_not_a_bytes_object")
def test_bytes_json_encode_success(self): val = bytes([4, 5, 6]) self.assertEqual(s.bytes().json_encode(val), b64encode(val).decode())
def test_bytes_validate_type_error(self): self._error(s.bytes().validate, "this_is_not_a_bytes_object")
def test_bytes_hex_str_decode(): assert s.bytes(format="hex").str_decode("DEADBEEF") == b"\xde\xad\xbe\xef"
def test_bytes_disallow_none(self): self._error(s.bytes().json_encode, None)
def create( self, _body: s.bytes(format="binary")) -> s.dict({"id": s.str()}): if _body != b"hello_body": raise BadRequest("_body not hello_body") return {"id": "foo"}
def test_bytes_disallow_none(): _error(s.bytes().json_encode, None)
def test_bytes_invalid_format(): with pytest.raises(ValueError): s.bytes(format="foo")
def test_bytes_binary_str_encode(): _error(s.bytes(format="binary").str_encode, b"12345")
def test_bytes_binary_str_decode(): _error(s.bytes(format="binary").str_decode, "ain't gonna decode")
def test_bytes_hex_str_encode(): assert s.bytes(format="hex").str_encode(b"\xde\xad\xbe\xef") == "deadbeef"
def test_bytes_str_encode_success(self): val = bytes([0, 2, 4, 6, 8]) self.assertEqual(s.bytes().str_encode(val), b64encode(val).decode())
def test_bytes_allow_none(): assert s.bytes(nullable=True).json_encode(None) == None
def test_bytes_str_decode_success(self): val = bytes([1, 3, 5, 7, 9]) self.assertEqual(s.bytes().str_decode(b64encode(val).decode()), val)
def test_bytes_validate_type_success(self): s.bytes().validate(bytes([1, 2, 3]))
def test_bytes_allow_none(self): self.assertEqual(s.bytes(nullable=True).json_encode(None), None)
import roax.resource as r import roax.schema as s import unittest from roax.static import StaticResource from roax.resource import operation _schema = s.bytes(format="binary") _content = b"This is the content that will be returned." class TestResource(StaticResource): def __init__(self): super().__init__(_content, _schema) class TestStaticResource(unittest.TestCase): def test_read(self): tr = TestResource() self.assertEqual(tr.read(), _content) if __name__ == "__main__": unittest.main()
class Bin: id: s.uuid() bin: s.bytes(format="binary")
def test_bytes_str_decode_success(): val = bytes([1, 3, 5, 7, 9]) assert s.bytes().str_decode(b64encode(val).decode()) == val