Skip to content

Commit

Permalink
Add test for issue #387
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Jun 14, 2022
1 parent bdc48f5 commit 1ef4926
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of the django-environ.
#
# Copyright (c) 2021, Serghei Iakovlev <egrep@protonmail.ch>
# Copyright (c) 2021-2022, Serghei Iakovlev <egrep@protonmail.ch>
# Copyright (c) 2013-2021, Daniele Faraglia <daniele.faraglia@gmail.com>
#
# For the full copyright and license information, please view
Expand Down Expand Up @@ -63,6 +63,7 @@ def generate_data(cls):
ESCAPED_VAR=r'\$baz',
INT_LIST='42,33',
INT_TUPLE='(42,33)',
MIX_TUPLE='(42,Test)',
STR_LIST_WITH_SPACES=' foo, bar',
EMPTY_LIST='',
DICT_VAR='foo=bar,test=on',
Expand Down
11 changes: 10 additions & 1 deletion tests/test_env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of the django-environ.
#
# Copyright (c) 2021, Serghei Iakovlev <egrep@protonmail.ch>
# Copyright (c) 2021-2022, Serghei Iakovlev <egrep@protonmail.ch>
# Copyright (c) 2013-2021, Daniele Faraglia <daniele.faraglia@gmail.com>
#
# For the full copyright and license information, please view
Expand Down Expand Up @@ -150,6 +150,15 @@ def test_int_tuple(self):
assert_type_and_value(tuple, (42, 33), self.env.tuple('INT_TUPLE', int))
assert_type_and_value(tuple, ('42', '33'), self.env.tuple('INT_TUPLE'))

def test_mix_tuple_issue_387(self):
"""Cast a tuple of mixed types.
Casts a string like "(42,Test)" to a tuple like (42, 'Test').
See: https://github.com/joke2k/django-environ/issues/387 for details."""
caster = lambda v: int(v) if v.isdigit() else v.strip()
cast = lambda t: tuple(map(caster, [c for c in t.strip('()').split(',')]))
assert_type_and_value(tuple, (42, 'Test'), self.env( 'MIX_TUPLE', default=(0, ''), cast=cast))

def test_str_list_with_spaces(self):
assert_type_and_value(list, [' foo', ' bar'],
self.env('STR_LIST_WITH_SPACES', cast=[str]))
Expand Down
1 change: 1 addition & 0 deletions tests/test_env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ MULTILINE_ESCAPED_STR_VAR=---BEGIN---\\n---END---
INT_LIST=42,33
CYRILLIC_VAR=фуубар
INT_TUPLE=(42,33)
MIX_TUPLE=(42,Test)
DATABASE_ORACLE_TNS_URL=oracle://user:password@sid
DATABASE_ORACLE_URL=oracle://user:password@host:1521/sid
DATABASE_REDSHIFT_URL=redshift://user:password@examplecluster.abc123xyz789.us-west-2.redshift.amazonaws.com:5439/dev
Expand Down

0 comments on commit 1ef4926

Please sign in to comment.