-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from mdrachuk/camel-case-json
Camel Case for JSON
- Loading branch information
Showing
10 changed files
with
131 additions
and
13 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
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,32 @@ | ||
from dataclasses import dataclass | ||
|
||
from serious import JsonModel | ||
|
||
|
||
@dataclass | ||
class Snack: | ||
butterbeer: int | ||
dragon_tartare: int | ||
hogwarts_steak_and_kidney_pie_: int | ||
_pumpkin__fizz: int | ||
|
||
|
||
def test_json_transforms_case_by_default(): | ||
model = JsonModel(Snack) | ||
actual = model.dump(Snack(1, 2, 3, 4)) | ||
expected = '{"butterbeer": 1, "dragonTartare": 2, "hogwartsSteakAndKidneyPie": 3, "pumpkinFizz": 4}' | ||
assert actual == expected | ||
|
||
|
||
def test_json_transforms_case(): | ||
model = JsonModel(Snack, camel_case=True) | ||
actual = model.dump(Snack(1, 2, 3, 4)) | ||
expected = '{"butterbeer": 1, "dragonTartare": 2, "hogwartsSteakAndKidneyPie": 3, "pumpkinFizz": 4}' | ||
assert actual == expected | ||
|
||
|
||
def test_json_skips_transformation(): | ||
model = JsonModel(Snack, camel_case=False) | ||
actual = model.dump(Snack(1, 2, 3, 4)) | ||
expected = '{"butterbeer": 1, "dragon_tartare": 2, "hogwarts_steak_and_kidney_pie_": 3, "_pumpkin__fizz": 4}' | ||
assert actual == expected |
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
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,31 @@ | ||
from collections import namedtuple | ||
from typing import Callable | ||
|
||
from serious.utils import snake_to_camel, camel_to_snake | ||
|
||
Ex = namedtuple('Example', ['snake', 'camel']) | ||
|
||
|
||
class TestCases: | ||
|
||
def setup_class(self): | ||
self.examples = [ | ||
Ex('_http_response', 'httpResponse'), | ||
Ex('name__space', 'httpResponse'), | ||
Ex('some_str_3_one_two_three_234', 'someStr3AbcOneTwoThree234'), | ||
Ex('some32_cows', 'some32Cows'), | ||
] | ||
|
||
def each_example(self, test: Callable[[Ex], None]): | ||
for example in self.examples: | ||
test(example) | ||
|
||
def test_snake_to_camel(self): | ||
self.each_example(lambda ex: snake_to_camel(ex.snake) == ex.camel) | ||
|
||
def test_camel_to_snake(self): | ||
self.each_example(lambda ex: camel_to_snake(ex.camel) == ex.snake) | ||
|
||
def check_symmetry(self): | ||
self.each_example(lambda ex: snake_to_camel(camel_to_snake(ex.camel)) == ex.camel) | ||
self.each_example(lambda ex: camel_to_snake(snake_to_camel(ex.snake)) == ex.snake) |