From 6be37164410f32667f2cd6d99d038a65c6e0f4f5 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Mon, 11 Sep 2023 11:51:48 +1000 Subject: [PATCH] Refactor matching functions Matching functions were calling assert functions. Rewrote the assert functions to be internal matching functions for now. --- internal/autotag/gallery_test.go | 19 +++++-- internal/autotag/image_test.go | 19 +++++-- internal/autotag/performer_test.go | 6 +-- internal/autotag/scene_test.go | 19 +++++-- internal/autotag/studio_test.go | 6 +-- internal/autotag/tag_test.go | 6 +-- pkg/models/mocks/asserts.go | 85 ------------------------------ 7 files changed, 57 insertions(+), 103 deletions(-) delete mode 100644 pkg/models/mocks/asserts.go diff --git a/internal/autotag/gallery_test.go b/internal/autotag/gallery_test.go index abfff32109d..23c3d931ee6 100644 --- a/internal/autotag/gallery_test.go +++ b/internal/autotag/gallery_test.go @@ -14,6 +14,19 @@ const galleryExt = "zip" var testCtx = context.Background() +// returns got == expected +// ignores expected.UpdatedAt, but ensures that got.UpdatedAt is set and not null +func galleryPartialsEqual(got, expected models.GalleryPartial) bool { + // updated at should be set and not null + if !got.UpdatedAt.Set || got.UpdatedAt.Null { + return false + } + // else ignore the exact value + got.UpdatedAt = models.OptionalTime{} + + return assert.ObjectsAreEqual(got, expected) +} + func TestGalleryPerformers(t *testing.T) { t.Parallel() @@ -54,7 +67,7 @@ func TestGalleryPerformers(t *testing.T) { }, } - return mocks.AssertGalleryPartial(t, got, expected) + return galleryPartialsEqual(got, expected) }) mockGalleryReader.On("UpdatePartial", testCtx, galleryID, matchPartial).Return(nil, nil).Once() } @@ -101,7 +114,7 @@ func TestGalleryStudios(t *testing.T) { StudioID: models.NewOptionalInt(studioID), } - return mocks.AssertGalleryPartial(t, got, expected) + return galleryPartialsEqual(got, expected) }) mockGalleryReader.On("UpdatePartial", testCtx, galleryID, matchPartial).Return(nil, nil).Once() } @@ -179,7 +192,7 @@ func TestGalleryTags(t *testing.T) { }, } - return mocks.AssertGalleryPartial(t, got, expected) + return galleryPartialsEqual(got, expected) }) mockGalleryReader.On("UpdatePartial", testCtx, galleryID, matchPartial).Return(nil, nil).Once() } diff --git a/internal/autotag/image_test.go b/internal/autotag/image_test.go index 111bf52c977..06991beea1f 100644 --- a/internal/autotag/image_test.go +++ b/internal/autotag/image_test.go @@ -11,6 +11,19 @@ import ( const imageExt = "jpg" +// returns got == expected +// ignores expected.UpdatedAt, but ensures that got.UpdatedAt is set and not null +func imagePartialsEqual(got, expected models.ImagePartial) bool { + // updated at should be set and not null + if !got.UpdatedAt.Set || got.UpdatedAt.Null { + return false + } + // else ignore the exact value + got.UpdatedAt = models.OptionalTime{} + + return assert.ObjectsAreEqual(got, expected) +} + func TestImagePerformers(t *testing.T) { t.Parallel() @@ -51,7 +64,7 @@ func TestImagePerformers(t *testing.T) { }, } - return mocks.AssertImagePartial(t, got, expected) + return imagePartialsEqual(got, expected) }) mockImageReader.On("UpdatePartial", testCtx, imageID, matchPartial).Return(nil, nil).Once() } @@ -98,7 +111,7 @@ func TestImageStudios(t *testing.T) { StudioID: models.NewOptionalInt(studioID), } - return mocks.AssertImagePartial(t, got, expected) + return imagePartialsEqual(got, expected) }) mockImageReader.On("UpdatePartial", testCtx, imageID, matchPartial).Return(nil, nil).Once() } @@ -176,7 +189,7 @@ func TestImageTags(t *testing.T) { }, } - return mocks.AssertImagePartial(t, got, expected) + return imagePartialsEqual(got, expected) }) mockImageReader.On("UpdatePartial", testCtx, imageID, matchPartial).Return(nil, nil).Once() } diff --git a/internal/autotag/performer_test.go b/internal/autotag/performer_test.go index b2bc90d3adc..aa0a43d92f8 100644 --- a/internal/autotag/performer_test.go +++ b/internal/autotag/performer_test.go @@ -98,7 +98,7 @@ func testPerformerScenes(t *testing.T, performerName, expectedRegex string) { }, } - return mocks.AssertScenePartial(t, got, expected) + return scenePartialsEqual(got, expected) }) mockSceneReader.On("UpdatePartial", mock.Anything, sceneID, matchPartial).Return(nil, nil).Once() } @@ -193,7 +193,7 @@ func testPerformerImages(t *testing.T, performerName, expectedRegex string) { }, } - return mocks.AssertImagePartial(t, got, expected) + return imagePartialsEqual(got, expected) }) mockImageReader.On("UpdatePartial", mock.Anything, imageID, matchPartial).Return(nil, nil).Once() } @@ -288,7 +288,7 @@ func testPerformerGalleries(t *testing.T, performerName, expectedRegex string) { }, } - return mocks.AssertGalleryPartial(t, got, expected) + return galleryPartialsEqual(got, expected) }) mockGalleryReader.On("UpdatePartial", mock.Anything, galleryID, matchPartial).Return(nil, nil).Once() } diff --git a/internal/autotag/scene_test.go b/internal/autotag/scene_test.go index ece292a1b3c..a714c364c41 100644 --- a/internal/autotag/scene_test.go +++ b/internal/autotag/scene_test.go @@ -29,6 +29,19 @@ var testEndSeparators = []string{ ",", } +// asserts that got == expected +// ignores expected.UpdatedAt, but ensures that got.UpdatedAt is set and not null +func scenePartialsEqual(got, expected models.ScenePartial) bool { + // updated at should be set and not null + if !got.UpdatedAt.Set || got.UpdatedAt.Null { + return false + } + // else ignore the exact value + got.UpdatedAt = models.OptionalTime{} + + return assert.ObjectsAreEqual(got, expected) +} + func generateNamePatterns(name, separator, ext string) []string { var ret []string ret = append(ret, fmt.Sprintf("%s%saaa.%s", name, separator, ext)) @@ -190,7 +203,7 @@ func TestScenePerformers(t *testing.T) { }, } - return mocks.AssertScenePartial(t, got, expected) + return scenePartialsEqual(got, expected) }) mockSceneReader.On("UpdatePartial", testCtx, sceneID, matchPartial).Return(nil, nil).Once() } @@ -234,7 +247,7 @@ func TestSceneStudios(t *testing.T) { StudioID: models.NewOptionalInt(studioID), } - return mocks.AssertScenePartial(t, got, expected) + return scenePartialsEqual(got, expected) }) mockSceneReader.On("UpdatePartial", testCtx, sceneID, matchPartial).Return(nil, nil).Once() } @@ -312,7 +325,7 @@ func TestSceneTags(t *testing.T) { }, } - return mocks.AssertScenePartial(t, got, expected) + return scenePartialsEqual(got, expected) }) mockSceneReader.On("UpdatePartial", testCtx, sceneID, matchPartial).Return(nil, nil).Once() } diff --git a/internal/autotag/studio_test.go b/internal/autotag/studio_test.go index 47174922c14..aa52c9c5179 100644 --- a/internal/autotag/studio_test.go +++ b/internal/autotag/studio_test.go @@ -157,7 +157,7 @@ func testStudioScenes(t *testing.T, tc testStudioCase) { StudioID: models.NewOptionalInt(studioID), } - return mocks.AssertScenePartial(t, got, expected) + return scenePartialsEqual(got, expected) }) mockSceneReader.On("UpdatePartial", mock.Anything, sceneID, matchPartial).Return(nil, nil).Once() } @@ -260,7 +260,7 @@ func testStudioImages(t *testing.T, tc testStudioCase) { StudioID: models.NewOptionalInt(studioID), } - return mocks.AssertImagePartial(t, got, expected) + return imagePartialsEqual(got, expected) }) mockImageReader.On("UpdatePartial", mock.Anything, imageID, matchPartial).Return(nil, nil).Once() } @@ -362,7 +362,7 @@ func testStudioGalleries(t *testing.T, tc testStudioCase) { StudioID: models.NewOptionalInt(studioID), } - return mocks.AssertGalleryPartial(t, got, expected) + return galleryPartialsEqual(got, expected) }) mockGalleryReader.On("UpdatePartial", mock.Anything, galleryID, matchPartial).Return(nil, nil).Once() } diff --git a/internal/autotag/tag_test.go b/internal/autotag/tag_test.go index b014e49c769..4b183200490 100644 --- a/internal/autotag/tag_test.go +++ b/internal/autotag/tag_test.go @@ -160,7 +160,7 @@ func testTagScenes(t *testing.T, tc testTagCase) { }, } - return mocks.AssertScenePartial(t, got, expected) + return scenePartialsEqual(got, expected) }) mockSceneReader.On("UpdatePartial", mock.Anything, sceneID, matchPartial).Return(nil, nil).Once() } @@ -267,7 +267,7 @@ func testTagImages(t *testing.T, tc testTagCase) { }, } - return mocks.AssertImagePartial(t, got, expected) + return imagePartialsEqual(got, expected) }) mockImageReader.On("UpdatePartial", mock.Anything, imageID, matchPartial).Return(nil, nil).Once() } @@ -374,7 +374,7 @@ func testTagGalleries(t *testing.T, tc testTagCase) { }, } - return mocks.AssertGalleryPartial(t, got, expected) + return galleryPartialsEqual(got, expected) }) mockGalleryReader.On("UpdatePartial", mock.Anything, galleryID, matchPartial).Return(nil, nil).Once() diff --git a/pkg/models/mocks/asserts.go b/pkg/models/mocks/asserts.go deleted file mode 100644 index 8791f596429..00000000000 --- a/pkg/models/mocks/asserts.go +++ /dev/null @@ -1,85 +0,0 @@ -package mocks - -import ( - "github.com/stretchr/testify/assert" - - "github.com/stashapp/stash/pkg/models" -) - -// asserts that got == expected -// ignores expected.UpdatedAt, but ensures that got.UpdatedAt is set and not null -func AssertGalleryPartial(t assert.TestingT, got, expected models.GalleryPartial) bool { - // updated at should be set and not null - if !got.UpdatedAt.Set || got.UpdatedAt.Null { - return false - } - // else ignore the exact value - got.UpdatedAt = models.OptionalTime{} - - return assert.Equal(t, got, expected) -} - -// asserts that got == expected -// ignores expected.UpdatedAt, but ensures that got.UpdatedAt is set and not null -func AssertImagePartial(t assert.TestingT, got, expected models.ImagePartial) bool { - // updated at should be set and not null - if !got.UpdatedAt.Set || got.UpdatedAt.Null { - return false - } - // else ignore the exact value - got.UpdatedAt = models.OptionalTime{} - - return assert.Equal(t, got, expected) -} - -// asserts that got == expected -// ignores expected.UpdatedAt, but ensures that got.UpdatedAt is set and not null -func AssertPerformerPartial(t assert.TestingT, got, expected models.PerformerPartial) bool { - // updated at should be set and not null - if !got.UpdatedAt.Set || got.UpdatedAt.Null { - return false - } - // else ignore the exact value - got.UpdatedAt = models.OptionalTime{} - - return assert.Equal(t, got, expected) -} - -// asserts that got == expected -// ignores expected.UpdatedAt, but ensures that got.UpdatedAt is set and not null -func AssertScenePartial(t assert.TestingT, got, expected models.ScenePartial) bool { - // updated at should be set and not null - if !got.UpdatedAt.Set || got.UpdatedAt.Null { - return false - } - // else ignore the exact value - got.UpdatedAt = models.OptionalTime{} - - return assert.Equal(t, got, expected) -} - -// asserts that got == expected -// ignores expected.UpdatedAt, but ensures that got.UpdatedAt is set and not null -func AssertStudioPartial(t assert.TestingT, got, expected models.StudioPartial) bool { - // updated at should be set and not null - if !got.UpdatedAt.Set || got.UpdatedAt.Null { - return false - } - // else ignore the exact value - got.UpdatedAt = models.OptionalTime{} - - return assert.Equal(t, got, expected) -} - -// asserts that got == expected -// ignores expected.UpdatedAt, but ensures that got.UpdatedAt is set and not null -func AssertTagPartial(t assert.TestingT, got, expected models.TagPartial) bool { - // updated at should be set and not null - if !got.UpdatedAt.Set || got.UpdatedAt.Null { - return false - } - // else ignore the exact value - got.UpdatedAt = models.OptionalTime{} - - return assert.Equal(t, got, expected) -}