-
Couldn't load subscription status.
- Fork 1.7k
add test case and improved coverage for datasource/file/s3 #2441
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
base: development
Are you sure you want to change the base?
add test case and improved coverage for datasource/file/s3 #2441
Conversation
…r into EN/test-datasource-file-s3
There was a problem hiding this 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-elseconditions 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") |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
| f := S3File{ | ||
| logger: mockLogger, | ||
| metrics: mockMetrics, | ||
| conn: mockS3, | ||
| } | ||
|
|
||
| fs := &FileSystem{ | ||
| s3File: f, | ||
| conn: mockS3, | ||
| logger: mockLogger, | ||
| config: config, | ||
| metrics: mockMetrics, | ||
| } |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
| // Create a mock S3 client | ||
| mockS3 := NewMocks3Client(ctrl) | ||
| mockLogger := NewMockLogger(ctrl) | ||
| mockMetrics := NewMockMetrics(ctrl) |
There was a problem hiding this comment.
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.
Pull Request Template
Description:
Additional Information:
Tests use mock objects for S3 client
Testify assertions used for better test readability
Helper functions created for consistent test setup
Checklist:
Thank you for your contribution!