Best Python code snippet using Testify_python
test_functional.py
Source:test_functional.py
1"""2Larger functional tests for fields and widgets.3"""4from __future__ import unicode_literals5from django import forms6from selectable.forms import AutoCompleteSelectField, AutoCompleteSelectMultipleField7from selectable.forms import AutoCompleteSelectWidget, AutoComboboxSelectWidget8from selectabletest.models import ManyThing, OtherThing, ThingLookup9from .base import BaseSelectableTestCase, parsed_inputs10__all__ = (11 'FuncAutoCompleteSelectTestCase',12 'FuncSelectModelChoiceTestCase',13 'FuncComboboxModelChoiceTestCase',14 'FuncManytoManyMultipleSelectTestCase',15 'FuncFormTestCase',16)17class OtherThingForm(forms.ModelForm):18 thing = AutoCompleteSelectField(lookup_class=ThingLookup)19 class Meta(object):20 model = OtherThing21 fields = ('name', 'thing',)22class FuncAutoCompleteSelectTestCase(BaseSelectableTestCase):23 def setUp(self):24 self.test_thing = self.create_thing()25 def test_valid_form(self):26 "Valid form using an AutoCompleteSelectField."27 data = {28 'name': self.get_random_string(),29 'thing_0': self.test_thing.name, # Text input30 'thing_1': self.test_thing.pk, # Hidden input31 }32 form = OtherThingForm(data=data)33 self.assertTrue(form.is_valid(), str(form.errors))34 def test_invalid_form_missing_selected_pk(self):35 "Invalid form using an AutoCompleteSelectField."36 data = {37 'name': self.get_random_string(),38 'thing_0': self.test_thing.name, # Text input39 'thing_1': '', # Hidden input40 }41 form = OtherThingForm(data=data)42 self.assertFalse(form.is_valid(), 'Form should not be valid')43 self.assertFalse('name' in form.errors)44 self.assertTrue('thing' in form.errors)45 def test_invalid_form_missing_name(self):46 "Invalid form using an AutoCompleteSelectField."47 data = {48 'name': '',49 'thing_0': self.test_thing.name, # Text input50 'thing_1': self.test_thing.pk, # Hidden input51 }52 form = OtherThingForm(data=data)53 self.assertFalse(form.is_valid(), 'Form should not be valid')54 self.assertTrue('name' in form.errors)55 self.assertFalse('thing' in form.errors)56 def test_invalid_but_still_selected(self):57 "Invalid form should keep selected item."58 data = {59 'name': '',60 'thing_0': self.test_thing.name, # Text input61 'thing_1': self.test_thing.pk, # Hidden input62 }63 form = OtherThingForm(data=data)64 self.assertFalse(form.is_valid(), 'Form should not be valid')65 rendered_form = form.as_p()66 inputs = parsed_inputs(rendered_form)67 # Selected text should be populated68 thing_0 = inputs['thing_0'][0]69 self.assertEqual(thing_0.attrs['value'], self.test_thing.name)70 # Selected pk should be populated71 thing_1 = inputs['thing_1'][0]72 self.assertEqual(int(thing_1.attrs['value']), self.test_thing.pk)73 def test_populate_from_model(self):74 "Populate from existing model."75 other_thing = OtherThing.objects.create(thing=self.test_thing, name='a')76 form = OtherThingForm(instance=other_thing)77 rendered_form = form.as_p()78 inputs = parsed_inputs(rendered_form)79 # Selected text should be populated80 thing_0 = inputs['thing_0'][0]81 self.assertEqual(thing_0.attrs['value'], self.test_thing.name)82 # Selected pk should be populated83 thing_1 = inputs['thing_1'][0]84 self.assertEqual(int(thing_1.attrs['value']), self.test_thing.pk)85class SelectWidgetForm(forms.ModelForm):86 class Meta(object):87 model = OtherThing88 fields = ('name', 'thing',)89 widgets = {90 'thing': AutoCompleteSelectWidget(lookup_class=ThingLookup)91 }92class FuncSelectModelChoiceTestCase(BaseSelectableTestCase):93 """94 Functional tests for AutoCompleteSelectWidget compatibility95 with a ModelChoiceField.96 """97 def setUp(self):98 self.test_thing = self.create_thing()99 def test_valid_form(self):100 "Valid form using an AutoCompleteSelectWidget."101 data = {102 'name': self.get_random_string(),103 'thing_0': self.test_thing.name, # Text input104 'thing_1': self.test_thing.pk, # Hidden input105 }106 form = SelectWidgetForm(data=data)107 self.assertTrue(form.is_valid(), str(form.errors))108 def test_missing_pk(self):109 "Invalid form (missing required pk) using an AutoCompleteSelectWidget."110 data = {111 'name': self.get_random_string(),112 'thing_0': self.test_thing.name, # Text input113 'thing_1': '', # Hidden input missing114 }115 form = SelectWidgetForm(data=data)116 self.assertFalse(form.is_valid())117 self.assertTrue('thing' in form.errors)118 def test_invalid_pk(self):119 "Invalid form (invalid pk value) using an AutoCompleteSelectWidget."120 data = {121 'name': self.get_random_string(),122 'thing_0': self.test_thing.name, # Text input123 'thing_1': 'XXX', # Hidden input doesn't match a PK124 }125 form = SelectWidgetForm(data=data)126 self.assertFalse(form.is_valid())127 self.assertTrue('thing' in form.errors)128 def test_post_compatibility(self):129 """130 If new items are not allowed then the original field131 name can be included in the POST with the selected id.132 """133 data = {134 'name': self.get_random_string(),135 'thing': self.test_thing.pk,136 }137 form = SelectWidgetForm(data=data)138 self.assertTrue(form.is_valid(), str(form.errors))139class ComboboxSelectWidgetForm(forms.ModelForm):140 class Meta(object):141 model = OtherThing142 fields = ('name', 'thing',)143 widgets = {144 'thing': AutoComboboxSelectWidget(lookup_class=ThingLookup)145 }146class FuncComboboxModelChoiceTestCase(BaseSelectableTestCase):147 """148 Functional tests for AutoComboboxSelectWidget compatibility149 with a ModelChoiceField.150 """151 def setUp(self):152 self.test_thing = self.create_thing()153 def test_valid_form(self):154 "Valid form using an AutoComboboxSelectWidget."155 data = {156 'name': self.get_random_string(),157 'thing_0': self.test_thing.name, # Text input158 'thing_1': self.test_thing.pk, # Hidden input159 }160 form = ComboboxSelectWidgetForm(data=data)161 self.assertTrue(form.is_valid(), str(form.errors))162 def test_missing_pk(self):163 "Invalid form (missing required pk) using an AutoComboboxSelectWidget."164 data = {165 'name': self.get_random_string(),166 'thing_0': self.test_thing.name, # Text input167 'thing_1': '', # Hidden input missing168 }169 form = ComboboxSelectWidgetForm(data=data)170 self.assertFalse(form.is_valid())171 self.assertTrue('thing' in form.errors)172 def test_invalid_pk(self):173 "Invalid form (invalid pk value) using an AutoComboboxSelectWidget."174 data = {175 'name': self.get_random_string(),176 'thing_0': self.test_thing.name, # Text input177 'thing_1': 'XXX', # Hidden input doesn't match a PK178 }179 form = ComboboxSelectWidgetForm(data=data)180 self.assertFalse(form.is_valid())181 self.assertTrue('thing' in form.errors)182 def test_post_compatibility(self):183 """184 If new items are not allowed then the original field185 name can be included in the POST with the selected id.186 """187 data = {188 'name': self.get_random_string(),189 'thing': self.test_thing.pk,190 }191 form = ComboboxSelectWidgetForm(data=data)192 self.assertTrue(form.is_valid(), str(form.errors))193class ManyThingForm(forms.ModelForm):194 things = AutoCompleteSelectMultipleField(lookup_class=ThingLookup)195 class Meta(object):196 model = ManyThing197 fields = ('name', 'things',)198class FuncManytoManyMultipleSelectTestCase(BaseSelectableTestCase):199 """200 Functional tests for AutoCompleteSelectMultipleField compatibility201 with a ManyToManyField.202 """203 def setUp(self):204 self.test_thing = self.create_thing()205 def test_valid_form(self):206 "Valid form using an AutoCompleteSelectMultipleField."207 data = {208 'name': self.get_random_string(),209 'things_0': '', # Text input210 'things_1': [self.test_thing.pk, ], # Hidden inputs211 }212 form = ManyThingForm(data=data)213 self.assertTrue(form.is_valid(), str(form.errors))214 def test_valid_save(self):215 "Saving data from a valid form."216 data = {217 'name': self.get_random_string(),218 'things_0': '', # Text input219 'things_1': [self.test_thing.pk, ], # Hidden inputs220 }221 form = ManyThingForm(data=data)222 manything = form.save()223 self.assertEqual(manything.name, data['name'])224 things = manything.things.all()225 self.assertEqual(things.count(), 1)226 self.assertTrue(self.test_thing in things)227 def test_not_required(self):228 "Valid form where many to many is not required."229 data = {230 'name': self.get_random_string(),231 'things_0': '', # Text input232 'things_1': [], # Hidden inputs233 }234 form = ManyThingForm(data=data)235 form.fields['things'].required = False236 self.assertTrue(form.is_valid(), str(form.errors))237 def test_not_required_save(self):238 "Saving data when many to many is not required."239 data = {240 'name': self.get_random_string(),241 'things_0': '', # Text input242 'things_1': [], # Hidden inputs243 }244 form = ManyThingForm(data=data)245 form.fields['things'].required = False246 manything = form.save()247 self.assertEqual(manything.name, data['name'])248 things = manything.things.all()249 self.assertEqual(things.count(), 0)250 def test_has_changed(self):251 "Populate intial data from a model."252 manything = ManyThing.objects.create(name='Foo')253 thing_1 = self.create_thing()254 manything.things.add(thing_1)255 data = {256 'name': manything.name,257 'things_0': '', # Text input258 'things_1': [thing_1.pk], # Hidden inputs259 }260 form = ManyThingForm(data=data, instance=manything)261 self.assertTrue(form.has_changed())262 def test_post_compatibility(self):263 """264 If new items are not allowed then the original field265 name can be included in the POST with the selected ids.266 """267 data = {268 'name': self.get_random_string(),269 'things': [self.test_thing.pk, ],270 }271 form = ManyThingForm(data=data)272 self.assertTrue(form.is_valid(), str(form.errors))273class SimpleForm(forms.Form):274 "Non-model form usage."275 thing = AutoCompleteSelectField(lookup_class=ThingLookup)276 new_thing = AutoCompleteSelectField(lookup_class=ThingLookup, allow_new=True)277 things = AutoCompleteSelectMultipleField(lookup_class=ThingLookup)278class FuncFormTestCase(BaseSelectableTestCase):279 """280 Functional tests for using AutoCompleteSelectField281 and AutoCompleteSelectMultipleField outside the context282 of a ModelForm.283 """284 def setUp(self):285 self.test_thing = self.create_thing()286 def test_blank_new_item(self):287 "Regression test for #91. new_thing is required but both are blank."288 data = {289 'thing_0': self.test_thing.name,290 'thing_1': self.test_thing.pk,291 'new_thing_0': '',292 'new_thing_1': '',293 'things_0': '',294 'things_1': [self.test_thing.pk, ]295 }296 form = SimpleForm(data=data)297 self.assertFalse(form.is_valid())298 self.assertTrue('new_thing' in form.errors)299 def test_has_changed_with_empty_permitted(self):300 """301 Regression test for #92. has_changed fails when there is no initial and302 allow_new=False.303 """304 data = {305 'thing_0': '',306 'thing_1': self.test_thing.pk,307 'new_thing_0': self.test_thing.name,308 'new_thing_1': self.test_thing.pk,309 'things_0': '',310 'things_1': [self.test_thing.pk, ]311 }312 form = SimpleForm(data=data, empty_permitted=True)313 self.assertTrue(form.has_changed())314 self.assertTrue(form.is_valid(), str(form.errors))315 def test_not_changed(self):316 """317 Regression test for #92. has_changed fails when there is no initial and318 allow_new=False.319 """320 data = {321 'thing_0': self.test_thing.name,322 'thing_1': self.test_thing.pk,323 'new_thing_0': self.test_thing.name,324 'new_thing_1': self.test_thing.pk,325 'things_0': '',326 'things_1': [self.test_thing.pk, ]327 }328 initial = {329 'thing': self.test_thing.pk,330 'new_thing': self.test_thing.pk,331 'things': [self.test_thing.pk, ]332 }333 form = SimpleForm(data=data, initial=initial)334 self.assertFalse(form.has_changed())335 self.assertTrue(form.is_valid(), str(form.errors))336 def test_not_changed_with_empty_permitted(self):337 """338 Regression test for #92. has_changed fails when there is no initial and339 allow_new=False.340 """341 data = {342 'thing_0': '',343 'thing_1': '',344 'new_thing_0': '',345 'new_thing_1': '',346 'things_0': '',347 'things_1': '',348 }349 initial = {350 'thing': '',351 'new_thing': '',352 'things': '',353 }354 form = SimpleForm(data=data, initial=initial, empty_permitted=True)355 self.assertFalse(form.has_changed(), str(form.changed_data))356 self.assertTrue(form.is_valid(), str(form.errors))357 def test_no_initial_with_empty_permitted(self):358 """359 If empty data is submitted and allowed with no initial then360 the form should not be seen as changed.361 """362 data = {363 'thing_0': '',364 'thing_1': '',365 'new_thing_0': '',366 'new_thing_1': '',367 'things_0': '',368 'things_1': '',369 }370 form = SimpleForm(data=data, empty_permitted=True)371 self.assertFalse(form.has_changed(), str(form.changed_data))372 self.assertTrue(form.is_valid(), str(form.errors))373 def test_no_data_with_empty_permitted(self):374 """375 If no data is submitted and allowed with no initial then376 the form should not be seen as changed.377 """378 form = SimpleForm(data={}, empty_permitted=True)379 self.assertFalse(form.has_changed(), str(form.changed_data))380 self.assertTrue(form.is_valid(), str(form.errors))381 def test_select_multiple_changed(self):382 """383 Detect changes for a multiple select input with and without384 initial data.385 """386 data = {387 'thing_0': '',388 'thing_1': '',389 'new_thing_0': '',390 'new_thing_1': '',391 'things_0': '',392 'things_1': [self.test_thing.pk, ]393 }394 form = SimpleForm(data=data)395 self.assertTrue(form.has_changed())396 self.assertTrue('things' in form.changed_data)397 initial = {398 'thing': '',399 'new_thing': '',400 'things': [self.test_thing.pk, ],401 }402 form = SimpleForm(data=data, initial=initial)403 self.assertFalse(form.has_changed(), str(form.changed_data))404 initial = {405 'thing': '',406 'new_thing': '',407 'things': [],408 }409 form = SimpleForm(data=data, initial=initial)410 self.assertTrue(form.has_changed())411 self.assertTrue('things' in form.changed_data)412 def test_single_select_changed(self):413 """414 Detect changes for a single select input with and without415 initial data.416 """417 data = {418 'thing_0': '',419 'thing_1': self.test_thing.pk,420 'new_thing_0': '',421 'new_thing_1': '',422 'things_0': '',423 'things_1': ''424 }425 form = SimpleForm(data=data)426 self.assertTrue(form.has_changed())427 self.assertTrue('thing' in form.changed_data)428 initial = {429 'thing': self.test_thing.pk,430 'new_thing': '',431 'things': '',432 }433 form = SimpleForm(data=data, initial=initial)434 self.assertFalse(form.has_changed(), str(form.changed_data))435 initial = {436 'thing': '',437 'new_thing': '',438 'things': '',439 }440 form = SimpleForm(data=data, initial=initial)441 self.assertTrue(form.has_changed())442 self.assertTrue('thing' in form.changed_data)443 def test_new_select_changed(self):444 """445 Detect changes for a single select input which allows new items446 with and without initial data.447 """448 data = {449 'thing_0': '',450 'thing_1': '',451 'new_thing_0': 'Foo',452 'new_thing_1': '',453 'things_0': '',454 'things_1': ''455 }456 form = SimpleForm(data=data)457 self.assertTrue(form.has_changed())458 self.assertTrue('new_thing' in form.changed_data)459 initial = {460 'thing': '',461 'new_thing': ['Foo', None],462 'things': '',463 }464 form = SimpleForm(data=data, initial=initial)465 self.assertFalse(form.has_changed(), str(form.changed_data))466 initial = {467 'thing': '',468 'new_thing': '',469 'things': '',470 }471 form = SimpleForm(data=data, initial=initial)472 self.assertTrue(form.has_changed())...
test_thing.py
Source:test_thing.py
...6from HABApp.openhab.items import Thing7from HABApp.openhab.map_events import get_event8from pendulum import set_test_now, DateTime, UTC9@pytest.fixture(scope="function")10def test_thing(ir: HINT_ITEM_REGISTRY):11 set_test_now(DateTime(2000, 1, 1, tzinfo=UTC))12 thing = HABApp.openhab.items.Thing('test_thing')13 yield thing14 set_test_now()15def get_status_event(status: str) -> ThingStatusInfoEvent:16 data = {17 'topic': 'smarthome/things/test_thing/status',18 'payload': f'{{"status":"{status}","statusDetail":"NONE"}}',19 'type': 'ThingStatusInfoEvent'20 }21 event = get_event(data)22 assert isinstance(event, ThingStatusInfoEvent)23 return event24def test_thing_status_events(test_thing: Thing):...
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!!