Skip to content

Commit

Permalink
feat: add content assembler statefull table and stateless view and te…
Browse files Browse the repository at this point in the history
…stn #20
  • Loading branch information
jobyjames1 committed Oct 28, 2024
1 parent 50a488c commit cd35df0
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/pattern/content-assembler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,19 @@ time-consuming to re-run the same command in the CLI manually each time a file
changes, you can use _watch mode_ instead.

See: [`surveilrctl.ts`](../../std/surveilrctl.ts).

## How to Run the Tests

To execute test and ensure that `surveilr` is functioning correctly:

1. Run the tests using Deno:

```bash
deno test -A # Executes test
```

This process will create an 'assurance' folder, where you can find the files
related to the test, including the database and ingestion folder

The `-A` flag provides all necessary permissions for the tests to run, including
file system access and network permissions.
117 changes: 117 additions & 0 deletions lib/pattern/content-assembler/package_sql_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { assertEquals, assertExists } from "jsr:@std/assert@1";
import { DB } from "https://deno.land/x/sqlite@v3.8/mod.ts";
const DEFAULT_RSSD_PATH = "./resource-surveillance.sqlite.db";

Deno.test("Statefull tables check", async (t) => {
await t.step("Check database", async () => {
assertExists(
await Deno.stat(DEFAULT_RSSD_PATH).catch(() => null),
`❌ Error: ${DEFAULT_RSSD_PATH} does not exist`,
);
});
const db = new DB(DEFAULT_RSSD_PATH);

await t.step("Flattened email anchor", () => {
try {
db.execute(`DROP TABLE IF EXISTS ur_transform_html_flattened_email_anchor;
CREATE TABLE ur_transform_html_flattened_email_anchor AS
SELECT
uniform_resource_transform_id,
uniform_resource_id,
json_extract(json_each.value, '$.attributes.href') AS anchor,
json_extract(json_each.value, '$.children[0]') AS anchor_text
FROM
uniform_resource_transform,
json_each(content)`);
} catch (e) {
console.error(
`Failed to create table ur_transform_html_flattened_email_anchor: ${e.message}`,
);
}
const result = db.query(
`SELECT COUNT(*) AS count FROM ur_transform_html_flattened_email_anchor`,
);
assertEquals(result.length, 1);
});

await t.step("Email anchor subscription filter existence check", () => {
try {
const result = db.query(
`SELECT COUNT(*)
FROM sqlite_master
WHERE type='table' AND name='ur_transform_html_email_anchor_subscription_filter';`,
);

// Access the count value directly
const count = result[0][0] || 0;
assertEquals(count, 1, "Table does not exist.");
} catch (e) {
console.error(
`Failed to verify existence of table ur_transform_html_email_anchor_subscription_filter: ${e.message}`,
);
}
});

await t.step("Email anchor existence check", () => {
try {
const result = db.query(
`SELECT COUNT(*)
FROM sqlite_master
WHERE type='table' AND name='ur_transform_html_email_anchor';`,
);

// Access the count value directly
const count = result[0][0] || 0;
assertEquals(count, 1, "Table does not exist.");
} catch (e) {
console.error(
`Failed to verify existence of table ur_transform_html_email_anchor: ${e.message}`,
);
}
});

await t.step("Inbox", () => {
try {
db.execute(`DROP VIEW IF EXISTS inbox;
CREATE VIEW inbox AS
SELECT
ur_imap.uniform_resource_id AS base_uniform_resource_id,
ur_imap."from" AS message_from,
ur_imap."subject" AS message_subject,
ur_imap."date" AS message_date,
ur_extended.uniform_resource_id AS extended_uniform_resource_id,
ur_extended.uri AS extended_uri
FROM
ur_ingest_session_imap_acct_folder_message ur_imap
JOIN
uniform_resource ur_base
ON ur_base.uniform_resource_id = ur_imap.uniform_resource_id
JOIN
uniform_resource ur_extended
ON ur_extended.uri = ur_base.uri || '/html'
WHERE
ur_extended.uri LIKE '%/html'`);
} catch (e) {
console.error(
`Failed to create table inbox: ${e.message}`,
);
}
const result = db.query(
`SELECT COUNT(*) AS count FROM inbox`,
);
assertEquals(result.length, 1);
});

db.close();
});

Deno.test("Stateless tables check", async (t) => {
await t.step("Check database", async () => {
assertExists(
await Deno.stat(DEFAULT_RSSD_PATH).catch(() => null),
`❌ Error: ${DEFAULT_RSSD_PATH} does not exist`,
);
});
const db = new DB(DEFAULT_RSSD_PATH);
db.close();
});

0 comments on commit cd35df0

Please sign in to comment.