def view2(request): # country = Canada, state = Ontario form = DemoForm(request.GET) self.assertEqual(len(form.fields["country"].choices.queryset), 3) self.assertEqual(len(form.fields["state"].choices.queryset), 2) self.assertEqual(len(form.fields["city"].choices.queryset), 3) self.assertEqual(len(form.fields["neighborhood"].choices.queryset), 0) self.assertFalse(form.is_valid()) form2 = form.ajaxRequest(request) self.assertEqual(len(form2.fields["country"].choices.queryset), 3) self.assertEqual(len(form2.fields["state"].choices.queryset), 2) self.assertEqual(len(form2.fields["city"].choices.queryset), 3) self.assertEqual(len(form2.fields["neighborhood"].choices.queryset), 0) self.assertFalse(form2.is_valid()) with self.assertRaises(Exception): form2.save() # ajax forms are not meant to be saved
def view4(request): # country = Canada, state = Ontario, city=San Francisco # Should ignore San Francisco and show valid choices for Ontario form = DemoForm(request.GET) self.assertEqual(len(form.fields["country"].choices.queryset), 3) self.assertEqual(len(form.fields["state"].choices.queryset), 2) self.assertEqual(len(form.fields["city"].choices.queryset), 3) self.assertEqual(len(form.fields["neighborhood"].choices.queryset), 7) #should be 0, this is invalid self.assertFalse(form.is_valid()) form2 = form.ajaxRequest(request) self.assertEqual(len(form2.fields["country"].choices.queryset), 3) self.assertEqual(len(form2.fields["state"].choices.queryset), 2) self.assertEqual(len(form2.fields["city"].choices.queryset), 3) self.assertEqual(len(form2.fields["neighborhood"].choices.queryset), 0) self.assertFalse(form2.is_valid()) with self.assertRaises(Exception): form2.save() # ajax forms are not meant to be saved
def view3(request): # country = Canada, state = Ontario, city=Toronto, neighborhood=North York # This is the one fully valid case form = DemoForm(request.GET) self.assertEqual(len(form.fields["country"].choices.queryset), 3) self.assertEqual(len(form.fields["state"].choices.queryset), 2) self.assertEqual(len(form.fields["city"].choices.queryset), 3) self.assertEqual(len(form.fields["neighborhood"].choices.queryset), 3) self.assertTrue(form.is_valid()) form2 = form.ajaxRequest(request) self.assertEqual(len(form2.fields["country"].choices.queryset), 3) self.assertEqual(len(form2.fields["state"].choices.queryset), 2) self.assertEqual(len(form2.fields["city"].choices.queryset), 3) self.assertEqual(len(form2.fields["neighborhood"].choices.queryset), 3) self.assertFalse(form2.is_valid()) with self.assertRaises(Exception): form2.save() # ajax forms are not meant to be saved