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_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_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_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()