Skip to content

Commit

Permalink
test: add unit tests and fix existing ts errors
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc-bq committed Feb 6, 2025
1 parent 330ac91 commit 465e2fa
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 8 deletions.
4 changes: 4 additions & 0 deletions mysql/lib/dialect/mysql2_driver_dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class MySQL2DriverDialect implements DriverDialect {
}

setKeepAliveProperties(props: Map<string, any>, keepAliveProps: any) {
if (keepAliveProps instanceof Map) {
keepAliveProps = Object.fromEntries(keepAliveProps);
}

if (keepAliveProps && keepAliveProps[MySQL2DriverDialect.KEEP_ALIVE_PROPERTY_NAME] !== undefined) {
throw new UnsupportedMethodError("Keep alive configuration is not supported for MySQL2.");
}
Expand Down
4 changes: 4 additions & 0 deletions pg/lib/dialect/node_postgres_driver_dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class NodePostgresDriverDialect implements DriverDialect {
return;
}

if (keepAliveProps instanceof Map) {
keepAliveProps = Object.fromEntries(keepAliveProps);
}

const keepAlive = keepAliveProps[NodePostgresDriverDialect.KEEP_ALIVE_PROPERTY_NAME];
const keepAliveInitialDelayMillis = keepAliveProps[NodePostgresDriverDialect.KEEP_ALIVE_INITIAL_DELAY_MILLIS_PROPERTY_NAME];

Expand Down
5 changes: 4 additions & 1 deletion tests/unit/aurora_connection_tracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { ClientWrapper } from "../../common/lib/client_wrapper";
import { HostInfo } from "../../common/lib/host_info";
import { MySQLClientWrapper } from "../../common/lib/mysql_client_wrapper";
import { jest } from "@jest/globals";
import { DriverDialect } from "../../common/lib/driver_dialect/driver_dialect";
import { MySQL2DriverDialect } from "../../mysql/lib/dialect/mysql2_driver_dialect";

const props = new Map<string, any>();
const SQL_ARGS = ["sql"];
Expand All @@ -48,8 +50,9 @@ const mockRdsUtils = mock(RdsUtils);
const mockClient = mock(AwsClient);
const mockHostInfo = mock(HostInfo);

const mockDriverDialect: DriverDialect = mock(MySQL2DriverDialect);
const mockClientInstance = instance(mockClient);
const mockClientWrapper: ClientWrapper = new MySQLClientWrapper(undefined, mockHostInfo, props);
const mockClientWrapper: ClientWrapper = new MySQLClientWrapper(undefined, mockHostInfo, props, mockDriverDialect);

mockClientInstance.targetClient = mockClientWrapper;

Expand Down
7 changes: 5 additions & 2 deletions tests/unit/aurora_initial_connection_strategy_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { AwsWrapperError } from "../../common/lib/utils/errors";
import { MySQLClientWrapper } from "../../common/lib/mysql_client_wrapper";
import { jest } from "@jest/globals";
import { PgClientWrapper } from "../../common/lib/pg_client_wrapper";
import { DriverDialect } from "../../common/lib/driver_dialect/driver_dialect";
import { MySQL2DriverDialect } from "../../mysql/lib/dialect/mysql2_driver_dialect";

const mockPluginService = mock(PluginService);
const mockHostListProviderService = mock<HostListProviderService>();
Expand All @@ -48,6 +50,7 @@ const hostInfo = hostInfoBuilder.withHost("host").build();

const writerHostInfo = hostInfoBuilder.withHost("host").withRole(HostRole.WRITER).build();
const readerHostInfo = hostInfoBuilder.withHost("host").withHost(HostRole.READER).build();
const mockDriverDialect: DriverDialect = mock(MySQL2DriverDialect);

describe("Aurora initial connection strategy plugin", () => {
let props: Map<string, any>;
Expand All @@ -62,8 +65,8 @@ describe("Aurora initial connection strategy plugin", () => {
plugin.initHostProvider(hostInfo, props, instance(mockHostListProviderService), mockFunc);
WrapperProperties.OPEN_CONNECTION_RETRY_TIMEOUT_MS.set(props, 1000);

writerClient = new MySQLClientWrapper(undefined, writerHostInfo, new Map<string, any>());
readerClient = new MySQLClientWrapper(undefined, readerHostInfo, new Map<string, any>());
writerClient = new MySQLClientWrapper(undefined, writerHostInfo, new Map<string, any>(), mockDriverDialect);
readerClient = new MySQLClientWrapper(undefined, readerHostInfo, new Map<string, any>(), mockDriverDialect);
});

afterEach(() => {
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/database_dialect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ describe("test database dialects", () => {
databaseType,
expectedDialect!.dialects,
props,
mockDriverDialect,
null
mockDriverDialect
);
await pluginService.updateDialect(mockClientWrapper);
expect(pluginService.getDialect()).toBe(expectedDialectClass);
Expand Down
73 changes: 73 additions & 0 deletions tests/unit/driver_dialect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { mock } from "ts-mockito";
import { MySQL2DriverDialect } from "../../mysql/lib/dialect/mysql2_driver_dialect";
import { HostInfo } from "../../common/lib/host_info";
import { UnsupportedMethodError } from "../../common/lib/utils/errors";
import { NodePostgresDriverDialect } from "../../pg/lib/dialect/node_postgres_driver_dialect";

const mockHostInfo: HostInfo = mock(HostInfo);
const emptyProps: Map<string, any> = new Map<string, any>();

describe("driverDialectTest", () => {
it("test_connectWithKeepAliveProps_MySQL_shouldThrow", async () => {
const keepAliveProps = new Map<string, any>([
["keepAlive", true],
["keepAliveInitialDelayMillis", 1234]
]);

const props = new Map<string, any>([["wrapperKeepAliveProperties", keepAliveProps]]);

const dialect = new MySQL2DriverDialect();
const unsupportedError = new UnsupportedMethodError("Keep alive configuration is not supported for MySQL2.");

await expect(dialect.connect(mockHostInfo, props)).rejects.toThrow(unsupportedError);

const keepAliveObj = {
keepAlive: true,
keepAliveInitialDelayMillis: 1234
};

const propsWithObj = new Map<string, any>([["wrapperKeepAliveProperties", keepAliveObj]]);

await expect(dialect.connect(mockHostInfo, propsWithObj)).rejects.toThrow(unsupportedError);
});

it("test_connectWithKeepAliveProps_PG_shouldSucceed", async () => {
const keepAliveMap = new Map<string, any>([
["keepAlive", true],
["keepAliveInitialDelayMillis", 1234]
]);

const dialect = new NodePostgresDriverDialect();

dialect.setKeepAliveProperties(emptyProps, keepAliveMap);
expect(emptyProps.get("keepAlive")).toBe(true);
expect(emptyProps.get("keepAliveInitialDelayMillis")).toBe(1234);

emptyProps.clear();

const keepAliveObj = {
keepAlive: true,
keepAliveInitialDelayMillis: 1234
};

dialect.setKeepAliveProperties(emptyProps, keepAliveObj);
expect(emptyProps.get("keepAlive")).toBe(true);
expect(emptyProps.get("keepAliveInitialDelayMillis")).toBe(1234);
});
});
5 changes: 4 additions & 1 deletion tests/unit/failover_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import { Messages } from "../../common/lib/utils/messages";
import { HostChangeOptions } from "../../common/lib/host_change_options";
import { NullTelemetryFactory } from "../../common/lib/utils/telemetry/null_telemetry_factory";
import { MySQLClientWrapper } from "../../common/lib/mysql_client_wrapper";
import { DriverDialect } from "../../common/lib/driver_dialect/driver_dialect";
import { MySQL2DriverDialect } from "../../mysql/lib/dialect/mysql2_driver_dialect";

const builder = new HostInfoBuilder({ hostAvailabilityStrategy: new SimpleHostAvailabilityStrategy() });

Expand All @@ -52,8 +54,9 @@ let mockWriterFailoverHandlerInstance;
const mockWriterFailoverHandler: ClusterAwareWriterFailoverHandler = mock(ClusterAwareWriterFailoverHandler);
const mockReaderResult: ReaderFailoverResult = mock(ReaderFailoverResult);
const mockWriterResult: WriterFailoverResult = mock(WriterFailoverResult);
const mockDriverDialect: DriverDialect = mock(MySQL2DriverDialect);

const mockClientWrapper = new MySQLClientWrapper(undefined, mockHostInfo, new Map<string, any>());
const mockClientWrapper = new MySQLClientWrapper(undefined, mockHostInfo, new Map<string, any>(), mockDriverDialect);

const properties: Map<string, any> = new Map();

Expand Down
17 changes: 15 additions & 2 deletions tests/unit/writer_failover_handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { WriterFailoverResult } from "../../common/lib/plugins/failover/writer_f
import { ClientWrapper } from "../../common/lib/client_wrapper";
import { PgDatabaseDialect } from "../../pg/lib/dialect/pg_database_dialect";
import { MySQLClientWrapper } from "../../common/lib/mysql_client_wrapper";
import { DriverDialect } from "../../common/lib/driver_dialect/driver_dialect";
import { MySQL2DriverDialect } from "../../mysql/lib/dialect/mysql2_driver_dialect";

const builder = new HostInfoBuilder({ hostAvailabilityStrategy: new SimpleHostAvailabilityStrategy() });

Expand All @@ -44,12 +46,23 @@ const mockClient = mock(AwsPGClient); // Using AwsPGClient in order to have abst
const mockClientInstance = instance(mockClient);
const mockPluginService = mock(PluginService);
const mockReaderFailover = mock(ClusterAwareReaderFailoverHandler);
const mockDriverDialect: DriverDialect = mock(MySQL2DriverDialect);

const mockTargetClient = { client: 123 };
const mockClientWrapper: ClientWrapper = new MySQLClientWrapper(mockTargetClient, builder.withHost("host").build(), new Map<string, any>());
const mockClientWrapper: ClientWrapper = new MySQLClientWrapper(
mockTargetClient,
builder.withHost("host").build(),
new Map<string, any>(),
mockDriverDialect
);

const mockTargetClientB = { client: 456 };
const mockClientWrapperB: ClientWrapper = new MySQLClientWrapper(mockTargetClientB, builder.withHost("host").build(), new Map<string, any>());
const mockClientWrapperB: ClientWrapper = new MySQLClientWrapper(
mockTargetClientB,
builder.withHost("host").build(),
new Map<string, any>(),
mockDriverDialect
);

describe("writer failover handler", () => {
beforeEach(() => {
Expand Down

0 comments on commit 465e2fa

Please sign in to comment.