Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
dshafer committed Jun 8, 2020
1 parent 3547999 commit ce093d1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
5 changes: 3 additions & 2 deletions django_declarative_apis/machinery/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ def _get_unexpanded_field_value(inst, field_name, field_type):
if not field_type.model_class:
return DEFAULT_UNEXPANDED_VALUE


display_key = field_type.display_key or field_type.model_class._meta.pk.name
inst_field_name = field_type.inst_field_name or field_name
is_multiple = isinstance(inst.__class__._meta.get_field(inst_field_name), ManyToOneRel)
is_multiple = isinstance(
inst.__class__._meta.get_field(inst_field_name), ManyToOneRel
)

if is_multiple:
# special case for keys that have multiple values (for instance, inverse fk relations)
Expand Down
12 changes: 8 additions & 4 deletions tests/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
models.TestModel: {
"pk": ALWAYS,
"int_field": ALWAYS,
"renamed_expandable_dict": expandable(inst_field_name='expandable_dict'),
"renamed_expandable_string": expandable(inst_field_name='expandable_string'),
"renamed_expandable_dict": expandable(inst_field_name="expandable_dict"),
"renamed_expandable_string": expandable(inst_field_name="expandable_string"),
},
models.ParentModel: {
"nonstandard_id": ALWAYS,
Expand All @@ -56,9 +56,13 @@
},
models.ChildModel: {
"pk": ALWAYS,
"renamed_test": expandable(model_class=models.TestModel, inst_field_name='test'),
"renamed_test": expandable(
model_class=models.TestModel, inst_field_name="test"
),
"name": ALWAYS,
"renamed_parent": expandable(model_class=models.ParentModel, inst_field_name='parent'),
"renamed_parent": expandable(
model_class=models.ParentModel, inst_field_name="parent"
),
},
}

Expand Down
46 changes: 27 additions & 19 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,31 +186,39 @@ def test_rename_expandable_foreign_key(self):
self.p1c1, filters.RENAMED_EXPANDABLE_MODEL_FIELDS
)

self.assertEqual(filtered['renamed_test'], {'id': self.p1c1.test.pk})
self.assertEqual(filtered['renamed_parent'], {'nonstandard_id': self.p1c1.parent.pk})
self.assertEqual(filtered["renamed_test"], {"id": self.p1c1.test.pk})
self.assertEqual(
filtered["renamed_parent"], {"nonstandard_id": self.p1c1.parent.pk}
)

filtered = filtering.apply_filters_to_object(
self.p1c1, filters.RENAMED_EXPANDABLE_MODEL_FIELDS,
expand_header='renamed_test,renamed_parent'
self.p1c1,
filters.RENAMED_EXPANDABLE_MODEL_FIELDS,
expand_header="renamed_test,renamed_parent",
)
self.assertTrue(2, len(filtered['renamed_test']))
self.assertIn('int_field', filtered['renamed_test'])
self.assertNotIn('renamed_expandable_dict', filtered['renamed_test'])
self.assertTrue(4, len(filtered['renamed_parent']))
self.assertTrue(2, len(filtered["renamed_test"]))
self.assertIn("int_field", filtered["renamed_test"])
self.assertNotIn("renamed_expandable_dict", filtered["renamed_test"])
self.assertTrue(4, len(filtered["renamed_parent"]))

filtered = filtering.apply_filters_to_object(
self.p1c1, filters.RENAMED_EXPANDABLE_MODEL_FIELDS,
expand_header='renamed_test,renamed_test.renamed_expandable_dict'
self.p1c1,
filters.RENAMED_EXPANDABLE_MODEL_FIELDS,
expand_header="renamed_test,renamed_test.renamed_expandable_dict",
)
self.assertTrue(3, len(filtered["renamed_test"]))
self.assertIn("renamed_expandable_dict", filtered["renamed_test"])
self.assertEqual(
self.test_model.expandable_dict,
filtered["renamed_test"]["renamed_expandable_dict"],
)
self.assertTrue(3, len(filtered['renamed_test']))
self.assertIn('renamed_expandable_dict', filtered['renamed_test'])
self.assertEqual(self.test_model.expandable_dict, filtered['renamed_test']['renamed_expandable_dict'])
self.assertNotIn('renamed_expandable_string', filtered['renamed_test'])
self.assertNotIn("renamed_expandable_string", filtered["renamed_test"])

filtered = filtering.apply_filters_to_object(
self.p1c1, filters.RENAMED_EXPANDABLE_MODEL_FIELDS,
expand_header='renamed_test,renamed_test.renamed_expandable_dict,renamed_test.renamed_expandable_string'
self.p1c1,
filters.RENAMED_EXPANDABLE_MODEL_FIELDS,
expand_header="renamed_test,renamed_test.renamed_expandable_dict,renamed_test.renamed_expandable_string",
)
self.assertTrue(4, len(filtered['renamed_test']))
self.assertIn('renamed_expandable_dict', filtered['renamed_test'])
self.assertIn('renamed_expandable_string', filtered['renamed_test'])
self.assertTrue(4, len(filtered["renamed_test"]))
self.assertIn("renamed_expandable_dict", filtered["renamed_test"])
self.assertIn("renamed_expandable_string", filtered["renamed_test"])

0 comments on commit ce093d1

Please sign in to comment.