Skip to content

Commit 7635a72

Browse files
authored
Merge pull request #137 from lisad/fix-column-blank-param
Cleans up the 'blank' param so it's only on string column, adds test,…
2 parents f74f64c + bdb274d commit 7635a72

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

phaser/column.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
Column: Use for strings & general purpose
1818
IntColumn, FloatColumn: Cast to int/float datatypes and apply number logic like min_value, max_value
1919
DateColumn, DateTimeColumn: Cast to date/time datatypes and apply calendar logic like default timezone
20-
21-
TODOs:
22-
* Probably the default column and the other columns should both inherit from a BaseColumn, so we can
23-
have fields like 'blank' on Column but not on IntColumn
20+
BooleanColumn: cast 1, 0, T, F, yes, no, TRUE, FALSE etc to python True and False values.
2421
2522
"""
2623

@@ -83,7 +80,7 @@ def __init__(self,
8380
self.use_exception = Column.ON_ERROR_VALUES[on_error]
8481

8582
if self.null is False and self.default is not None:
86-
raise Exception(f"Column {self.name} defined to error on null values, but also provides a non-null default")
83+
raise PhaserError(f"Column {self.name} defined to error on null values, but also provides a non-null default")
8784

8885
def check_required(self, data_headers):
8986
"""
@@ -128,7 +125,7 @@ def check_value(self, value):
128125
""" Raises chosen exception type if something is wrong with a value in the column.
129126
One can override this to use a different exception or check value in a different way
130127
(don't forget to call super().check_value() """
131-
if not self.blank and not value: # Python boolean casting returns false if string is empty
128+
if not self.blank and not value.strip(): # Python boolean casting returns false if string is empty
132129
raise self.use_exception(f"Column `{self.name}' had blank value")
133130
if self.allowed_values and not (value in self.allowed_values):
134131
raise self.use_exception(f"Column '{self.name}' had value {value} not found in allowed values")
@@ -215,7 +212,6 @@ def __init__(self,
215212
name,
216213
required=True,
217214
null=True,
218-
blank=True,
219215
default=None,
220216
fix_value_fn=None,
221217
rename=None,
@@ -227,7 +223,7 @@ def __init__(self,
227223
super().__init__(name,
228224
required=required,
229225
null=null,
230-
blank=blank,
226+
blank=True, # Ignores this column - only null=T/F matters
231227
default=default,
232228
fix_value_fn=fix_value_fn,
233229
rename=rename,
@@ -289,7 +285,6 @@ def __init__(self,
289285
name,
290286
required=True,
291287
null=True,
292-
blank=True,
293288
default=None,
294289
fix_value_fn=None,
295290
rename=None,
@@ -303,7 +298,7 @@ def __init__(self,
303298
super().__init__(name,
304299
required=required,
305300
null=null,
306-
blank=blank,
301+
blank=True, # Ignores this param - only null=T/F matters
307302
default=default,
308303
fix_value_fn=fix_value_fn,
309304
rename=rename,

tests/test_columns.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# Constructor tests
1212
def test_null_forbidden_but_null_default():
13-
with pytest.raises(Exception):
13+
with pytest.raises(PhaserError):
1414
Column(name='bogus', null=False, default='homer')
1515

1616

@@ -20,6 +20,12 @@ def test_invalid_on_error():
2020
assert "Supported on_error values" in excinfo.value.message
2121

2222

23+
@pytest.mark.parametrize('column_type', [IntColumn, FloatColumn, DateColumn, DateTimeColumn, BooleanColumn])
24+
def test_non_string_column_types_dont_have_empty_param(column_type):
25+
with pytest.raises(TypeError):
26+
column_type(name='inconsistent', null=False, blank=True)
27+
28+
2329
# Simple feature tests
2430

2531
def test_required_values():
@@ -35,6 +41,13 @@ def test_null_forbidden():
3541
col.check_and_cast_value({'employeeid': None})
3642

3743

44+
@pytest.mark.parametrize('value', ["", " ", "\t"])
45+
def test_blank_forbidden(value):
46+
col = Column(name='not_blank', null=False, blank=False)
47+
with pytest.raises(DataErrorException):
48+
col.check_and_cast_value({'not_blank': value})
49+
50+
3851
def test_default_value():
3952
col = Column('location', default='HQ')
4053
input = [{'location': 'Atlanta'}, {'location': None}]
@@ -85,7 +98,7 @@ def test_multiple_functions():
8598
assert col.fix_value(" ACTIVE ") == "Active "
8699

87100

88-
# Test naming fetaures
101+
# Test naming features
89102

90103

91104
def test_rename():

0 commit comments

Comments
 (0)