Best Python code snippet using autotest_python
tests.py
Source:tests.py
2from .models import User, Article3class ChangesMixinBeforeAndCurrentTestCase(TestCase):4 def test_empty(self):5 user = User()6 self.assertDictContainsSubset({'id': None, 'name': ''}, user.old_state())7 self.assertDictContainsSubset({'id': None, 'name': ''}, user.previous_state())8 self.assertDictContainsSubset({'id': None, 'name': ''}, user.current_state())9 self.assertEqual({}, user.old_changes())10 self.assertEqual({}, user.changes())11 def test_new(self):12 user = User(name='Foo Bar')13 self.assertDictContainsSubset({'id': None, 'name': 'Foo Bar'}, user.old_state())14 self.assertDictContainsSubset({'id': None, 'name': 'Foo Bar'}, user.previous_state())15 self.assertDictContainsSubset({'id': None, 'name': 'Foo Bar'}, user.current_state())16 self.assertEqual({}, user.old_changes())17 self.assertEqual({}, user.changes())18 def test_change_from_new(self):19 user = User()20 user.name = 'Foo Bar'21 self.assertDictContainsSubset({'id': None, 'name': ''}, user.old_state())22 self.assertDictContainsSubset({'id': None, 'name': ''}, user.previous_state())23 self.assertDictContainsSubset({'id': None, 'name': 'Foo Bar'}, user.current_state())24 self.assertEqual({'name': ('', 'Foo Bar')}, user.old_changes())25 self.assertEqual({'name': ('', 'Foo Bar')}, user.changes())26 def test_change_from_db(self):27 user = User(name='Foo Bar')28 user.save()29 self.assertDictContainsSubset({'id': None, 'name': 'Foo Bar'}, user.old_state())30 self.assertDictContainsSubset({'id': 1, 'name': 'Foo Bar'}, user.previous_state())31 self.assertDictContainsSubset({'id': 1, 'name': 'Foo Bar'}, user.current_state())32 user = User.objects.filter(pk=user.pk)[0]33 user.name = 'My Real Name'34 self.assertDictContainsSubset({'id': 1, 'name': 'Foo Bar'}, user.old_state())35 self.assertDictContainsSubset({'id': 1, 'name': 'Foo Bar'}, user.previous_state())36 self.assertDictContainsSubset({'id': 1, 'name': 'My Real Name'}, user.current_state())37 self.assertEqual({'name': ('Foo Bar', 'My Real Name')}, user.old_changes())38 def test_save(self):39 user = User()40 user.name = 'Foo Bar'41 user.save()42 user.name = 'My Real Name'43 pk = user.pk44 self.assertDictContainsSubset({'id': None, 'name': ''}, user.old_state())45 self.assertDictContainsSubset({'id': pk, 'name': 'Foo Bar'}, user.previous_state())46 self.assertDictContainsSubset({'id': pk, 'name': 'My Real Name'}, user.current_state())47 self.assertDictEqual({'id': (None, pk), 'name': ('', 'My Real Name')}, user.old_changes())48 self.assertFalse(user.was_persisted())49 self.assertTrue(user.is_persisted())50 user.save()51 self.assertDictContainsSubset({'id': pk, 'name': 'Foo Bar'}, user.old_state())52 self.assertDictContainsSubset({'id': pk, 'name': 'My Real Name'}, user.previous_state())53 self.assertDictContainsSubset({'id': pk, 'name': 'My Real Name'}, user.current_state())54 self.assertEqual({'name': ('Foo Bar', 'My Real Name')}, user.old_changes())55 self.assertTrue(user.was_persisted())56 self.assertTrue(user.is_persisted())57 user.name = 'I Changed My Mind'58 user.save()59 self.assertDictContainsSubset({'id': pk, 'name': 'My Real Name'}, user.old_state())60 self.assertDictContainsSubset({'id': pk, 'name': 'I Changed My Mind'}, user.current_state())61 self.assertEqual({'name': ('My Real Name', 'I Changed My Mind')}, user.old_changes())62 self.assertTrue(user.was_persisted())63 self.assertTrue(user.is_persisted())64 def test_new_is_was_persisted(self):65 user = User()66 self.assertFalse(user.was_persisted())67 self.assertFalse(user.is_persisted())68 user.save()69 self.assertFalse(user.was_persisted())70 self.assertTrue(user.is_persisted())71 user.delete()72 self.assertTrue(user.was_persisted())73 self.assertFalse(user.is_persisted())74 user.save()75 self.assertFalse(user.was_persisted())76 self.assertTrue(user.is_persisted())77 user.delete()78 self.assertTrue(user.was_persisted())79 self.assertFalse(user.is_persisted())80 def test_foreign_key(self):81 me = User()82 me.save()83 you = User()84 you.save()85 article = Article(title='Hello World', user=me)86 self.assertDictContainsSubset({'id': None, 'user': me}, article.old_state())87 self.assertDictContainsSubset({'id': None, 'user': me}, article.previous_state())88 self.assertDictContainsSubset({'id': None, 'user': me}, article.current_state())89 article.save()90 self.assertDictContainsSubset({'id': None, 'user': me}, article.old_state())91 self.assertDictContainsSubset({'id': article.pk, 'user': me}, article.previous_state())92 self.assertDictContainsSubset({'id': article.pk, 'user': me}, article.current_state())93 article.user = you94 self.assertDictContainsSubset({'id': None, 'user': me}, article.old_state())95 self.assertDictContainsSubset({'id': article.pk, 'user': me}, article.previous_state())96 self.assertDictContainsSubset({'id': article.pk, 'user': you}, article.current_state())97 article.save()98 self.assertDictContainsSubset({'id': article.pk, 'user': me}, article.old_state())99 self.assertDictContainsSubset({'id': article.pk, 'user': you}, article.previous_state())100 self.assertDictContainsSubset({'id': article.pk, 'user': you}, article.current_state())101 def test_foreign_key_id(self):102 me = User()103 me.save()104 you = User()105 you.save()106 article = Article(title='Hello World', user_id=me.id)107 self.assertDictContainsSubset({'id': None, 'user_id': me.pk}, article.old_state())108 self.assertDictContainsSubset({'id': None, 'user_id': me.pk}, article.previous_state())109 self.assertDictContainsSubset({'id': None, 'user_id': me.pk}, article.current_state())110 article.save()111 self.assertDictContainsSubset({'id': None, 'user_id': me.pk}, article.old_state())112 self.assertDictContainsSubset({'id': article.pk, 'user_id': me.pk}, article.previous_state())113 self.assertDictContainsSubset({'id': article.pk, 'user_id': me.pk}, article.current_state())114 article.user = you115 self.assertDictContainsSubset({'id': None, 'user_id': me.pk}, article.old_state())116 self.assertDictContainsSubset({'id': article.pk, 'user_id': me.pk}, article.previous_state())117 self.assertDictContainsSubset({'id': article.pk, 'user_id': you.pk}, article.current_state())118 article.save()119 self.assertDictContainsSubset({'id': article.pk, 'user_id': me.pk}, article.old_state())120 self.assertDictContainsSubset({'id': article.pk, 'user_id': you.pk}, article.previous_state())...
test_export.py
Source:test_export.py
...34 self.assertEqual('application/json', resp['Content-Type'])35 result = resp.json()36 # verify data37 self.assertEqual(2, len(result))38 self.assertDictContainsSubset({'name': 'A.01', 'node_type': 'Rack'}, result[0])39 self.assertDictContainsSubset({'node_type': 'ODF', 'name': 'NI-TEST-ODF-02'}, result[1])40 # Check racked equipment41 rack_equp = result[0]['children']42 self.assertEqual(2, len(rack_equp))43 odf1_result = rack_equp[0]44 self.assertDictContainsSubset({'node_type': 'ODF', 'name': 'NI-TEST-ODF-01', 'max_ports': 24}, odf1_result)45 # Check ODF ports46 odf1_ports = odf1_result['children']47 self.assertEqual(3, len(odf1_ports))48 self.assertDictContainsSubset({'node_type': 'Port', 'name': '1', 'port_type': ''}, odf1_ports[0])49 self.assertDictContainsSubset({'node_type': 'Port', 'name': '2', 'description': ''}, odf1_ports[1])50 self.assertDictContainsSubset({'node_type': 'Port', 'name': '3'}, odf1_ports[2])51 # Check router52 router = rack_equp[1]53 self.assertDictContainsSubset({'node_type': 'Router', 'name': 'ni-test.routers.nordu.net'}, router)54 def test_decommissioned_equipment(self):55 self.skipTest('Not working as expected yet.')56 # Setup test data57 site = helpers.create_unique_node_handle(self.user, "Test site", "site", "Location")58 site_node = site.get_node()59 rack = helpers.get_generic_node_handle(self.user, "A.01", "rack", "Location")60 helpers.set_has(self.user, site_node, rack.handle_id)61 odf = helpers.create_unique_node_handle(self.user, "NI-TEST-ODF-01", "odf", "Physical")62 odf_node = odf.get_node()63 helpers.dict_update_node(self.user, odf.handle_id, {"max_ports": 24})64 helpers.set_location(self.user, odf_node, rack.handle_id)65 decom_on = helpers.create_unique_node_handle(self.user, "NI-TEST-ON-01", "optical-node", "Physical")66 decom_on_node = decom_on.get_node()67 helpers.dict_update_node(self.user, decom_on.handle_id, {"operational_state": "Decommissioned"})68 helpers.set_location(self.user, decom_on_node, rack.handle_id)69 helpers.create_port(decom_on_node, "1", self.user)70 helpers.create_port(decom_on_node, "2", self.user)71 # End test data72 resp = self.client.get(self.get_full_url(site) + "export")73 self.assertEqual('application/json', resp['Content-Type'])74 result = resp.json()75 # verify data76 self.assertEqual(1, len(result))77 rack_result = result[0]78 self.assertDictContainsSubset({'name': 'A.01', 'node_type': 'Rack'}, rack_result)79 rack_equip = rack_result['children']80 # Decommissioned equipment should be gone81 self.assertEqual(1, len(rack_equip))82 self.assertDictContainsSubset({'node_type': 'ODF', 'name': 'NI-TEST-ODF-01'}, rack_equip[0])83 self.assertEqual(2, len(rack_equip[0]['children']))84 def test_export_optical_node(self):85 # Setup test data86 site = helpers.create_unique_node_handle(self.user, "Test site", "site", "Location")87 optical_node = helpers.create_unique_node_handle(self.user, "NI-TEST-ROADM", "optical-node", "Physical")88 on_node = optical_node.get_node()89 helpers.set_location(self.user, on_node, site.handle_id)90 helpers.dict_update_node(self.user, optical_node.handle_id, {"operational_state": "In service", "rack_units": "2", "type": "ciena6500"})91 # start testing92 resp = self.client.get(self.get_full_url(site) + "export")93 self.assertEqual('application/json', resp['Content-Type'])94 result = resp.json()95 # verify data96 self.assertEqual(1, len(result))97 on_result = result[0]98 self.assertEqual({u'name': u'NI-TEST-ROADM', u'node_type': u'Optical Node', u'type': u'ciena6500', u'rack_units': u'2', u'children': [], u'operational_state': u'In service'}, on_result)99 # Replace the now deprecated unittest assert100 def assertDictContainsSubset(self, what, superset):101 for k in what:102 self.assertIn(k, superset)...
validation.py
Source:validation.py
...10 def test_ng_length(self):11 first_name = self.dom('input[name=first_name]')12 self.assertEqual(len(first_name), 1)13 attrib = dict(first_name[0].attrib.items())14 self.assertDictContainsSubset({'ng-required': 'true'}, attrib)15 self.assertDictContainsSubset({'ng-minlength': '3'}, attrib)16 self.assertDictContainsSubset({'ng-maxlength': '20'}, attrib)17 lis = self.dom('label[for=id_first_name]').parent().next().children('ul.djng-form-errors > li')18 if django.VERSION[1] == 5:19 self.assertEqual(len(lis), 1)20 else:21 self.assertEqual(len(lis), 3)22 attrib = dict(lis[0].attrib.items())23 self.assertDictContainsSubset({'ng-show': 'form.first_name.$error.required'}, attrib)24 def test_type(self):25 email_field = self.dom('input[name=email]')26 self.assertEqual(len(email_field), 1)27 attrib = dict(email_field[0].attrib.items())28 self.assertNotIn('required', attrib)29 self.assertDictContainsSubset({'ng-model': 'email'}, attrib)30 if django.VERSION[1] == 5:31 self.assertDictContainsSubset({'type': 'text'}, attrib)32 else:33 self.assertDictContainsSubset({'type': 'email'}, attrib)34 def test_regex(self):35 last_name = self.dom('input[name=last_name]')36 self.assertEqual(len(last_name), 1)37 attrib = dict(last_name[0].attrib.items())38 self.assertDictContainsSubset({'ng-pattern': '/^[A-Z][a-z -]+/'}, attrib)39class NgFormValidationWithModelMixinTest(TestCase):40 def setUp(self):41 subscription_form = SubscriptionFormWithNgModel(scope_prefix='subscribe_data')42 self.dom = PyQuery(str(subscription_form))43 def test_ng_model(self):44 first_name = self.dom('input[name=first_name]')45 self.assertEqual(len(first_name), 1)46 attrib = dict(first_name[0].attrib.items())47 self.assertDictContainsSubset({'ng-model': 'subscribe_data.first_name'}, attrib)48 def test_decimal_field(self):49 weight = self.dom('input[name=weight]')50 self.assertEqual(len(weight), 1)51 attrib = dict(weight[0].attrib.items())52 if django.VERSION[1] == 5:53 self.assertDictContainsSubset({'type': 'text'}, attrib)54 else:55 self.assertDictContainsSubset({'type': 'number'}, attrib)56 self.assertDictContainsSubset({'min': '42'}, attrib)57 self.assertDictContainsSubset({'max': '95'}, attrib)58 self.assertDictContainsSubset({'ng-model': 'subscribe_data.weight'}, attrib)59 def test_float_field(self):60 height = self.dom('input[name=height]')61 self.assertEqual(len(height), 1)62 attrib = dict(height[0].attrib.items())63 if django.VERSION[1] == 5:64 self.assertDictContainsSubset({'type': 'text'}, attrib)65 else:66 self.assertDictContainsSubset({'type': 'number'}, attrib)67 self.assertDictContainsSubset({'min': '1.48'}, attrib)68 self.assertDictContainsSubset({'max': '1.95'}, attrib)...
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!!