Skip to content

Commit

Permalink
updated asserts and testing helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
halvardssm committed Jun 22, 2024
1 parent a307add commit a5586af
Show file tree
Hide file tree
Showing 4 changed files with 407 additions and 124 deletions.
251 changes: 249 additions & 2 deletions sql/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
SqlTransactionable,
} from "./mod.ts";

/**
* Check if an object has a property
*/
function hasProperty<T>(obj: T, property: string | symbol | number): boolean {
let currentProto = obj;

Expand All @@ -31,7 +34,29 @@ function hasProperty<T>(obj: T, property: string | symbol | number): boolean {
}

/**
* Check if an object has properties, and returns the properties that are missing
* Check if an object has properties
*/
export function hasProperties<T extends string | symbol>(
value: unknown,
properties: Array<T>,
): value is { [K in T]: unknown } {
assertExists(value);

const missing: Array<T> = [];

for (const property of properties) {
if (
!hasProperty(value as { [K in T]: unknown }, property)
) {
missing.push(property);
}
}

return missing.length === 0;
}

/**
* Check if an object has properties and throws if not
*/
export function assertHasProperties<T extends string | symbol>(
value: unknown,
Expand Down Expand Up @@ -66,12 +91,24 @@ export function isSqlError(err: unknown): err is SqlError {
}

/**
* Check if an error is a SqlError
* Asserts that an error is a SqlError
*/
export function assertIsSqlError(err: unknown): asserts err is SqlError {
assertInstanceOf(err, SqlError);
}

/**
* Check if an object is an AsyncDisposable
*/
export function isAsyncDisposable(
value: unknown,
): value is AsyncDisposable {
return hasProperties(value, [Symbol.asyncDispose]);
}

/**
* Asserts that an object is an AsyncDisposable
*/
export function assertIsAsyncDisposable(
value: unknown,
): asserts value is AsyncDisposable {
Expand All @@ -83,6 +120,29 @@ export function assertIsAsyncDisposable(
);
}

/**
* Check if an object is an SqlConnection
*/
export function isSqlConnection(
value: unknown,
): value is SqlConnection {
return isAsyncDisposable(value) && hasProperties(
value,
[
"options",
"connected",
"connect",
"close",
"execute",
"queryMany",
"queryManyArray",
],
);
}

/**
* Asserts that an object is an SqlConnection
*/
export function assertIsSqlConnection(
value: unknown,
): asserts value is SqlConnection {
Expand All @@ -101,6 +161,24 @@ export function assertIsSqlConnection(
);
}

/**
* Check if an object is an SqlConnectable
*/
export function isSqlConnectable(
value: unknown,
): value is SqlConnectable {
return isAsyncDisposable(value) && hasProperties(
value,
[
"connection",
"connected",
],
) && isSqlConnection(value.connection);
}

/**
* Asserts that an object is an SqlConnectable
*/
export function assertIsSqlConnectable(
value: unknown,
): asserts value is SqlConnectable {
Expand All @@ -115,6 +193,31 @@ export function assertIsSqlConnectable(
assertIsSqlConnection(value.connection);
}

/**
* Check if an object is an SqlPreparedStatement
*/
export function isSqlPreparedStatement(
value: unknown,
): value is SqlPreparedStatement {
return isSqlConnectable(value) && hasProperties(
value,
[
"sql",
"options",
"execute",
"query",
"queryOne",
"queryMany",
"queryArray",
"queryOneArray",
"queryManyArray",
],
);
}

/**
* Asserts that an object is an SqlPreparedStatement
*/
export function assertIsSqlPreparedStatement(
value: unknown,
): asserts value is SqlPreparedStatement {
Expand All @@ -135,6 +238,30 @@ export function assertIsSqlPreparedStatement(
);
}

/**
* Check if an object is an SqlQueriable
*/
export function isSqlQueriable(
value: unknown,
): value is SqlQueriable {
return isSqlConnectable(value) && hasProperties(
value,
[
"options",
"execute",
"query",
"queryOne",
"queryMany",
"queryArray",
"queryOneArray",
"queryManyArray",
],
);
}

/**
* Asserts that an object is an SqlQueriable
*/
export function assertIsSqlQueriable(
value: unknown,
): asserts value is SqlQueriable {
Expand All @@ -153,6 +280,24 @@ export function assertIsSqlQueriable(
],
);
}

/**
* Check if an object is an SqlTransaction
*/
export function isSqlPreparable(
value: unknown,
): value is SqlQueriable {
return isSqlQueriable(value) && hasProperties(
value,
[
"prepare",
],
);
}

/**
* Asserts that an object is an SqlTransaction
*/
export function assertIsSqlPreparable(
value: unknown,
): asserts value is SqlQueriable {
Expand All @@ -165,6 +310,27 @@ export function assertIsSqlPreparable(
);
}

/**
* Check if an object is an SqlTransaction
*/
export function isSqlTransaction(
value: unknown,
): value is SqlTransaction {
return isSqlPreparable(value) && hasProperties(
value,
[
"inTransaction",
"commitTransaction",
"rollbackTransaction",
"createSavepoint",
"releaseSavepoint",
],
);
}

/**
* Asserts that an object is an SqlTransaction
*/
export function assertIsSqlTransaction(
value: unknown,
): asserts value is SqlTransaction {
Expand All @@ -181,6 +347,24 @@ export function assertIsSqlTransaction(
);
}

/**
* Check if an object is an SqlTransactionable
*/
export function isSqlTransactionable(
value: unknown,
): value is SqlTransactionable {
return isSqlPreparable(value) && hasProperties(
value,
[
"beginTransaction",
"transaction",
],
);
}

/**
* Asserts that an object is an SqlTransactionable
*/
export function assertIsSqlTransactionable(
value: unknown,
): asserts value is SqlTransactionable {
Expand All @@ -194,13 +378,38 @@ export function assertIsSqlTransactionable(
);
}

/**
* Check if an object is an SqlEventable
*/
export function isSqlEventable(
value: unknown,
): value is SqlEventable {
return hasProperties(value, ["eventTarget"]) &&
value.eventTarget instanceof EventTarget;
}

/**
* Asserts that an object is an SqlEventable
*/
export function assertIsSqlEventable(
value: unknown,
): asserts value is SqlEventable {
assertHasProperties(value, ["eventTarget"]);
assertInstanceOf(value.eventTarget, EventTarget);
}

/**
* Check if an object is an SqlClient
*/
export function isSqlClient(value: unknown): value is SqlClient {
return isSqlConnection(value) && isSqlQueriable(value) &&
isSqlTransactionable(value) && isSqlEventable(value) &&
hasProperties(value, ["options"]);
}

/**
* Asserts that an object is an SqlClient
*/
export function assertIsSqlClient(value: unknown): asserts value is SqlClient {
assertIsSqlConnection(value);
assertIsSqlQueriable(value);
Expand All @@ -209,6 +418,23 @@ export function assertIsSqlClient(value: unknown): asserts value is SqlClient {
assertHasProperties(value, ["options"]);
}

/**
* Check if an object is an SqlPoolClient
*/
export function isSqlPoolClient(
value: unknown,
): value is SqlPoolClient {
return isSqlConnectable(value) && isSqlTransactionable(value) &&
hasProperties(value, [
"options",
"disposed",
"release",
]);
}

/**
* Asserts that an object is an SqlPoolClient
*/
export function assertIsSqlPoolClient(
value: unknown,
): asserts value is SqlPoolClient {
Expand All @@ -221,6 +447,27 @@ export function assertIsSqlPoolClient(
]);
}

/**
* Check if an object is an SqlClientPool
*/
export function isSqlClientPool(
value: unknown,
): value is SqlClientPool {
return isSqlEventable(value) && isAsyncDisposable(value) &&
hasProperties(value, [
"connectionUrl",
"options",
"connected",
"connect",
"close",
"deferredStack",
"acquire",
]);
}

/**
* Asserts that an object is an SqlClientPool
*/
export function assertIsSqlClientPool(
value: unknown,
): asserts value is SqlClientPool {
Expand Down
2 changes: 1 addition & 1 deletion sql/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.0-2",
"version": "0.0.0-5",
"name": "@stdext/sql",
"lock": false,
"exports": {
Expand Down
Loading

0 comments on commit a5586af

Please sign in to comment.