Skip to content

Commit

Permalink
added tests for subset errors
Browse files Browse the repository at this point in the history
  • Loading branch information
culebron committed Sep 3, 2021
1 parent 53a7bbc commit 3b21e3e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ jobs:
- name: Test with pytest
run: |
pytest --cov=erde
coverage html
# coverage html # not used
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ See [the example](examples/2_minimal_cli_app/) for more code and instructions.

Instead of doing this:

df.rename(columns={'oldname1': 'newname1', 'oldname2': 'newname2'}, inplace=True)
df.drop(['oldcol3', 'oldcol4'], inplace=True, axis=1, errors='ignore')
df.rename(columns={'oldname1': 'newname1'}, inplace=True)
df.drop(['oldcol2'], inplace=True, axis=1, errors='ignore')

you can simply do this:

from erde import subset
df = subset(df, 'oldname1: newname1, oldname2: newname2, -oldcol3, -oldcol4, *')
df = subset(df, 'oldname1: newname1, -oldcol2, *')

Or even run this from command line:

erde subset old_file.gpkg oldname1:newname1,oldname2:newname2,-oldcol3,-oldcol4,* new_file.gpkg
erde subset old_file.gpkg oldname1:newname1,-oldcol2,* new_file.gpkg

### Routing

Expand Down
5 changes: 5 additions & 0 deletions erde/op/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ def parse_str(columns):
result = []
for i in columns.split(','):
j = [k.strip() for k in i.strip().split(':')]

for k in j:
if len(k) == 0 or (len(k) == 1 and k.startswith('-')):
raise ValueError(f'Bad column name: "{i}": zero name length.')

if len(j) > 2:
raise ValueError(f"column name must have 0 or 1 colons (:) got {len(i) - 1} in '")

Expand Down
30 changes: 30 additions & 0 deletions tests/test_subset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pandas as pd
from erde.op import subset
from pytest import raises

def test_parse_strings_errors():
good_string = 'old1:new1,old2,old3:new3'
assert subset.parse_str(good_string) == [['old1', 'new1'], ['old2', None], ['old3', 'new3']]

bad_string = 'old1:new1,old2:new2:verynew2'
with raises(ValueError):
subset.parse_str(bad_string)

with raises(ValueError):
subset.parse_str('-old1:new1')

with raises(ValueError):
subset.parse_str('old1,old2:new2,-,old4')

with raises(ValueError):
subset.parse_str('')


def test_main_errors():
df = pd.DataFrame({'col1': range(5), 'col2': range(10, 15), 'col3': range(20, 25)})
for v in [123456, None, True, False, df]:
with raises(TypeError):
subset.main(df, v)

with raises(KeyError):
subset.main(df, 'missing_column')

0 comments on commit 3b21e3e

Please sign in to comment.