def test_tdataset(): a = App("testkey") t1 = a.objects.create("obj1", key="key1") t1.insert_array( [{"t": 2, "d": 73}, {"t": 5, "d": 84}, {"t": 8, "d": 81}, {"t": 11, "d": 79}] ) ds = Dataset(a, t1=0, t2=8.1, dt=2) ds.add("temperature", t1) res = ds.run() assert 5 == len(res) lists_equal( res, [ {"t": 0, "d": {"temperature": 73}}, {"t": 2, "d": {"temperature": 73}}, {"t": 4, "d": {"temperature": 84}}, {"t": 6, "d": {"temperature": 84}}, {"t": 8, "d": {"temperature": 81}}, ], ) for o in a.objects(): o.delete()
def test_basics(): a = App("testkey") assert a.owner.username == "test" a.owner.name = "Myname" assert a.owner.name == "Myname" assert len(a.objects()) == 0 o = a.objects.create("myobj", {"schema": {"type": "number"}}) assert o.name == "myobj" assert o.type == "timeseries" assert len(a.objects()) == 1 assert o == a.objects[o.id] assert o.length() == 0 o.append(2) assert o.length() == 1 d = o[:] assert len(d) == 1 assert d[0]["d"] == 2 assert "dt" not in d[0] o.append(3, duration=9) d = o[:] assert len(d) == 2 assert d[1]["d"] == 3 assert d[1]["dt"] == 9 o.remove() # Clear the timeseries assert len(o) == 0 o.delete() assert len(a.objects()) == 0
def test_metamod(): a = App("testkey") o = a.objects.create("myobj", otype="timeseries") o.meta = {"schema": {"type": "number"}} assert o.cached_data["meta"]["schema"]["type"] == "number" assert o.meta["schema"]["type"] == "number" assert o.meta.schema["type"] == "number" with pytest.raises(Exception): o.meta = {"foo": "bar"} with pytest.raises(Exception): o.meta = {"schema": "lol"} del o.meta.schema o.read( ) # TODO: this is currently needed because the schema is reset to default on server, and this is not reflected in local cache assert o.meta.schema is not None assert len(o.meta.schema) == 0 o.meta.schema = {"type": "number"} assert o.meta.schema["type"] == "number" with pytest.raises(Exception): o.meta.lol = "lel" for o in a.objects(): o.delete()
def test_merge(): a = App("testkey") t1 = a.objects.create("obj1", key="key1") t2 = a.objects.create("obj2", key="key2") t3 = a.objects.create("obj3", key="key3") t1.insert_array( [ {"t": 1, "d": 1}, {"t": 2, "d": 1}, {"t": 3, "d": 1}, {"t": 4, "d": 1}, {"t": 5, "d": 1}, ] ) t2.insert_array( [ {"t": 1.1, "d": 2}, {"t": 2.1, "d": 2}, {"t": 3.1, "d": 2}, {"t": 4.1, "d": 2}, {"t": 5.1, "d": 2}, ] ) t3.insert_array( [ {"t": 1.2, "d": 3}, {"t": 2.2, "d": 3}, {"t": 3.3, "d": 3}, {"t": 4.4, "d": 3}, {"t": 5.5, "d": 3}, ] ) m = Merge(a) m.add(t1) m.add(t2.id, t1=3.0) m.add(t3, i1=1, i2=2) result = m.run() lists_equal( result, [ {"t": 1, "d": 1}, {"t": 2, "d": 1}, {"t": 2.2, "d": 3}, {"t": 3, "d": 1}, {"t": 3.1, "d": 2}, {"t": 4, "d": 1}, {"t": 4.1, "d": 2}, {"t": 5, "d": 1}, {"t": 5.1, "d": 2}, ], ) for o in a.objects(): o.delete()
async def test_objects_async(): app = App("testkey",session="async") objs = await app.objects() assert len(objs) == 0 obj = await app.objects.create("My Timeseries", type="timeseries", meta={"schema":{"type":"number"}}, tags="myts mydata", key="myts") objs = await app.objects() assert len(objs) == 1 assert objs[0] == obj assert objs[0].id==obj.id assert objs[0].meta == obj.meta assert objs[0]["name"] == "My Timeseries" assert objs[0]["type"] == "timeseries" assert (await (obj.name)) == "My Timeseries" # Reading and Updating Properties # Reads the key property from the server assert (await obj.key) == "myts" # Uses the previously read cached data, avoiding a server query assert obj["key"] == "myts" await obj.read() assert obj["key"]=="myts" await obj.update(description="My description") assert obj["description"] == "My description" assert obj.meta == {"schema": {"type": "number"}} assert (await obj.meta()) == {"schema": {"type": "number"}} await obj.meta.update(schema={"type": "string"}) assert obj.meta == {"schema": {"type": "string"}} assert (await obj.meta()) == {"schema": {"type": "string"}} await obj.meta.delete("schema") assert (await obj.meta()) == {"schema": {}} assert (await app.objects(key="myts"))[0] == obj with pytest.raises(Exception): assert obj["app"]==app with pytest.raises(Exception): assert (await obj.app) == app await app.read() assert obj["app"]==app assert (await obj.app) == app await obj.delete() assert len(await (app.objects()))==0
def test_app(): app = App("testkey",session="sync") assert app.id == "self" app.read() assert app.id!="self" app.description = "hello worldd" assert app["description"] == "hello worldd" assert app["owner"]["username"]=="test" assert app.owner["username"]=="test"
async def test_ts_async(): app = App("testkey",session="async") obj = await app.objects.create("My Timeseries", type="timeseries", meta={"schema":{"type":"number"}}, tags="myts mydata", key="myts") assert (await obj.length())==0 # Add a datapoint 5 with current timestamp to the series await obj.append(5) assert (await obj(i=-1))[0]["d"]==5 # Insert the given array of data await obj.insert_array([{"d": 6, "t": time.time()},{"d": 7, "t": time.time()+0.01, "dt": 5.3}]) assert (await obj.length())==3 assert (await obj(i=-1))[0]["d"]==7 assert (await obj(i1=1)).d()==[6,7] ts = await obj(t1="now-1d") assert len(ts)==3 ts = await obj(t1="now-1d",t2="now-10s") assert len(ts)==0 await obj.remove(i=-1) assert (await obj.length())==2 assert (await obj[-1])["d"]==6 await obj.delete()
def test_ts(): a = App("testkey") ts = a.objects.create("myts", key="key1") assert len(ts) == 0 ts.insert("hi!") assert len(ts) == 1 assert len(ts(t1="now-10s")) == 1 assert len(ts(t1="now")) == 0
def test_metamod(): a = App("testkey") o = a.objects.create("myobj", otype="timeseries") o.meta = {"schema": {"type": "number"}} assert o.cached_data["meta"]["schema"]["type"] == "number" assert o.cached_data["meta"]["actor"] == False assert o.meta["schema"]["type"] == "number" assert o.meta["actor"] == False
def test_notifications(): a = App("testkey") assert len(a.notifications()) == 0 a.notify("hi", "hello") assert len(a.notifications()) == 1 a.notifications.delete("hi") assert len(a.notifications()) == 0
def test_appscope(): # Makes sure that removing owner's access doesn't affect the App's access a = App("testkey") o = a.objects.create("myobj") assert o.access == "*" o.key = "lol" o.owner_scopes = "read" assert o.read()["key"] == "lol" o.key = "hiya" assert o.read()["key"] == "hiya"
async def test_app_async(): app = App("testkey",session="async") assert app.id == "self" await app.read() assert app.id!="self" await app.update(description="hello worldd") assert app["description"] == "hello worldd" assert app["owner"]["username"]=="test" assert (await app.owner)["username"]=="test"
def test_ts(): a = App("testkey") ts = a.objects.create("myts", key="key1") assert len(ts) == 0 ts.insert("hi!") assert len(ts) == 1 assert len(ts(t1="now-10s")) == 1 assert len(ts(t1="now")) == 0 assert ts[0]["d"] == "hi!" assert ts[-1]["d"] == "hi!" ts.insert("hello") assert ts[0]["d"] == "hi!" assert ts[-1]["d"] == "hello" ts.delete()
def test_metamod(): a = App("testkey") o = a.objects.create("myobj", otype="timeseries") o.meta = {"schema": {"type": "number"}} assert o.cached_data["meta"]["schema"]["type"] == "number" # assert o.cached_data["meta"]["actor"] == False assert o.meta["schema"]["type"] == "number" # assert o.meta["actor"] == False with pytest.raises(Exception): o.meta = {"foo": "bar"} with pytest.raises(Exception): o.meta = {"schema": "lol"}
def test_kv(): a = App("testkey") assert len(a.kv()) == 0 a.kv["test"] = True assert len(a.kv()) == 1 assert a.kv["test"] == True del a.kv["test"] assert len(a.kv()) == 0
async def test_basics_async(): a = App("testkey", session="async") await (await a.owner).update(name="Myname2") assert await (await a.owner).name == "Myname2" assert len(await a.objects()) == 0 o = await a.objects.create("myobj2", {"schema": {"type": "number"}}) assert o.name == "myobj2" assert o.type == "timeseries" assert len(await a.objects()) == 1 assert o == await a.objects[o.id] await o.delete()
def test_app_keychange(): # This test has to happen last, because once the app key is changed, # there is no more logging in from other tests. app = App("testkey",session="sync") result = app.update(name="My Test App") assert not "access_token" in result result = app.update(access_token=True) with pytest.raises(HeedyException): app.read() assert result["access_token"]!="testkey" newapp = App(result["access_token"],url=app.session.url) assert newapp.name == "My Test App"
def test_ts(): app = App("testkey") obj = app.objects.create("My Timeseries", type="timeseries", meta={"schema":{"type":"number"}}, tags="myts mydata", key="myts") assert len(obj)==0 obj.append(5) assert obj[-1]["d"]==5 # Insert the given array of data obj.insert_array([{"d": 6, "t": time.time()},{"d": 7, "t": time.time()+0.01, "dt": 5.3}]) assert len(obj)==3 assert obj[-1]["d"]==7 assert obj[1:].d()==[6,7] assert obj[1:].d()==[6,7] ts = obj(t1="now-1d") assert len(ts)==3 ts = obj(t1="now-1d",t2="now-10s") assert len(ts)==0 ts = obj(t1="1 hour ago") assert len(ts)==3 assert obj(transform="sum")[0]["d"]==18 obj.remove(i=-1) assert len(obj)==2 assert obj[-1]["d"]==6 obj.remove(t=ts[0]["t"]) assert len(obj)==1 assert obj[0]["d"]==6 obj.delete()
def test_df(): a = App("testkey") ts = a.objects.create("myts", key="key1") assert len(ts) == 0 assert isinstance(ts[:].to_df(), pd.DataFrame) ts.insert(5) ts.insert(6) assert isinstance(ts[:].to_df(), pd.DataFrame) assert len(ts[:].to_df().d) == 2 assert ts[:].to_df().d[0] == 5 assert ts[:].to_df().d[1] == 6 assert isinstance(ts[:], DatapointArray) assert isinstance(ts(output_type="dataframe"), pd.DataFrame) Timeseries.output_type = "dataframe" assert isinstance(ts[:], pd.DataFrame) assert isinstance(ts(output_type="list"), DatapointArray) ts.delete()
def test_objects(): app = App("testkey") assert len(app.objects())==0 obj = app.objects.create("My Timeseries", type="timeseries", meta={"schema":{"type":"number"}}, tags="myts mydata", key="myts") objs = app.objects() assert len(objs) == 1 assert objs[0] == obj assert objs[0].id==obj.id assert objs[0].meta == obj.meta assert objs[0]["name"] == "My Timeseries" assert objs[0]["type"] == "timeseries" assert obj.name == "My Timeseries" # Reading and Updating properties # Reads the key property from the server assert obj.key == "myts" # Uses the previously read cached data, avoiding a server query assert obj["key"] == "myts" obj.read() assert obj["key"]=="myts" obj.description = "My description" assert obj.description == "My description" obj.update(description="My description2") assert obj["description"] == "My description2" obj["description"] = "My description3" assert obj.description == "My description3" assert obj.meta == {"schema": {"type": "number"}} assert obj.meta() == {"schema": {"type": "number"}} obj.meta.schema = {"type": "string"} assert obj.meta == {"schema": {"type": "string"}} assert obj.meta() == {"schema": {"type": "string"}} obj.meta["schema"] = {"type": "boolean"} assert obj.meta == {"schema": {"type": "boolean"}} del obj.meta.schema assert obj.meta() == {"schema": {}} assert app.objects(key="myts")[0] == obj with pytest.raises(Exception): assert obj["app"]==app with pytest.raises(Exception): assert obj.app == app app.read() assert obj["app"]==app assert obj.app == app obj.delete() assert len(app.objects())==0
def test_tags_and_keys(): a = App("testkey") a.objects.create("obj1", tags="tag1 tag2", key="key1") a.objects.create("obj2", tags="tag1 tag3", key="key2") assert len(a.objects(key="key1")) == 1 assert len(a.objects(key="key2")) == 1 assert len(a.objects(key="key3")) == 0 assert len(a.objects(key="")) == 0 assert len(a.objects(tags="tag1")) == 2 assert len(a.objects(tags="tag1 tag3")) == 1 assert len(a.objects(tags="tag1 tag3", key="key1")) == 0 with pytest.raises(Exception): a.objects.create("obj3", tags="tag1 tag3", key="key2") a.objects(key="key1")[0].tags = "tag4" assert len(a.objects(tags="tag1")) == 1 assert len(a.objects(tags="tag4")) == 1 a.objects(key="key2")[0].key = "" assert len(a.objects(key="key2")) == 0 assert len(a.objects(key="")) == 1 for o in a.objects(): o.delete()
def test_appschema(): a = App("testkey") with pytest.raises(Exception): a.settings = {"lol": 12} a.settings_schema = {"lol": {"type": "number", "default": 42}} assert a.settings["lol"] == 42 a.settings = {"lol": 12} assert a.settings["lol"] == 12 with pytest.raises(Exception): a.settings = {"lol": "hi"} with pytest.raises(Exception): a.settings = {"lol": 24, "hee": 1} with pytest.raises(Exception): a.settings_schema = {"lol": {"type": "string", "default": "hi"}} a.update( settings={"lol": "hi"}, settings_schema={"lol": { "type": "string", "default": "hello" }}, ) assert a.settings["lol"] == "hi" a.settings_schema = {"lol": {"type": "string"}, "li": {"type": "number"}} with pytest.raises(Exception): a.settings_schema = { "lol": { "type": "string" }, "li": { "type": "number" }, "required": ["li"], }
from heedy import App c = App("gV9A4/J0eS4nLGAnzYyn") s = c.listObjects()[1] print(s.length()) print(s.append("hi")) print(s.length()) print(s()) print(s.delete(i=0)) print(s.length()) print(s())
def test_indexerror(): a = App("testkey") ts = a.objects.create("myobj") ts.insert("hi!") assert len(ts(i1=-1, i2=0)) == 0