Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove FakeCacheService and TestCacheModule #2238

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions src/datasources/accounts/accounts.datasource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { TestDbFactory } from '@/__tests__/db.factory';
import type { IConfigurationService } from '@/config/configuration.service.interface';
import { AccountsDatasource } from '@/datasources/accounts/accounts.datasource';
import { FakeCacheService } from '@/datasources/cache/__tests__/fake.cache.service';
import { redisClientFactory } from '@/__tests__/redis-client.factory';
import type { RedisClientType } from '@/datasources/cache/cache.module';
import type { ICacheService } from '@/datasources/cache/cache.service.interface';
import { RedisCacheService } from '@/datasources/cache/redis.cache.service';
import { MAX_TTL } from '@/datasources/cache/constants';
import { CacheDir } from '@/datasources/cache/entities/cache-dir.entity';
import { CachedQueryResolver } from '@/datasources/db/v1/cached-query-resolver';
Expand Down Expand Up @@ -38,14 +41,21 @@ const encryptionApiMock = {
} as jest.MockedObjectDeep<IEncryptionApi>;

describe('AccountsDatasource tests', () => {
let fakeCacheService: FakeCacheService;
let redisClient: RedisClientType;
let cacheService: ICacheService;
let sql: postgres.Sql;
let migrator: PostgresDatabaseMigrator;
let target: AccountsDatasource;
const testDbFactory = new TestDbFactory();

beforeAll(async () => {
fakeCacheService = new FakeCacheService();
redisClient = await redisClientFactory();
cacheService = new RedisCacheService(
redisClient,
mockLoggingService,
mockConfigurationService,
'',
);
sql = await testDbFactory.createTestDatabase(faker.string.uuid());
migrator = new PostgresDatabaseMigrator(sql);
await migrator.migrate();
Expand All @@ -54,9 +64,9 @@ describe('AccountsDatasource tests', () => {
});

target = new AccountsDatasource(
fakeCacheService,
cacheService,
sql,
new CachedQueryResolver(mockLoggingService, fakeCacheService),
new CachedQueryResolver(mockLoggingService, cacheService),
mockLoggingService,
mockConfigurationService,
encryptionApiManagerMock,
Expand All @@ -68,12 +78,13 @@ describe('AccountsDatasource tests', () => {

afterEach(async () => {
await sql`TRUNCATE TABLE accounts, groups, account_data_types CASCADE`;
fakeCacheService.clear();
await redisClient.flushAll();
jest.clearAllMocks();
});

afterAll(async () => {
await testDbFactory.destroyTestDatabase(sql);
await redisClient.quit();
});

describe('createAccount', () => {
Expand All @@ -96,7 +107,7 @@ describe('AccountsDatasource tests', () => {

// check the account is stored in the cache
const cacheDir = new CacheDir(`account_${createAccountDto.address}`, '');
const cacheContent = await fakeCacheService.hGet(cacheDir);
const cacheContent = await cacheService.hGet(cacheDir);
expect(JSON.parse(cacheContent as string)).toStrictEqual(
expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -128,7 +139,7 @@ describe('AccountsDatasource tests', () => {

// check the account is stored in the cache
const cacheDir = new CacheDir(`account_${createAccountDto.address}`, '');
const cacheContent = await fakeCacheService.hGet(cacheDir);
const cacheContent = await cacheService.hGet(cacheDir);
expect(JSON.parse(cacheContent as string)).toStrictEqual(
expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -174,9 +185,9 @@ describe('AccountsDatasource tests', () => {
return faker.number.int({ min: 10 });
});
target = new AccountsDatasource(
fakeCacheService,
cacheService,
sql,
new CachedQueryResolver(mockLoggingService, fakeCacheService),
new CachedQueryResolver(mockLoggingService, cacheService),
mockLoggingService,
mockConfigurationService,
encryptionApiManagerMock,
Expand Down Expand Up @@ -216,9 +227,9 @@ describe('AccountsDatasource tests', () => {
return faker.number.int({ min: 10 });
});
target = new AccountsDatasource(
fakeCacheService,
cacheService,
sql,
new CachedQueryResolver(mockLoggingService, fakeCacheService),
new CachedQueryResolver(mockLoggingService, cacheService),
mockLoggingService,
mockConfigurationService,
encryptionApiManagerMock,
Expand Down Expand Up @@ -291,7 +302,7 @@ describe('AccountsDatasource tests', () => {
}),
);
const cacheDir = new CacheDir(`account_${createAccountDto.address}`, '');
const cacheContent = await fakeCacheService.hGet(cacheDir);
const cacheContent = await cacheService.hGet(cacheDir);
expect(JSON.parse(cacheContent as string)).toStrictEqual(
expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -381,7 +392,7 @@ describe('AccountsDatasource tests', () => {
`account_data_settings_${createAccountDto.address}`,
'',
);
await fakeCacheService.hSet(
await cacheService.hSet(
accountDataSettingsCacheDir,
faker.string.alpha(),
MAX_TTL,
Expand All @@ -390,7 +401,7 @@ describe('AccountsDatasource tests', () => {
`counterfactual_safes_${createAccountDto.address}`,
'',
);
await fakeCacheService.hSet(
await cacheService.hSet(
counterfactualSafesCacheDir,
faker.string.alpha(),
MAX_TTL,
Expand All @@ -407,18 +418,18 @@ describe('AccountsDatasource tests', () => {
`account_${createAccountDto.address}`,
'',
);
const cached = await fakeCacheService.hGet(accountCacheDir);
expect(cached).toBeUndefined();
const cached = await cacheService.hGet(accountCacheDir);
expect(cached).toBeNull();

// the settings and counterfactual safes are deleted from the cache
const accountDataSettingsCached = await fakeCacheService.hGet(
const accountDataSettingsCached = await cacheService.hGet(
accountDataSettingsCacheDir,
);
expect(accountDataSettingsCached).toBeUndefined();
const counterfactualSafesCached = await fakeCacheService.hGet(
expect(accountDataSettingsCached).toBeNull();
const counterfactualSafesCached = await cacheService.hGet(
counterfactualSafesCacheDir,
);
expect(counterfactualSafesCached).toBeUndefined();
expect(counterfactualSafesCached).toBeNull();

expect(mockLoggingService.debug).toHaveBeenCalledTimes(2);
expect(mockLoggingService.debug).toHaveBeenNthCalledWith(1, {
Expand Down Expand Up @@ -485,7 +496,7 @@ describe('AccountsDatasource tests', () => {
),
);
const cacheDir = new CacheDir('account_data_types', '');
const cacheContent = await fakeCacheService.hGet(cacheDir);
const cacheContent = await cacheService.hGet(cacheDir);
expect(JSON.parse(cacheContent as string)).toStrictEqual(
expect.arrayContaining(
dataTypes.map((dataType) =>
Expand Down Expand Up @@ -585,7 +596,7 @@ describe('AccountsDatasource tests', () => {
);

expect(actual).toStrictEqual(expect.arrayContaining(expected));
const cacheContent = await fakeCacheService.hGet(
const cacheContent = await cacheService.hGet(
new CacheDir(`account_data_settings_${createAccountDto.address}`, ''),
);
expect(JSON.parse(cacheContent as string)).toStrictEqual(
Expand Down Expand Up @@ -726,7 +737,7 @@ describe('AccountsDatasource tests', () => {
`account_data_settings_${createAccountDto.address}`,
'',
);
const cacheContent = await fakeCacheService.hGet(cacheDir);
const cacheContent = await cacheService.hGet(cacheDir);
expect(JSON.parse(cacheContent as string)).toStrictEqual(
expect.arrayContaining(
accountDataSettings.map((accountDataSetting) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { TestDbFactory } from '@/__tests__/db.factory';
import type { IConfigurationService } from '@/config/configuration.service.interface';
import { CounterfactualSafesDatasource } from '@/datasources/accounts/counterfactual-safes/counterfactual-safes.datasource';
import { FakeCacheService } from '@/datasources/cache/__tests__/fake.cache.service';
import { redisClientFactory } from '@/__tests__/redis-client.factory';
import type { RedisClientType } from '@/datasources/cache/cache.module';
import type { ICacheService } from '@/datasources/cache/cache.service.interface';
import { RedisCacheService } from '@/datasources/cache/redis.cache.service';
import { CacheDir } from '@/datasources/cache/entities/cache-dir.entity';
import { CachedQueryResolver } from '@/datasources/db/v1/cached-query-resolver';
import { PostgresDatabaseMigrator } from '@/datasources/db/v1/postgres-database.migrator';
Expand All @@ -26,14 +29,21 @@ const mockConfigurationService = jest.mocked({
} as jest.MockedObjectDeep<IConfigurationService>);

describe('CounterfactualSafesDatasource tests', () => {
let fakeCacheService: FakeCacheService;
let redisClient: RedisClientType;
let cacheService: ICacheService;
let sql: postgres.Sql;
let migrator: PostgresDatabaseMigrator;
let target: CounterfactualSafesDatasource;
const testDbFactory = new TestDbFactory();

beforeAll(async () => {
fakeCacheService = new FakeCacheService();
redisClient = await redisClientFactory();
cacheService = new RedisCacheService(
redisClient,
mockLoggingService,
mockConfigurationService,
'',
);
sql = await testDbFactory.createTestDatabase(faker.string.uuid());
migrator = new PostgresDatabaseMigrator(sql);
await migrator.migrate();
Expand All @@ -42,22 +52,23 @@ describe('CounterfactualSafesDatasource tests', () => {
});

target = new CounterfactualSafesDatasource(
fakeCacheService,
cacheService,
sql,
new CachedQueryResolver(mockLoggingService, fakeCacheService),
new CachedQueryResolver(mockLoggingService, cacheService),
mockLoggingService,
mockConfigurationService,
);
});

afterEach(async () => {
await sql`TRUNCATE TABLE accounts, account_data_settings, counterfactual_safes CASCADE`;
fakeCacheService.clear();
await redisClient.flushAll();
jest.clearAllMocks();
});

afterAll(async () => {
await testDbFactory.destroyTestDatabase(sql);
await redisClient.quit();
});

describe('createCounterfactualSafe', () => {
Expand Down Expand Up @@ -115,9 +126,9 @@ describe('CounterfactualSafesDatasource tests', () => {
});

target = new CounterfactualSafesDatasource(
fakeCacheService,
cacheService,
sql,
new CachedQueryResolver(mockLoggingService, fakeCacheService),
new CachedQueryResolver(mockLoggingService, cacheService),
mockLoggingService,
mockConfigurationService,
);
Expand Down Expand Up @@ -177,9 +188,9 @@ describe('CounterfactualSafesDatasource tests', () => {
});

target = new CounterfactualSafesDatasource(
fakeCacheService,
cacheService,
sql,
new CachedQueryResolver(mockLoggingService, fakeCacheService),
new CachedQueryResolver(mockLoggingService, cacheService),
mockLoggingService,
mockConfigurationService,
);
Expand Down Expand Up @@ -216,19 +227,15 @@ describe('CounterfactualSafesDatasource tests', () => {
`counterfactual_safes_${createAccountDto.address}`,
'',
);
await fakeCacheService.hSet(
cacheDir,
JSON.stringify([]),
faker.number.int(),
);
await cacheService.hSet(cacheDir, JSON.stringify([]), faker.number.int());

// the cache is cleared after creating a new CF Safe for the same account
await target.createCounterfactualSafe({
account,
createCounterfactualSafeDto:
createCounterfactualSafeDtoBuilder().build(),
});
expect(await fakeCacheService.hGet(cacheDir)).toBeUndefined();
expect(await cacheService.hGet(cacheDir)).toBeNull();
});
});

Expand Down Expand Up @@ -280,7 +287,7 @@ describe('CounterfactualSafesDatasource tests', () => {
`${counterfactualSafe.chain_id}_counterfactual_safe_${counterfactualSafe.predicted_address}`,
'',
);
const cacheContent = await fakeCacheService.hGet(cacheDir);
const cacheContent = await cacheService.hGet(cacheDir);
expect(JSON.parse(cacheContent as string)).toHaveLength(1);
expect(mockLoggingService.debug).toHaveBeenCalledTimes(2);
expect(mockLoggingService.debug).toHaveBeenNthCalledWith(1, {
Expand Down Expand Up @@ -319,7 +326,7 @@ describe('CounterfactualSafesDatasource tests', () => {
`${counterfactualSafe.chainId}_counterfactual_safe_${counterfactualSafe.predictedAddress}`,
'',
);
expect(await fakeCacheService.hGet(cacheDir)).toBeUndefined();
expect(await cacheService.hGet(cacheDir)).toBeNull();
expect(mockLoggingService.debug).toHaveBeenCalledTimes(2);
expect(mockLoggingService.debug).toHaveBeenNthCalledWith(1, {
type: 'cache_miss',
Expand Down Expand Up @@ -392,7 +399,7 @@ describe('CounterfactualSafesDatasource tests', () => {
`counterfactual_safes_${createAccountDto.address}`,
'',
);
const cacheContent = await fakeCacheService.hGet(cacheDir);
const cacheContent = await cacheService.hGet(cacheDir);
expect(JSON.parse(cacheContent as string)).toHaveLength(2);
expect(mockLoggingService.debug).toHaveBeenCalledTimes(2);
expect(mockLoggingService.debug).toHaveBeenNthCalledWith(1, {
Expand Down Expand Up @@ -464,7 +471,7 @@ describe('CounterfactualSafesDatasource tests', () => {
`${counterfactualSafe.chain_id}_counterfactual_safe_${counterfactualSafe.predicted_address}`,
'',
);
const beforeDeletion = await fakeCacheService.hGet(cacheDir);
const beforeDeletion = await cacheService.hGet(cacheDir);
expect(JSON.parse(beforeDeletion as string)).toHaveLength(1);

// the counterfactualSafe is deleted from the database and the cache
Expand All @@ -483,14 +490,14 @@ describe('CounterfactualSafesDatasource tests', () => {
}),
).rejects.toThrow();

const afterDeletion = await fakeCacheService.hGet(cacheDir);
expect(afterDeletion).toBeUndefined();
const afterDeletion = await cacheService.hGet(cacheDir);
expect(afterDeletion).toBeNull();
const cacheDirByAddress = new CacheDir(
`counterfactual_safes_${createAccountDto.address}`,
'',
);
const cachedByAddress = await fakeCacheService.hGet(cacheDirByAddress);
expect(cachedByAddress).toBeUndefined();
const cachedByAddress = await cacheService.hGet(cacheDirByAddress);
expect(cachedByAddress).toBeNull();
});
});

Expand Down Expand Up @@ -541,15 +548,13 @@ describe('CounterfactualSafesDatasource tests', () => {
);
expect(actual).toHaveLength(0);
// cache is cleared
expect(await cacheService.hGet(counterfactualSafesCacheDir)).toBeNull();
expect(
await fakeCacheService.hGet(counterfactualSafesCacheDir),
).toBeUndefined();
expect(
await fakeCacheService.hGet(counterfactualSafeCacheDirs[0]),
).toBeUndefined();
await cacheService.hGet(counterfactualSafeCacheDirs[0]),
).toBeNull();
expect(
await fakeCacheService.hGet(counterfactualSafeCacheDirs[1]),
).toBeUndefined();
await cacheService.hGet(counterfactualSafeCacheDirs[1]),
).toBeNull();
});
});
});
Loading
Loading