chore(server): change save -> update in asset repository (#8055)

* `save` -> `update`

* change return type

* include relations

* fix tests

* remove when mocks

* fix

* stricter typing

* simpler type
This commit is contained in:
Mert
2024-03-19 22:42:10 -04:00
committed by GitHub
parent 9e4bab7494
commit 2daed747cd
14 changed files with 128 additions and 138 deletions

View File

@@ -111,7 +111,7 @@ describe(StorageTemplateService.name, () => {
expect(storageMock.checkFileExists).not.toHaveBeenCalled();
expect(storageMock.rename).not.toHaveBeenCalled();
expect(storageMock.copyFile).not.toHaveBeenCalled();
expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();
expect(moveMock.create).not.toHaveBeenCalled();
expect(moveMock.update).not.toHaveBeenCalled();
expect(storageMock.stat).not.toHaveBeenCalled();
@@ -122,14 +122,6 @@ describe(StorageTemplateService.name, () => {
const newMotionPicturePath = `upload/library/${userStub.user1.id}/2022/2022-06-19/${assetStub.livePhotoStillAsset.id}.mp4`;
const newStillPicturePath = `upload/library/${userStub.user1.id}/2022/2022-06-19/${assetStub.livePhotoStillAsset.id}.jpeg`;
when(assetMock.save)
.calledWith({ id: assetStub.livePhotoStillAsset.id, originalPath: newStillPicturePath })
.mockResolvedValue(assetStub.livePhotoStillAsset);
when(assetMock.save)
.calledWith({ id: assetStub.livePhotoMotionAsset.id, originalPath: newMotionPicturePath })
.mockResolvedValue(assetStub.livePhotoMotionAsset);
when(assetMock.getByIds)
.calledWith([assetStub.livePhotoStillAsset.id], { exifInfo: true })
.mockResolvedValue([assetStub.livePhotoStillAsset]);
@@ -175,11 +167,11 @@ describe(StorageTemplateService.name, () => {
expect(assetMock.getByIds).toHaveBeenCalledWith([assetStub.livePhotoStillAsset.id], { exifInfo: true });
expect(assetMock.getByIds).toHaveBeenCalledWith([assetStub.livePhotoMotionAsset.id], { exifInfo: true });
expect(storageMock.checkFileExists).toHaveBeenCalledTimes(2);
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.livePhotoStillAsset.id,
originalPath: newStillPicturePath,
});
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.livePhotoMotionAsset.id,
originalPath: newMotionPicturePath,
});
@@ -200,10 +192,6 @@ describe(StorageTemplateService.name, () => {
newPath: previousFailedNewPath,
});
when(assetMock.save)
.calledWith({ id: assetStub.image.id, originalPath: newPath })
.mockResolvedValue(assetStub.image);
when(assetMock.getByIds)
.calledWith([assetStub.image.id], { exifInfo: true })
.mockResolvedValue([assetStub.image]);
@@ -232,7 +220,7 @@ describe(StorageTemplateService.name, () => {
oldPath: assetStub.image.originalPath,
newPath,
});
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.image.id,
originalPath: newPath,
});
@@ -257,10 +245,6 @@ describe(StorageTemplateService.name, () => {
newPath: previousFailedNewPath,
});
when(assetMock.save)
.calledWith({ id: assetStub.image.id, originalPath: newPath })
.mockResolvedValue(assetStub.image);
when(assetMock.getByIds)
.calledWith([assetStub.image.id], { exifInfo: true })
.mockResolvedValue([assetStub.image]);
@@ -291,7 +275,7 @@ describe(StorageTemplateService.name, () => {
oldPath: previousFailedNewPath,
newPath,
});
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.image.id,
originalPath: newPath,
});
@@ -307,10 +291,6 @@ describe(StorageTemplateService.name, () => {
.mockResolvedValue({ size: 5000 } as Stats);
when(cryptoMock.hashFile).calledWith(newPath).mockResolvedValue(Buffer.from('different-hash', 'utf8'));
when(assetMock.save)
.calledWith({ id: assetStub.image.id, originalPath: newPath })
.mockResolvedValue(assetStub.image);
when(assetMock.getByIds)
.calledWith([assetStub.image.id], { exifInfo: true })
.mockResolvedValue([assetStub.image]);
@@ -345,7 +325,7 @@ describe(StorageTemplateService.name, () => {
expect(storageMock.copyFile).toHaveBeenCalledWith(assetStub.image.originalPath, newPath);
expect(storageMock.unlink).toHaveBeenCalledWith(newPath);
expect(storageMock.unlink).toHaveBeenCalledTimes(1);
expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();
});
it.each`
@@ -374,10 +354,6 @@ describe(StorageTemplateService.name, () => {
newPath: previousFailedNewPath,
});
when(assetMock.save)
.calledWith({ id: assetStub.image.id, originalPath: newPath })
.mockResolvedValue(assetStub.image);
when(assetMock.getByIds)
.calledWith([assetStub.image.id], { exifInfo: true })
.mockResolvedValue([assetStub.image]);
@@ -404,7 +380,7 @@ describe(StorageTemplateService.name, () => {
expect(storageMock.rename).not.toHaveBeenCalled();
expect(storageMock.copyFile).not.toHaveBeenCalled();
expect(moveMock.update).not.toHaveBeenCalled();
expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();
},
);
});
@@ -427,7 +403,6 @@ describe(StorageTemplateService.name, () => {
items: [assetStub.image],
hasNextPage: false,
});
assetMock.save.mockResolvedValue(assetStub.image);
userMock.getList.mockResolvedValue([userStub.user1]);
moveMock.create.mockResolvedValue({
id: '123',
@@ -449,7 +424,7 @@ describe(StorageTemplateService.name, () => {
expect(assetMock.getAll).toHaveBeenCalled();
expect(storageMock.checkFileExists).toHaveBeenCalledTimes(2);
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.image.id,
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id+1.jpg',
});
@@ -474,7 +449,7 @@ describe(StorageTemplateService.name, () => {
expect(storageMock.rename).not.toHaveBeenCalled();
expect(storageMock.copyFile).not.toHaveBeenCalled();
expect(storageMock.checkFileExists).not.toHaveBeenCalledTimes(2);
expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();
});
it('should skip when an asset is probably a duplicate', async () => {
@@ -495,7 +470,7 @@ describe(StorageTemplateService.name, () => {
expect(storageMock.rename).not.toHaveBeenCalled();
expect(storageMock.copyFile).not.toHaveBeenCalled();
expect(storageMock.checkFileExists).not.toHaveBeenCalledTimes(2);
expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();
});
it('should move an asset', async () => {
@@ -503,7 +478,6 @@ describe(StorageTemplateService.name, () => {
items: [assetStub.image],
hasNextPage: false,
});
assetMock.save.mockResolvedValue(assetStub.image);
userMock.getList.mockResolvedValue([userStub.user1]);
moveMock.create.mockResolvedValue({
id: '123',
@@ -520,7 +494,7 @@ describe(StorageTemplateService.name, () => {
'/original/path.jpg',
'upload/library/user-id/2023/2023-02-23/asset-id.jpg',
);
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.image.id,
originalPath: 'upload/library/user-id/2023/2023-02-23/asset-id.jpg',
});
@@ -531,7 +505,6 @@ describe(StorageTemplateService.name, () => {
items: [assetStub.image],
hasNextPage: false,
});
assetMock.save.mockResolvedValue(assetStub.image);
userMock.getList.mockResolvedValue([userStub.storageLabel]);
moveMock.create.mockResolvedValue({
id: '123',
@@ -548,7 +521,7 @@ describe(StorageTemplateService.name, () => {
'/original/path.jpg',
'upload/library/label-1/2023/2023-02-23/asset-id.jpg',
);
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.image.id,
originalPath: 'upload/library/label-1/2023/2023-02-23/asset-id.jpg',
});
@@ -592,7 +565,7 @@ describe(StorageTemplateService.name, () => {
expect(storageMock.utimes).toHaveBeenCalledWith(newPath, expect.any(Date), expect.any(Date));
expect(storageMock.unlink).toHaveBeenCalledWith(assetStub.image.originalPath);
expect(storageMock.unlink).toHaveBeenCalledTimes(1);
expect(assetMock.save).toHaveBeenCalledWith({
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.image.id,
originalPath: newPath,
});
@@ -630,7 +603,7 @@ describe(StorageTemplateService.name, () => {
'upload/library/user-id/2023/2023-02-23/asset-id.jpg',
);
expect(storageMock.stat).toHaveBeenCalledWith('upload/library/user-id/2023/2023-02-23/asset-id.jpg');
expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();
});
it('should not update the database if the move fails', async () => {
@@ -656,7 +629,7 @@ describe(StorageTemplateService.name, () => {
'/original/path.jpg',
'upload/library/user-id/2023/2023-02-23/asset-id.jpg',
);
expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();
});
it('should not move read-only asset', async () => {
@@ -670,7 +643,6 @@ describe(StorageTemplateService.name, () => {
],
hasNextPage: false,
});
assetMock.save.mockResolvedValue(assetStub.image);
userMock.getList.mockResolvedValue([userStub.user1]);
await sut.handleMigration();
@@ -678,7 +650,7 @@ describe(StorageTemplateService.name, () => {
expect(assetMock.getAll).toHaveBeenCalled();
expect(storageMock.rename).not.toHaveBeenCalled();
expect(storageMock.copyFile).not.toHaveBeenCalled();
expect(assetMock.save).not.toHaveBeenCalled();
expect(assetMock.update).not.toHaveBeenCalled();
});
});
});