()
| 18 | |
| 19 | |
| 20 | def test_all(): |
| 21 | num = '{num:06d}'.format(num=random.randint(0,999999)) |
| 22 | biscuit_file = Path(Path(__file__).parent / 'backup_biscuits' / f"{datetime.now().strftime('%Y%m%d.%h%m%s')}_biscuits_from_tests.json") |
| 23 | |
| 24 | try: |
| 25 | # create one keypair / biscuit for all use-cases and objects |
| 26 | biscuit = SXTBiscuit('all_uses', new_keypair=True) |
| 27 | biscuit.add_capability('*', sxt.GRANT.ALL) |
| 28 | biscuit.save(biscuit_file) # in case something goes wrong... we can rm at the end |
| 29 | |
| 30 | # Create table |
| 31 | table = SXTTable(f'SXTTest.MyTestTable_{num}', key_manager=biscuit.key_manager, access_type=sxt.TABLE_ACCESS.PUBLIC_READ, SpaceAndTime_parent=sxt) |
| 32 | table.add_biscuit_object(biscuit) |
| 33 | table.create_ddl = table.create_ddl_sample |
| 34 | table_success, resopnse = table.create() |
| 35 | assert table_success |
| 36 | assert table.exists |
| 37 | |
| 38 | # load some made-up data |
| 39 | data = [{'MyID':n, 'MyName':chr(n), 'MyDate':'2024-01-01'} for n in range(65,65+26)] |
| 40 | insert_success, response = table.insert.with_list_of_dicts(data) |
| 41 | assert insert_success |
| 42 | |
| 43 | # Select, using built-in function |
| 44 | select_success, data = table.select() |
| 45 | assert select_success |
| 46 | assert len(data) == 26 |
| 47 | assert data[0]['MYDATE'][:10] == '2024-01-01' |
| 48 | assert 64 < data[0]['MYID'] < 90 |
| 49 | assert sorted([d['MYID'] for d in data])[0] == 65 # sorted here, in python |
| 50 | |
| 51 | # Select, using custom SQL |
| 52 | select_success, data = table.select(f'Select * from {table.table_name} order by MyID') |
| 53 | assert select_success |
| 54 | assert len(data) == 26 |
| 55 | assert data[0]['MYID'] == 65 # sorted in the SQL |
| 56 | |
| 57 | # create view on table |
| 58 | view = SXTView(f'SXTTest.MyTestView_{num}_Evens', key_manager=biscuit.key_manager, SpaceAndTime_parent=sxt) |
| 59 | view.add_biscuit_object(biscuit) |
| 60 | view.create_ddl = f""" |
| 61 | CREATE VIEW {view.view_name} |
| 62 | {view.with_statement} AS |
| 63 | SELECT * FROM {table.table_name} WHERE MyID % 2 = 0 """ |
| 64 | view_success, response = view.create() |
| 65 | assert view_success |
| 66 | assert view.exists |
| 67 | |
| 68 | # views can take a moment for network to sync sometimes, let's check a few times |
| 69 | for i in range(1,30): |
| 70 | select_success, data = view.select() |
| 71 | if select_success and len(data) == 13: break |
| 72 | time.sleep(10) |
| 73 | |
| 74 | # select from the EVENS view (only even numbers) |
| 75 | assert select_success |
| 76 | assert len(data) == 13 |
| 77 | assert sorted([d['MYID'] for d in data])[0] == 66 |
no test coverage detected