Skip to content

Conversation

@gourish25
Copy link

@gourish25 gourish25 commented Oct 17, 2025

Pull Request Template

Description:

  • This PR closes Unit Test for s3 in files/datasource #2442
  • Added comprehensive test coverage for S3 file operations in file_test.go
  • Added test coverage for S3 file system operations in fs_test.go
  • Implemented unit tests for S3File methods including Read, Write, Seek, Close, and metadata operations
  • Added tests for FileSystem methods including Connect, Create, Open, and directory operations

Additional Information:
Tests use mock objects for S3 client
Testify assertions used for better test readability
Helper functions created for consistent test setup

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.
  • 87.0% test coverage maintained

Thank you for your contribution!

Copy link
Member

@Umang01-hash Umang01-hash left a comment

Choose a reason for hiding this comment

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

  • Please remove all if-else conditions from the test methods and break them into smaller methods if needed for success and error cases.

)

var (
ErrGetObject = errors.New("failed to get object from S3")
Copy link
Member

Choose a reason for hiding this comment

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

Why are these errors exported? If not used outside of the package can we please unexport them?


err := f.Close()

if tc.expected == nil {
Copy link
Member

Choose a reason for hiding this comment

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

We should not have if-else conditions inside test methods, if needed you can break the method into two separate methods (ex: one for success cases and other for error).

}
}

var errS3Test = errors.New("s3 error")
Copy link
Member

Choose a reason for hiding this comment

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

It should also be declared along with other error vars at the top.

require.NoError(t, err, "ReadAll() unexpected error")
require.NotNil(t, reader, "ReadAll() returned nil reader on success")

switch expectedType {
Copy link
Member

Choose a reason for hiding this comment

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

Switch cases in test methods are not a very good practice. Please break the tests if needed and remove the switch case.

tt.setupMocks()
_, err := fs.Create(tt.createPath)

if tt.expectError {
Copy link
Member

Choose a reason for hiding this comment

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

Please remove this if-else block from this test method too.

Comment on lines +335 to +347
f := S3File{
logger: mockLogger,
metrics: mockMetrics,
conn: mockS3,
}

fs := &FileSystem{
s3File: f,
conn: mockS3,
logger: mockLogger,
config: config,
metrics: mockMetrics,
}
Copy link
Member

Choose a reason for hiding this comment

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

This code is being repeated 11 times inside the file can we please refactor and reuse it to avoid code duplication.

// Execute the test
_, err := fs.Open(tt.fileName)

if tt.expectError {
Copy link
Member

Choose a reason for hiding this comment

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

No if else inside test methods please

Comment on lines +544 to +547
// Create a mock S3 client
mockS3 := NewMocks3Client(ctrl)
mockLogger := NewMockLogger(ctrl)
mockMetrics := NewMockMetrics(ctrl)
Copy link
Member

Choose a reason for hiding this comment

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

I saw these three initializations 7 times in the file, let's try and reuse it from a common function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unit Test for s3 in files/datasource

2 participants