Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow all types to be used in field mapping
- Loading branch information
Showing
3 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import unittest | ||
import typing | ||
|
||
from rest_framework_dataclasses import field_utils | ||
from rest_framework_dataclasses.types import Literal | ||
|
||
|
||
class FieldUtilsTest(unittest.TestCase): | ||
def test_lookup_type(self): | ||
mapping = { | ||
str: 1, | ||
int: 2, | ||
typing.Union[int, str]: 3 | ||
} | ||
|
||
self.assertEqual(field_utils.lookup_type_in_mapping(mapping, str), 1) | ||
self.assertEqual(field_utils.lookup_type_in_mapping(mapping, int), 2) | ||
self.assertEqual(field_utils.lookup_type_in_mapping(mapping, typing.Union[int, str]), 3) | ||
|
||
self.assertEqual(field_utils.lookup_type_in_mapping(mapping, type('email', (str, ), {})), 1) | ||
|
||
with self.assertRaises(KeyError): | ||
field_utils.lookup_type_in_mapping(mapping, float) | ||
with self.assertRaises(KeyError): | ||
field_utils.lookup_type_in_mapping(mapping, Literal['a', 'b']) |