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

Smoke unit test for redis migrations #9117

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from

Conversation

archibald1418
Copy link
Contributor

Motivation and context

Basic coverage for redis migrations support from #8898

How has this been tested?

Prepare

  • A .py migration file with class Migration is generated for each test in the directory of a django application
  • The migration file's Migration.run classmethod is patched with unittest.Mock to assert mock_calls from the migration tool to confirm that the migration was discovered and applied
  • Redis is patched with fakeredis instance throughout the whole test suite since our main concern is the migration tool itself, not the Redis database.

Case 1: Migration is added and applied successfully

  • Generate a correct stub file named 000_first.py with the following contents:
from cvat.apps.redis_handler.migration_loader import BaseMigration

class Migration(BaseMigration):
    @classmethod
    def run(cls): ...

In order for the stub to be discoverable by the migration tool, its name should start with numbers and it should contain a class Migration which is a subclass of BaseMigration.

  • Patch run method with a mock.
  • Call django's migrateredis.py script with django.management.call_command('migrateredis')
  • Assert that engine.001_cleanup_scheduled_jobs and redis_handler.001_first are added as members to Redis set cvat:applied_migrations
  • Assert that our run mock was called once

Case 2: Migration is not added and not applied

  • Flush fakeredis after previous case
  • Generate a bad stub file named 000_second.py with the following contents:
class Migration:
    @classmethod
    def run(cls): ...

Since the Migration class is not a subclass of BaseMigration, we expect that

  • LoaderError is raised
  • cvat:applied_migrations is an empty set
  • Migration.run is not called

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • I have linked related issues (see GitHub docs)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Oleg Valiulin added 2 commits February 18, 2025 22:03
@codecov-commenter
Copy link

codecov-commenter commented Feb 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.90%. Comparing base (cb7306b) to head (df6a60b).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9117      +/-   ##
===========================================
+ Coverage    73.83%   73.90%   +0.06%     
===========================================
  Files          431      432       +1     
  Lines        44804    44809       +5     
  Branches      3892     3892              
===========================================
+ Hits         33081    33114      +33     
+ Misses       11723    11695      -28     
Components Coverage Δ
cvat-ui 77.50% <ø> (-0.03%) ⬇️
cvat-server 70.94% <100.00%> (+0.14%) ⬆️

self.addCleanup(self.test_migration.cleanup)

call_command("migrateredis")
exp_migrations = {
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 please explain the purpose of generation test migration? why existing migrations are not enough? I do not really think that we need to modify/add files from cvat/apps when running tests. Otherwise, the generated files may not be removed e.g. when we stop debugging before cleanup is called.
  • Do I understand correctly that we need to update this test each time when a new Redis migration is added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the existing migration files are correct, which means I cannot test a scenario where the tool throws LoaderError.
Though, case 1 could be rewritten to use existing migration files

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 need to update this test each time when a new Redis migration is added?

I didn't get it, why would that be the case? Tests are using fake Redis database

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't get it, why would that be the case? Tests are using fake Redis database

I'm talking about the case when we add one more redis migration. In that case the self.assertEqual(conn.smembers("cvat:applied_migrations"), exp_migrations) will fail. Please, check that the expected by the test migration keys are added to cvat:applied_migrations instead of comparing all set members.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes totally makes sense, applied

self.assertEqual(conn.smembers("cvat:applied_migrations"), exp_migrations)
self.test_migration.runner_mock.assert_called_once()

def test_migration_not_applied(self, redis1, _):
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the more appropriate test should cover a case when migrateredis is called with --check arg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense

Copy link

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.

3 participants