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

tests(pypi): add tests for fetching package from pypi #39

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ba11b0y
Copy link
Contributor

@ba11b0y ba11b0y commented Feb 25, 2023

Since it wasn't easy to test the existing worker code, I've tried to refactor it so that it can be mocked and also added a test for fetching PackageData from PyPi.

@puerco @nishakm if this seems fine, I'll go ahead and write more tests to increase coverage.

@@ -105,7 +105,7 @@ func NewPypiPackageDataFactory(client *helper.Client) PypiPackageDataFactory {
func (pf *pypiPackageDataFactory) GetPackageData(packageJSONURL string) (PypiPackageData, error) {
packageInfo := PypiPackageData{}

packageJSONURL = strings.Replace(packageJSONURL, "pypi.org", "", 1)
packageJSONURL = strings.ReplaceAll(packageJSONURL, "pypi.org", "")
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you edit your commit to explain why this change is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the http client was changed to accept a baseURL, so that external http requests can be mocked, I had to strip off pypi.org from the base url and hence in the first commit, I had introduced

packageJSONURL = strings.Replace(packageJSONURL, "pypi.org", "", 1)

which was later replaced by just to abide by the code style of the codebase.

packageJSONURL = strings.ReplaceAll(packageJSONURL, "pypi.org", "")

I thought the chore prefix to the commit made sense, but I'm open to adding a comment or changing the commit message for more verbosity.

}

return r, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is good for now. Thanks!

Copy link
Contributor

@nishakm nishakm 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 the PR! It looks to me that there are some significant changes added beyond writing tests. My suggestion is to submit smaller changes and explain in the commit why the change is needed. For example, why should we add a new PyPiSoftwareFactory datatype? What is missing from the current implementation? How does the change help with further changes or the final goal?

Comment on lines +87 to +92
type pypiPackageDataFactory struct {
client *helper.Client
}

client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return nil, err
}
type PypiPackageDataFactory interface {
GetPackageData(packageJSONURL string) (PypiPackageData, error)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm trying to understand why we need a new interface type. Could you explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The interface was introduced so that all receiver functions could be mocked in tests.
For instance, BuildModule fetches package data using GetPackageData and for writing unit tests for BuildModule it might be necessary to mock the call to GetPackageData.

@nishakm
Copy link
Contributor

nishakm commented Mar 1, 2023

Hi @ba11b0y, it would be much easier to figure out what is happening with the tests failing if your changes were small. That would help a lot to get the changes merged. Thanks for the work you put into this!

@ba11b0y
Copy link
Contributor Author

ba11b0y commented Mar 2, 2023

Hi @ba11b0y, it would be much easier to figure out what is happening with the tests failing if your changes were small. That would help a lot to get the changes merged. Thanks for the work you put into this!

I see the tests are failing on main for the swift and npm parsers. But for my changes I've pushed 7e1e693 to fix them.

And my apologies for sending in a huge PR to review, I understand it makes the review all the more difficult :/
Do you want me to close this one and send in smaller ones?
Thanks for taking time to review this!

@ba11b0y
Copy link
Contributor Author

ba11b0y commented Jun 7, 2023

Putting this on hold until this is actually needed.

@ba11b0y ba11b0y marked this pull request as draft June 7, 2023 17:15
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