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

chore(server): Increase domain package unit test coverage (asset and integrationapi) #1302

Merged
merged 14 commits into from
Nov 29, 2024
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
2 changes: 2 additions & 0 deletions server/pkg/asset/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAsset_Type(t *testing.T) {
uuid: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
thread: thid,
archiveExtractionStatus: &gotStatus,
flatFiles: false,
}

assert.Equal(t, aid, got.ID())
Expand All @@ -46,6 +47,7 @@ func TestAsset_Type(t *testing.T) {
assert.Equal(t, &wantPreviewType, got.PreviewType())
assert.Equal(t, "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", got.UUID())
assert.Equal(t, thid, got.Thread())
assert.Equal(t, false, got.FlatFiles())
assert.Equal(t, &wantStatus, got.ArchiveExtractionStatus())
}

Expand Down
6 changes: 5 additions & 1 deletion server/pkg/asset/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Input struct {
uuid string
thread ThreadID
archiveExtractionStatus *ArchiveExtractionStatus
flatFiles bool
}

func TestBuilder_Build(t *testing.T) {
Expand All @@ -53,6 +54,7 @@ func TestBuilder_Build(t *testing.T) {
uuid: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
thread: thid,
archiveExtractionStatus: lo.ToPtr(ArchiveExtractionStatusPending),
flatFiles: false,
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add test case for true flatFiles value

While the current test case covers flatFiles: false, consider adding another test case where flatFiles is set to true to ensure complete coverage of this new field's behavior.

Here's a suggested additional test case:

 tests := Tests{
+    {
+        name: "should create an asset with flatFiles enabled",
+        input: Input{
+            id:                      aid,
+            project:                 pid,
+            createdAt:               tim,
+            createdByUser:           uid,
+            fileName:                "hoge",
+            size:                    size,
+            previewType:             lo.ToPtr(PreviewTypeImage),
+            uuid:                    "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
+            thread:                  thid,
+            archiveExtractionStatus: lo.ToPtr(ArchiveExtractionStatusPending),
+            flatFiles:               true,
+        },
+        want: &Asset{
+            id:                      aid,
+            project:                 pid,
+            createdAt:               tim,
+            user:                    &uid,
+            fileName:                "hoge",
+            size:                    size,
+            previewType:             PreviewTypeFromRef(lo.ToPtr("image")),
+            uuid:                    "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
+            thread:                  thid,
+            archiveExtractionStatus: lo.ToPtr(ArchiveExtractionStatusPending),
+            flatFiles:               true,
+        },
+    },

Also applies to: 70-70

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code deosnt handle if flatfiles is true/ false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonkarel, understood. Since the code doesn't handle different behaviors when flatFiles is true or false, adding an additional test case isn't necessary. Thank you for the clarification.


✏️ Learnings added
Learnt from: jasonkarel
PR: reearth/reearth-cms#1302
File: server/pkg/asset/builder_test.go:57-57
Timestamp: 2024-11-12T05:50:23.518Z
Learning: In the `server/pkg/asset` package, the `flatFiles` field does not affect behavior based on its value, so adding test cases for different `flatFiles` values is unnecessary.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

},
want: &Asset{
id: aid,
Expand All @@ -65,6 +67,7 @@ func TestBuilder_Build(t *testing.T) {
uuid: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
thread: thid,
archiveExtractionStatus: lo.ToPtr(ArchiveExtractionStatusPending),
flatFiles: false,
},
},
{
Expand Down Expand Up @@ -217,7 +220,8 @@ func TestBuilder_Build(t *testing.T) {
Type(tt.input.previewType).
UUID(tt.input.uuid).
Thread(tt.input.thread).
ArchiveExtractionStatus(tt.input.archiveExtractionStatus)
ArchiveExtractionStatus(tt.input.archiveExtractionStatus).
FlatFiles(tt.input.flatFiles)
if !tt.input.createdByUser.IsNil() {
ab.CreatedByUser(tt.input.createdByUser)
}
Expand Down
123 changes: 118 additions & 5 deletions server/pkg/asset/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ func TestFile_FileType(t *testing.T) {

dir := NewFile().Name("dir").Path("/aaa").Children([]*File{c}).Build()
assert.True(t, dir.IsDir())

// object is nil test
f = nil
assert.Equal(t, "", f.Name())
assert.Equal(t, uint64(0), f.Size())
assert.Equal(t, "", f.ContentType())
assert.Equal(t, "", f.Path())
}

func TestFile_Children(t *testing.T) {
Expand Down Expand Up @@ -60,14 +67,36 @@ func TestFile_Files(t *testing.T) {
},
}

assert.Equal(t, []*File{
tests := []struct {
name string
files *File
want []*File
}{
{
path: "aaa/a/a.txt",
name: "success",
files: f,
want: []*File{
{
path: "aaa/a/a.txt",
},
{
path: "aaa/b.txt",
},
},
},
{
path: "aaa/b.txt",
name: "file object is empyy",
files: nil,
want: nil,
},
}, f.FlattenChildren())
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
flatten := tt.files.FlattenChildren()
assert.Equal(t, flatten, tt.want)
})
}
}

func TestFile_SetFiles(t *testing.T) {
Expand Down Expand Up @@ -194,5 +223,89 @@ func Test_FoldFiles(t *testing.T) {
}

func Test_File_RootPath(t *testing.T) {
assert.Equal(t, "xx/xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/hoge.zip", (&File{path: "hoge.zip"}).RootPath("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"))
tests := []struct {
name string
file *File
uuid string
want string
}{
{
name: "success",
file: &File{path: "hoge.zip"},
uuid: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
want: "xx/xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/hoge.zip",
},
{
name: "File object is nil",
file: nil,
uuid: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := tt.file.RootPath(tt.uuid)
assert.Equal(t, result, tt.want)
})
}
}

func Test_Clone(t *testing.T) {
tests := []struct {
name string
file *File
want *File
}{
{
name: "success",
file: &File{
name: "test",
size: 1,
contentType: "type",
path: "hoge.zip",
children: []*File{
{name: "a.txt", path: "/hello/good/a.txt", size: 10, contentType: "text/plain"},
{name: "b.txt", path: "/hello/good/b.txt", size: 10, contentType: "text/plain"},
},
},
want: &File{
name: "test",
size: 1,
contentType: "type",
path: "hoge.zip",
children: []*File{
{name: "a.txt", path: "/hello/good/a.txt", size: 10, contentType: "text/plain"},
{name: "b.txt", path: "/hello/good/b.txt", size: 10, contentType: "text/plain"},
},
},
},
{
name: "file is nil",
file: nil,
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cloned := tt.file.Clone()
assert.Equal(t, cloned, tt.want)
})
}
}

func Test_FilePath(t *testing.T) {
t.Parallel()
assert.Equal(t,
[]string{
"/hello/c.txt",
},
(&File{
name: "hello.zip", path: "/hello.zip", size: 100, contentType: "application/zip",
files: []*File{
{name: "c.txt", path: "/hello/c.txt", size: 20, contentType: "text/plain"},
},
}).FilePaths(),
)
}
13 changes: 11 additions & 2 deletions server/pkg/asset/preview_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ func TestPreviewType_DetectPreviewType(t *testing.T) {
f1 := file.File{
Name: "image.png",
ContentType: "image/png",
}
}
want1 := PreviewTypeImage
got1 := DetectPreviewType(&f1)
assert.Equal(t, want1, *got1)

f2 := file.File{
Name: "file.geojson",
ContentType: "application/json",
}
}
want2 := PreviewTypeGeo
got2 := DetectPreviewType(&f2)
assert.Equal(t, want2, *got2)
Expand Down Expand Up @@ -278,6 +278,10 @@ func TestPreviewType_PreviewTypeFromExtension(t *testing.T) {
want6 := PreviewTypeGeoMvt
got6 := PreviewTypeFromExtension(ext6)
assert.Equal(t, want6, got6)

want7 := PreviewTypeUnknown
got7 := PreviewTypeFromExtension("")
assert.Equal(t, want7, got7)
}

func TestPreviewType_String(t *testing.T) {
Expand Down Expand Up @@ -316,3 +320,8 @@ func TestPreviewType_StringRef(t *testing.T) {
})
}
}

func TestPreviewType_Prev(t *testing.T) {
t.Parallel()
assert.Equal(t, func() *PreviewType { pt := PreviewType("image"); return &pt }(), PreviewTypeImage.Ref())
}
4 changes: 4 additions & 0 deletions server/pkg/asset/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func TestStatus_StatusFromRef(t *testing.T) {
res = ArchiveExtractionStatusFromRef(s)
assert.Equal(t, &f, res)

s = lo.ToPtr("test")
res = ArchiveExtractionStatusFromRef(s)
assert.Nil(t, res)

s = nil
res = ArchiveExtractionStatusFromRef(s)
assert.Nil(t, res)
Expand Down
Loading