From cd35df062c865beb795dcc166c2f8492fb320c68 Mon Sep 17 00:00:00 2001 From: Joby James Date: Mon, 28 Oct 2024 14:25:24 +0530 Subject: [PATCH] feat: add content assembler statefull table and stateless view and testn #20 --- lib/pattern/content-assembler/README.md | 16 +++ .../content-assembler/package_sql_test.ts | 117 ++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 lib/pattern/content-assembler/package_sql_test.ts diff --git a/lib/pattern/content-assembler/README.md b/lib/pattern/content-assembler/README.md index ccbc77320..2d93cc9ed 100644 --- a/lib/pattern/content-assembler/README.md +++ b/lib/pattern/content-assembler/README.md @@ -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. diff --git a/lib/pattern/content-assembler/package_sql_test.ts b/lib/pattern/content-assembler/package_sql_test.ts new file mode 100644 index 000000000..4ede136c3 --- /dev/null +++ b/lib/pattern/content-assembler/package_sql_test.ts @@ -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(); +});