-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from ferbncode/dump-tests
CB-286: Add tests for dump manager functions
- Loading branch information
Showing
4 changed files
with
123 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import os | ||
import tempfile | ||
from datetime import datetime | ||
from click.testing import CliRunner | ||
from critiquebrainz.data.testing import DataTestCase | ||
from critiquebrainz.data import utils | ||
import critiquebrainz.db.license as db_license | ||
import critiquebrainz.db.users as db_users | ||
import critiquebrainz.db.review as db_review | ||
from critiquebrainz.db.user import User | ||
|
||
utils.with_request_context = utils.with_test_request_context # noqa | ||
from critiquebrainz.data import dump_manager # pylint:disable=wrong-import-position | ||
|
||
|
||
def get_archives(root_dir): | ||
"""Returns a dictionary of bz2 archives and their respective paths. | ||
Args: | ||
root_dir: Path of the root dir. | ||
Returns: | ||
Dictionary with the following structure: | ||
{ | ||
archive_name: archive_path | ||
} | ||
""" | ||
archives = {} | ||
for roots, _, files in os.walk(root_dir): | ||
for f in files: | ||
if f.endswith('tar.bz2'): | ||
archives[f] = os.path.join(roots, f) | ||
return archives | ||
|
||
|
||
class DumpManagerTestCase(DataTestCase): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.tempdir = tempfile.mkdtemp() | ||
self.runner = CliRunner() | ||
self.license = db_license.create( | ||
id=u'test', | ||
full_name=u"Test License", | ||
) | ||
|
||
def test_full_db(self): | ||
self.runner.invoke(dump_manager.full_db, ['--location', self.tempdir]) | ||
self.assertTrue(os.listdir(self.tempdir)[0].endswith('.tar.bz2')) | ||
|
||
def test_json_dumps(self): | ||
date = datetime.today().strftime('%Y%m%d') | ||
self.runner.invoke(dump_manager.json, ['--location', self.tempdir]) | ||
self.assertIn(f'critiquebrainz-{date}-{self.license["id"]}-json.tar.bz2', os.listdir(self.tempdir)) | ||
|
||
def test_public(self): | ||
self.runner.invoke(dump_manager.public, ['--location', self.tempdir]) | ||
archives = get_archives(self.tempdir).keys() | ||
self.assertIn('cbdump.tar.bz2', archives) | ||
self.assertIn('cbdump-reviews-all.tar.bz2', archives) | ||
self.assertIn(f'cbdump-reviews-{self.license["id"]}.tar.bz2', archives) | ||
|
||
def test_importer(self): | ||
user = User(db_users.get_or_create("Tester", new_user_data={ | ||
"display_name": "test user", | ||
})) | ||
review = db_review.create( | ||
user_id=user.id, | ||
entity_id="e7aad618-fa86-3983-9e77-405e21796eca", | ||
entity_type="release_group", | ||
text="Testing", | ||
rating=5, | ||
is_draft=False, | ||
license_id=self.license["id"], | ||
) | ||
|
||
# Make dumps and delete entities | ||
self.runner.invoke(dump_manager.public, ['--location', self.tempdir]) | ||
archives = get_archives(self.tempdir) | ||
db_review.delete(review['id']) | ||
db_users.delete(user.id) | ||
self.assertEqual(db_users.total_count(), 0) | ||
self.assertEqual(db_review.get_count(), 0) | ||
|
||
# Import dumps - cbdump.tar.bz2 and cbdump-reviews-all.tar.bz2 and check if data imported properly | ||
self.runner.invoke(dump_manager.importer, [archives['cbdump.tar.bz2']]) | ||
self.assertEqual(db_users.total_count(), 1) | ||
|
||
self.runner.invoke(dump_manager.importer, [archives['cbdump-reviews-all.tar.bz2']]) | ||
self.assertEqual(db_review.get_count(), 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters