1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
import os
import copy
import pkg_resources
from random import randint, random
import txaio
from unittest import skipIf
if 'USE_TWISTED' in os.environ and os.environ['USE_TWISTED']:
from twisted.trial import unittest
txaio.use_twisted()
else:
import unittest
txaio.use_asyncio()
from autobahn.xbr import HAS_XBR
from autobahn.wamp.exception import InvalidPayload
if HAS_XBR:
from autobahn.xbr import FbsRepository
@skipIf(not HAS_XBR, 'package autobahn[xbr] not installed')
class TestFbsBase(unittest.TestCase):
"""
FlatBuffers tests base class, loads test schemas.
"""
def setUp(self):
self.repo = FbsRepository('autobahn')
self.archives = []
for fbs_file in ['demo.bfbs', 'wamp-control.bfbs']:
archive = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/{}'.format(fbs_file))
self.repo.load(archive)
self.archives.append(archive)
class TestFbsValidateTestTableA(TestFbsBase):
def test_validate_TestTableA_valid(self):
valid_args = [
True,
randint(-127, -1),
randint(1, 255),
randint(-2 ** 15, -1),
randint(1, 2 ** 16 - 1),
randint(-2 ** 31, -1),
randint(1, 2 ** 32 - 1),
randint(-2 ** 63, -1),
randint(1, 2 ** 64 - 1),
2.0 + random(),
2.0 + random(),
]
try:
self.repo.validate('demo.TestTableA', args=valid_args, kwargs={})
except Exception as exc:
self.assertTrue(False, f'Inventory.validate() raised an exception: {exc}')
def test_validate_TestTableA_invalid(self):
valid_args = [
True,
randint(-127, -1),
randint(1, 255),
randint(-2 ** 15, -1),
randint(1, 2 ** 16 - 1),
randint(-2 ** 31, -1),
randint(1, 2 ** 32 - 1),
randint(-2 ** 63, -1),
randint(1, 2 ** 64 - 1),
2.0 + random(),
2.0 + random(),
]
# mandatory field with wrong type
for i in range(len(valid_args)):
# copy valid value, and set one column to a value of wrong type
invalid_args = copy.copy(valid_args)
if i == 0:
# first column should be bool, so make it invalid with an int value
invalid_args[0] = 666
else:
# all other columns are something different from bool, so make it invalid with a bool value
invalid_args[i] = True
self.assertRaisesRegex(InvalidPayload, 'invalid type', self.repo.validate,
'demo.TestTableA', invalid_args, {})
# mandatory field with wrong type `None`
if True:
for i in range(len(valid_args)):
# copy valid value, and set one column to a value of wrong type
invalid_args = copy.copy(valid_args)
invalid_args[i] = None
self.assertRaisesRegex(InvalidPayload, 'invalid type', self.repo.validate,
'demo.TestTableA', invalid_args, {})
# mandatory field missing
if True:
for i in range(len(valid_args)):
invalid_args = valid_args[:i]
self.assertRaisesRegex(InvalidPayload, 'missing positional argument', self.repo.validate,
'demo.TestTableA', invalid_args, {})
|