Skip to content

Commit

Permalink
Add tests for validation (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
mboudet authored Oct 14, 2022
1 parent 00e3919 commit fdcec09
Show file tree
Hide file tree
Showing 7 changed files with 517 additions and 20 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/lint_and_package.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Lint
name: Lint and tests
on: [push, pull_request]
jobs:
lint:
Expand All @@ -14,6 +14,22 @@ jobs:
- name: Flake8
run: flake8 checkcel --ignore=E501,W504

pytest:
runs-on: ubuntu-latest
needs: lint
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Install pytest
run: pip install pytest
- name: Install checkcel
run: pip install .
- name: Test
run: pytest -v tests/

pypi:
runs-on: ubuntu-latest
needs: lint
Expand Down
35 changes: 22 additions & 13 deletions checkcel/checkcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
class Checkcel(Checkplate):
def __init__(
self,
source,
source="",
data=None,
format="spreadsheet",
delimiter=",",
sheet=0,
Expand All @@ -22,6 +23,7 @@ def __init__(
self.missing_validators = None
self.missing_fields = None
self.source = source
self.data = data
self.format = format
self.delimiter = delimiter
self.sheet = int(sheet)
Expand All @@ -31,6 +33,9 @@ def __init__(
self.column_set = set()
self.ignore_missing_validators = False

if not (self.source or self.data is not None):
raise Exception("Need to provide either a source or the data (as a pandas dataframe)")

if format not in ["spreadsheet", "tabular"]:
raise Exception("Type must be either spreadsheet or tabular")

Expand Down Expand Up @@ -84,21 +89,25 @@ def _log_missing(self, missing_items):

def validate(self):
self.logger.info(
"\nValidating {}(source={})".format(self.__class__.__name__, self.source)
"\nValidating {}{}".format(self.__class__.__name__, "(source={})".format(self.source) if self.source else "")
)

if self.format == "spreadsheet":
with warnings.catch_warnings():
warnings.simplefilter("ignore")
df = pandas.read_excel(self.source, sheet_name=self.sheet, keep_default_na=False, skiprows=self.row)
else:
df = pandas.read_csv(self.source, sep=self.delimiter, skiprows=self.row)
if self.source:
if self.format == "spreadsheet":
with warnings.catch_warnings():
warnings.simplefilter("ignore")
df = pandas.read_excel(self.source, sheet_name=self.sheet, keep_default_na=False, skiprows=self.row)
else:
df = pandas.read_csv(self.source, sep=self.delimiter, skiprows=self.row)

if len(df) == 0:
self.logger.info(
"\033[1;33m" + "Source file has no data" + "\033[0m"
)
return False

if len(df) == 0:
self.logger.info(
"\033[1;33m" + "Source file has no data" + "\033[0m"
)
return False
else:
df = self.data

df = df.loc[:, ~df.columns.str.contains('^Unnamed')]

Expand Down
2 changes: 1 addition & 1 deletion checkcel/checkplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, validators={}, empty_ok=False, ignore_case=False, ignore_spac
self.ignore_case = ignore_case
self.ignore_space = ignore_space
# self.trim_values = False
for validator in self.validators:
for validator in self.validators.values():
validator._set_attributes(self.empty_ok, self.ignore_case, self.ignore_space)

def load_from_python_file(self, file_path):
Expand Down
16 changes: 11 additions & 5 deletions checkcel/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,14 @@ def validate(self, field, row_number, row={}):
if self.ignore_space:
field = field.strip()

if field == "" and self.empty_ok:
return
if not field:
if self.empty_ok:
return
else:
raise ValidationException(
"Field cannot be empty"
)

if self.unique_with and not self.unique_check:
self._precheck_unique_with(row)

Expand Down Expand Up @@ -807,11 +813,11 @@ def validate(self, field, row_number, row={}):

if self.format == "DD":
regex_lat = r"[-+]?((90(\.0+)?)|([1-8]?\d(\.\d+)?))[NSns]?"
regex_long = r"[-+]?((180(\.0+)?)|([1-8]?\d(\.\d+)?))[wWeE]?"
regex_long = r"[-+]?((180(\.0+)?)|(((1[0-7]\d)|([1-9]?\d))(\.\d+)?))[wWeE]?"

else:
regex_lat = r"((90[°|\s]\s*)(0{1,2}['|\s]\s*)(0{1,2}([.|,]0{1,20})?[\"|\s]\s*)|(([1-8]\d|\d)[°|\s]\s*)(([0-5]\d|\d)['|\s]\s*)(([0-5]\d|\d)([.|,]\d{1,20})?[\"|\s]\s*))[NSns]"
regex_long = r"((180[°|\s]\s*)(0{1,2}['|\s]\s*)(0{1,2}([.|,]0{1,20})?[\"|\s]\s*)|((1[0-7]\d|\d\d|\d)[°|\s]\s*)(([0-5]\d|\d)['|\s]\s*)(([0-5]\d|\d)([.|,]\d{1,20})?[\"|\s]\s*))[EWew]"
regex_lat = r"((([1-8]?\d)(°\s?|\s)([1-5]?\d|60)('\s?|\s)?([1-5]?\d(\.\d+)?|60)(\"\s?|\s)?)|(90(°\s?|\s)0('\s?|\s)0(\"\s?|\s)?))[NSns]?"
regex_long = r"((((1[0-7][0-9])|([0-9]{1,2}))(°\s?|\s)([1-5]?\d|60)('\s?|\s)([1-5]?\d(\.\d+)?|60)(\"\s?|\s)?)|(180(°\s?|\s)0('\s?|\s)0(\"\s?|\s)?))[EWew]?"

if self.only_long:
regex = r"^{}$".format(regex_long)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ odfpy
pandas
email-validator
pyyaml
requests
Empty file added tests/__init__.py
Empty file.
Loading

0 comments on commit fdcec09

Please sign in to comment.