def test_many_to_many_field(self): model1 = ExampleModelTwoFactory.create(name="Foo") model2 = ExampleModelTwoFactory.create(name="Bar") serializer = ExampleModelOneSerializer() field_repr = serializer.get_field_representation() self.assertEqual(field_repr["many_to_many_field"]["display"], "verbatim") self.assertEqual(field_repr["many_to_many_field"]["filter"], "choice") self.assertIn( { "label": model1.name, "value": str(model1.uuid) }, field_repr["many_to_many_field"]["configuration"]["options"], ) self.assertIn( { "label": model2.name, "value": str(model2.uuid) }, field_repr["many_to_many_field"]["configuration"]["options"], )
def test_many_to_many_field(self): model1 = ExampleModelTwoFactory.create(name="Foo") model2 = ExampleModelTwoFactory.create(name="Bar") serializer = ExampleModelOneSerializer() field_repr = serializer.get_field_representation() self.assertEqual(field_repr["many_to_many_field"]["display"], "verbatim") self.assertEqual(field_repr["many_to_many_field"]["filter"], "many_to_many_field")
def test_foreign_key_field_filtering(self): em2 = ExampleModelTwoFactory() ExampleModelOneFactory(other_model=em2, boolean_field=True) ExampleModelOneFactory(boolean_field=True) response = self.client.get(self.url + "?other_model=" + str(em2.uuid)) self.assertEqual(response.data["total_result_count"], 1)
def test_many_to_many_field_filtering(self): em2 = ExampleModelTwoFactory(name="Test Item") valid_model = ExampleModelOneFactory(other_model=em2, boolean_field=True) ExampleModelOneFactory(boolean_field=True) valid_model.many_to_many_field.add(em2) response = self.client.get(self.url + "?many_to_many_field=" + str(em2.uuid)) self.assertEqual(response.data["total_result_count"], 1) self.assertEqual( response.data["results"][0]["many_to_many_field"], ["Test Item"] )
def test_pagination_info_string(self): date_time = datetime.now() foreign_model = ExampleModelTwoFactory() ExampleModelOneFactory.create_batch(60) response = self.client.get(self.url, {}) self.assertEqual( response.data["meta"]["footer_info"], ("page_result_count 50 " "total_result_count 60 " "current_page 1 " "page_count 2 " "first_result_count 1 " "last_result_count 50 " "per_page 50 "), )
def test_getting_data_fields_present(self): date_time = datetime.now() foreign_model = ExampleModelTwoFactory() test_model_1 = ExampleModelOneFactory( integer_field=1, integer_choice_field=2, float_field=10.0, decimal_field=Decimal("10"), boolean_field=True, nullboolean_field=False, char_field="demo", choice_field="foo", text_field="admin", slug_field="a-slug", date_field=date_time.date(), other_model=foreign_model, ) test_model_1.many_to_many_field.add(foreign_model) response = self.client.get(self.url, {}) self.assertEqual(response.data["total_result_count"], 1) self.assertEqual(response.data["next"], None) self.assertEqual(response.data["previous"], None) result = response.data["results"][0] self.assertEqual(result["integer_field"], 1) self.assertEqual(result["integer_choice_field"], 2) self.assertEqual(result["float_field"], 10.0) self.assertEqual(result["decimal_field"], "10.00") self.assertEqual(result["boolean_field"], True) self.assertEqual(result["nullboolean_field"], False) self.assertEqual(result["char_field"], "demo") self.assertEqual(result["choice_field"], "foo") self.assertEqual(result["text_field"], "admin") self.assertEqual(result["slug_field"], "a-slug") self.assertEqual(result["date_field"], str(date_time.date())) self.assertEqual(result["other_model__name"], foreign_model.name) self.assertEqual(result["other_model__number"], foreign_model.number)