Skip to content
Merged
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ jobs:
uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: setup StackQL
uses: stackql/setup-stackql@v2.0.0
with:
use_wrapper: false
- name: Run tests
run: deno test --allow-net --allow-read --allow-write --allow-env --allow-run
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
- `executeQueriesAsync` - executes a list of queries and returns an array of rows (objects) - queries need to return the same columns/schema

## Test
### Requirement
- To run the tests locally, [install StackQL](https://stackql.io/docs/installing-stackql) first.
```
deno test --allow-net --allow-read --allow-write --allow-env --allow-run <test file path>
```
4 changes: 2 additions & 2 deletions src/services/downloader.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertExists } from "https://deno.land/std@0.206.0/assert/assert_exists.ts";
import { Downloader } from "./downloader.ts";
import { removeStackQLDownload } from "../../tests/utils.ts";
import { removeStackQLDownload } from "../../testing/utils.ts";

Deno.test("Downloader setupStackQL Integration Test", async () => {
// Arrange
Expand All @@ -17,7 +17,7 @@ Deno.test("Downloader setupStackQL Integration Test", async () => {
// Check if the binary exists after setupStackQL is called

console.log(
"Test passed: setupStackQL completed without errors and binary exists."
"Test passed: setupStackQL completed without errors and binary exists.",
);
} catch (error) {
console.error("Test failed:", error);
Expand Down
14 changes: 14 additions & 0 deletions src/services/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Server } from "./server.ts";
import { assert } from "https://deno.land/std@0.207.0/assert/assert.ts";
import { startStackQLServer } from "../../testing/utils.ts";

Deno.test("Successful Connection", async () => {
const { closeProcess } = await startStackQLServer();
const server = new Server();
const pg = await server.connect(
"postgres://postgres:password@localhost:5444/postgres",
);
assert(pg);
await server.close();
await closeProcess();
});
53 changes: 53 additions & 0 deletions src/services/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Client } from "https://deno.land/x/postgres/mod.ts";

export class Server {
private client: Client | null = null;

constructor(connectionString?: string) {
if (connectionString) {
this.client = new Client(connectionString);
}
}

public async connect(connectionString?: string) {
const maxRetries = 3;
let currentAttempt = 0;

while (currentAttempt < maxRetries) {
try {
const connection = Deno.env.get("POSTGRES") || connectionString;
if (!connection) {
throw new Error(
"Connection string not found \n Please set the POSTGRES environment variable or pass the connection string as an argument",
);
}

console.log("connecting", connection);
this.client = new Client(connection);
await this.client.connect();
console.log("connected");
return this.client;
} catch (error) {
currentAttempt++;
console.log(`Attempt ${currentAttempt} failed: ${error.message}`);

if (currentAttempt >= maxRetries) {
throw new Error(
`Could not connect to the server after ${maxRetries} attempts: ${error.message}`,
);
}

// Wait for 1 second before the next attempt
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
}

public async close() {
if (this.client) {
await this.client.end();
}
}

// Additional methods for query execution can be added here
}
56 changes: 56 additions & 0 deletions src/stackql.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { assertStringIncludes } from "https://deno.land/std@0.206.0/assert/mod.ts";
import { StackQL } from "./stackql.ts";
import { removeStackQLDownload, startStackQLServer } from "../testing/utils.ts";
import {
assertEquals,
assertExists,
} from "https://deno.land/std@0.160.0/testing/asserts.ts";

Deno.test("StackQL runQuery - Successful Execution", async () => {
// Arrange
await removeStackQLDownload();
const stackQL = new StackQL();
await stackQL.initialize({ serverMode: false });
const pullQuery = "REGISTRY PULL okta;";
const testQuery = "SHOW PROVIDERS"; // Replace with a valid query for your context

// Act
await stackQL.runQuery(pullQuery);
const result = await stackQL.runQuery(testQuery);

// Assert
assertStringIncludes(result, "name");
assertStringIncludes(result, "version");
assertStringIncludes(result, "okta");
});

Deno.test("StackQL runServerQuery - Successful Execution", async () => {
const { closeProcess } = await startStackQLServer();
const stackQL = new StackQL();

try {
// Arrange
await stackQL.initialize({
serverMode: true,
connectionString: "postgres://postgres:password@localhost:5444/postgres",
});
const pullQuery = "REGISTRY PULL github;";
const testQuery = "SHOW SERVICES IN github LIKE '%repos%';"; // Replace with a valid query for your context

// Act
await stackQL.runServerQuery(pullQuery);
const results = await stackQL.runServerQuery(testQuery);
assertExists(results);
assertEquals(results.length, 1);
const result = results[0] as {
name: string;
};
assertEquals(result.name, "repos");

// Assert
} finally {
// Cleanup
await closeProcess();
await stackQL.closeConnection();
}
});
59 changes: 53 additions & 6 deletions src/stackql.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
import { assertExists } from "https://deno.land/std@0.206.0/assert/assert_exists.ts";
import { Downloader } from "./services/downloader.ts";
import { fileExists } from "./utils.ts";
import { Server } from "./services/server.ts";
import { Client } from "https://deno.land/x/postgres@v0.17.0/client.ts";

export interface StackQLConfig {
binaryPath?: string;
serverMode?: boolean;
connectionString?: string;
}

export class StackQL {
private binaryPath?: string;
private downloader: Downloader = new Downloader();
constructor() {}
private async initialize() {
private serverMode = false;
private connection?: Client; //TODO: wrap connection into Server class
private format: "object" = "object";
constructor() {
}
public async initialize(config: StackQLConfig) {
this.binaryPath = config.binaryPath;
this.serverMode = config.serverMode || false;
if (this.serverMode) {
await this.setupConnection(config.connectionString);
return;
}
if (this.binaryPath && fileExists(this.binaryPath)) {
return;
}
this.binaryPath = await this.downloader.setupStackQL();
}

public async runQuery(query: string) {
if (!this.binaryPath) {
await this.initialize();
assertExists(this.binaryPath);
private async setupConnection(connectionString?: string) {
const server = new Server();
this.connection = await server.connect(connectionString);
}

public async closeConnection() {
if (this.connection) {
await this.connection.end();
}
}

public async runQuery(query: string) {
assertExists(this.binaryPath);
const process = new Deno.Command(this.binaryPath, {
args: ["exec", query], // Ensure this command is correct
stdout: "piped",
Expand All @@ -33,4 +62,22 @@ export class StackQL {
throw new Error(`StackQL query failed: ${errorMessage}`);
}
}

private async queryObjectFormat(query: string) {
assertExists(this.connection);
const pgResult = await this.connection.queryObject(query);
return pgResult.rows;
}

public async runServerQuery(query: string) {
try {
if (this.format === "object") {
const result = await this.queryObjectFormat(query);
return result;
}
} catch (error) {
console.error(error);
throw new Error(`StackQL server query failed: ${error.message}`);
}
}
}
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const fileExists = (path: string) => {
export const fileExists = (path?: string) => {
if (!path) {
return false;
}
try {
Deno.statSync(path);
return true;
Expand Down
44 changes: 44 additions & 0 deletions testing/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { existsSync } from "https://deno.land/std/fs/mod.ts";
import { join } from "https://deno.land/std@0.133.0/path/mod.ts";

export const removeStackQLDownload = async () => {
const projectDir = Deno.cwd();
const stackqlPath = join(projectDir, ".stackql");
console.log("stackqlPath", stackqlPath);
console.log("existsSync(stackqlPath)", existsSync(stackqlPath));
if (existsSync(stackqlPath)) {
console.log("Removing .stackql directory");
await Deno.remove(stackqlPath, { recursive: true });
}
};

export const startStackQLServer = async (port = 5444) => {
const command = new Deno.Command("stackql", {
args: [
"srv",
"--pgsrv.address=0.0.0.0",
`--pgsrv.port=${port}`,
],
stdout: "inherit",
stderr: "inherit",
});
const process = command.spawn();
//TODO: find a way to wait for the server to be ready
const closeProcess = async () => {
try {
console.log("Closing process");
process.kill();
await process.status;
} catch (error) {
const alreadyClosed = error.message.includes(
"Child process has already terminated",
);
if (alreadyClosed) {
console.log("Process already closed");
return;
}
throw error;
}
};
return { closeProcess };
};
20 changes: 0 additions & 20 deletions tests/stackql.test.ts

This file was deleted.

13 changes: 0 additions & 13 deletions tests/utils.ts

This file was deleted.