Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
devel:languages:python:django
python-djangorestframework-camel-case
tests.py
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File tests.py of Package python-djangorestframework-camel-case
from copy import deepcopy from unittest import TestCase, mock from django.conf import settings from django.http import QueryDict from django.utils.functional import lazy from django.test.client import RequestFactory from rest_framework.utils.serializer_helpers import ReturnDict from djangorestframework_camel_case.util import camelize, underscoreize settings.configure() from djangorestframework_camel_case.middleware import CamelCaseMiddleWare class ImportTest(TestCase): def test_import_all(self): """ A quick test that just imports everything, should crash in case any Django or DRF modules change """ from djangorestframework_camel_case import parser from djangorestframework_camel_case import render from djangorestframework_camel_case import settings from djangorestframework_camel_case import middleware assert parser assert render assert settings assert middleware class UnderscoreToCamelTestCase(TestCase): def test_under_to_camel_keys(self): data = { "two_word": 1, "long_key_with_many_underscores": 2, "only_1_key": 3, "only_one_letter_a": 4, "b_only_one_letter": 5, "only_c_letter": 6, "mix_123123a_and_letters": 7, "mix_123123aa_and_letters_complex": 8, "no_underscore_before123": 9, } output = { "twoWord": 1, "longKeyWithManyUnderscores": 2, "only1Key": 3, "onlyOneLetterA": 4, "bOnlyOneLetter": 5, "onlyCLetter": 6, "mix123123aAndLetters": 7, "mix123123aaAndLettersComplex": 8, "noUnderscoreBefore123": 9, } self.assertEqual(camelize(data), output) def test_tuples(self): data = {"multiple_values": (1, 2), "data": [1, 3, 4]} output = {"multipleValues": [1, 2], "data": [1, 3, 4]} self.assertEqual(camelize(data), output) def test_camel_to_under_input_untouched_for_sequence(self): data = [{"firstInput": 1}, {"secondInput": 2}] reference_input = deepcopy(data) camelize(data) self.assertEqual(data, reference_input) def test_recursive_camelize_with_ignored_fields_and_keys(self): ignore_fields = ("ignored_field", "newKeyIgnoredField", "ignored_field_and_key", "ignoredFieldAndKey2") ignore_keys = ("ignored_key", "newKeyIgnoredKey", "ignored_field_and_key", "ignoredFieldAndKey2") data = { "ignored_field": {"no_change_recursive": 1}, "change_me": {"change_recursive": 2}, "new_key_ignored_field": {"also_no_change": 3}, "ignored_key": {"also_change_recursive": 4}, "new_key_ignored_key": {"change_is_here_to_stay": 5}, "ignored_field_and_key": {"no_change_here": 6}, "ignored_field_and_key2": {"no_change_here_either": 7}, } output = { "ignoredField": {"no_change_recursive": 1}, "changeMe": {"changeRecursive": 2}, "newKeyIgnoredField": {"also_no_change": 3}, "ignored_key": {"alsoChangeRecursive": 4}, "new_key_ignored_key": {"changeIsHereToStay": 5}, "ignored_field_and_key": {"no_change_here": 6}, "ignored_field_and_key2": {"no_change_here_either": 7}, } self.assertEqual(camelize(data, ignore_fields=ignore_fields, ignore_keys=ignore_keys), output) class CamelToUnderscoreTestCase(TestCase): def test_camel_to_under_keys(self): data = { "twoWord": 1, "longKeyWithManyUnderscores": 2, "only1Key": 3, "onlyOneLetterA": 4, "bOnlyOneLetter": 5, "onlyCLetter": 6, "mix123123aAndLetters": 7, "mix123123aaAndLettersComplex": 8, "wordWITHCaps": 9, "key10": 10, "anotherKey1": 11, "anotherKey10": 12, "optionS1": 13, "optionS10": 14, "UPPERCASE": 15, } output = { "two_word": 1, "long_key_with_many_underscores": 2, "only_1_key": 3, "only_one_letter_a": 4, "b_only_one_letter": 5, "only_c_letter": 6, "mix_123123a_and_letters": 7, "mix_123123aa_and_letters_complex": 8, "word_with_caps": 9, "key_10": 10, "another_key_1": 11, "another_key_10": 12, "option_s_1": 13, "option_s_10": 14, "uppercase": 15, } self.assertEqual(underscoreize(data), output) def test_camel_to_under_keys_with_no_underscore_before_number(self): data = {"noUnderscoreBefore123": 1} output = {"no_underscore_before123": 1} options = {"no_underscore_before_number": True} self.assertEqual(underscoreize(data, **options), output) def test_under_to_camel_input_untouched_for_sequence(self): data = [{"first_input": 1}, {"second_input": 2}] reference_input = deepcopy(data) underscoreize(data) self.assertEqual(data, reference_input) def test_recursive_underscoreize_with_ignored_fields_and_keys(self): ignore_fields = ("ignoredField", "new_key_ignored_field", "ignoredFieldAndKey", "ignored_field_and_key_2") ignore_keys = ("ignoredKey", "new_key_ignored_key", "ignoredFieldAndKey", "ignored_field_and_key_2") data = { "ignoredField": {"noChangeRecursive": 1}, "changeMeField": {"changeRecursive": 2}, "newKeyIgnoredField": {"alsoNoChange": 3}, "ignoredKey": {"changeRecursiveAgain": 4}, "newKeyIgnoredKey": {"changeIsHereToStay": 5}, "ignoredFieldAndKey": {"noChangeHere": 6}, "ignoredFieldAndKey2": {"noChangeHereEither": 7}, } output = { "ignored_field": {"noChangeRecursive": 1}, "change_me_field": {"change_recursive": 2}, "new_key_ignored_field": {"alsoNoChange": 3}, "ignoredKey": {"change_recursive_again": 4}, "newKeyIgnoredKey": {"change_is_here_to_stay": 5}, "ignoredFieldAndKey": {"noChangeHere": 6}, "ignoredFieldAndKey2": {"noChangeHereEither": 7}, } self.assertEqual(underscoreize(data, ignore_fields=ignore_fields, ignore_keys=ignore_keys), output) class NonStringKeyTest(TestCase): def test_non_string_key(self): data = {1: "test"} self.assertEqual(underscoreize(camelize(data)), data) def return_string(text): return text lazy_func = lazy(return_string, str) class PromiseStringTest(TestCase): def test_promise_strings(self): data = {lazy_func("test_key"): lazy_func("test_value value")} camelized = camelize(data) self.assertEqual(camelized, {"testKey": "test_value value"}) result = underscoreize(camelized) self.assertEqual(result, {"test_key": "test_value value"}) class ReturnDictTest(TestCase): def test_return_dict(self): data = ReturnDict({"id": 3, "value": "val"}, serializer=object()) camelized = camelize(data) self.assertEqual(data, camelized) self.assertEqual(data.serializer, camelized.serializer) class GeneratorAsInputTestCase(TestCase): def _underscore_generator(self): yield {"simple_is_better": "than complex"} yield {"that_is": "correct"} def _camel_generator(self): yield {"simpleIsBetter": "than complex"} yield {"thatIs": "correct"} def test_camelize_iterates_over_generator(self): data = self._underscore_generator() output = [{"simpleIsBetter": "than complex"}, {"thatIs": "correct"}] self.assertEqual(camelize(data), output) def test_underscoreize_iterates_over_generator(self): data = self._camel_generator() output = [{"simple_is_better": "than complex"}, {"that_is": "correct"}] self.assertEqual(underscoreize(data), output) class CamelToUnderscoreQueryDictTestCase(TestCase): def test_camel_to_under_keys(self): query_dict = QueryDict("testList=1&testList=2", mutable=True) data = { "twoWord": 1, "longKeyWithManyUnderscores": 2, "only1Key": 3, "onlyOneLetterA": 4, "bOnlyOneLetter": 5, "onlyCLetter": 6, "mix123123aAndLetters": 7, "mix123123aaAndLettersComplex": 8, "wordWITHCaps": 9, "key10": 10, "anotherKey1": 11, "anotherKey10": 12, "optionS1": 13, "optionS10": 14, "UPPERCASE": 15, } query_dict.update(data) output_query = QueryDict("test_list=1&test_list=2", mutable=True) output = { "two_word": 1, "long_key_with_many_underscores": 2, "only_1_key": 3, "only_one_letter_a": 4, "b_only_one_letter": 5, "only_c_letter": 6, "mix_123123a_and_letters": 7, "mix_123123aa_and_letters_complex": 8, "word_with_caps": 9, "key_10": 10, "another_key_1": 11, "another_key_10": 12, "option_s_1": 13, "option_s_10": 14, "uppercase": 15, } output_query.update(output) self.assertEqual(underscoreize(query_dict), output_query) class CamelCaseMiddleWareTestCase(TestCase): def test_camel_case_to_underscore_query_params(self): get_response_mock = mock.MagicMock() middleware = CamelCaseMiddleWare(get_response_mock) query_dict = QueryDict("testList=1&testList=2", mutable=True) data = { "twoWord": "1", "longKeyWithManyUnderscores": "2", "only1Key": "3", "onlyOneLetterA": "4", "bOnlyOneLetter": "5", "onlyCLetter": "6", "mix123123aAndLetters": "7", "mix123123aaAndLettersComplex": "8", "wordWITHCaps": "9", "key10": "10", "anotherKey1": "11", "anotherKey10": "12", "optionS1": "13", "optionS10": "14", "UPPERCASE": "15", } query_dict.update(data) output_query = QueryDict("test_list=1&test_list=2", mutable=True) output = { "two_word": "1", "long_key_with_many_underscores": "2", "only_1_key": "3", "only_one_letter_a": "4", "b_only_one_letter": "5", "only_c_letter": "6", "mix_123123a_and_letters": "7", "mix_123123aa_and_letters_complex": "8", "word_with_caps": "9", "key_10": "10", "another_key_1": "11", "another_key_10": "12", "option_s_1": "13", "option_s_10": "14", "uppercase": "15", } output_query.update(output) request = RequestFactory().get("/", query_dict) middleware(request) (args, kwargs) = get_response_mock.call_args self.assertEqual(args[0].GET, output_query)
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor