def test_ip_range() -> None: bind_vars: Json = {} model = QueryModel( Query.by(IsTerm(["foo"])).on_section("reported"), Model.empty()) result = in_subnet( "crs", bind_vars, FunctionTerm("in_subnet", "foo.bla", ["192.168.1.0/24"]), model) assert result == "BIT_AND(IPV4_TO_NUMBER(crs.foo.bla), 4294967040) == @0" assert bind_vars["0"] == 3232235776
def test_has_key() -> None: bind_vars: Json = {} model = QueryModel(Query.by("foo"), Model.empty()) result = has_key("crs", bind_vars, FunctionTerm("has_key", "foo.bla", [["a", "b", "c"]]), model) assert result == "@fn0 ALL IN ATTRIBUTES(crs.foo.bla, true)" assert bind_vars["fn0"] == ["a", "b", "c"] bind_vars2: Json = {} result = has_key("crs", bind_vars2, FunctionTerm("has_key", "foo.bla", ["a"]), model) assert result == "HAS(crs.foo.bla, @fn0)" assert bind_vars2["fn0"] == "a"
async def test_emit_recurrent_events() -> None: message_bus = MessageBus() sender = InMemoryEventSender() model = ModelHandlerStatic(Model.empty()) sub = SubscriptionHandler( InMemoryDb[Subscriber](Subscriber, lambda x: x.id), message_bus) queue = WorkerTaskQueue() fast = timedelta(seconds=0.001) periodic = emit_recurrent_events(sender, model, sub, queue, message_bus, fast, fast) await periodic.start() while len(sender.events) < 3: await asyncio.sleep(0.01) await periodic.stop() model_event, subscriber_event, worker_event = sender.events[0:3] assert model_event.kind == CoreEvent.ModelInfo assert model_event.counters["model_count"] == 0 assert subscriber_event.kind == CoreEvent.SubscriberInfo assert subscriber_event.counters["subscriber_count"] == 0 assert worker_event.kind == CoreEvent.WorkerQueueInfo assert worker_event.counters["worker_count"] == 0