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))
def test_python_query_typechecking(): e = Events() e.add_recspan_info(0, 100, {}) e.add_event(0, 10, 11, { "a": 1, "b": "asdf", "c": True, "d": 1.5, "e": None }) p = e.placeholder_event() assert list(p["e"] == 1) == [] assert len(list(p["e"] == None)) == 1 for bad in (True, "asdf"): assert_raises(EventsError, p["a"].__eq__, bad) assert_raises(EventsError, p["a"].__gt__, bad) assert_raises(EventsError, p["a"].__lt__, bad) assert_raises(EventsError, p["a"].__le__, bad) assert_raises(EventsError, p["a"].__ge__, bad) assert_raises(EventsError, p["a"].__and__, p["c"]) assert_raises(EventsError, p["a"].__and__, p["c"]) assert_raises(EventsError, p["a"].__invert__) assert_raises(EventsError, list, p["e"])
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))
def test_matches(): e = Events() e.add_recspan_info(0, 100, {}) e1 = e.add_event(0, 10, 11, {"a": 1, "b": "hi"}) e2 = e.add_event(0, 20, 21, {"a": 2, "b": "hi"}) assert e1.matches("a == 1") assert not e1.matches("a == 2") assert e2.matches("a == 2") assert not e2.matches("a == 1") p = e.placeholder_event() assert list(p.matches("a == 1")) == [e1]
def test_python_query_typechecking(): e = Events() e.add_recspan_info(0, 100, {}) e.add_event(0, 10, 11, {"a": 1, "b": "asdf", "c": True, "d": 1.5, "e": None}) p = e.placeholder_event() assert list(p["e"] == 1) == [] assert len(list(p["e"] == None)) == 1 for bad in (True, "asdf"): assert_raises(EventsError, p["a"].__eq__, bad) assert_raises(EventsError, p["a"].__gt__, bad) assert_raises(EventsError, p["a"].__lt__, bad) assert_raises(EventsError, p["a"].__le__, bad) assert_raises(EventsError, p["a"].__ge__, bad) assert_raises(EventsError, p["a"].__and__, p["c"]) assert_raises(EventsError, p["a"].__and__, p["c"]) assert_raises(EventsError, p["a"].__invert__) assert_raises(EventsError, list, p["e"])
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_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])