Skip to content

Commit 0d7c8b3

Browse files
test: add object storage unit tests
1 parent 8b0d7c9 commit 0d7c8b3

3 files changed

Lines changed: 581 additions & 0 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package object
2+
3+
import (
4+
"testing"
5+
6+
objectstorage "github.com/ionos-cloud/sdk-go-bundle/products/objectstorage/v2"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func strPtr(s string) *string { return &s }
11+
12+
func TestObjectsToIdentifiers(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
objects []objectstorage.Object
16+
want []objectstorage.ObjectIdentifier
17+
}{
18+
{
19+
name: "empty slice",
20+
objects: []objectstorage.Object{},
21+
want: []objectstorage.ObjectIdentifier{},
22+
},
23+
{
24+
name: "multiple objects with keys",
25+
objects: []objectstorage.Object{
26+
{Key: strPtr("photos/a.jpg")},
27+
{Key: strPtr("docs/b.pdf")},
28+
},
29+
want: []objectstorage.ObjectIdentifier{
30+
{Key: "photos/a.jpg"},
31+
{Key: "docs/b.pdf"},
32+
},
33+
},
34+
{
35+
name: "object with nil Key returns empty string",
36+
objects: []objectstorage.Object{
37+
{Key: nil},
38+
},
39+
want: []objectstorage.ObjectIdentifier{
40+
{Key: ""},
41+
},
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
got := objectsToIdentifiers(tt.objects)
48+
assert.Equal(t, tt.want, got)
49+
})
50+
}
51+
}
52+
53+
func TestVersionsToIdentifiers(t *testing.T) {
54+
tests := []struct {
55+
name string
56+
versions []objectstorage.ObjectVersion
57+
want []objectstorage.ObjectIdentifier
58+
}{
59+
{
60+
name: "empty slice",
61+
versions: []objectstorage.ObjectVersion{},
62+
want: []objectstorage.ObjectIdentifier{},
63+
},
64+
{
65+
name: "version with VersionId set",
66+
versions: []objectstorage.ObjectVersion{
67+
{Key: strPtr("file.txt"), VersionId: strPtr("v1")},
68+
},
69+
want: []objectstorage.ObjectIdentifier{
70+
{Key: "file.txt", VersionId: strPtr("v1")},
71+
},
72+
},
73+
{
74+
name: "version with nil VersionId results in nil VersionId",
75+
versions: []objectstorage.ObjectVersion{
76+
{Key: strPtr("file.txt"), VersionId: nil},
77+
},
78+
want: []objectstorage.ObjectIdentifier{
79+
{Key: "file.txt", VersionId: nil},
80+
},
81+
},
82+
{
83+
name: "mix of versioned and unversioned",
84+
versions: []objectstorage.ObjectVersion{
85+
{Key: strPtr("a.txt"), VersionId: strPtr("v1")},
86+
{Key: strPtr("b.txt"), VersionId: nil},
87+
{Key: strPtr("c.txt"), VersionId: strPtr("v3")},
88+
},
89+
want: []objectstorage.ObjectIdentifier{
90+
{Key: "a.txt", VersionId: strPtr("v1")},
91+
{Key: "b.txt", VersionId: nil},
92+
{Key: "c.txt", VersionId: strPtr("v3")},
93+
},
94+
},
95+
}
96+
97+
for _, tt := range tests {
98+
t.Run(tt.name, func(t *testing.T) {
99+
got := versionsToIdentifiers(tt.versions)
100+
assert.Equal(t, tt.want, got)
101+
})
102+
}
103+
}
104+
105+
func TestDeleteMarkersToIdentifiers(t *testing.T) {
106+
tests := []struct {
107+
name string
108+
markers []objectstorage.DeleteMarkerEntry
109+
want []objectstorage.ObjectIdentifier
110+
}{
111+
{
112+
name: "empty slice",
113+
markers: []objectstorage.DeleteMarkerEntry{},
114+
want: []objectstorage.ObjectIdentifier{},
115+
},
116+
{
117+
name: "marker with VersionId set",
118+
markers: []objectstorage.DeleteMarkerEntry{
119+
{Key: strPtr("removed.txt"), VersionId: strPtr("dm1")},
120+
},
121+
want: []objectstorage.ObjectIdentifier{
122+
{Key: "removed.txt", VersionId: strPtr("dm1")},
123+
},
124+
},
125+
{
126+
name: "marker with nil VersionId results in nil VersionId",
127+
markers: []objectstorage.DeleteMarkerEntry{
128+
{Key: strPtr("removed.txt"), VersionId: nil},
129+
},
130+
want: []objectstorage.ObjectIdentifier{
131+
{Key: "removed.txt", VersionId: nil},
132+
},
133+
},
134+
}
135+
136+
for _, tt := range tests {
137+
t.Run(tt.name, func(t *testing.T) {
138+
got := deleteMarkersToIdentifiers(tt.markers)
139+
assert.Equal(t, tt.want, got)
140+
})
141+
}
142+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package object
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
objectstorage "github.com/ionos-cloud/sdk-go-bundle/products/objectstorage/v2"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func ptr[T any](v T) *T { return &v }
12+
13+
func TestConvertObjects(t *testing.T) {
14+
fixedTime := time.Date(2025, 6, 15, 10, 30, 0, 0, time.UTC)
15+
storageClass := objectstorage.OBJECTSTORAGECLASS_STANDARD
16+
17+
tests := []struct {
18+
name string
19+
input []objectstorage.Object
20+
expected []listObjectInfo
21+
}{
22+
{
23+
name: "empty slice",
24+
input: []objectstorage.Object{},
25+
expected: []listObjectInfo{},
26+
},
27+
{
28+
name: "all fields populated",
29+
input: []objectstorage.Object{
30+
{
31+
Key: ptr("photos/cat.jpg"),
32+
Size: ptr(int32(1024)),
33+
LastModified: &objectstorage.IonosTime{Time: fixedTime},
34+
StorageClass: &storageClass,
35+
ETag: ptr("\"abc123\""),
36+
},
37+
},
38+
expected: []listObjectInfo{
39+
{
40+
Key: "photos/cat.jpg",
41+
Size: "1.0 KiB",
42+
LastModified: fixedTime,
43+
StorageClass: "STANDARD",
44+
ETag: "\"abc123\"",
45+
},
46+
},
47+
},
48+
{
49+
name: "nil LastModified yields zero time",
50+
input: []objectstorage.Object{
51+
{
52+
Key: ptr("file.txt"),
53+
Size: ptr(int32(512)),
54+
LastModified: nil,
55+
StorageClass: &storageClass,
56+
ETag: ptr("\"def456\""),
57+
},
58+
},
59+
expected: []listObjectInfo{
60+
{
61+
Key: "file.txt",
62+
Size: "512 B",
63+
LastModified: time.Time{},
64+
StorageClass: "STANDARD",
65+
ETag: "\"def456\"",
66+
},
67+
},
68+
},
69+
{
70+
name: "nil StorageClass yields empty string",
71+
input: []objectstorage.Object{
72+
{
73+
Key: ptr("data.bin"),
74+
Size: ptr(int32(2048)),
75+
LastModified: &objectstorage.IonosTime{Time: fixedTime},
76+
StorageClass: nil,
77+
ETag: ptr("\"ghi789\""),
78+
},
79+
},
80+
expected: []listObjectInfo{
81+
{
82+
Key: "data.bin",
83+
Size: "2.0 KiB",
84+
LastModified: fixedTime,
85+
StorageClass: "",
86+
ETag: "\"ghi789\"",
87+
},
88+
},
89+
},
90+
{
91+
name: "nil Size returns 0 B",
92+
input: []objectstorage.Object{
93+
{
94+
Key: ptr("empty.txt"),
95+
Size: nil,
96+
LastModified: &objectstorage.IonosTime{Time: fixedTime},
97+
StorageClass: &storageClass,
98+
ETag: ptr("\"jkl012\""),
99+
},
100+
},
101+
expected: []listObjectInfo{
102+
{
103+
Key: "empty.txt",
104+
Size: "0 B",
105+
LastModified: fixedTime,
106+
StorageClass: "STANDARD",
107+
ETag: "\"jkl012\"",
108+
},
109+
},
110+
},
111+
{
112+
name: "multiple objects converted correctly",
113+
input: []objectstorage.Object{
114+
{
115+
Key: ptr("small.txt"),
116+
Size: ptr(int32(100)),
117+
LastModified: &objectstorage.IonosTime{Time: fixedTime},
118+
StorageClass: &storageClass,
119+
ETag: ptr("\"aaa\""),
120+
},
121+
{
122+
Key: ptr("medium.bin"),
123+
Size: ptr(int32(1048576)), // 1 MiB
124+
LastModified: &objectstorage.IonosTime{Time: fixedTime.Add(time.Hour)},
125+
StorageClass: &storageClass,
126+
ETag: ptr("\"bbb\""),
127+
},
128+
{
129+
Key: ptr("large.iso"),
130+
Size: ptr(int32(1073741824)), // 1 GiB (overflows int32, but tests the path)
131+
LastModified: nil,
132+
StorageClass: nil,
133+
ETag: ptr("\"ccc\""),
134+
},
135+
},
136+
expected: []listObjectInfo{
137+
{
138+
Key: "small.txt",
139+
Size: "100 B",
140+
LastModified: fixedTime,
141+
StorageClass: "STANDARD",
142+
ETag: "\"aaa\"",
143+
},
144+
{
145+
Key: "medium.bin",
146+
Size: "1.0 MiB",
147+
LastModified: fixedTime.Add(time.Hour),
148+
StorageClass: "STANDARD",
149+
ETag: "\"bbb\"",
150+
},
151+
{
152+
Key: "large.iso",
153+
Size: "1.0 GiB",
154+
LastModified: time.Time{},
155+
StorageClass: "",
156+
ETag: "\"ccc\"",
157+
},
158+
},
159+
},
160+
}
161+
162+
for _, tt := range tests {
163+
t.Run(tt.name, func(t *testing.T) {
164+
result := convertObjects(tt.input)
165+
assert.Equal(t, tt.expected, result)
166+
})
167+
}
168+
}

0 commit comments

Comments
 (0)