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

Add unit test for generating data source #5439

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gajananan
Copy link
Contributor

Summary

Add unit tests for Swagger datasource definition generation (#5283)

  • Implement unit tests to validate datasource definition generation from Swagger files.
  • Ensure hardcoded OpenAPI v2 docs produce expected datasource definitions.
  • Maintain robustness of generation command to prevent accidental breaks.

Ref #(5300)

Change Type

Mark the type of change your PR introduces:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

…c#5283)

- Implement unit tests to validate datasource definition generation from Swagger files.
- Ensure hardcoded OpenAPI v2 docs produce expected datasource definitions.
- Maintain robustness of generation command to prevent accidental breaks.

Signed-off-by: Kugamoorthy Gajananan <[email protected]>
…c#5283)

- Implement unit tests to validate datasource definition generation from Swagger files.
- Ensure hardcoded OpenAPI v2 docs produce expected datasource definitions.
- Maintain robustness of generation command to prevent accidental breaks.
- Renamed generate_test to main_test.go and moved it to cmd/dev/

Signed-off-by: Kugamoorthy Gajananan <[email protected]>
@gajananan gajananan requested a review from a team as a code owner February 14, 2025 22:30
Copy link
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

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

Thanks for this! This looks fairly good, but a couple questions about the implementation.

I also noticed a data race warning; you might want to try running with -race -count 20 to see if you can shake it out.


// Handle the case for missing OpenAPI file
if tt.name == "missing OpenAPI file" {
mu.Lock()
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a mutex around this?

Copy link
Contributor Author

@gajananan gajananan Feb 17, 2025

Choose a reason for hiding this comment

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

This segment of code should not be there, the mutex. should not be.

Comment on lines +122 to +127
if tt.name == "missing OpenAPI file" {
mu.Lock()
assert.FileExists(t, tt.openAPIFile, "OpenAPI file should not exist")
mu.Unlock()
return
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we ever get here, given that tt.expectedError == true for the "missing OpenAPI file" case, and that exits on line 109?

(This will allow you to simplify the test code, which is another win)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is correct. it won't reach there.

Comment on lines +137 to +138
// Add a slight delay to ensure the output is captured correctly
time.Sleep(100 * time.Millisecond)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a "Sleep" after we've read and compared all the content?

Copy link
Contributor Author

@gajananan gajananan Feb 17, 2025

Choose a reason for hiding this comment

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

Due to the following code to capture output from Command execution, at times the output was not captured properly, I tried few option to see, it is one of them. I agree it does not make sense when I look at it.

                       // Create a pipe to capture the output,
			r, w, _ := os.Pipe()
			os.Stdout = w
			// Redirect the output to the buffer in a separate goroutine
			outC := make(chan string)
			go func() {
				var buf bytes.Buffer
				_, err = io.Copy(&buf, r)
				assert.NoError(t, err, "Buffer copy should not produce an error")

				outC <- buf.String()
			}()

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.

2 participants