Skip to content

Commit

Permalink
Changed names in dtos and wrote unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeweatherhead committed Sep 23, 2023
1 parent 1b5f904 commit e6480a2
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 14 deletions.
2 changes: 1 addition & 1 deletion backend/src/diffs/diffs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class DiffsService {
).fill(0);
for (let idx = 0; idx < arrLength; idx++) {
const logicalIndex = this.getLogicalIndex(
diffDTOs[idx].S3DiffID,
diffDTOs[idx].S3DiffIndex,
nextDiffID,
arrLength,
);
Expand Down
4 changes: 2 additions & 2 deletions backend/src/diffs/dto/diffs.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export class DiffDTO {
MarkdownID: string;
UserID: number;
DisplayID: number;
S3DiffID: number;
S3DiffIndex: number;
LastModified: Date;
Content: string;
SnapshotID: string;
Expand All @@ -13,7 +13,7 @@ export class DiffDTO {
this.MarkdownID = undefined;
this.UserID = undefined;
this.DisplayID = undefined;
this.S3DiffID = undefined;
this.S3DiffIndex = undefined;
this.LastModified = undefined;
this.Content = undefined;
this.SnapshotID = undefined;
Expand Down
7 changes: 1 addition & 6 deletions backend/src/s3/s3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export class S3Service {
const diffDTOs: DiffDTO[] = [];

const firstDiffID =
snapshotDTO.S3SnapshotID *
snapshotDTO.S3SnapshotIndex *
parseInt(process.env.DIFFS_PER_SNAPSHOT);

for (
Expand Down Expand Up @@ -584,11 +584,6 @@ export class S3Service {

const filePath = `${diffDTO.UserID}/${diffDTO.MarkdownID}`;

console.log(
'fileDTO.Content: ',
fileDTO.Content,
);

try {
await this.s3Client.send(
new PutObjectCommand({
Expand Down
4 changes: 2 additions & 2 deletions backend/src/snapshots/dto/snapshot.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export class SnapshotDTO {
MarkdownID: string;
UserID: number;
DisplayID: number;
S3SnapshotID: number;
S3SnapshotIndex: number;
LastModified: Date;
Content: string;
OldestSnapshot: boolean;
Expand All @@ -13,7 +13,7 @@ export class SnapshotDTO {
this.MarkdownID = undefined;
this.UserID = undefined;
this.DisplayID = undefined;
this.S3SnapshotID = undefined;
this.S3SnapshotIndex = undefined;
this.LastModified = undefined;
this.Content = undefined;
this.OldestSnapshot = false;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/snapshots/snapshots.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class SnapshotService {
).fill(0);
for (let idx = 0; idx < arrLength; idx++) {
const logicalIndex = this.getLogicalIndex(
snapshotDTOs[idx].S3SnapshotID,
snapshotDTOs[idx].S3SnapshotIndex,
nextDiffID,
arrLength,
);
Expand Down
49 changes: 49 additions & 0 deletions backend/src/version_control/version_control.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import { Repository } from 'typeorm';
import { MarkdownFile } from '../markdown_files/entities/markdown_file.entity';
import { SnapshotService } from '../snapshots/snapshots.service';
import { Snapshot } from '../snapshots/entities/snapshots.entity';
import { DiffDTO } from '../diffs/dto/diffs.dto';

describe('VersionControlService', () => {
let service: VersionControlService;
let diffsService: DiffsService;
let markdownFilesService: MarkdownFilesService;
let snapshotService: SnapshotService;
let s3Service: S3Service;
let s3ServiceMock: S3ServiceMock;

Expand Down Expand Up @@ -64,9 +66,56 @@ describe('VersionControlService', () => {
s3ServiceMock = module.get<S3ServiceMock>(
S3ServiceMock,
);

snapshotService = module.get<SnapshotService>(
SnapshotService,
);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('saveSnapshot', () => {
const diffDTO = new DiffDTO();
const nextSnaphshotID = 0;
it('should call the s3Service and save snapshot correctly', async () => {
jest.spyOn(s3Service, 'saveSnapshot');

jest
.spyOn(Repository.prototype, 'findOne')
.mockResolvedValueOnce(new Snapshot());

jest
.spyOn(Repository.prototype, 'save')
.mockResolvedValueOnce(new Snapshot());

jest
.spyOn(Repository.prototype, 'findOneBy')
.mockResolvedValueOnce(
new MarkdownFile(),
);

jest
.spyOn(Repository.prototype, 'save')
.mockResolvedValueOnce(
new MarkdownFile(),
);

jest.spyOn(
snapshotService,
'updateSnapshot',
);

jest.spyOn(
markdownFilesService,
'incrementNextSnapshotID',
);

const response = await service.saveSnapshot(
diffDTO,
nextSnaphshotID,
);
});
});
});
4 changes: 2 additions & 2 deletions backend/src/version_control/version_control.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class VersionControlService {
snapshotDTO.MarkdownID =
snapshot.MarkdownID;
snapshotDTO.UserID = snapshot.UserID;
snapshotDTO.S3SnapshotID =
snapshotDTO.S3SnapshotIndex =
snapshot.S3SnapshotIndex;
snapshotDTO.LastModified =
snapshot.LastModified;
Expand All @@ -291,7 +291,7 @@ export class VersionControlService {
diffDTO.DiffID = diff.DiffID;
diffDTO.MarkdownID = diff.MarkdownID;
diffDTO.UserID = diff.UserID;
diffDTO.S3DiffID = diff.S3DiffIndex;
diffDTO.S3DiffIndex = diff.S3DiffIndex;
diffDTO.LastModified = diff.LastModified;
diffDTO.SnapshotID = diff.SnapshotID;
diffDTOs.push(diffDTO);
Expand Down

0 comments on commit e6480a2

Please sign in to comment.