Skip to content

Commit 2ce4492

Browse files
Add tests for gs file
1 parent bdfd860 commit 2ce4492

File tree

1 file changed

+231
-0
lines changed

1 file changed

+231
-0
lines changed

backend/gs/file_test.go

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,237 @@ func (ts *fileTestSuite) TestMoveAndCopyBuffered() {
539539
}
540540
}
541541

542+
func (ts *fileTestSuite) TestSize() {
543+
contents := "hello world!"
544+
bucketName := "bucki"
545+
objectName := "some/path/file.txt"
546+
server := fakestorage.NewServer(
547+
Objects{
548+
fakestorage.Object{
549+
ObjectAttrs: fakestorage.ObjectAttrs{
550+
BucketName: bucketName,
551+
Name: objectName,
552+
ContentType: "text/plain",
553+
ContentEncoding: "utf8",
554+
},
555+
Content: []byte(contents),
556+
},
557+
},
558+
)
559+
defer server.Stop()
560+
fs := NewFileSystem(WithClient(server.Client()))
561+
562+
file, err := fs.NewFile(bucketName, "/"+objectName)
563+
ts.Require().NoError(err, "Shouldn't fail creating new file")
564+
565+
size, err := file.Size()
566+
ts.NoError(err, "Size() should not return an error for existing file")
567+
ts.Equal(uint64(len(contents)), size, "Size should match the content length")
568+
}
569+
570+
func (ts *fileTestSuite) TestSizeError() {
571+
bucketName := "bucki"
572+
objectName := "nonexistent.txt"
573+
server := fakestorage.NewServer(Objects{})
574+
defer server.Stop()
575+
fs := NewFileSystem(WithClient(server.Client()))
576+
577+
file, err := fs.NewFile(bucketName, "/"+objectName)
578+
ts.Require().NoError(err, "Shouldn't fail creating new file")
579+
580+
_, err = file.Size()
581+
ts.Error(err, "Size() should return an error for non-existent file")
582+
}
583+
584+
func (ts *fileTestSuite) TestLastModified() {
585+
contents := "hello world!"
586+
bucketName := "bucki"
587+
objectName := "some/path/file.txt"
588+
server := fakestorage.NewServer(
589+
Objects{
590+
fakestorage.Object{
591+
ObjectAttrs: fakestorage.ObjectAttrs{
592+
BucketName: bucketName,
593+
Name: objectName,
594+
ContentType: "text/plain",
595+
ContentEncoding: "utf8",
596+
},
597+
Content: []byte(contents),
598+
},
599+
},
600+
)
601+
defer server.Stop()
602+
fs := NewFileSystem(WithClient(server.Client()))
603+
604+
file, err := fs.NewFile(bucketName, "/"+objectName)
605+
ts.Require().NoError(err, "Shouldn't fail creating new file")
606+
607+
lastMod, err := file.LastModified()
608+
ts.NoError(err, "LastModified() should not return an error for existing file")
609+
ts.NotNil(lastMod, "LastModified should return a non-nil time")
610+
}
611+
612+
func (ts *fileTestSuite) TestLastModifiedError() {
613+
bucketName := "bucki"
614+
objectName := "nonexistent.txt"
615+
server := fakestorage.NewServer(Objects{})
616+
defer server.Stop()
617+
fs := NewFileSystem(WithClient(server.Client()))
618+
619+
file, err := fs.NewFile(bucketName, "/"+objectName)
620+
ts.Require().NoError(err, "Shouldn't fail creating new file")
621+
622+
_, err = file.LastModified()
623+
ts.Error(err, "LastModified() should return an error for non-existent file")
624+
}
625+
626+
func (ts *fileTestSuite) TestStat() {
627+
contents := "hello world!"
628+
bucketName := "bucki"
629+
objectName := "some/path/file.txt"
630+
server := fakestorage.NewServer(
631+
Objects{
632+
fakestorage.Object{
633+
ObjectAttrs: fakestorage.ObjectAttrs{
634+
BucketName: bucketName,
635+
Name: objectName,
636+
ContentType: "text/plain",
637+
ContentEncoding: "utf8",
638+
},
639+
Content: []byte(contents),
640+
},
641+
},
642+
)
643+
defer server.Stop()
644+
fs := NewFileSystem(WithClient(server.Client()))
645+
646+
file, err := fs.NewFile(bucketName, "/"+objectName)
647+
ts.Require().NoError(err, "Shouldn't fail creating new file")
648+
649+
fileInfo, err := file.Stat()
650+
ts.NoError(err, "Stat() should not return an error for existing file")
651+
ts.NotNil(fileInfo, "FileInfo should not be nil")
652+
ts.Equal("file.txt", fileInfo.Name(), "FileInfo name should match file name")
653+
ts.Equal(int64(len(contents)), fileInfo.Size(), "FileInfo size should match content length")
654+
ts.False(fileInfo.IsDir(), "FileInfo should indicate file is not a directory")
655+
ts.NotNil(fileInfo.ModTime(), "ModTime should not be nil")
656+
ts.Equal(0644, int(fileInfo.Mode()), "Mode should be 0644")
657+
ts.Nil(fileInfo.Sys(), "Sys should return nil")
658+
}
659+
660+
func (ts *fileTestSuite) TestStatError() {
661+
bucketName := "bucki"
662+
objectName := "nonexistent.txt"
663+
server := fakestorage.NewServer(Objects{})
664+
defer server.Stop()
665+
fs := NewFileSystem(WithClient(server.Client()))
666+
667+
file, err := fs.NewFile(bucketName, "/"+objectName)
668+
ts.Require().NoError(err, "Shouldn't fail creating new file")
669+
670+
_, err = file.Stat()
671+
ts.Error(err, "Stat() should return an error for non-existent file")
672+
}
673+
674+
func (ts *fileTestSuite) TestTouchExistingFileWithVersioning() {
675+
contents := "hello world!"
676+
bucketName := "bucki"
677+
objectName := "some/path/file.txt"
678+
server := fakestorage.NewServer(
679+
Objects{
680+
fakestorage.Object{
681+
ObjectAttrs: fakestorage.ObjectAttrs{
682+
BucketName: bucketName,
683+
Name: objectName,
684+
ContentType: "text/plain",
685+
ContentEncoding: "utf8",
686+
},
687+
Content: []byte(contents),
688+
},
689+
},
690+
)
691+
defer server.Stop()
692+
client := server.Client()
693+
694+
// Enable versioning on the bucket manually
695+
ctx := context.Background()
696+
bucket := client.Bucket(bucketName)
697+
_, err := bucket.Update(ctx, storage.BucketAttrsToUpdate{
698+
VersioningEnabled: true,
699+
})
700+
ts.NoError(err, "Setting versioning should not error")
701+
702+
fs := NewFileSystem(WithClient(client))
703+
704+
file, err := fs.NewFile(bucketName, "/"+objectName)
705+
ts.Require().NoError(err, "Shouldn't fail creating new file")
706+
707+
// This should use the updateLastModifiedByMoving path since versioning is enabled
708+
err = file.Touch()
709+
ts.NoError(err, "Touch() should not return an error for existing file with versioning")
710+
711+
// Check the file still exists and is accessible
712+
exists, err := file.Exists()
713+
ts.NoError(err, "Exists() should not return an error")
714+
ts.True(exists, "File should still exist after Touch with versioning")
715+
}
716+
717+
func (ts *fileTestSuite) TestTouchExistingFileWithoutVersioning() {
718+
contents := "hello world!"
719+
bucketName := "bucki"
720+
objectName := "some/path/file.txt"
721+
server := fakestorage.NewServer(
722+
Objects{
723+
fakestorage.Object{
724+
ObjectAttrs: fakestorage.ObjectAttrs{
725+
BucketName: bucketName,
726+
Name: objectName,
727+
ContentType: "text/plain",
728+
ContentEncoding: "utf8",
729+
},
730+
Content: []byte(contents),
731+
},
732+
},
733+
)
734+
defer server.Stop()
735+
client := server.Client()
736+
fs := NewFileSystem(WithClient(client))
737+
738+
file, err := fs.NewFile(bucketName, "/"+objectName)
739+
ts.Require().NoError(err, "Shouldn't fail creating new file")
740+
741+
// This should use the updateLastModifiedByAttrUpdate path since versioning is not enabled
742+
err = file.Touch()
743+
ts.NoError(err, "Touch() should not return an error for existing file without versioning")
744+
745+
// Check the file still exists and is accessible
746+
exists, err := file.Exists()
747+
ts.NoError(err, "Exists() should not return an error")
748+
ts.True(exists, "File should still exist after Touch without versioning")
749+
}
750+
751+
func (ts *fileTestSuite) TestNameAndPath() {
752+
fs := NewFileSystem()
753+
objectName := "some/path/file.txt"
754+
755+
file, err := fs.NewFile("bucket", "/"+objectName)
756+
ts.NoError(err, "Shouldn't fail creating new file")
757+
758+
ts.Equal("file.txt", file.Name(), "Name should be just the filename")
759+
ts.Equal("/some/path/file.txt", file.Path(), "Path should be the full path")
760+
}
761+
762+
func (ts *fileTestSuite) TestURI() {
763+
fs := NewFileSystem()
764+
objectName := "some/path/file.txt"
765+
766+
file, err := fs.NewFile("bucket", "/"+objectName)
767+
ts.NoError(err, "Shouldn't fail creating new file")
768+
769+
ts.Equal("gs://bucket/some/path/file.txt", file.URI(), "URI should be correctly formatted")
770+
ts.Equal("gs://bucket/some/path/file.txt", file.String(), "String() should return URI")
771+
}
772+
542773
func TestFile(t *testing.T) {
543774
suite.Run(t, new(fileTestSuite))
544775
}

0 commit comments

Comments
 (0)