Skip to content

Commit 2716916

Browse files
committed
Refactor validation code and add not-None validation
1 parent 66334f1 commit 2716916

File tree

4 files changed

+102
-14
lines changed

4 files changed

+102
-14
lines changed

basest/core/decode.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
)
66

77
from .encode import encode_raw
8-
from .utils import ints_to_symbols, symbol_table_is_unique, symbols_to_ints
8+
from .utils import ints_to_symbols, symbols_to_ints, validate_symbol_tables
99

1010

1111
def decode_raw(input_base, output_base, input_ratio, output_ratio, input_data):
@@ -54,12 +54,12 @@ def decode(
5454
Assumes standard base64-style padding using the given input padding symbol,
5555
but can handle unpadded input just fine.
5656
"""
57-
# validate the uniqueness of both symbol tables before continuing
58-
if (
59-
(not symbol_table_is_unique(input_symbol_table, input_padding)) or
60-
(not symbol_table_is_unique(output_symbol_table))
61-
):
62-
raise ValueError('Unique symbol tables required')
57+
# validate both symbol tables and the padding symbol before continuing
58+
validate_symbol_tables(
59+
input_symbol_table,
60+
input_padding,
61+
output_symbol_table
62+
)
6363
# create workon copy of input data and convert symbols to raw ints
6464
# NOTE: input symbol table here includes the padding character
6565
input_workon = symbols_to_ints(

basest/core/encode.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
absolute_import, division, print_function, unicode_literals
55
)
66

7-
from .utils import ints_to_symbols, symbol_table_is_unique, symbols_to_ints
7+
from .utils import ints_to_symbols, symbols_to_ints, validate_symbol_tables
88

99

1010
def _nearest_length(input_length, input_ratio):
@@ -96,12 +96,12 @@ def encode(
9696
Uses standard base64-style padding if needed, using the given padding
9797
symbol.
9898
"""
99-
# validate the uniqueness of both symbol tables before continuing
100-
if (
101-
(not symbol_table_is_unique(input_symbol_table)) or
102-
(not symbol_table_is_unique(output_symbol_table, output_padding))
103-
):
104-
raise ValueError('Unique symbol tables required')
99+
# validate both symbol tables and the padding symbol before continuing
100+
validate_symbol_tables(
101+
output_symbol_table,
102+
output_padding,
103+
input_symbol_table
104+
)
105105
# create workon copy of input data and convert symbols to raw ints
106106
input_workon = symbols_to_ints(input_data, input_symbol_table)
107107
# use encode_raw() to encode the data

basest/core/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,21 @@ def symbol_table_is_unique(symbol_table, padding_symbol=None):
3333
# otherwise, check that padding_symbol isn't in the symbol table
3434
# NOTE: this assumes that `None` is never in the symbol table
3535
return padding_symbol not in symbol_table
36+
37+
38+
def validate_symbol_tables(symbol_table, padding_symbol, other_symbol_table):
39+
"""
40+
Validates two symbol tables (the padding symbol being used alongside the
41+
first one).
42+
Raises ValueError if either of the symbol tables (or padding symbol) fail
43+
validation.
44+
"""
45+
if None in (symbol_table + [padding_symbol] + other_symbol_table):
46+
raise ValueError(
47+
'None cannot be used in symbol tables nor for padding'
48+
)
49+
elif (
50+
(not symbol_table_is_unique(symbol_table, padding_symbol)) or
51+
(not symbol_table_is_unique(other_symbol_table))
52+
):
53+
raise ValueError('Unique symbol tables required')

tests/core/test_encode_decode.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,41 @@ def test_encode_rejects_non_unique_symbol_tables(
151151
[]
152152
)
153153

154+
def test_encode_rejects_output_symbol_table_containing_padding_symbol(
155+
self
156+
):
157+
"""
158+
When the output symbol table passed to encode() contains the padding
159+
symbol, ValueError should be raised.
160+
"""
161+
with self.assertRaises(ValueError):
162+
encode(1, ['a'], 1, ['b'], 'b', 1, 1, [])
163+
164+
@data(
165+
(list('abcd'), list('efghijk'), None),
166+
(list('1234'), [1, 2, 3, None], '#'),
167+
([None, 2, 3, 4], list('cabuges'), '#')
168+
)
169+
@unpack
170+
def test_encode_rejects_none_used_in_symbol_tables_and_padding(
171+
self,
172+
input_symbol_table,
173+
output_symbol_table,
174+
padding_symbol
175+
):
176+
"""
177+
When any of the symbol tables or the padding symbol passed to encode()
178+
are or contain None, ValueError should be raised.
179+
"""
180+
with self.assertRaises(ValueError):
181+
encode(
182+
len(input_symbol_table), input_symbol_table,
183+
len(output_symbol_table), output_symbol_table,
184+
padding_symbol,
185+
1, 1,
186+
[]
187+
)
188+
154189
@data(
155190
# Base-64, using most common alphabet - no padding
156191
(
@@ -270,6 +305,41 @@ def test_decode_rejects_non_unique_symbol_tables(
270305
[]
271306
)
272307

308+
def test_decode_rejects_input_symbol_table_containing_padding_symbol(
309+
self
310+
):
311+
"""
312+
When the input symbol table passed to decode() contains the padding
313+
symbol, ValueError should be raised.
314+
"""
315+
with self.assertRaises(ValueError):
316+
decode(1, ['a'], 'a', 1, ['b'], 1, 1, [])
317+
318+
@data(
319+
(list('abcd'), list('efghijk'), None),
320+
(list('1234'), [1, 2, 3, None], '#'),
321+
([None, 2, 3, 4], list('cabuges'), '#')
322+
)
323+
@unpack
324+
def test_decode_rejects_none_used_in_symbol_tables_and_padding(
325+
self,
326+
input_symbol_table,
327+
output_symbol_table,
328+
padding_symbol
329+
):
330+
"""
331+
When any of the symbol tables or the padding symbol passed to decode()
332+
are or contain None, ValueError should be raised.
333+
"""
334+
with self.assertRaises(ValueError):
335+
decode(
336+
len(input_symbol_table), input_symbol_table,
337+
padding_symbol,
338+
len(output_symbol_table), output_symbol_table,
339+
1, 1,
340+
[]
341+
)
342+
273343
@data(
274344
# Base-64, using most common alphabet with no padding needed
275345
(

0 commit comments

Comments
 (0)