class Token: """Token for accessing Kaptos server.""" id: s.uuid(description="Identifies the token.") type: s.str(description="Token type.", enum={"password", "session", "reset"}) value: s.str(description="Token value.") subject: s.uuid(description="Reference to subject of token.") created: s.datetime(description="Date and time token was created.") expires: s.datetime(description="Date and time token expires.") _required = "type value subject created"
class Report: """Station reception report.""" id: s.uuid(description="Identifies the reception report.") task_id: s.uuid(description="Identifies the task that reception report is for.") station_id: s.uuid( description="Identifies the station filing the reception report." ) time: s.datetime(description="Date and time that the signal was received.") frequency: s.int(description="Frequency of received signal, in hertz.") modulation: ks.modulation(description="Modulation of received signal.") location: roax.geo.Point( description="Location of the receiving station at the time of reception." ) bearing: ks.bearing(description="Bearing toward received signal, in degrees true.") duration: s.int( description="Duration of received signal, in seconds rounded-up.", min=1 ) strength: s.int(description="Strength of the received signal, in dBm.") nmea: s.str( description="$GPRMC NMEA sentence received from GPS at the time the signal was detected." ) _required = ( "task_id station_id time frequency modulation location bearing duration strength nmea", )
def test_crud(resource): body = DC( id=uuid4(), str="string", dict={"a": 1}, list=[1, 2, 3], _set={"foo", "bar"}, int=1, float=2.3, bool=True, bytes=b"12345", date=s.date().str_decode("2019-01-01"), datetime=s.datetime().str_decode("2019-01-01T01:01:01Z"), ) resource.create(body.id, body) assert resource.read(body.id) == body body.dict = {"a": 2} body.list = [2, 3, 4] body._set = None body.int = 2 body.float = 1.0 body.bool = False body.bytes = None body.date = None body.datetime = None resource.update(body.id, body) assert resource.read(body.id) == body resource.patch(body.id, {"str": "bacon"}) body = resource.read(body.id) assert body.str == "bacon" resource.delete(body.id) with pytest.raises(r.NotFound): resource.read(body.id)
class User: """User who owns/administers teams, tasks and/or stations.""" id: s.uuid(description="Identifies the user.") email: s.str(description="Email address of the user.") password: s.bytes( description="Hash of the password for user to authenticate with server." ) name: s.str(description="Full name of the user.") call_sign: s.str(description="Call sign of the user.") status: s.str(enum={"active", "suspended"}, description="Status of the user.") created: s.datetime(description="Date and time user record was created.") failures: s.list( items=s.datetime(), description="Date and time of recent consecutive login failures.", ) _required = "email name status created"
class Task: """Monitoring task.""" id: s.uuid(description="Identifies the monitoring task.") team_id: s.uuid( description= "Identifies the organization the monitoring task managed under.") start_time: s.datetime( description= "Date and time (inclusive) that the task is scheduled to begin.") end_time: s.datetime( description= "Date and time (inclusive) that the task is scheduled to end.") frequency: s.int(description="Dial frequency to monitor, in hertz.") modulation: ks.modulation( description="Modulation of signals to be processed.") bandwidth: s.int(description="Monitoring bandwidth, in hertz.") _required = "team_id start_time end_time frequency modulation bandwidth"
def test( self, a: s.list(s.str()), b: s.set(s.str()), c: s.int(), d: s.float(), e: s.bool(), f: s.bytes(), g: s.datetime(), h: s.uuid(), ): pass
class DC: id: s.uuid() str: s.str(nullable=True) dict: s.dict({"a": s.int()}, nullable=True) list: s.list(s.int(), nullable=True) _set: s.set(s.str(), nullable=True) int: s.int(nullable=True) float: s.float(nullable=True) bool: s.bool(nullable=True) bytes: s.bytes(format="byte", nullable=True) date: s.date(nullable=True) datetime: s.datetime(nullable=True)
class Signal: """Detected signal, computed from station reception reports.""" id: s.uuid(description="Identifies signal.") task_id: s.uuid(description="Identifies task that signal is for.") report_ids: s.set(description="Station receiption reports of signal.", items=s.uuid()) time: s.datetime(description="Date and time of signal.") duration: s.int(description="Duration of signal, in seconds.", min=1) location: roax.geo.Point( description="Computed location of transmitting station.") cep: s.int(description="Circle error probable of location, in metres.") _required = "task_id report_ids time duration location cep"
def test_datetime_allow_none(self): self.assertEqual(s.datetime(nullable=True).json_encode(None), None)
def test_datetime_str_decode_error(self): self._error(s.datetime().str_decode, "1425691090160")
def test_datetime_disallow_none(self): self._error(s.datetime().json_encode, None)
class TestSecurityScheme(HTTPBasicSecurityScheme): def authenticate(self, user_id, password): if user_id == "sparky" and password == "punkydoodle": return {"user_id": user_id, "role": "god"} _scheme = TestSecurityScheme("WallyWorld") http1 = TestSecurityRequirement(_scheme) _r1_schema = s.dict({ "id": s.str(), "foo": s.int(), "bar": s.bool(), "dt": s.datetime(required=False), }) class _Resource1(Resource): schema = _r1_schema @operation( params={ "id": _r1_schema.properties["id"], "_body": _r1_schema }, returns=s.dict({"id": _r1_schema.properties["id"]}), security=[], )
def test_datetime_json_decode_missing_tz(self): self.assertEqual(s.datetime().str_decode("2020-10-11T12:13:14"), datetime(2020, 10, 11, 12, 13, 14, 0, _UTC))
def test_datetime_str_encode_truncate_microsecond(self): self.assertEqual( s.datetime().str_encode(datetime(2018, 1, 2, 3, 4, 5, 123456, _UTC)), "2018-01-02T03:04:05Z")
def test_datetime_str_encode_truncate_microsecond(): assert (s.datetime().str_encode(datetime(2018, 1, 2, 3, 4, 5, 123456, _UTC)) == "2018-01-02T03:04:05Z")
def test_datetime_str_decode_truncate_microsecond(): assert s.datetime().str_decode("2018-01-02T03:04:05.123456Z") == datetime( 2018, 1, 2, 3, 4, 5, 0, _UTC)
def test_datetime_json_decode_offset(self): self.assertEqual(s.datetime().json_decode("2019-09-10T11:12:13+01:00"), datetime(2019, 9, 10, 10, 12, 13, 0, _UTC))
def test_datetime_json_encode_success_aware(self): self.assertEqual( s.datetime().json_encode(datetime(2017, 6, 7, 8, 9, 10, 0, _UTC)), "2017-06-07T08:09:10Z")
def test_datetime_json_encode_error(self): self._error(s.datetime().json_encode, "definitely_not_a_datetime")
def test_datetime_json_encode_success_naive(self): self.assertEqual( s.datetime().json_encode(datetime(2016, 7, 8, 9, 10, 11)), "2016-07-08T09:10:11Z")
def test_datetime_validate_type_error(self): self._error(s.datetime().validate, "this_is_not_a_datetime")
def test_datetime_validate_type_success(self): s.datetime().validate(datetime(2015, 6, 7, 8, 9, 10, 0, _UTC))
def test_datetime_str_encode_retain_microsecond(self): self.assertEqual( s.datetime(fractional=True).str_encode( datetime(2018, 1, 2, 3, 4, 5, 123456, _UTC)), "2018-01-02T03:04:05.123456Z")
def test_datetime_json_decode_error(self): self._error(s.datetime().json_decode, "1425691090159")
def test_datetime_str_decode_truncate_microsecond(self): self.assertEqual( s.datetime().str_decode("2018-01-02T03:04:05.123456Z"), datetime(2018, 1, 2, 3, 4, 5, 0, _UTC))
def test_datetime_str_decode_z(self): self.assertEqual(s.datetime().str_decode("2021-11-12T13:14:15Z"), datetime(2021, 11, 12, 13, 14, 15, 0, _UTC))
"realm": self.realm, "user_id": user_id, "role": "god", } _scheme = _TestBasicSecurityScheme("WallyWorld") http1 = _TestBasicSecurityRequirement(_scheme) _r1_schema = s.dict( { "id": s.str(), "foo": s.int(), "bar": s.bool(), "dt": s.datetime() }, "id foo bar") class _BasicResource1(Resource): schema = _r1_schema @operation(security=[]) def create(self, id: _r1_schema.props["id"], _body: _r1_schema) -> s.dict({"id": _r1_schema.props["id"]}): return {"id": id} @operation(security=[]) def update(self, id: _r1_schema.props["id"], _body: _r1_schema): return
def test_datetime_str_decode_offset(self): self.assertEqual(s.datetime().str_decode("2022-12-13T14:15:16+01:00"), datetime(2022, 12, 13, 13, 15, 16, 0, _UTC))
def test_datetime_json_decode_z(self): self.assertEqual(s.datetime().json_decode("2018-08-09T10:11:12Z"), datetime(2018, 8, 9, 10, 11, 12, 0, _UTC))
import pytest import roax.monitor import roax.schema as s from datetime import datetime, timedelta, timezone from roax.monitor import Measurement _tags = {"name": "test"} _dt = lambda string: s.datetime().str_decode(string) _now = lambda: datetime.now(tz=timezone.utc) def test_simple_counter_type(): simple = roax.monitor.SimpleMonitor() _type = "counter" simple.track("test", _type, _tags, 60, 60) simple.record(Measurement(_tags, _dt("2018-12-01T00:00:00Z"), _type, 1)) simple.record(Measurement(_tags, _dt("2018-12-01T00:00:10.1Z"), _type, 2)) simple.record(Measurement(_tags, _dt("2018-12-01T00:00:20.2Z"), _type, 3)) simple.record(Measurement(_tags, _dt("2018-12-01T00:00:30.3Z"), _type, 4)) simple.record(Measurement(_tags, _dt("2018-12-01T00:00:59.999Z"), _type, 5)) simple.record(Measurement(_tags, _dt("2018-12-01T00:01:01Z"), _type, 10)) simple.record(Measurement(_tags, _dt("2018-12-01T00:01:02Z"), _type, 20)) simple.record(Measurement(_tags, _dt("2018-12-01T00:01:03Z"), _type, 30)) simple.record(Measurement(_tags, _dt("2018-12-01T00:01:04Z"), _type, 40)) simple.record(Measurement(_tags, _dt("2018-12-01T00:01:05Z"), _type, 50)) series = simple.series["test"] assert series.type == _type