Best Python code snippet using autotest_python
resources_unittest.py
Source:resources_unittest.py
...30 host.locked = True31 host.save()32 def test_simple_filtering(self):33 response = self.request('get', 'hosts?locked=true&has_label=label1')34 self.check_collection(response, 'hostname', ['host1', 'host2'])35 def test_in_filtering(self):36 response = self.request('get', 'hosts?hostname:in=host1,host2')37 self.check_collection(response, 'hostname', ['host1', 'host2'])38 def test_paging(self):39 response = self.request('get', 'hosts?start_index=1&items_per_page=2')40 self.check_collection(response, 'hostname', ['host2', 'host3'])41 self.assertEquals(response['total_results'], 9)42 self.assertEquals(response['items_per_page'], 2)43 self.assertEquals(response['start_index'], 1)44 def test_full_representations(self):45 response = self.request(46 'get', 'hosts?hostname=host1&full_representations=true')47 self.check_collection(response, 'hostname', ['host1'])48 host = response['members'][0]49 # invalid only included in full representation50 self.assertEquals(host['invalid'], False)51class MiscellaneousTest(AfeResourceTestCase):52 def test_trailing_slash(self):53 response = self.request('get', 'hosts/host1/')54 self.assertEquals(response['hostname'], 'host1')55class AtomicGroupClassTest(AfeResourceTestCase):56 def test_collection(self):57 response = self.request('get', 'atomic_group_classes')58 self.check_collection(response, 'name', ['atomic1', 'atomic2'],59 length=2)60 def test_entry(self):61 response = self.request('get', 'atomic_group_classes/atomic1')62 self.assertEquals(response['name'], 'atomic1')63 self.assertEquals(response['max_number_of_machines'], 2)64 def test_labels(self):65 self.check_relationship('atomic_group_classes/atomic1', 'labels',66 'label', 'name', ['label4', 'label5'])67class LabelTest(AfeResourceTestCase):68 def test_collection(self):69 response = self.request('get', 'labels')70 self.check_collection(response, 'name', ['label1', 'label2'], length=9,71 check_number=2)72 label1 = self.sorted_by(response['members'], 'name')[0]73 self.assertEquals(label1['is_platform'], False)74 def test_entry(self):75 response = self.request('get', 'labels/label1')76 self.assertEquals(response['name'], 'label1')77 self.assertEquals(response['is_platform'], False)78 self.assertEquals(response['atomic_group_class'], None)79 def test_hosts(self):80 self.check_relationship('labels/label1', 'hosts', 'host', 'hostname',81 ['host1'])82class UserTest(AfeResourceTestCase):83 def test_collection(self):84 response = self.request('get', 'users')85 self.check_collection(response, 'username',86 ['autotest_system', 'debug_user'])87 def test_entry(self):88 response = self.request('get', 'users/debug_user')89 self.assertEquals(response['username'], 'debug_user')90 me_response = self.request('get', 'users/@me')91 self.assertEquals(response, me_response)92 def test_acls(self):93 self.check_relationship('users/debug_user', 'acls', 'acl', 'name',94 ['Everyone', 'my_acl'])95 def test_accessible_hosts(self):96 group = models.AclGroup.objects.create(name='mygroup')97 models.User.objects.get(login='debug_user').aclgroup_set = [group]98 self.hosts[0].aclgroup_set = [group]99 user = self.request('get', 'users/debug_user')100 response = self.request('get', user['accessible_hosts']['href'])101 self.check_collection(response, 'hostname', ['host1'])102class AclTest(AfeResourceTestCase):103 def test_collection(self):104 response = self.request('get', 'acls')105 self.check_collection(response, 'name', ['Everyone', 'my_acl'])106 def test_entry(self):107 response = self.request('get', 'acls/my_acl')108 self.assertEquals(response['name'], 'my_acl')109 def test_users(self):110 self.check_relationship('acls/my_acl', 'users', 'user', 'username',111 ['autotest_system', 'debug_user'])112 def test_hosts(self):113 self.check_relationship('acls/my_acl', 'hosts', 'host', 'hostname',114 ['host1', 'host2'], length=9, check_number=2)115class HostTest(AfeResourceTestCase):116 def test_collection(self):117 response = self.request('get', 'hosts')118 self.check_collection(response, 'hostname', ['host1', 'host2'],119 length=9, check_number=2)120 host1 = self.sorted_by(response['members'], 'hostname')[0]121 self.assertEquals(host1['platform']['name'], 'myplatform')122 self.assertEquals(host1['locked'], False)123 self.assertEquals(host1['status'], 'Ready')124 def test_entry(self):125 response = self.request('get', 'hosts/host1')126 self.assertEquals(response['protection_level'], 'No protection')127 def test_labels(self):128 self.check_relationship('hosts/host1', 'labels', 'label', 'name',129 ['label1', 'myplatform'])130 def test_acls(self):131 self.check_relationship('hosts/host1', 'acls', 'acl', 'name',132 ['my_acl'])133 def test_queue_entries(self):134 self._create_job(hosts=[1])135 host = self.request('get', 'hosts/host1')136 entries = self.request('get', host['queue_entries']['href'])137 self.check_collection(entries, ['job', 'id'], [1])138 def test_health_tasks(self):139 models.SpecialTask.schedule_special_task(140 host=self.hosts[0], task=models.SpecialTask.Task.VERIFY)141 host = self.request('get', 'hosts/host1')142 tasks = self.request('get', host['health_tasks']['href'])143 self.check_collection(tasks, 'task_type', ['Verify'])144 def test_put(self):145 response = self.request('put', 'hosts/host1', data={'locked': True})146 self.assertEquals(response['locked'], True)147 response = self.request('get', 'hosts/host1')148 self.assertEquals(response['locked'], True)149 self.assertEquals(response['locked_by']['username'], 'debug_user')150 def test_post(self):151 data = {'hostname': 'newhost',152 'platform': {'href': self.URI_PREFIX + '/labels/myplatform'},153 'protection_level': 'Do not verify'}154 response = self.request('post', 'hosts', data=data)155 self.assertEquals(response, self.URI_PREFIX + '/hosts/newhost')156 host = models.Host.objects.get(hostname='newhost')157 self.assertEquals(host.platform().name, 'myplatform')158 self.assertEquals(host.protection, models.Host.Protection.DO_NOT_VERIFY)159 def _check_labels(self, host, expected_labels):160 label_names = sorted(label.name for label in host.labels.all())161 self.assertEquals(label_names, sorted(expected_labels))162 def test_add_label(self):163 labels_href = self.request('get', 'hosts/host1')['labels']['href']164 data = {'label': self.URI_PREFIX + '/labels/label2'}165 response = self.request('post', labels_href, data=data)166 self._check_labels(self.hosts[0], ['label1', 'label2', 'myplatform'])167 def test_remove_label(self):168 labels_href = self.request('get', 'hosts/host1')['labels']['href']169 labels_href += '&label=label1'170 labelings = self.request('get', labels_href)['members']171 self.assertEquals(len(labelings), 1)172 self.request('delete', labelings[0]['href'])173 self._check_labels(self.hosts[0], ['myplatform'])174 def test_delete(self):175 self.request('delete', 'hosts/host1')176 hosts = models.Host.valid_objects.filter(hostname='host1')177 self.assertEquals(len(hosts), 0)178class TestTest(AfeResourceTestCase): # yes, we're testing the "tests" resource179 def test_collection(self):180 response = self.request('get', 'tests')181 self.check_collection(response, 'name', ['mytest'])182 def test_entry(self):183 response = self.request('get', 'tests/mytest')184 self.assertEquals(response['name'], 'mytest')185 self.assertEquals(response['control_file_type'], 'Server')186 self.assertEquals(response['control_file_path'], '/path/to/mytest')187 def test_dependencies(self):188 models.Test.objects.get(name='mytest').dependency_labels = [self.label3]189 self.check_relationship('tests/mytest', 'dependencies', 'label', 'name',190 ['label3'])191class ExecutionInfoTest(AfeResourceTestCase):192 def setUp(self):193 super(ExecutionInfoTest, self).setUp()194 def mock_read_control_file(test):195 return self.CONTROL_FILE_CONTENTS196 self.god.stub_with(control_file, 'read_control_file',197 mock_read_control_file)198 def test_get(self):199 response = self.request('get', 'execution_info?tests=mytest')200 info = response['execution_info']201 self.assert_(self.CONTROL_FILE_CONTENTS in info['control_file'])202 self.assertEquals(info['is_server'], True)203 self.assertEquals(info['machines_per_execution'], 1)204class QueueEntriesRequestTest(AfeResourceTestCase):205 def test_get(self):206 response = self.request(207 'get',208 'queue_entries_request?hosts=host1,host2&meta_hosts=label1')209 # choose an arbitrary but consistent ordering to ease checking210 def entry_href(entry):211 if 'host' in entry:212 return entry['host']['href']213 return entry['meta_host']['href']214 entries = sorted(response['queue_entries'], key=entry_href)215 expected = [216 {'host': {'href': self.URI_PREFIX + '/hosts/host1'}},217 {'host': {'href': self.URI_PREFIX + '/hosts/host2'}},218 {'meta_host': {'href': self.URI_PREFIX + '/labels/label1'}}]219 self.assertEquals(entries, expected)220class JobTest(AfeResourceTestCase):221 def setUp(self):222 super(JobTest, self).setUp()223 for _ in xrange(2):224 self._create_job(hosts=[1, 2])225 job = models.Job.objects.get(id=1)226 job.control_file = self.CONTROL_FILE_CONTENTS227 job.save()228 models.JobKeyval.objects.create(job=job, key='mykey', value='myvalue')229 def test_collection(self):230 response = self.request('get', 'jobs')231 self.check_collection(response, 'id', [1, 2])232 def test_keyval_filtering(self):233 response = self.request('get', 'jobs?has_keyval=mykey=myvalue')234 self.check_collection(response, 'id', [1])235 def test_entry(self):236 response = self.request('get', 'jobs/1')237 self.assertEquals(response['id'], 1)238 self.assertEquals(response['name'], 'test')239 self.assertEquals(response['keyvals'], {'mykey': 'myvalue'})240 info = response['execution_info']241 self.assertEquals(info['control_file'], self.CONTROL_FILE_CONTENTS)242 self.assertEquals(info['is_server'], False)243 self.assertEquals(info['cleanup_before_job'], 'Never')244 self.assertEquals(info['cleanup_after_job'], 'Always')245 self.assertEquals(info['machines_per_execution'], 1)246 self.assertEquals(info['run_verify'], True)247 def test_queue_entries(self):248 job = self.request('get', 'jobs/1')249 entries = self.request('get', job['queue_entries']['href'])250 self.check_collection(entries, ['host', 'hostname'], ['host1', 'host2'])251 def _test_post_helper(self, owner):252 data = {'name': 'myjob',253 'execution_info': {'control_file': self.CONTROL_FILE_CONTENTS,254 'is_server': True},255 'owner': owner,256 'drone_set': models.DroneSet.default_drone_set_name(),257 'queue_entries':258 [{'host': {'href': self.URI_PREFIX + '/hosts/host1'}},259 {'host': {'href': self.URI_PREFIX + '/hosts/host2'}}]}260 response = self.request('post', 'jobs', data=data)261 self.assertEquals(response, self.URI_PREFIX + '/jobs/3')262 job = models.Job.objects.get(id=3)263 self.assertEquals(job.name, 'myjob')264 self.assertEquals(job.control_file, self.CONTROL_FILE_CONTENTS)...
test_hashing.py
Source:test_hashing.py
...12 return hash(x)13class BaseTest(TestCase):14 def setUp(self):15 self.cfunc = jit(nopython=True)(hash_usecase)16 def check_collection(self, values):17 cfunc = self.cfunc18 values = list(values)19 hashes = [cfunc(x) for x in values]20 for x in hashes:21 self.assertIsInstance(x, utils.INT_TYPES)22 def check_distribution(hashes):23 distinct = set(hashes)24 if len(distinct) < 0.95 * len(values):25 # Display hash collisions, for ease of debugging26 counter = defaultdict(list)27 for v, h in zip(values, hashes):28 counter[h].append(v)29 collisions = [(h, v) for h, v in counter.items()30 if len(v) > 1]31 collisions = "\n".join("%s: %s" % (h, v)32 for h, v in sorted(collisions))33 self.fail("too many hash collisions: \n%s" % collisions)34 check_distribution(hashes)35 def int_samples(self, typ=np.int64):36 for start in (0, -50, 60000, 1<<32):37 info = np.iinfo(typ)38 if not info.min <= start <= info.max:39 continue40 n = 10041 yield range(start, start + n)42 yield range(start, start + 100 * n, 100)43 yield range(start, start + 128 * n, 128)44 def float_samples(self, typ):45 info = np.finfo(typ)46 for start in (0, 10, info.max ** 0.5, info.max / 1000.0):47 n = 10048 min_step = max(info.tiny, start * info.resolution)49 for step in (1.2, min_step ** 0.5, min_step):50 if step < min_step:51 continue52 a = np.linspace(start, start + n * step, n)53 a = a.astype(typ)54 yield a55 yield -a56 yield a + a.mean()57 # Infs, nans, zeros58 a = typ([0.0, 0.5, -0.0, -1.0, float('inf'), -float('inf'), float('nan')])59 yield a60 def complex_samples(self, typ, float_ty):61 for real in self.float_samples(float_ty):62 for imag in self.float_samples(float_ty):63 # Ensure equal sizes64 real = real[:len(imag)]65 imag = imag[:len(real)]66 a = real + typ(1j) * imag67 yield a68class TestNumberHashing(BaseTest):69 """70 Test hashing of number types.71 """72 def check_ints(self, typ):73 def check_values(values):74 values = sorted(set(typ(x) for x in values))75 self.check_collection(values)76 for a in self.int_samples(typ):77 check_values(a)78 def check_floats(self, typ):79 for a in self.float_samples(typ):80 self.assertEqual(a.dtype, np.dtype(typ))81 self.check_collection(a)82 def check_complex(self, typ, float_ty):83 for a in self.complex_samples(typ, float_ty):84 self.assertEqual(a.dtype, np.dtype(typ))85 self.check_collection(a)86 @tag('important')87 def test_ints(self):88 self.check_ints(np.int8)89 self.check_ints(np.uint16)90 self.check_ints(np.int32)91 self.check_ints(np.uint64)92 @tag('important')93 def test_floats(self):94 self.check_floats(np.float32)95 self.check_floats(np.float64)96 @tag('important')97 def test_complex(self):98 self.check_complex(np.complex64, np.float32)99 self.check_complex(np.complex128, np.float64)100 def test_bool(self):101 self.check_collection([False, True])102class TestTupleHashing(BaseTest):103 """104 Test hashing of tuples.105 """106 def check_tuples(self, value_generator, split):107 for values in value_generator:108 tuples = [split(a) for a in values]109 self.check_collection(tuples)110 def test_homogenous_tuples(self):111 typ = np.uint64112 def split2(i):113 """114 Split i's bits into 2 integers.115 """116 i = typ(i)117 return (i & typ(0x5555555555555555),118 i & typ(0xaaaaaaaaaaaaaaaa),119 )120 def split3(i):121 """122 Split i's bits into 3 integers.123 """...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!