Skip to content

Commit

Permalink
remove JSON Schema validation in lieu of ETS (#75)
Browse files Browse the repository at this point in the history
* remove JSON Schema validation in lieu of ETS

* fix flake8
  • Loading branch information
tomkralidis authored Nov 1, 2024
1 parent f34131a commit 39d422c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 111 deletions.
2 changes: 0 additions & 2 deletions pywis_pubsub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from pywis_pubsub.publish import publish
from pywis_pubsub.schema import schema
from pywis_pubsub.subscribe import subscribe
from pywis_pubsub.validation import validate_
from pywis_pubsub.verification import verify


Expand All @@ -47,7 +46,6 @@ def message():
pass


message.add_command(validate_)
message.add_command(verify)

cli.add_command(message)
Expand Down
4 changes: 2 additions & 2 deletions pywis_pubsub/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from pywis_pubsub import util
from pywis_pubsub.message import LINK_TYPES
from pywis_pubsub.mqtt import MQTTPubSubClient
from pywis_pubsub.validation import validate_
from pywis_pubsub.ets import validate


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -248,7 +248,7 @@ def publish(ctx, file_, config, url, topic, datetime_, identifier,

if file_ is not None:
if config.get('validate_message', False):
ctx.invoke(validate_, message=file_)
ctx.invoke(validate, message=file_)
file_.seek(0)
message = json.load(file_)
else:
Expand Down
12 changes: 6 additions & 6 deletions pywis_pubsub/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@

from pywis_pubsub import cli_options
from pywis_pubsub import util
from pywis_pubsub.ets import WNMTestSuite
from pywis_pubsub.geometry import is_message_within_bbox
from pywis_pubsub.hook import load_hook
from pywis_pubsub.message import get_link, get_data
from pywis_pubsub.mqtt import MQTTPubSubClient
from pywis_pubsub.storage import STORAGES
from pywis_pubsub.validation import validate_message
from pywis_pubsub.verification import data_verified


Expand All @@ -51,11 +51,11 @@ def on_message_handler(client, userdata, msg):
try:
if userdata.get('validate_message', False):
LOGGER.debug('Validating message')
success, err = validate_message(msg_dict)
if not success:
LOGGER.error(f'Message is not a valid notification: {err}')
return
except RuntimeError as err:

ts = WNMTestSuite(msg_dict)
_ = ts.run_tests(fail_on_schema_validation=True)

except Exception as err:
LOGGER.error(f'Cannot validate message: {err}')
return

Expand Down
82 changes: 0 additions & 82 deletions pywis_pubsub/validation.py

This file was deleted.

37 changes: 18 additions & 19 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from requests import Session
from pywis_pubsub.ets import WNMTestSuite
from pywis_pubsub.kpi import calculate_grade, WNMKeyPerformanceIndicators
from pywis_pubsub.validation import validate_message
from pywis_pubsub.verification import verify_data

TESTDATA_DIR = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -54,24 +53,6 @@ def tearDown(self):
"""return to pristine state"""
pass

def test_validation(self):
"""Test validation"""

with open(get_abspath('test_valid.json')) as fh:
data = json.load(fh)
is_valid, errors = validate_message(data)
self.assertTrue(is_valid)

with open(get_abspath('test_invalid.json')) as fh:
data = json.load(fh)
is_valid, errors = validate_message(data)
self.assertFalse(is_valid)

with open(get_abspath('test_malformed.json')) as fh:
with self.assertRaises(json.decoder.JSONDecodeError):
data = json.load(fh)
is_valid, errors = validate_message(data)

@patch.object(Session, 'get')
def test_verification(self, mock_get):
"""Test verification"""
Expand Down Expand Up @@ -127,6 +108,24 @@ def test_fail(self):
with self.assertRaises(ValueError):
ts.run_tests(fail_on_schema_validation=True)

with self.assertRaises(ValueError):
with open(get_abspath('test_invalid.json')) as fh:
record = json.load(fh)
ts = WNMTestSuite(record)
results = ts.run_tests(fail_on_schema_validation=True)

codes = [r['code'] for r in results['ets-report']['tests']]

self.assertEqual(codes.count('FAILED'), 1)
self.assertEqual(codes.count('PASSED'), 6)
self.assertEqual(codes.count('SKIPPED'), 0)

with self.assertRaises(json.decoder.JSONDecodeError):
with open(get_abspath('test_malformed.json')) as fh:
record = json.load(fh)
ts = WNMTestSuite(record)
results = ts.run_tests()


class WNMKPITest(unittest.TestCase):
"""WNM KPI tests of tests"""
Expand Down

0 comments on commit 39d422c

Please sign in to comment.