Skip to content

Commit

Permalink
fix: unit test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc-bq committed Dec 19, 2024
1 parent 1afb184 commit 36c737f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 41 deletions.
4 changes: 2 additions & 2 deletions common/lib/session_state_service_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class SessionStateServiceImpl implements SessionStateService {
// Apply current state for all 5 states: autoCommit, readOnly, catalog, schema, transactionIsolation
for (const key of Object.keys(this.copySessionState)) {
const state = this.copySessionState[key];
if (state.constructor === SessionStateField) {
if (state instanceof SessionStateField) {
await this.applyCurrentState(targetClient, state);
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ export class SessionStateServiceImpl implements SessionStateService {
// The states that will be set are: autoCommit, readonly, schema, catalog, transactionIsolation.
for (const key of Object.keys(this.copySessionState)) {
const state = this.copySessionState[key];
if (state.constructor === SessionStateField) {
if (state instanceof SessionStateField) {
await this.setPristineStateOnTarget(targetClient, state, key);
}
}
Expand Down
65 changes: 26 additions & 39 deletions tests/unit/session_state_service_impl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

import { anything, instance, mock, reset, spy, verify, when } from "ts-mockito";
import { anything, instance, mock, reset, spy, when } from "ts-mockito";
import { SessionStateServiceImpl } from "../../common/lib/session_state_service_impl";
import { PluginService } from "../../common/lib/plugin_service";
import { AwsPGClient } from "../../pg/lib";
Expand All @@ -27,7 +27,7 @@ import { HostInfoBuilder } from "../../common/lib/host_info_builder";
import { SimpleHostAvailabilityStrategy } from "../../common/lib/host_availability/simple_host_availability_strategy";
import { MySQLDatabaseDialect } from "../../mysql/lib/dialect/mysql_database_dialect";
import { PgDatabaseDialect } from "../../pg/lib/dialect/pg_database_dialect";
import { TransactionIsolationLevel } from "../../common/lib/utils/transaction_isolation_level";
import { MySQL2DriverDialect } from "../../mysql/lib/dialect/mysql2_driver_dialect";

const hostInfoBuilder = new HostInfoBuilder({ hostAvailabilityStrategy: new SimpleHostAvailabilityStrategy() });
const mockPluginService = mock(PluginService);
Expand All @@ -39,6 +39,7 @@ let sessionStateService: SessionStateService;
const mockPgClientWrapper: PgClientWrapper = mock(PgClientWrapper);
const mockMySQLClientWrapper: MySQLClientWrapper = mock(MySQLClientWrapper);
const hostInfo = new HostInfoBuilder({ hostAvailabilityStrategy: new SimpleHostAvailabilityStrategy() }).withHost("host").build();
const mockMySQLDriverDialect = mock(MySQL2DriverDialect);

describe("testSessionStateServiceImpl", () => {
beforeEach(() => {
Expand All @@ -49,8 +50,8 @@ describe("testSessionStateServiceImpl", () => {
sessionStateService = new SessionStateServiceImpl(instance(mockPluginService), new Map());
awsPGClient.targetClient = new PgClientWrapper(undefined, hostInfoBuilder.withHost("host").build(), new Map());
mockAwsPGClient.targetClient = new PgClientWrapper(undefined, hostInfoBuilder.withHost("host").build(), new Map());
awsMySQLClient.targetClient = new MySQLClientWrapper(undefined, hostInfoBuilder.withHost("host").build(), new Map());
mockAwsMySQLClient.targetClient = new MySQLClientWrapper(undefined, hostInfoBuilder.withHost("host").build(), new Map());
awsMySQLClient.targetClient = new MySQLClientWrapper(undefined, hostInfoBuilder.withHost("host").build(), new Map(), mockMySQLDriverDialect);
mockAwsMySQLClient.targetClient = new MySQLClientWrapper(undefined, hostInfoBuilder.withHost("host").build(), new Map(), mockMySQLDriverDialect);
when(mockMySQLClientWrapper.query(anything())).thenResolve();
when(mockPgClientWrapper.query(anything())).thenResolve();
when(mockPluginService.getSessionStateService()).thenReturn(sessionStateService);
Expand All @@ -77,25 +78,25 @@ describe("testSessionStateServiceImpl", () => {
awsClient.targetClient = new PgClientWrapper(undefined, hostInfo, new Map());
when(mockPluginService.getDialect()).thenReturn(new PgDatabaseDialect());
} else {
awsClient.targetClient = new MySQLClientWrapper(undefined, hostInfo, new Map());
awsClient.targetClient = new MySQLClientWrapper(undefined, hostInfo, new Map(), mockMySQLDriverDialect);
when(mockPluginService.getDialect()).thenReturn(new MySQLDatabaseDialect());
}
when(mockPluginService.getCurrentClient()).thenReturn(awsClient);
when(mockAwsClient.isReadOnly()).thenReturn(pristineValue);
expect(sessionStateService.getReadOnly()).toBe(undefined);
sessionStateService.setupPristineReadOnly();
sessionStateService.setReadOnly(value);
const sessionStateSpy = spy(sessionStateService);
expect(sessionStateService.getReadOnly()).toBe(value);

sessionStateService.begin();
await sessionStateService.applyPristineSessionState(awsClient);
sessionStateService.complete();

if (shouldReset) {
verify(sessionStateSpy.setReadOnly(anything())).once();
// Should reset to pristine value
expect(sessionStateService.getReadOnly()).toBe(pristineValue);
} else {
verify(sessionStateSpy.setReadOnly(anything())).never();
// No-op, value should stay unchanged
expect(sessionStateService.getReadOnly()).toBe(value);
}
});

Expand All @@ -114,17 +115,18 @@ describe("testSessionStateServiceImpl", () => {
expect(sessionStateService.getAutoCommit()).toBe(undefined);
sessionStateService.setupPristineAutoCommit();
sessionStateService.setAutoCommit(value);
const sessionStateSpy = spy(sessionStateService);
expect(sessionStateService.getAutoCommit()).toBe(value);

sessionStateService.begin();
await sessionStateService.applyPristineSessionState(awsClient);
sessionStateService.complete();

if (shouldReset) {
verify(sessionStateSpy.setAutoCommit(pristineValue)).once();
// Should reset to pristine value
expect(sessionStateService.getAutoCommit()).toBe(pristineValue);
} else {
verify(sessionStateSpy.setAutoCommit(anything())).never();
// No-op, value should stay unchanged
expect(sessionStateService.getAutoCommit()).toBe(value);
}
});

Expand All @@ -143,17 +145,18 @@ describe("testSessionStateServiceImpl", () => {
expect(sessionStateService.getCatalog()).toBe(undefined);
sessionStateService.setupPristineCatalog();
sessionStateService.setCatalog(value);
const sessionStateSpy = spy(sessionStateService);
expect(sessionStateService.getCatalog()).toBe(value);

sessionStateService.begin();
await sessionStateService.applyPristineSessionState(awsClient);
sessionStateService.complete();

if (shouldReset) {
verify(sessionStateSpy.setCatalog(pristineValue)).once();
// Should reset to pristine value
expect(sessionStateService.getCatalog()).toBe(pristineValue);
} else {
verify(sessionStateSpy.setCatalog(anything())).never();
// No-op, value should stay unchanged
expect(sessionStateService.getCatalog()).toBe(value);
}
});

Expand All @@ -172,17 +175,18 @@ describe("testSessionStateServiceImpl", () => {
expect(sessionStateService.getSchema()).toBe(undefined);
sessionStateService.setupPristineSchema();
sessionStateService.setSchema(value);
const sessionStateSpy = spy(sessionStateService);
expect(sessionStateService.getSchema()).toBe(value);

sessionStateService.begin();
await sessionStateService.applyPristineSessionState(awsClient);
sessionStateService.complete();

if (shouldReset) {
verify(sessionStateSpy.setSchema(pristineValue)).once();
// Should reset to pristine value
expect(sessionStateService.getSchema()).toBe(pristineValue);
} else {
verify(sessionStateSpy.setSchema(anything())).never();
// No-op, value should stay unchanged
expect(sessionStateService.getSchema()).toBe(value);
}
});

Expand Down Expand Up @@ -210,35 +214,18 @@ describe("testSessionStateServiceImpl", () => {
expect(sessionStateService.getTransactionIsolation()).toBe(undefined);
sessionStateService.setupPristineTransactionIsolation();
sessionStateService.setTransactionIsolation(value);
const sessionStateSpy = spy(sessionStateService);
expect(sessionStateService.getTransactionIsolation()).toBe(value);

sessionStateService.begin();
await sessionStateService.applyPristineSessionState(awsClient);
sessionStateService.complete();

if (shouldReset) {
verify(sessionStateSpy.setTransactionIsolation(pristineValue)).once();
// Should reset to pristine value
expect(sessionStateService.getTransactionIsolation()).toBe(pristineValue);
} else {
verify(sessionStateSpy.setTransactionIsolation(anything())).never();
// No-op, value should stay unchanged
expect(sessionStateService.getTransactionIsolation()).toBe(value);
}
});

it("test default pg client wrapper state", async () => {
const clientWrapper = new PgClientWrapper({}, hostInfo, new Map());
expect(clientWrapper.sessionState.readOnly.value).toBe(false);
expect(clientWrapper.sessionState.autoCommit.value).toBe(undefined);
expect(clientWrapper.sessionState.catalog.value).toBe(undefined);
expect(clientWrapper.sessionState.schema.value).toBe("");
expect(clientWrapper.sessionState.transactionIsolation.value).toBe(TransactionIsolationLevel.TRANSACTION_READ_COMMITTED);
});

it("test default mysql client wrapper state", async () => {
const clientWrapper = new MySQLClientWrapper({}, hostInfo, new Map());
expect(clientWrapper.sessionState.readOnly.value).toBe(false);
expect(clientWrapper.sessionState.autoCommit.value).toBe(true);
expect(clientWrapper.sessionState.catalog.value).toBe("");
expect(clientWrapper.sessionState.schema.value).toBe(undefined);
expect(clientWrapper.sessionState.transactionIsolation.value).toBe(TransactionIsolationLevel.TRANSACTION_REPEATABLE_READ);
});
});

0 comments on commit 36c737f

Please sign in to comment.