async def query( self, *, sort_as_tree: bool = False, filter_as_tree: bool = False, filter: Union[MailboxCondition, FilterOperator, None] = None, sort: Optional[Comparator] = None, position: int = 0, anchor: Optional[Id] = None, anchor_offset: int = 0, limit: Optional[UnsignedInt] = None, calculate_total: bool = False, account_id: Optional[Id] = None, ) -> QueryResult: result = await self._query( sort_as_tree=sort_as_tree, filter_as_tree=filter_as_tree, filter=filter, sort=sort, position=position, anchor=anchor, anchor_offset=anchor_offset, limit=limit, calculate_total=calculate_total, account_id=account_id, ) return decode(result[1], QueryResult)
def test_decode_collections(self): for value, vtype, expected in ( ([1, 2, 3, 4], List[int], [1, 2, 3, 4]), ([1, 2, 3, 4], Set[int], {1, 2, 3, 4}), ): with self.subTest((value, vtype, expected)): result = base.decode(value, vtype) self.assertEqual(result, expected) for value, vtype, expected in ( ([1, 2, 3, 4], List[MyInt], [1, 2, 3, 4]), ([1, 2, 3, 4], Set[MyInt], {1, 2, 3, 4}), ): with self.subTest((value, vtype, expected)): result = base.decode(value, vtype) self.assertEqual(result, expected) for v in result: self.assertIsInstance(v, MyInt)
async def get( self, *, ids: Union[ResultReference, Iterable[Id], None] = None, properties: Optional[List[str]] = None, account_id: Optional[Id] = None, ) -> GetResult[Email]: result = await self._get(ids=ids, properties=properties, account_id=account_id) return decode(result[1], GetResult[Email])
def test_decode_generics(self): data = { "name": "foobar", "items": [{"key": "a", "value": "b"}, {"key": "car", "value": "hatchback"}], } expected = MultiItems( name="foobar", items=[Pair(key="a", value="b"), Pair(key="car", value="hatchback")], ) result = base.decode(data, MultiItems[Pair]) self.assertEqual(result, expected)
async def set( self, *, if_in_state: Optional[str] = None, create: Optional[Dict[Id, Any]] = None, update: Optional[Dict[Id, List[str]]] = None, destroy: Optional[List[Id]] = None, account_id: Optional[Id] = None, ) -> SetResult[Email]: result = await self._set( if_in_state=if_in_state, create=create, update=update, destroy=destroy, account_id=account_id, ) return decode(result[1], SetResult[Email])
async def set( self, *, on_destroy_remove_emails: bool = False, if_in_state: Optional[str] = None, create: Optional[Dict[Id, Any]] = None, update: Optional[Dict[Id, List[str]]] = None, destroy: Optional[List[Id]] = None, account_id: Optional[Id] = None, ) -> SetResult[Mailbox]: result = await self._set( on_destroy_remove_emails=on_destroy_remove_emails, if_in_state=if_in_state, create=create, update=update, destroy=destroy, account_id=account_id, ) return decode(result[1], SetResult[Mailbox])
def test_decode_primitives(self): sentinel = object() for value, vtype, expected in ( (True, bool, True), (False, bool, False), (123, int, 123), (123, MyInt, MyInt(123)), (b"name", bytes, b"name"), ("name", str, "name"), (None, type(None), None), (sentinel, Any, sentinel), (True, Optional[bool], True), (None, Optional[bool], None), (123, Optional[MyInt], MyInt(123)), (None, Optional[MyInt], None), ): with self.subTest((value, vtype, expected)): result = base.decode(value, vtype) self.assertEqual(result, expected) self.assertEqual(type(result), type(expected))
def test_decode_datatype(self): result = base.decode( { "name": "something", "from": 1234, "items": [1, 3, 9], "bucketValues": {"foo": 1, "bar": 2}, }, Foo, ) expected = Foo( name="something", from_=1234, items=[1, 3, 9], bucket_values={"foo": 1, "bar": 2}, ) self.assertEqual(result, expected) self.assertIsInstance(result.items[0], MyInt) self.assertIsInstance(result.bucket_values["foo"], MyInt) result = base.decode( { "foos": [ { "name": "something", "from": 1234, "items": [1, 3, 9], "bucketValues": {"foo": 1, "bar": 2}, "maybe": None, }, { "name": "else", "from": 1234, "items": [1, 3, 9], "bucketValues": {"foo": 1, "bar": 2}, }, ], "maybe": { "name": "another", "from": 1234, "items": [1, 3, 9], "bucketValues": {"foo": 1, "bar": 2}, "maybe": None, }, }, Nested, ) expected = Nested( foos=[ Foo( name="something", from_=1234, items=[1, 3, 9], bucket_values={"foo": 1, "bar": 2}, maybe=None, ), Foo( name="else", from_=1234, items=[1, 3, 9], bucket_values={"foo": 1, "bar": 2}, ), ], maybe=Foo( name="another", from_=1234, items=[1, 3, 9], bucket_values={"foo": 1, "bar": 2}, maybe=None, ), ) self.assertEqual(result, expected)
def test_decode_bad_input(self): with self.assertRaisesRegex(TypeError, r"invalid data .* for .*Foo"): base.decode([1, 2], Foo)
def test_decode_unsupported_types(self): with self.assertRaisesRegex(NotImplementedError, "can't decode union types"): base.decode(123, Union[int, str]) with self.assertRaisesRegex(NotImplementedError, "can't decode union types"): base.decode(123, Union[int, str, None]) with self.assertRaisesRegex(NotImplementedError, "can't decode union types"): base.decode(123, Optional[Union[int, str]]) with self.assertRaisesRegex(NotImplementedError, "can't decode object keys"): base.decode(123, Dict[Foo, str]) with self.assertRaisesRegex(NotImplementedError, "failed to decode"): base.decode(123, Iterable[int]) class Frob: pass with self.assertRaisesRegex(NotImplementedError, "failed to decode"): base.decode(123, Frob)