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 tests to ParselyAPIConnection and minor refactor of it #81

Merged
merged 24 commits into from
Oct 23, 2023

Conversation

wzieba
Copy link
Collaborator

@wzieba wzieba commented Oct 20, 2023

Description

This PR adds unit tests for ParselyAPIConnection and adds some minor refactor to it.

How to test

Review and running unit tests should be just fine. Due to async nature of, well, AsyncTask, you can also verify that each test execution waited for AsyncTask to finish. You should see Pixel request success or Pixel request exception at the end of test logs.

wzieba added 22 commits October 19, 2023 10:22
Just above we clear `eventQueue` and purge locally stored events. There's no need to check the size of the queues.
This way, we don't have to check for nullability of the tracker. Also using `ParselyAPIConnection` without first initialising `ParselyTracker` doesn't seem to make any use case, so I think it's a save change.
BREAKING CHANGE: `ParselyAPIConnection` is no longer accessible for library consumers.

This is a deliberate design change. The `ParselyAPIConnection` on its own doesn't provide value for library consumers: it doesn't do anything domain-specific for Parsely, except purging queue after successful events. I think it's completely save to make it accessible only from the library.
No need for this field to be `public`
Every time SDK purges in-memory events queue, it purges stored queue and vice-versa. The extraction of method asserts this behaviour and increases readability
To unit test HTTP communication logic, framework agnostic.
Previously, the `ParselyAPIConnection` task was not finishing execution before the test finished. Only because the task's `onBackground` was fast, the test was passing. To remove this flakiness, the introduced change uses LooperMode.Mode.PAUSED to wait for AsyncTask execution to finish, before running assertions.
The `object` state persisted between tests, what influenced results depending on order of tests execution.
There's no need to return `HttpURLConnection`. This change simplifies implementation.
@codecov
Copy link

codecov bot commented Oct 20, 2023

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (22a8241) 20.44% compared to head (0f21123) 32.95%.

Additional details and impacted files
@@             Coverage Diff             @@
##             main      #81       +/-   ##
===========================================
+ Coverage   20.44%   32.95%   +12.51%     
===========================================
  Files           5        5               
  Lines         362      355        -7     
  Branches       58       56        -2     
===========================================
+ Hits           74      117       +43     
+ Misses        278      225       -53     
- Partials       10       13        +3     
Files Coverage Δ
...m/parsely/parselyandroid/ParselyAPIConnection.java 96.00% <100.00%> (+96.00%) ⬆️
...ava/com/parsely/parselyandroid/ParselyTracker.java 8.58% <0.00%> (+7.72%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

}

companion object {
val pixelPayload = """
Copy link
Collaborator Author

@wzieba wzieba Oct 20, 2023

Choose a reason for hiding this comment

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

The auto-format doesn't work here :/ I didn't format this by hand because it looks ok (readable) and I'm concerned about future edits of this field (and a need to format again by hand).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion (💡): I actually wanted to suggest using test/resources/pixel_payload.json instead. Wdyt? 🤔

Comment on lines +114 to +115
private class FakeTracker : ParselyTracker(
"siteId", 10, ApplicationProvider.getApplicationContext()
Copy link
Collaborator Author

@wzieba wzieba Oct 20, 2023

Choose a reason for hiding this comment

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

Initially, I wanted to introduce a package private interface (QueueManager?) but due to limitations of Java (interface methods must be public) I couldn't do it. Hence, the fake class extends the whole ParselyTracker.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Praise (❤️): Actually, I like what you did here, so 👍 !

@wzieba wzieba requested a review from ParaskP7 October 20, 2023 10:38
@wzieba wzieba marked this pull request as ready for review October 20, 2023 10:38
Copy link
Collaborator

@ParaskP7 ParaskP7 left a comment

Choose a reason for hiding this comment

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

👋 @wzieba !

I have reviewed and tested this PR as per the instructions, everything works as expected, great job on yet another test added! 🌟 🧪 🌟


I have left few a couple of suggestions (💡) for you to consider. I am going to approve this PR anyway, since none is blocking. I am NOT going to merge this PR yet to give you some time to apply any of my suggestions. However, feel free to ignore them and merge the PR yourself.

}

companion object {
val pixelPayload = """
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion (💡): I actually wanted to suggest using test/resources/pixel_payload.json instead. Wdyt? 🤔

Comment on lines +114 to +115
private class FakeTracker : ParselyTracker(
"siteId", 10, ApplicationProvider.getApplicationContext()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Praise (❤️): Actually, I like what you did here, so 👍 !

@wzieba
Copy link
Collaborator Author

wzieba commented Oct 23, 2023

Thanks @ParaskP7 for the review! I've addressed your suggestions 👍

@wzieba wzieba merged commit b2882f5 into main Oct 23, 2023
3 checks passed
@wzieba wzieba deleted the api_connection_unit_tests branch October 23, 2023 11:49
@ParaskP7
Copy link
Collaborator

Awesome, thanks @wzieba , everything LGTM! 🙇 ❤️ 🚀

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