def test_misc_queries(): # ANY, at, __iter__, __len__ e = Events() e.add_recspan_info(0, 100, {}) e.add_recspan_info(1, 100, {}) e.add_event(0, 20, 21, {"a": -1}) e.add_event(0, 10, 11, {"a": 1}) e.add_event(0, 30, 31, {"a": 100}) e.add_event(1, 15, 16, {}) assert len(e.events_query(True)) == 4 # Always sorted by index: assert [ev.start_tick for ev in e.events_query(True)] == [10, 20, 30, 15]
def test_Event(): # set/get/del, index # dict methods e = Events() r1 = e.add_recspan_info(1, 100, {}) d = {"a": 1, "b": "hello", "c": True} ev1 = e.add_event(1, 10, 12, d) assert ev1.recspan_id == 1 assert ev1.start_tick == 10 assert ev1.stop_tick == 12 assert dict(ev1.iteritems()) == d ev1["added"] = True assert ev1["added"] == True assert_raises(KeyError, ev1.__getitem__, "notadded") assert_raises(KeyError, ev1.__delitem__, "notadded") del ev1["added"] assert_raises(KeyError, ev1.__getitem__, "added") assert_raises(KeyError, ev1.__delitem__, "added") assert "a" in ev1 assert "added" not in ev1 assert_raises(ValueError, ev1.__setitem__, "c", "asdf") assert ev1["c"] == True assert ev1.get("c") == True assert ev1.get("notadded") is None assert ev1.get("notadded", 10) == 10 assert sorted(list(ev1)) == ["a", "b", "c"] assert sorted(ev1.iterkeys()) == ["a", "b", "c"] assert sorted(ev1.itervalues()) == sorted(d.itervalues()) assert list(ev1.iterkeys()) == ev1.keys() assert list(ev1.itervalues()) == ev1.values() assert list(ev1.iteritems()) == ev1.items() assert ev1.has_key("a") assert not ev1.has_key("z") assert ev1.overlaps(ev1) assert not ev1.overlaps(0, 9, 11) assert ev1.overlaps(1, 9, 11) assert ev1.overlaps(1, 10, 12) assert ev1.overlaps(1, 10, 11) assert ev1.overlaps(1, 11, 12) assert not ev1.overlaps(1, 0, 2) assert not ev1.overlaps(1, 100, 102) # Check half-openness assert not ev1.overlaps(0, 9, 10) assert not ev1.overlaps(0, 12, 15) # Nothing overlaps an empty interval assert not ev1.overlaps(0, 11, 11) # Check .update ev1.update({"new1": "asdf", "new2": 3.14}) assert ev1["new1"] == "asdf" assert ev1["new2"] == 3.14 # smoke test repr(ev1) assert len(e.events_query(True)) == 1 ev1.delete() assert len(e.events_query(True)) == 0 # XX: maybe this should be a ValueError or something, but checking for # whether the event object itself exists in __getitem__ is extra work that # doesn't seem worth bothering with. assert_raises(KeyError, ev1.__getitem__, "a") assert_raises(EventsError, ev1.__setitem__, "a", 1) # smoke test for deleted one repr(ev1)
def test_python_query(): # all operators # types (esp. including None) # index e = Events() e.add_recspan_info(0, 100, {"recspan_zero": True}) e.add_recspan_info(1, 100, {"recspan_zero": False, "recspan_extra": "hi"}) ev10 = e.add_event(0, 10, 11, {"a": 1, "b": "asdf", "c": True, "d": 1.5, "e": None}) ev20 = e.add_event(1, 20, 25, {"a": -1, "b": "fdsa", "c": False, "d": -3.14, "e": None}) ev21 = e.add_event(0, 21, 26, {"a": 1, "b": "asdf", "c": False, "d": -3.14, "e": 123}) p = e.placeholder_event() def t(q, expected): assert [ev.start_tick for ev in q] == expected assert_raises(TypeError, lambda q: not q, e.events_query(True)) assert_raises(EventsError, p.start_tick.__eq__, []) t(p.start_tick == 10, [10]) t(p.start_tick != 10, [21, 20]) t(p.start_tick < 10, []) t(p.start_tick > 10, [21, 20]) t(p.start_tick >= 20, [21, 20]) t(p.start_tick <= 20, [10, 20]) t(~(p.start_tick == 20), [10, 21]) t(~(p.start_tick != 20), [20]) t(p["a"] == 1, [10, 21]) t(p["a"] != 1, [20]) t(p["a"] < 1, [20]) t(p["a"] > 1, []) t(p["a"] >= 1, [10, 21]) t(p["a"] <= 1, [10, 21, 20]) t(~(p["a"] == 1), [20]) t(~(p["a"] != 1), [10, 21]) t(p["a"] == 1.5, []) t(p["a"] < 1.5, [10, 21, 20]) t(p["a"] < 0.5, [20]) t(p["a"] > 0.5, [10, 21]) t(p["b"] == "asdf", [10, 21]) t(p["b"] != "asdf", [20]) t(p["b"] < "asdf", []) t(p["b"] > "asdf", [20]) t(p["b"] >= "asdf", [10, 21, 20]) t(p["b"] <= "asdf", [10, 21]) t(p["b"] <= "b", [10, 21]) t(p["b"] >= "b", [20]) t(~(p["b"] == "asdf"), [20]) t(~(p["b"] != "asdf"), [10, 21]) t(p["c"] == True, [10]) t(p["c"] != True, [21, 20]) t(p["c"] == False, [21, 20]) t(p["c"] != False, [10]) t(p["c"], [10]) t(~p["c"], [21, 20]) t(p["c"] < True, [21, 20]) t(p["c"] <= True, [10, 21, 20]) t(p["c"] > True, []) t(p["c"] > False, [10]) t(p["c"] >= False, [10, 21, 20]) t(~(p["c"] == True), [21, 20]) t(~(p["c"] != True), [10]) t(p["d"] == 1.5, [10]) t(p["d"] != 1.5, [21, 20]) t(p["d"] < 1.5, [21, 20]) t(p["d"] > 1.5, []) t(p["d"] >= 1.5, [10]) t(p["d"] <= 1.5, [10, 21, 20]) t(~(p["d"] == 1.5), [21, 20]) t(~(p["d"] != 1.5), [10]) t(p["d"] == 1, []) t(p["d"] < 10, [10, 21, 20]) t(p["d"] < 1, [21, 20]) t(p["d"] > 1, [10]) t(p["e"] == None, [10, 20]) t(p["e"] != None, [21]) t(p.has_key("e"), [10, 21, 20]) t(~p.has_key("e"), []) t(p["nonexistent"] == 10, []) t(p["nonexistent"] != 10, []) t(p["nonexistent"] == None, []) t(p["nonexistent"].exists(), []) t(p.has_key("nonexistent"), []) t(~p["nonexistent"].exists(), [10, 21, 20]) t(~p.has_key("nonexistent"), [10, 21, 20]) t(p["a"] > p["d"], [21, 20]) t(p["a"] < p["d"], [10]) t((p.start_tick < 21) & (p["a"] == 1), [10]) t((p.start_tick < 21) | (p["a"] == 1), [10, 21, 20]) t(~((p.start_tick < 21) & (p["a"] == 1)), [21, 20]) t(~((p.start_tick < 21) | (p["a"] == 1)), []) t(p.stop_tick == 26, [21]) t(p.overlaps(ev10), [10]) t(p.overlaps(ev20), [20]) t(p.overlaps(ev21), [21]) t(p.overlaps(0, 5, 10), []) t(p.overlaps(0, 5, 11), [10]) t(p.overlaps(0, 11, 20), []) t(p.overlaps(0, 11, 100), [21]) for i in xrange(20, 25): t(p.overlaps(1, i, i + 1), [20]) assert_raises(ValueError, p.overlaps, p) t(p.recspan_info["recspan_zero"], [10, 21]) t(~p.recspan_info["recspan_zero"], [20]) t(p.recspan_info["recspan_extra"].exists(), [20])
def test_events_method(): # all the different calling conventions e = Events() e.add_recspan_info(0, 100, {}) e.add_event(0, 10, 11, {"a": 1, "b": True}) e.add_event(0, 20, 21, {"a": -1, "b": True}) assert [ev.start_tick for ev in e.events_query()] == [10, 20] assert [ev.start_tick for ev in e.events_query(True)] == [10, 20] assert not list(e.events_query(False)) assert [ev.start_tick for ev in e.events_query({"a": 1})] == [10] assert [ev.start_tick for ev in e.events_query({"a": 1, "b": True})] == [10] assert [ev.start_tick for ev in e.events_query({"a": 1, "b": False})] == [] assert [ev.start_tick for ev in e.events_query({"b": True})] == [10, 20] assert [ev.start_tick for ev in e.events_query({"_RECSPAN_ID": 0})] == [10, 20] assert [ev.start_tick for ev in e.events_query({"_RECSPAN_ID": 1})] == [] assert [ev.start_tick for ev in e.events_query({"_START_TICK": 10})] == [10] assert [ev.start_tick for ev in e.events_query({"_STOP_TICK": 11})] == [10] assert [ev.start_tick for ev in e.events_query(e.placeholder_event()["a"] == 1)] == [10] assert_raises(ValueError, e.events_query, []) e2 = Events() assert_raises(ValueError, e.events_query, e2.events_query(True))