Files
upstream-aw-core/tests/test_schemas.py
upstream-mirror 8757f2fd5e
Some checks failed
Build / Test on ubuntu-latest, py-3.8 (push) Successful in 4m21s
Lint / lint (push) Successful in 25s
Build / Test on macOS-latest, py-3.8 (push) Has been cancelled
Build / Test on windows-latest, py-3.8 (push) Has been cancelled
CodeQL / Analyze (python) (push) Failing after 1s
snapshot of https://github.com/ActivityWatch/aw-core.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:17:07 +05:00

61 lines
1.8 KiB
Python

import unittest
from jsonschema import validate as _validate, FormatChecker
from jsonschema.exceptions import ValidationError
from aw_core import schema
from aw_core.models import Event
# TODO: Include date-time format
# https://python-jsonschema.readthedocs.io/en/latest/validate/#jsonschema.FormatChecker
# The default FormatChecker, uses the date-time checker
fc = FormatChecker(["date-time"])
valid_timestamp = "1937-01-01T12:00:27.87+00:20"
event_schema = schema.get_json_schema("event")
class EventSchemaTest(unittest.TestCase):
def setUp(self):
self.schema = event_schema
def validate(self, obj):
_validate(obj, self.schema, format_checker=fc)
def test_event(self):
event = Event(timestamp=valid_timestamp, data={"label": "test"})
self.validate(event.to_json_dict())
def test_data(self):
self.validate(
{"timestamp": valid_timestamp, "data": {"label": "test", "number": 1.1}}
)
def test_timestamp(self):
self.validate({"timestamp": valid_timestamp})
def test_timestamp_invalid_string(self):
with self.assertRaises(ValidationError):
self.validate({"timestamp": ""})
with self.assertRaises(ValidationError):
self.validate({"timestamp": "123"})
def test_timestamp_invalid_number(self):
with self.assertRaises(ValidationError):
self.validate({"timestamp": 2})
def test_duration(self):
self.validate({"timestamp": valid_timestamp, "duration": 1000})
self.validate({"timestamp": valid_timestamp, "duration": 3.13})
def test_duration_invalid_string(self):
with self.assertRaises(ValidationError):
self.validate({"timestamp": valid_timestamp, "duration": "not a number"})
if __name__ == "__main__":
unittest.main()