How to use foreign_key_name method in autotest

Best Python code snippet using autotest_python

test_foreign_keys.py

Source:test_foreign_keys.py Github

copy

Full Screen

1# nuScenes dev-kit.2# Code written by Holger Caesar, 2020.3import itertools4import os5import unittest6from collections import defaultdict7from typing import List, Dict, Any8from nuimages.nuimages import NuImages9class TestForeignKeys(unittest.TestCase):10 def __init__(self, _: Any = None, version: str = 'v1.0-mini', dataroot: str = None):11 """12 Initialize TestForeignKeys.13 Note: The second parameter is a dummy parameter required by the TestCase class.14 :param version: The NuImages version.15 :param dataroot: The root folder where the dataset is installed.16 """17 super().__init__()18 self.version = version19 if dataroot is None:20 self.dataroot = os.environ['NUIMAGES']21 else:22 self.dataroot = dataroot23 self.nuim = NuImages(version=self.version, dataroot=self.dataroot, verbose=False)24 def runTest(self) -> None:25 """26 Dummy function required by the TestCase class.27 """28 pass29 def test_foreign_keys(self) -> None:30 """31 Test that every foreign key points to a valid token.32 """33 # Index the tokens of all tables.34 index = dict()35 for table_name in self.nuim.table_names:36 print('Indexing table %s...' % table_name)37 table: list = self.nuim.__getattr__(table_name)38 tokens = [row['token'] for row in table]39 index[table_name] = set(tokens)40 # Go through each table and check the foreign_keys.41 for table_name in self.nuim.table_names:42 table: List[Dict[str, Any]] = self.nuim.__getattr__(table_name)43 if self.version.endswith('-test') and len(table) == 0: # Skip test annotations.44 continue45 keys = table[0].keys()46 # Check 1-to-1 link.47 one_to_one_names = [k for k in keys if k.endswith('_token') and not k.startswith('key_')]48 for foreign_key_name in one_to_one_names:49 print('Checking one-to-one key %s in table %s...' % (foreign_key_name, table_name))50 foreign_table_name = foreign_key_name.replace('_token', '')51 foreign_tokens = set([row[foreign_key_name] for row in table])52 # Check all tokens are valid.53 if self.version.endswith('-mini') and foreign_table_name == 'category':54 continue # Mini does not cover all categories.55 foreign_index = index[foreign_table_name]56 self.assertTrue(foreign_tokens.issubset(foreign_index))57 # Check all tokens are covered.58 # By default we check that all tokens are covered. Exceptions are listed below.59 if table_name == 'object_ann':60 if foreign_table_name == 'category':61 remove = set([cat['token'] for cat in self.nuim.category if cat['name']62 in ['vehicle.ego', 'flat.driveable_surface']])63 foreign_index = foreign_index.difference(remove)64 elif foreign_table_name == 'sample_data':65 foreign_index = None # Skip as sample_datas may have no object_ann.66 elif table_name == 'surface_ann':67 if foreign_table_name == 'category':68 remove = set([cat['token'] for cat in self.nuim.category if cat['name']69 not in ['vehicle.ego', 'flat.driveable_surface']])70 foreign_index = foreign_index.difference(remove)71 elif foreign_table_name == 'sample_data':72 foreign_index = None # Skip as sample_datas may have no surface_ann.73 if foreign_index is not None:74 self.assertEqual(foreign_tokens, foreign_index)75 # Check 1-to-many link.76 one_to_many_names = [k for k in keys if k.endswith('_tokens')]77 for foreign_key_name in one_to_many_names:78 print('Checking one-to-many key %s in table %s...' % (foreign_key_name, table_name))79 foreign_table_name = foreign_key_name.replace('_tokens', '')80 foreign_tokens_nested = [row[foreign_key_name] for row in table]81 foreign_tokens = set(itertools.chain(*foreign_tokens_nested))82 # Check that all tokens are valid.83 foreign_index = index[foreign_table_name]84 self.assertTrue(foreign_tokens.issubset(foreign_index))85 # Check all tokens are covered.86 if self.version.endswith('-mini') and foreign_table_name == 'attribute':87 continue # Mini does not cover all categories.88 if foreign_index is not None:89 self.assertEqual(foreign_tokens, foreign_index)90 # Check prev and next.91 prev_next_names = [k for k in keys if k in ['previous', 'next']]92 for foreign_key_name in prev_next_names:93 print('Checking prev-next key %s in table %s...' % (foreign_key_name, table_name))94 foreign_table_name = table_name95 foreign_tokens = set([row[foreign_key_name] for row in table if len(row[foreign_key_name]) > 0])96 # Check that all tokens are valid.97 foreign_index = index[foreign_table_name]98 self.assertTrue(foreign_tokens.issubset(foreign_index))99 def test_prev_next(self) -> None:100 """101 Test that the prev and next points in sample_data cover all entries and have the correct ordering.102 """103 # Register all sample_datas.104 sample_to_sample_datas = defaultdict(lambda: [])105 for sample_data in self.nuim.sample_data:106 sample_to_sample_datas[sample_data['sample_token']].append(sample_data['token'])107 print('Checking prev-next pointers for completeness and correct ordering...')108 for sample in self.nuim.sample:109 # Compare the above sample_datas against those retrieved by using prev and next pointers.110 sd_tokens_pointers = self.nuim.get_sample_content(sample['token'])111 sd_tokens_all = sample_to_sample_datas[sample['token']]112 self.assertTrue(set(sd_tokens_pointers) == set(sd_tokens_all),113 'Error: Inconsistency in prev/next pointers!')114 timestamps = []115 for sd_token in sd_tokens_pointers:116 sample_data = self.nuim.get('sample_data', sd_token)117 timestamps.append(sample_data['timestamp'])118 self.assertTrue(sorted(timestamps) == timestamps, 'Error: Timestamps not properly sorted!')119if __name__ == '__main__':120 # Runs the tests without aborting on error.121 for nuim_version in ['v1.0-train', 'v1.0-val', 'v1.0-test', 'v1.0-mini']:122 print('Running TestForeignKeys for version %s...' % nuim_version)123 test = TestForeignKeys(version=nuim_version)124 test.test_foreign_keys()125 test.test_prev_next()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful