From 19b60a4e87ff59b22eef7de2c0c45a2ff4fe2632 Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Wed, 28 Feb 2024 12:27:59 +1100 Subject: [PATCH 1/4] MAINT: added separate column validation --- orsopy/fileio/base.py | 30 ++++++++++++++++++++++++++++++ orsopy/fileio/tests/test_schema.py | 6 +++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/orsopy/fileio/base.py b/orsopy/fileio/base.py index 3875186..5c6e266 100644 --- a/orsopy/fileio/base.py +++ b/orsopy/fileio/base.py @@ -954,6 +954,33 @@ def _read_header_data(file: Union[TextIO, str], validate: bool = False) -> Tuple return dct_list, data, version +def _validate_columns(cols: List): + """ + Checks that the first four columns have the correct properties, i.e. names, units, etc. + + :param cols: list of column information entries. + """ + if len(cols) < 2: + raise ValueError( + "The ORSO specification requires at least 'Qz' and 'R' columns to be present" + ) + + names = ["Qz", "R", "sR", "sQz"] + for idx, col in enumerate(cols): + col_name = col.get("name", None) + if col_name is not None and col_name != names[idx]: + raise ValueError( + "The first four columns should be named 'Qz', 'R', 'sR', 'sQz')" + ) + if cols[0]["unit"] not in ["1/angstrom", "1/nm"]: + raise ValueError( + "The Qz column should have units of '1/angstrom' or '1/nm'" + ) + # sQz unit has to be the same as Qz + if len(cols) > 3 and cols[3].get("unit", None) is not None and cols[0]["unit"] != cols[3]["unit"]: + raise ValueError("The sQz column should have the same units as the Qz column") + + def _validate_header_data(dct_list: List[dict]): """ Checks whether a json dictionary corresponds to a valid ORSO header. @@ -971,6 +998,9 @@ def _validate_header_data(dct_list: List[dict]): """ import jsonschema + for dct in dct_list: + _validate_columns(dct.get("columns", [])) + vi = sys.version_info if vi.minor < 7: warnings.warn( diff --git a/orsopy/fileio/tests/test_schema.py b/orsopy/fileio/tests/test_schema.py index 5096b24..37ef7eb 100644 --- a/orsopy/fileio/tests/test_schema.py +++ b/orsopy/fileio/tests/test_schema.py @@ -44,7 +44,7 @@ def test_empty_ort(self): def test_wrong_schema(self): vi = sys.version_info - with self.assertRaises(jsonschema.exceptions.ValidationError): + with self.assertRaises(ValueError): _validate_header_data([{}]) test = fileio.Orso.empty() @@ -53,7 +53,7 @@ def test_wrong_schema(self): with self.assertWarns(fileio.base.ORSOSchemaWarning): _validate_header_data([test.to_dict()]) else: - with self.assertRaises(jsonschema.exceptions.ValidationError): + with self.assertRaises(ValueError): _validate_header_data([test.to_dict()]) test.columns[0].unit = "1/nm" @@ -64,5 +64,5 @@ def test_wrong_schema(self): with self.assertWarns(fileio.base.ORSOSchemaWarning): _validate_header_data([test.to_dict()]) else: - with self.assertRaises(jsonschema.exceptions.ValidationError): + with self.assertRaises(ValueError): _validate_header_data([test.to_dict()]) From bba5bc1e782d232ad455e7da456b27196ca65423 Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Wed, 28 Feb 2024 12:32:09 +1100 Subject: [PATCH 2/4] MAINT: added separate column validation --- orsopy/fileio/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/orsopy/fileio/base.py b/orsopy/fileio/base.py index 5c6e266..d71f69c 100644 --- a/orsopy/fileio/base.py +++ b/orsopy/fileio/base.py @@ -966,9 +966,9 @@ def _validate_columns(cols: List): ) names = ["Qz", "R", "sR", "sQz"] - for idx, col in enumerate(cols): + for name, col in zip(names, cols): col_name = col.get("name", None) - if col_name is not None and col_name != names[idx]: + if col_name is not None and col_name != name: raise ValueError( "The first four columns should be named 'Qz', 'R', 'sR', 'sQz')" ) From 3e0e6111251ea7a2c869d9607694921a61e57ca4 Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Wed, 28 Feb 2024 12:46:14 +1100 Subject: [PATCH 3/4] TST: can validate columns on 3.6 --- orsopy/fileio/tests/test_schema.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/orsopy/fileio/tests/test_schema.py b/orsopy/fileio/tests/test_schema.py index 37ef7eb..ae64211 100644 --- a/orsopy/fileio/tests/test_schema.py +++ b/orsopy/fileio/tests/test_schema.py @@ -49,20 +49,11 @@ def test_wrong_schema(self): test = fileio.Orso.empty() test.columns[0].unit = "test_fail_unit" - if vi.minor < 7: - with self.assertWarns(fileio.base.ORSOSchemaWarning): - _validate_header_data([test.to_dict()]) - else: - with self.assertRaises(ValueError): - _validate_header_data([test.to_dict()]) + with self.assertRaises(ValueError): + _validate_header_data([test.to_dict()]) test.columns[0].unit = "1/nm" _validate_header_data([test.to_dict()]) test.columns[1].name = "NotRightColumn" - - if vi.minor < 7: - with self.assertWarns(fileio.base.ORSOSchemaWarning): - _validate_header_data([test.to_dict()]) - else: - with self.assertRaises(ValueError): - _validate_header_data([test.to_dict()]) + with self.assertRaises(ValueError): + _validate_header_data([test.to_dict()]) From 4f3582a902602955d77c0ab2d3bb87ec33174de7 Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Wed, 28 Feb 2024 12:50:06 +1100 Subject: [PATCH 4/4] LINT --- orsopy/fileio/tests/test_schema.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/orsopy/fileio/tests/test_schema.py b/orsopy/fileio/tests/test_schema.py index ae64211..629a116 100644 --- a/orsopy/fileio/tests/test_schema.py +++ b/orsopy/fileio/tests/test_schema.py @@ -1,6 +1,5 @@ import json import unittest -import sys from pathlib import Path @@ -42,8 +41,6 @@ def test_empty_ort(self): _validate_header_data([test.to_dict()]) def test_wrong_schema(self): - vi = sys.version_info - with self.assertRaises(ValueError): _validate_header_data([{}]) test = fileio.Orso.empty()