Best Python code snippet using localstack_python
reverse_tests.py
Source:reverse_tests.py
...26 ptr = PTR.objects.create(27 name=random_label(), ip_str=ip, ip_type='6'28 )29 return ptr30 def create_domain(self, name, ip_type=None, delegated=False):31 if ip_type is None:32 ip_type = '4'33 if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):34 pass35 else:36 name = ip_to_domain_name(name, ip_type=ip_type)37 d = Domain.objects.create(name=name, delegated=delegated)38 self.assertTrue(d.is_reverse)39 return d40 def setUp(self):41 Domain.objects.all().delete()42 self.arpa = self.create_domain(name='arpa')43 self.i_arpa = self.create_domain(name='in-addr.arpa')44 self.i6_arpa = self.create_domain(name='ip6.arpa')45 self.domain = create_fake_zone('foo.mozilla.com', suffix='')46 self.s = create_fake_host(hostname='mozilla.com')47 # Reverse Domain test functions48 def test_soa_validators(self):49 m = self.create_domain(name='8')50 m.save()51 f_m = self.create_domain(name='8.2')52 f_m.save()53 n_f_m = self.create_domain(name='8.2.3')54 n_f_m.save()55 b_m = self.create_domain(name='8.3')56 b_m.save()57 s = SOA(primary="ns1.foo.com", contact="asdf", description="test")58 s.save()59 f_m.soa = s60 f_m.save()61 b_m.soa = s62 self.assertRaises(ValidationError, b_m.save)63 n_f_m = Domain.objects.get(pk=n_f_m.pk) # Refresh object64 n_f_m.soa = s65 n_f_m.save()66 m.soa = s67 m.save()68 b_m = Domain.objects.get(pk=b_m.pk) # Refresh object69 b_m.soa = s70 b_m.save()71 m.soa = None72 self.assertRaises(ValidationError, m.save)73 s2 = SOA(primary="ns1.foo.com", contact="asdf", description="test2")74 s2.save()75 m.soa = s276 self.assertRaises(ValidationError, m.save)77 def test_2_soa_validators(self):78 s1, _ = SOA.objects.get_or_create(79 primary="ns1.foo.gaz", contact="hostmaster.foo",80 description="foo.gaz2")81 d, _ = Domain.objects.get_or_create(name="11.in-addr.arpa")82 d.soa = None83 d.save()84 d1, _ = Domain.objects.get_or_create(name="12.in-addr.arpa")85 d1.soa = s186 d1.save()87 def test_3_soa_validators(self):88 s1, _ = SOA.objects.get_or_create(89 primary="ns1.foo2.gaz", contact="hostmaster.foo",90 description="foo.gaz2")91 d, _ = Domain.objects.get_or_create(name="gaz")92 d.soa = s193 d.save()94 r, _ = Domain.objects.get_or_create(name='9.in-addr.arpa')95 r.soa = s196 self.assertRaises(ValidationError, r.save)97 def test_remove_reverse_domain(self):98 self.create_domain(name='127', ip_type='4').save()99 rd1 = self.create_domain(name='127.193', ip_type='4')100 rd1.save()101 rd1.__repr__()102 rd1.__str__()103 rd2 = self.create_domain(name='127.193.8', ip_type='4')104 rd2.save()105 rd2.__repr__()106 rd2.__str__()107 ip1 = self.add_ptr_ipv4('127.193.8.1')108 self.assertEqual(ip1.reverse_domain, rd2)109 ip2 = self.add_ptr_ipv4('127.193.8.2')110 self.assertEqual(ip2.reverse_domain, rd2)111 ip3 = self.add_ptr_ipv4('127.193.8.3')112 self.assertEqual(ip3.reverse_domain, rd2)113 ip4 = self.add_ptr_ipv4('127.193.8.4')114 self.assertEqual(ip4.reverse_domain, rd2)115 rd2.delete()116 ptr1 = PTR.objects.filter(117 ip_lower=int(ipaddr.IPv4Address('127.193.8.1')),118 ip_type='4'119 )[0]120 self.assertEqual(ptr1.reverse_domain, rd1)121 ptr2 = PTR.objects.filter(122 ip_lower=int(ipaddr.IPv4Address('127.193.8.2')),123 ip_type='4'124 )[0]125 self.assertEqual(ptr2.reverse_domain, rd1)126 ptr3 = PTR.objects.filter(127 ip_lower=int(ipaddr.IPv4Address('127.193.8.2')),128 ip_type='4'129 )[0]130 self.assertEqual(ptr3.reverse_domain, rd1)131 ptr4 = PTR.objects.filter(132 ip_lower=int(ipaddr.IPv4Address('127.193.8.3')),133 ip_type='4'134 )[0]135 self.assertEqual(ptr4.reverse_domain, rd1)136 def test_remove_reverse_domain_sreg(self):137 self.create_domain(name='127', ip_type='4').save()138 rd1 = self.create_domain(name='127.193', ip_type='4')139 rd1.save()140 rd1.__repr__()141 rd1.__str__()142 rd2 = self.create_domain(name='127.193.8', ip_type='4')143 rd2.save()144 rd2.__repr__()145 rd2.__str__()146 ip1 = self.add_sreg_ipv4('127.193.8.1')147 self.assertEqual(ip1.reverse_domain, rd2)148 ip2 = self.add_sreg_ipv4('127.193.8.2')149 self.assertEqual(ip2.reverse_domain, rd2)150 ip3 = self.add_sreg_ipv4('127.193.8.3')151 self.assertEqual(ip3.reverse_domain, rd2)152 ip4 = self.add_sreg_ipv4('127.193.8.4')153 self.assertEqual(ip4.reverse_domain, rd2)154 rd2.delete()155 ptr1 = StaticReg.objects.filter(156 ip_lower=int(ipaddr.IPv4Address('127.193.8.1')),157 ip_type='4'158 )[0]159 self.assertEqual(ptr1.reverse_domain, rd1)160 ptr2 = StaticReg.objects.filter(161 ip_lower=int(ipaddr.IPv4Address('127.193.8.2')),162 ip_type='4'163 )[0]164 self.assertEqual(ptr2.reverse_domain, rd1)165 ptr3 = StaticReg.objects.filter(166 ip_lower=int(ipaddr.IPv4Address('127.193.8.2')),167 ip_type='4'168 )[0]169 self.assertEqual(ptr3.reverse_domain, rd1)170 ptr4 = StaticReg.objects.filter(171 ip_lower=int(ipaddr.IPv4Address('127.193.8.3')),172 ip_type='4'173 )[0]174 self.assertEqual(ptr4.reverse_domain, rd1)175 def do_generic_invalid_operation(self, data, exception, function):176 e = None177 try:178 function(**data)179 except exception, e:180 pass181 self.assertEqual(exception, type(e))182 def test_bad_nibble(self):183 bad_data = {'addr': "asdfas"}184 self.do_generic_invalid_operation(bad_data, ValidationError, nibbilize)185 bad_data = {'addr': 12341245}186 self.do_generic_invalid_operation(bad_data, ValidationError, nibbilize)187 bad_data = {'addr': "123.123.123.123"}188 self.do_generic_invalid_operation(bad_data, ValidationError, nibbilize)189 bad_data = {'addr': True}190 self.do_generic_invalid_operation(bad_data, ValidationError, nibbilize)191 bad_data = {'addr': False}192 self.do_generic_invalid_operation(bad_data, ValidationError, nibbilize)193 def test_remove_invalid_reverse_domain(self):194 rd1 = self.create_domain(name='130', ip_type='4')195 rd1.save()196 rd2 = self.create_domain(name='130.193', ip_type='4')197 rd2.save()198 rd3 = self.create_domain(name='130.193.8', ip_type='4')199 rd3.save()200 try:201 rd1.delete()202 except ValidationError, e:203 pass204 self.assertEqual(ValidationError, type(e))205 def test_master_domain(self):206 rd1 = self.create_domain(name='128', ip_type='4')207 rd1.save()208 rd2 = self.create_domain(name='128.193', ip_type='4')209 rd2.save()210 rd3 = self.create_domain(name='128.193.8', ip_type='4')211 rd3.save()212 self.assertEqual(rd3.master_domain, rd2)213 self.assertEqual(rd2.master_domain, rd1)214 self.assertEqual(rd1.master_domain, self.i_arpa)215 def test_add_reverse_domains(self):216 self.assertRaises(217 ValidationError, self.create_domain,218 **{'name': '192.168', 'ip_type': '4'}219 )220 rdx = self.create_domain(name='192', ip_type='4')221 rdx.save()222 rdy = self.create_domain(name='192.168', ip_type='4')223 rdy.save()224 self.assertRaises(225 ValidationError, self.create_domain,226 **{'name': '192.168', 'ip_type': '4'}227 )228 self.create_domain(name='128', ip_type='4').save()229 rd0 = self.create_domain(name='128.193', ip_type='4')230 rd0.save()231 ip1 = self.add_ptr_ipv4('128.193.8.1')232 self.assertEqual(ip1.reverse_domain, rd0)233 ip2 = self.add_ptr_ipv4('128.193.8.2')234 self.assertEqual(ip2.reverse_domain, rd0)235 ip3 = self.add_ptr_ipv4('128.193.8.3')236 self.assertEqual(ip3.reverse_domain, rd0)237 ip4 = self.add_ptr_ipv4('128.193.8.4')238 self.assertEqual(ip4.reverse_domain, rd0)239 rd1 = self.create_domain(name='128.193.8', ip_type='4')240 rd1.save()241 ptr1 = PTR.objects.filter(ip_lower=ipaddr.IPv4Address(242 '128.193.8.1').__int__(), ip_type='4')[0]243 self.assertEqual(ptr1.reverse_domain, rd1)244 ptr2 = PTR.objects.filter(ip_lower=ipaddr.IPv4Address(245 '128.193.8.2').__int__(), ip_type='4')[0]246 self.assertEqual(ptr2.reverse_domain, rd1)247 ptr3 = PTR.objects.filter(ip_lower=ipaddr.IPv4Address(248 '128.193.8.3').__int__(), ip_type='4')[0]249 self.assertEqual(ptr3.reverse_domain, rd1)250 ptr4 = PTR.objects.filter(ip_lower=ipaddr.IPv4Address(251 '128.193.8.4').__int__(), ip_type='4')[0]252 self.assertEqual(ptr4.reverse_domain, rd1)253 rd1.delete()254 ptr1 = PTR.objects.filter(ip_lower=ipaddr.IPv4Address(255 '128.193.8.1').__int__(), ip_type='4')[0]256 self.assertEqual(ptr1.reverse_domain, rd0)257 ptr2 = PTR.objects.filter(ip_lower=ipaddr.IPv4Address(258 '128.193.8.2').__int__(), ip_type='4')[0]259 self.assertEqual(ptr2.reverse_domain, rd0)260 ptr3 = PTR.objects.filter(ip_lower=ipaddr.IPv4Address(261 '128.193.8.2').__int__(), ip_type='4')[0]262 self.assertEqual(ptr3.reverse_domain, rd0)263 ptr4 = PTR.objects.filter(ip_lower=ipaddr.IPv4Address(264 '128.193.8.3').__int__(), ip_type='4')[0]265 self.assertEqual(ptr4.reverse_domain, rd0)266 def test_boot_strap_add_ipv6_domain(self):267 osu_block = "2.6.2.1.1.0.5.F.0.0.0"268 test_dname = osu_block + ".d.e.a.d.b.e.e.f"269 boot_strap_ipv6_reverse_domain(test_dname)270 self.assertRaises(271 self.create_domain,272 **{'name': '2.6.2.1.1.0.5.f.0.0.0', 'ip_type': '6'}273 )274 self.assertRaises(275 ValidationError, self.create_domain,276 **{'name': '2.6.2.1', 'ip_type': '6'}277 )278 self.assertRaises(279 self.create_domain,280 **{'name': '2.6.2.1.1.0.5.F.0.0.0.d.e.a.d', 'ip_type': '6'}281 )282 self.assertRaises(283 self.create_domain,284 **{'name': '2.6.2.1.1.0.5.F.0.0.0.d.e.a.d.b.e.e.f',285 'ip_type': '6'}286 )287 self.assertRaises(288 ValidationError, self.create_domain,289 **{'name': test_dname, 'ip_type': '6'}290 )291 # These should pass292 boot_strap_ipv6_reverse_domain('7.6.2.4')293 boot_strap_ipv6_reverse_domain('6.6.2.5.1')294 # These are pretty unrealistic since they prodtrude into the host part295 # of the address.296 boot_strap_ipv6_reverse_domain(297 '4.6.2.2.1.0.5.3.f.0.0.0.1.2.3.4.1.2.3.4.1.2.3.4.1.2.3.4.1.2.3.4')298 boot_strap_ipv6_reverse_domain(299 '5.6.2.3.1.0.5.3.f.0.0.0.1.2.3.4.1.2.3.4.1.2.3.4')300 def test_add_reverse_domainless_ips(self):301 e = None302 try:303 self.add_ptr_ipv4('8.8.8.8')304 except ValidationError, e:305 pass306 self.assertEqual(ValidationError, type(e))307 e = None308 try:309 self.add_ptr_ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:733')310 except ValidationError, e:311 pass312 self.assertEqual(ValidationError, type(e))313 e = None314 boot_strap_ipv6_reverse_domain("2.0.0.1")315 try:316 self.create_domain(name='2.0.0.1', ip_type='6').save()317 except ValidationError, e:318 pass319 self.assertEqual(ValidationError, type(e))320 e = None321 self.add_ptr_ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:733')322 def test_ipv6_to_longs(self):323 ip = ipaddr.IPv6Address('2001:0db8:85a3:0000:0000:8a2e:0370:733')324 ret = ipv6_to_longs(ip.__str__())325 self.assertEqual(ret, (2306139570357600256, 151930230802227))326 def test_bad_names(self):327 name = None328 self.assertRaises(ValidationError, self.create_domain,329 **{'name': name, 'ip_type': '6'})330 name = 124331 self.assertRaises(ValidationError, self.create_domain,332 **{'name': name, 'ip_type': '6'})333 name = "0.9.0"334 ip_type = "asdf"335 self.assertRaises(ValidationError, self.create_domain,336 **{'name': name, 'ip_type': ip_type})337 ip_type = None338 self.assertRaises(ValidationError, self.create_domain,339 **{'name': name, 'ip_type': ip_type})340 ip_type = 1234341 self.assertRaises(ValidationError, self.create_domain,342 **{'name': name, 'ip_type': ip_type})343 def test_add_remove_reverse_ipv6_domains(self):344 osu_block = "2620:105:F000"345 rd0 = boot_strap_ipv6_reverse_domain("2.6.2.0.0.1.0.5.f.0.0.0")346 ip1 = self.add_ptr_ipv6(osu_block + ":8000::1")347 self.assertEqual(ip1.reverse_domain, rd0)348 ip2 = self.add_ptr_ipv6(osu_block + ":8000::2")349 self.assertEqual(ip2.reverse_domain, rd0)350 ip3 = self.add_ptr_ipv6(osu_block + ":8000::3")351 self.assertEqual(ip3.reverse_domain, rd0)352 self.add_ptr_ipv6(osu_block + ":8000::4")353 rd1 = self.create_domain(name="2.6.2.0.0.1.0.5.f.0.0.0.8", ip_type='6')354 rd1.save()355 ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::1")356 ptr1 = PTR.objects.filter(357 ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]358 self.assertEqual(ptr1.reverse_domain, rd1)359 ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::2")360 ptr2 = PTR.objects.filter(361 ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]362 self.assertEqual(ptr2.reverse_domain, rd1)363 ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::3")364 ptr3 = PTR.objects.filter(365 ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]366 self.assertEqual(ptr3.reverse_domain, rd1)367 ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::4")368 ptr4 = PTR.objects.filter(369 ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]370 self.assertEqual(ptr4.reverse_domain, rd1)371 rd1.delete()372 ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::1")373 ptr1 = PTR.objects.filter(374 ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]375 self.assertEqual(ptr1.reverse_domain, rd0)376 ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::2")377 ptr2 = PTR.objects.filter(378 ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]379 self.assertEqual(ptr2.reverse_domain, rd0)380 ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::3")381 ptr3 = PTR.objects.filter(382 ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]383 self.assertEqual(ptr3.reverse_domain, rd0)384 ip_upper, ip_lower = ipv6_to_longs(osu_block + ":8000::4")385 ptr4 = PTR.objects.filter(386 ip_upper=ip_upper, ip_lower=ip_lower, ip_type='6')[0]387 self.assertEqual(ptr4.reverse_domain, rd0)388 def test_master_reverse_ipv6_domains(self):389 rds = []390 rd = self.create_domain(name='1', ip_type='6')391 rd.save()392 rds.append(rd)393 rd = self.create_domain(name='1.2', ip_type='6')394 rd.save()395 rds.append(rd)396 rd = self.create_domain(name='1.2.8', ip_type='6')397 rd.save()398 rds.append(rd)399 rd = self.create_domain(name='1.2.8.3', ip_type='6')400 rd.save()401 rds.append(rd)402 rd = self.create_domain(name='1.2.8.3.0', ip_type='6')403 rd.save()404 rds.append(rd)405 rd = self.create_domain(name='1.2.8.3.0.0', ip_type='6')406 rd.save()407 rds.append(rd)408 rd = self.create_domain(name='1.2.8.3.0.0.0', ip_type='6')409 rd.save()410 rds.append(rd)411 rd = self.create_domain(name='1.2.8.3.0.0.0.0', ip_type='6')412 rd.save()413 rds.append(rd)414 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4', ip_type='6')415 rd.save()416 rds.append(rd)417 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3', ip_type='6')418 rd.save()419 rds.append(rd)420 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4', ip_type='6')421 rd.save()422 rds.append(rd)423 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5', ip_type='6')424 rd.save()425 rds.append(rd)426 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6', ip_type='6')427 rd.save()428 rds.append(rd)429 rd = self.create_domain(430 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6', ip_type='6')431 rd.save()432 rds.append(rd)433 rd = self.create_domain(434 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5', ip_type='6')435 rd.save()436 rds.append(rd)437 rd = self.create_domain(438 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6', ip_type='6')439 rd.save()440 rds.append(rd)441 rd = self.create_domain(442 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7', ip_type='6')443 rd.save()444 rds.append(rd)445 rd = self.create_domain(446 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0', ip_type='6')447 rd.save()448 rds.append(rd)449 rd = self.create_domain(450 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0', ip_type='6')451 rd.save()452 rds.append(rd)453 rd = self.create_domain(454 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0', ip_type='6')455 rd.save()456 rds.append(rd)457 rd = self.create_domain(458 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0.0', ip_type='6')459 rd.save()460 rds.append(rd)461 rd = self.create_domain(462 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0.0.0', ip_type='6')463 rd.save()464 rds.append(rd)465 rd = self.create_domain(466 name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0.0.0.0', ip_type='6')467 rd.save()468 rds.append(rd)469 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0'470 '.0.0.0.0', ip_type='6')471 rd.save()472 rds.append(rd)473 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0'474 '.0.0.0.0.0', ip_type='6')475 rd.save()476 rds.append(rd)477 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0'478 '.0.0.0.0.0.0', ip_type='6')479 rd.save()480 rds.append(rd)481 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0'482 '.0.0.0.0.0.0.0', ip_type='6')483 rd.save()484 rds.append(rd)485 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0'486 '.0.0.0.0.0.0.0.0', ip_type='6')487 rd.save()488 rds.append(rd)489 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0'490 '.0.0.0.0.0.0.0.0.0', ip_type='6')491 rd.save()492 rds.append(rd)493 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0'494 '.0.0.0.0.0.0.0.0.0.3', ip_type='6')495 rd.save()496 rds.append(rd)497 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0'498 '.0.0.0.0.0.0.0.0.0.3.2', ip_type='6')499 rd.save()500 rds.append(rd)501 rd = self.create_domain(name='1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0.0'502 '.0.0.0.0.0.0.0.0.0.3.2.1', ip_type='6')503 rd.save()504 rds.append(rd)505 for rd in list(enumerate(rds)):506 if rd[0] == 0:507 self.assertEqual(rd[1].master_domain, self.i6_arpa)508 else:509 self.assertEqual(rd[1].master_domain, rds[rd[0] - 1])510 self.assertTrue(rd[1].get_absolute_url())511 self.assertTrue(rd[1].get_edit_url())512 self.assertTrue(rd[1].get_delete_url())513 try:514 Domain.objects.filter(515 name=ip_to_domain_name('1.2.8.3.0.0.0.0.4.3.4.5.6.6.5.6.7.0.0',516 ip_type='6'))[0].delete()517 except ValidationError, e:518 pass519 self.assertEqual(ValidationError, type(e))520 def test_delegation_add_domain(self):521 dom = self.create_domain(name='3', delegated=True)522 dom.save()523 self.assertRaises(ValidationError, self.create_domain,...
test_instance.py
Source:test_instance.py
...25 attributes=self.attributes,26 class_vars=class_vars,27 metas=metas,28 variables=variables)29 def create_domain(self, attributes=(), classes=(), metas=()):30 attr_vars = [ContinuousVariable(name=a) if isinstance(a, str) else a31 for a in attributes]32 class_vars = [ContinuousVariable(name=c) if isinstance(c, str) else c33 for c in classes]34 meta_vars = [DiscreteVariable(name=m, values=map(str, range(5)))35 if isinstance(m, str) else m36 for m in metas]37 domain = Domain(attr_vars, class_vars, meta_vars)38 return domain39 def test_init_x_no_data(self):40 domain = self.mock_domain()41 inst = Instance(domain)42 self.assertIsInstance(inst, Instance)43 self.assertIs(inst.domain, domain)44 self.assertEqual(inst._x.shape, (len(self.attributes), ))45 self.assertEqual(inst._y.shape, (0, ))46 self.assertEqual(inst._metas.shape, (0, ))47 self.assertTrue(all(isnan(x) for x in inst._x))48 def test_init_xy_no_data(self):49 domain = self.mock_domain(with_classes=True)50 inst = Instance(domain)51 self.assertIsInstance(inst, Instance)52 self.assertIs(inst.domain, domain)53 self.assertEqual(inst._x.shape, (len(self.attributes), ))54 self.assertEqual(inst._y.shape, (len(self.class_vars), ))55 self.assertEqual(inst._metas.shape, (0, ))56 self.assertTrue(all(isnan(x) for x in inst._x))57 self.assertTrue(all(isnan(x) for x in inst._y))58 def test_init_xym_no_data(self):59 domain = self.mock_domain(with_classes=True, with_metas=True)60 inst = Instance(domain)61 self.assertIsInstance(inst, Instance)62 self.assertIs(inst.domain, domain)63 self.assertEqual(inst._x.shape, (len(self.attributes), ))64 self.assertEqual(inst._y.shape, (len(self.class_vars), ))65 self.assertEqual(inst._metas.shape, (3, ))66 self.assertTrue(all(isnan(x) for x in inst._x))67 self.assertTrue(all(isnan(x) for x in inst._y))68 assert_array_nanequal(inst._metas,69 np.array([var.Unknown for var in domain.metas],70 dtype=object))71 def test_init_x_arr(self):72 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")])73 vals = np.array([42, 0])74 inst = Instance(domain, vals)75 assert_array_equal(inst._x, vals)76 self.assertEqual(inst._y.shape, (0, ))77 self.assertEqual(inst._metas.shape, (0, ))78 domain = self.create_domain()79 inst = Instance(domain, np.empty((0,)))80 self.assertEqual(inst._x.shape, (0, ))81 self.assertEqual(inst._y.shape, (0, ))82 self.assertEqual(inst._metas.shape, (0, ))83 def test_init_x_list(self):84 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")])85 lst = [42, 0]86 vals = np.array(lst)87 inst = Instance(domain, vals)88 assert_array_equal(inst._x, vals)89 self.assertEqual(inst._y.shape, (0, ))90 self.assertEqual(inst._metas.shape, (0, ))91 domain = self.create_domain()92 inst = Instance(domain, [])93 self.assertEqual(inst._x.shape, (0, ))94 self.assertEqual(inst._y.shape, (0, ))95 self.assertEqual(inst._metas.shape, (0, ))96 def test_init_xy_arr(self):97 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],98 [DiscreteVariable("y", values="ABC")])99 vals = np.array([42, 0, 1])100 inst = Instance(domain, vals)101 assert_array_equal(inst._x, vals[:2])102 self.assertEqual(inst._y.shape, (1, ))103 self.assertEqual(inst._y[0], 1)104 self.assertEqual(inst._metas.shape, (0, ))105 def test_init_xy_list(self):106 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],107 [DiscreteVariable("y", values="ABC")])108 vals = np.array([42, 0, 2])109 inst = Instance(domain, vals)110 assert_array_equal(inst._x, vals[:2])111 self.assertEqual(inst._y.shape, (1, ))112 self.assertEqual(inst._y[0], 2)113 self.assertEqual(inst._metas.shape, (0, ))114 def test_init_xym(self):115 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],116 [DiscreteVariable("y", values="ABC")],117 self.metas)118 for val in (np.array([42, "M", "B", "X", 43, "Foo"], dtype=object),119 [42, "M", "B", "X", 43, "Foo"]):120 inst = Instance(domain, val)121 self.assertIsInstance(inst, Instance)122 self.assertIs(inst.domain, domain)123 self.assertEqual(inst._x.shape, (2,))124 self.assertEqual(inst._y.shape, (1,))125 self.assertEqual(inst._metas.shape, (3,))126 assert_array_equal(inst._x, np.array([42, 0]))127 self.assertEqual(inst._y[0], 1)128 assert_array_equal(inst._metas, np.array([0, 43, "Foo"], dtype=object))129 def test_init_inst(self):130 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],131 [DiscreteVariable("y", values="ABC")],132 self.metas)133 vals = [42, "M", "B", "X", 43, "Foo"]134 inst = Instance(domain, vals)135 inst2 = Instance(domain, inst)136 assert_array_equal(inst2._x, np.array([42, 0]))137 self.assertEqual(inst2._y[0], 1)138 assert_array_equal(inst2._metas, np.array([0, 43, "Foo"], dtype=object))139 domain2 = self.create_domain(["z", domain[1], self.metas[1]],140 domain.class_vars,141 [self.metas[0], "w", domain[0]])142 inst2 = Instance(domain2, inst)143 assert_array_nanequal(inst2._x, np.array([Unknown, 0, 43]))144 self.assertEqual(inst2._y[0], 1)145 assert_array_nanequal(inst2._metas, np.array([0, Unknown, 42],146 dtype=object))147 def test_get_item(self):148 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],149 [DiscreteVariable("y", values="ABC")],150 self.metas)151 vals = [42, "M", "B", "X", 43, "Foo"]152 inst = Instance(domain, vals)153 for idx_int, idx_name, idx_var, value in ((0, "x", domain[0], 42),154 (1, "g", domain[1], "M"),155 (2, "y", domain.class_var, "B"),156 (-2, "Meta 2", self.metas[1], 43)):157 val = inst[idx_int]158 self.assertIsInstance(val, Value)159 self.assertEqual(inst[idx_int], value)160 self.assertEqual(inst[idx_name], value)161 self.assertEqual(inst[idx_var], value)162 with self.assertRaises(ValueError):163 inst["asdf"] = 42164 with self.assertRaises(ValueError):165 inst[ContinuousVariable("asdf")] = 42166 def test_list(self):167 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],168 [DiscreteVariable("y", values="ABC")],169 self.metas)170 vals = [42, "M", "B", "X", 43, "Foo"]171 inst = Instance(domain, vals)172 l = inst.list173 self.assertIsInstance(l, list)174 self.assertEqual(l, [42, "M", "B", "X", 43, "Foo"])175 self.assertGreater(len(l), len(inst))176 self.assertEqual(len(l), 6)177 def test_set_item(self):178 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],179 [DiscreteVariable("y", values="ABC")],180 self.metas)181 vals = [42, "M", "B", "X", 43, "Foo"]182 inst = Instance(domain, vals)183 for idx1, idx2, val in ((0, 0, 42),184 ("x", 0, 44),185 (1, 1, "F"),186 ("g", 1, "M"),187 (2, 2, "C"),188 ("y", 2, "A"),189 (domain.class_var, 2, "B"),190 (-1, -1, "Y"),191 ("Meta 1", -1, "Z"),192 (domain.metas[0], -1, "X")):193 inst[idx1] = val194 self.assertEqual(inst[idx2], val)195 with self.assertRaises(ValueError):196 inst[1] = "N"197 with self.assertRaises(ValueError):198 inst["asdf"] = 42199 def test_str(self):200 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")])201 inst = Instance(domain, [42, 0])202 self.assertEqual(str(inst), "[42, M]")203 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],204 [DiscreteVariable("y", values="ABC")])205 inst = Instance(domain, [42, "M", "B"])206 self.assertEqual(str(inst), "[42, M | B]")207 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],208 [DiscreteVariable("y", values="ABC")],209 self.metas)210 inst = Instance(domain, [42, "M", "B", "X", 43, "Foo"])211 self.assertEqual(str(inst), "[42, M | B] {X, 43, Foo}")212 domain = self.create_domain([],213 [DiscreteVariable("y", values="ABC")],214 self.metas)215 inst = Instance(domain, ["B", "X", 43, "Foo"])216 self.assertEqual(str(inst), "[ | B] {X, 43, Foo}")217 domain = self.create_domain([],218 [],219 self.metas)220 inst = Instance(domain, ["X", 43, "Foo"])221 self.assertEqual(str(inst), "[] {X, 43, Foo}")222 domain = self.create_domain(self.attributes)223 inst = Instance(domain, range(len(self.attributes)))224 self.assertEqual(225 str(inst),226 "[{}]".format(", ".join(f"{x:g}"227 for x in range(len(self.attributes)))))228 for attr in domain.variables:229 attr.number_of_decimals = 0230 self.assertEqual(231 str(inst),232 "[{}]".format(", ".join("{}".format(x)233 for x in range(len(self.attributes)))))234 def test_repr(self):235 domain = self.create_domain(self.attributes)236 inst = Instance(domain, range(len(self.attributes)))237 self.assertEqual(repr(inst), "[0, 1, 2, 3, 4, ...]")238 for attr in domain.variables:239 attr.number_of_decimals = 3240 self.assertEqual(repr(inst), "[0.000, 1.000, 2.000, 3.000, 4.000, ...]")241 for attr in domain.variables:242 attr.number_of_decimals = 0243 self.assertEqual(repr(inst), "[0, 1, 2, 3, 4, ...]")244 def test_eq(self):245 domain = self.create_domain(["x", DiscreteVariable("g", values="MF")],246 [DiscreteVariable("y", values="ABC")],247 self.metas)248 vals = [42, "M", "B", "X", 43, "Foo"]249 inst = Instance(domain, vals)250 inst2 = Instance(domain, vals)251 self.assertEqual(inst, inst2)252 self.assertEqual(inst2, inst)253 inst2[0] = 43254 self.assertNotEqual(inst, inst2)255 inst2[0] = Unknown256 self.assertNotEqual(inst, inst2)257 for index, val in ((2, "C"), (-1, "Y"), (-2, "33"), (-3, "Bar")):258 inst2 = Instance(domain, vals)259 inst2[index] = val260 self.assertNotEqual(inst, inst2)261 def test_instance_id(self):262 domain = self.create_domain(["x"])263 vals = [42]264 inst = Instance(domain, vals, id=42)265 self.assertEqual(inst.id, 42)266 inst2 = Instance(domain, vals)267 inst3 = Instance(domain, vals)...
create_domains.py
Source:create_domains.py
...5import boto6conn = boto.connect_sdb()7#conn.delete_domain('test-comments')8print conn.get_all_domains()9#conn.create_domain('test-users')10#conn.create_domain('test-stations')11#conn.create_domain('test-contacts')12conn.create_domain('test-comments')...
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!!