Read the Networks, their Datastream and Observation counts, and the feature flags the API is running with.
- Expected
- Two Networks that each own Datastreams; AUTHORIZATION=1 and NETWORK=1.
- Actual
- acsot: 11 datastreams / 22176 obs · psos: 9 / 18144 · AUTHORIZATION=1 NETWORK=1 ANONYMOUS_VIEWER=0 VERSIONING=1 REDIS=0
- Check
.env:AUTHORIZATION=1,NETWORK=1,ANONYMOUS_VIEWER=0. Restart theapicontainer after changing them. - Run the query below in the database container and note the two Network names and counts. The dummy-data generator picks two names at random from
psos,acsot,defminon every fresh build, so later steps say “your Network”.
SELECT n.name, count(DISTINCT d.id) AS datastreams, count(o.id) AS observationsFROM sensorthings."Network" nLEFT JOIN sensorthings."Datastream" d ON d.network_id = n.idLEFT JOIN sensorthings."Observation" o ON o.datastream_id = d.idGROUP BY n.name ORDER BY n.name;How it works. The seed generator creates the Networks, assigns every Datastream to one of them, and skips itself when data already exists, so counts stay stable across restarts.
Inputs. The running database and the API container's environment.
Behaviour. Scoping later in the guide is expressed against these names and counts.
What the test verifies. At least two Networks hold Datastreams, and auth plus Network scoping are switched on in the API that is being tested.
Depends on. NETWORK=1 creates the Network table and Datastream.network_id; without it there is nothing to scope by.
Source files
| Role | File | Symbol | Lines |
|---|---|---|---|
| Implementation | dummy_data/generator.py | generate_networks | 118–152 |
| Implementation | dummy_data/generator.py | create_data | 678–734 |
| Guide check | api/tests/guide/checks.py | ground_truth | 253–280 |
async def generate_networks(conn, commit_id): """ Generate two different networks and insert them into the database. Args: conn: The asyncpg connection object. commit_id: The commit id to associate with the networks (optional). Returns: List of inserted network IDs. """ names = random.sample(["psos", "acsot", "defmin"], 2) inserted_ids = [] if commit_id is not None: insert_sql = """ INSERT INTO sensorthings."Network" (name, commit_id) VALUES ($1, $2) RETURNING id; """ for name in names: row = await conn.fetchrow(insert_sql, name, commit_id) inserted_ids.append(row["id"]) else: insert_sql = """ INSERT INTO sensorthings."Network" (name) VALUES ($1) RETURNING id; """ for name in names: row = await conn.fetchrow(insert_sql, name) inserted_ids.append(row["id"]) return inserted_idsasync def create_data(): """ Generates dummy data and inserts it into the database. This function connects to the database using the provided connection URL, generates various types of dummy data, and inserts them into the respective tables in the database. If any error occurs during the data generation or insertion process, the changes are rolled back and an error message is printed. After the creation is complete, the database connection is closed. """ pool = await get_pool() try: async with pool.acquire() as conn: try: # `docker compose up` re-runs this container every time; without # this guard a second run collides on unique keys and leaves # extra empty Networks behind. if await conn.fetchval( 'SELECT EXISTS (SELECT 1 FROM sensorthings."Thing")' ): print( "Data already present, skipping dummy data " "(set CLEAR_DATA=1 to regenerate)." ) return user_id = None user_uri = "anonymous" if authorization: user = await get_user(conn) user_id = user["id"] user_uri = user["uri"] commit_id = None if versioning or authorization: commit_id = await generate_commit(conn, user_id, user_uri) if network: network_ids = await generate_networks(conn, commit_id) else: network_ids = [] await generate_things(conn, commit_id) await generate_locations(conn, commit_id) await generate_things_locations(conn) await generate_historicallocations(conn, commit_id) await generate_locations_historicallocations(conn) await generate_observedProperties(conn, commit_id) await generate_sensors(conn, commit_id) await generate_datastreams(conn, commit_id, network_ids) await generate_featuresofinterest(conn, commit_id) await generate_observations(conn, commit_id) except Exception as e: print(f"An error occured: {e}") finally: await pool.close()The executable check behind this step's result.
@check("s0")def ground_truth(t, ctx): rows = psql( 'SELECT n.id, n.name, count(DISTINCT d.id), count(o.id) FROM sensorthings."Network" n ' 'LEFT JOIN sensorthings."Datastream" d ON d.network_id = n.id ' 'LEFT JOIN sensorthings."Observation" o ON o.datastream_id = d.id ' "GROUP BY n.id, n.name ORDER BY n.id").split("\n") nets = [r.split("|") for r in rows if r] nets = [(int(i), n, int(d), int(o)) for i, n, d, o in nets if int(d) > 0] t.expect(len(nets) >= 2, "at least two Networks hold Datastreams", len(nets)) ctx.a_id, ctx.a, ctx.a_ds, ctx.a_obs = nets[0] ctx.b_id, ctx.b, ctx.b_ds, ctx.b_obs = nets[1] ctx.all_ds = int(psql('SELECT count(*) FROM sensorthings."Datastream"')) ctx.all_obs = int(psql('SELECT count(*) FROM sensorthings."Observation"')) ids = lambda nid: [int(x) for x in psql( # noqa: E731 f'SELECT id FROM sensorthings."Datastream" WHERE network_id = {nid} ORDER BY id').split()] ctx.a_ids, ctx.b_ids = ids(ctx.a_id), ids(ctx.b_id) flags = {} if API: for k in ("AUTHORIZATION", "NETWORK", "ANONYMOUS_VIEWER", "VERSIONING", "REDIS"): flags[k] = subprocess.run(["docker", "exec", API, "printenv", k], capture_output=True, text=True).stdout.strip() ctx.flags = flags t.expect(flags.get("AUTHORIZATION") == "1" and flags.get("NETWORK") == "1", "AUTHORIZATION=1 and NETWORK=1 on the running API", flags) t.expected = "Two Networks that each own Datastreams; AUTHORIZATION=1 and NETWORK=1." t.actual(f"{ctx.a}: {ctx.a_ds} datastreams / {ctx.a_obs} obs · {ctx.b}: {ctx.b_ds} / {ctx.b_obs} · " + " ".join(f"{k}={v}" for k, v in flags.items()))- passed: at least two Networks hold Datastreams
- passed: AUTHORIZATION=1 and NETWORK=1 on the running API