Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
mockFeaturesConfigEntity,
mockRepository,
MockType,
} from 'src/__mocks__';
} from 'src/__mocks__'
import { LocalFeaturesConfigRepository } from 'src/modules/feature/repositories/local.features-config.repository';
import { FeaturesConfigEntity } from 'src/modules/feature/entities/features-config.entity';
import { plainToClass } from 'class-transformer';
Expand Down Expand Up @@ -85,6 +85,21 @@ describe('LocalFeaturesConfigRepository', () => {

expect(result).toEqual(mockFeaturesConfig);
});
it('should fail to create with unique constraint and return existing', async () => {
repository.findOneBy.mockResolvedValueOnce(null);
repository.findOneBy.mockResolvedValueOnce(mockFeaturesConfig);
repository.save.mockRejectedValueOnce({ code: 'SQLITE_CONSTRAINT' });

const result = await service.getOrCreate();

expect(result).toEqual(mockFeaturesConfig);
});
it('should fail when failed to create new and error is not unique constraint', async () => {
repository.findOneBy.mockResolvedValueOnce(null);
repository.save.mockRejectedValueOnce(new Error());

await expect(service.getOrCreate()).rejects.toThrow(Error);
});
});

describe('update', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ export class LocalFeaturesConfigRepository extends FeaturesConfigRepository {
let entity = await this.repository.findOneBy({ id: this.id });

if (!entity) {
this.logger.log('Creating features config entity');
try {
this.logger.log('Creating features config entity');

entity = await this.repository.save(plainToClass(FeaturesConfigEntity, {
id: this.id,
data: defaultConfig,
controlNumber: this.generateControlNumber(),
}));
entity = await this.repository.save(plainToClass(FeaturesConfigEntity, {
id: this.id,
data: defaultConfig,
controlNumber: this.generateControlNumber(),
}));
} catch (e) {
if (e.code === 'SQLITE_CONSTRAINT') {
return this.getOrCreate();
}

throw e;
}
}

return classToClass(FeaturesConfig, entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,29 @@ describe('LocalAgreementsRepository', () => {
data: undefined,
});
});
it('should fail to create with unique constraint and return existing', async () => {
repository.findOneBy.mockResolvedValueOnce(null);
repository.findOneBy.mockResolvedValueOnce(mockAgreements);
repository.save.mockRejectedValueOnce({ code: 'SQLITE_CONSTRAINT' });

const result = await service.getOrCreate();

expect(result).toEqual(mockAgreements);
});
it('should fail when failed to create new and error is not unique constraint', async () => {
repository.findOneBy.mockResolvedValueOnce(null);
repository.save.mockRejectedValueOnce(new Error());

await expect(service.getOrCreate()).rejects.toThrow(Error);
});
});

describe('update', () => {
it('should update agreements', async () => {
const result = await service.update(mockSessionMetadata, mockAgreements);

expect(result).toEqual(mockAgreements);
expect(repository.update).toHaveBeenCalledWith({}, {
expect(repository.save).toHaveBeenCalledWith({
...mockAgreementsEntity,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ export class LocalAgreementsRepository extends AgreementsRepository {
let entity = await this.repository.findOneBy({});

if (!entity) {
entity = await this.repository.save(this.repository.create());
try {
entity = await this.repository.save(this.repository.create({ id: 1 }));
} catch (e) {
if (e.code === 'SQLITE_CONSTRAINT') {
return this.getOrCreate();
}

throw e;
}
}

return classToClass(Agreements, entity);
}

async update(_: SessionMetadata, agreements: Agreements): Promise<Agreements> {
await this.repository.update({}, classToClass(AgreementsEntity, agreements));
const entity = classToClass(AgreementsEntity, agreements);

await this.repository.save(entity);

return this.getOrCreate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,29 @@ describe('LocalSettingsRepository', () => {
data: undefined,
});
});
it('should fail to create with unique constraint and return existing', async () => {
repository.findOneBy.mockResolvedValueOnce(null);
repository.findOneBy.mockResolvedValueOnce(mockSettings);
repository.save.mockRejectedValueOnce({ code: 'SQLITE_CONSTRAINT' });

const result = await service.getOrCreate();

expect(result).toEqual(mockSettings);
});
it('should fail when failed to create new and error is not unique constraint', async () => {
repository.findOneBy.mockResolvedValueOnce(null);
repository.save.mockRejectedValueOnce(new Error());

await expect(service.getOrCreate()).rejects.toThrow(Error);
});
});

describe('update', () => {
it('should update settings', async () => {
const result = await service.update(mockSessionMetadata, mockSettings);

expect(result).toEqual(mockSettings);
expect(repository.update).toHaveBeenCalledWith({}, {
...mockSettingsEntity,
});
expect(repository.save).toHaveBeenCalledWith(mockSettingsEntity);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ export class LocalSettingsRepository extends SettingsRepository {
let entity = await this.repository.findOneBy({});

if (!entity) {
entity = await this.repository.save(this.repository.create());
try {
entity = await this.repository.save(this.repository.create({ id : 1 }));
} catch (e) {
if (e.code === 'SQLITE_CONSTRAINT') {
return this.getOrCreate();
}

throw e;
}
}

return classToClass(Settings, entity);
}

async update(_: SessionMetadata, settings: Settings): Promise<Settings> {
await this.repository.update({}, classToClass(SettingsEntity, settings));
const entity = classToClass(SettingsEntity, settings);

await this.repository.save(entity);

return this.getOrCreate();
}
Expand Down