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

Unit tests db schemas hashes #3016

Merged
merged 5 commits into from
May 28, 2024
Merged

Unit tests db schemas hashes #3016

merged 5 commits into from
May 28, 2024

Conversation

atorresveiga
Copy link
Contributor

Description

This PR introduces unit tests that verify the integrity of database schema migrations. Previously generated schemas are hashed and compared to ensure no unintended modifications occur during the migration process. This helps prevent potential crashes due to schema inconsistencies.

Benefits:

  • Increased confidence in database migrations.
  • Reduced risk of crashes during app updates.
  • Improved code maintainability with automated schema verification.

Testing

  • Check that if the hash is different than expected, the test will fail
  • Check that if the DB version is updated and the new hash is not added to the hashes map, the test will fail

@@ -69,8 +69,10 @@ import org.wordpress.android.fluxc.persistence.migrations.MIGRATION_7_8
import org.wordpress.android.fluxc.persistence.migrations.MIGRATION_8_9
import org.wordpress.android.fluxc.persistence.migrations.MIGRATION_9_10

const val WC_DATABASE_VERSION = 36
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved the DB version to a constant for easier reference in unit tests.


@Test
fun testDBHashes() {
for(i in 3..WC_DATABASE_VERSION){
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We will iterate from every DB version starting from 3 (first DB schema) and ending in the last version

@malinajirka malinajirka removed their request for review May 23, 2024 13:29
@kidinov kidinov self-assigned this May 27, 2024
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class MigrationSchemasTests {
Copy link
Contributor

@kidinov kidinov May 27, 2024

Choose a reason for hiding this comment

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

I'd put that into MigrationTests, just to remind people that they actually don't test validity of migration here and should write a test similar to all the rest tests there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed here

}

@Test
fun testDBHashes() {
Copy link
Contributor

Choose a reason for hiding this comment

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

np: shell we at least try to name tests someone readable, like we do in Woo repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using spaces is not allowed because of the DEX version, I updated the name of the test following the same pattern as the rest of the test in MigrationTests

com.android.tools.r8.internal.Tb: Space characters in SimpleName 'this is a test' are not allowed prior to DEX version 040

@kidinov kidinov self-requested a review May 27, 2024 14:02
Copy link
Contributor

@kidinov kidinov left a comment

Choose a reason for hiding this comment

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

While that makes it a little bit harder to make a mistake here, it's still more like a reminder and it doesn't actually test if the written migration is valid or not

Ideally on top of that to have an actual "update from a previous version" test as one of the CI steps before release uploading

@atorresveiga
Copy link
Contributor Author

Thanks for the review, @kidinov! This PR is ready for another round 😄

@atorresveiga atorresveiga requested a review from kidinov May 27, 2024 17:45
Copy link
Contributor

@kidinov kidinov left a comment

Choose a reason for hiding this comment

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

LGTM!

@kidinov kidinov merged commit 7d5b075 into trunk May 28, 2024
14 checks passed
@kidinov kidinov deleted the issue/unit-tests-db-schemas branch May 28, 2024 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants