-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_task_article_bulk_delete.py
68 lines (48 loc) · 3.04 KB
/
test_task_article_bulk_delete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from time import sleep
import json
from doajtest.helpers import DoajTestCase
from portality import models
from portality.tasks.article_bulk_delete import article_bulk_delete_manage, ArticleBulkDeleteBackgroundTask
from doajtest.fixtures import JournalFixtureFactory, AccountFixtureFactory, ArticleFixtureFactory
TEST_JOURNAL_COUNT = 2
TEST_ARTICLES_PER_JOURNAL = 25
class TestTaskArticleBulkDelete(DoajTestCase):
def setUp(self):
super(TestTaskArticleBulkDelete, self).setUp()
ArticleBulkDeleteBackgroundTask.BATCH_SIZE = 13
self.journals = []
self.articles = []
for j_src in JournalFixtureFactory.make_many_journal_sources(count=TEST_JOURNAL_COUNT):
j = models.Journal(**j_src)
self.journals.append(j)
j.save()
for i in range(0, TEST_ARTICLES_PER_JOURNAL):
a = models.Article(
**ArticleFixtureFactory.make_article_source(with_id=False, eissn=j.bibjson().first_eissn,
pissn=j.bibjson().first_pissn))
a.save()
self.articles.append(a)
sleep(2)
self._make_and_push_test_context(acc=models.Account(**AccountFixtureFactory.make_managing_editor_source()))
def tearDown(self):
super(TestTaskArticleBulkDelete, self).tearDown()
def test_01_bulk_delete(self):
"""Bulk delete journals as an admin, but leave some around to test queries in bulk delete job"""
# all articles with the last journal's ISSN. The articles for the other journal should remain intact.
del_q_should_terms = {"query": {"bool": {
"must": [{"match_all": {}}, {"terms": {"index.issn.exact": [self.journals[-1].bibjson().first_pissn]}}]}}}
summary = article_bulk_delete_manage(del_q_should_terms, dry_run=True)
assert summary.as_dict().get("affected", {}).get("articles") == 1 * TEST_ARTICLES_PER_JOURNAL, summary.as_dict()
summary = article_bulk_delete_manage(del_q_should_terms, dry_run=False)
assert summary.as_dict().get("affected", {}).get("articles") == TEST_ARTICLES_PER_JOURNAL
sleep(3)
job = models.BackgroundJob.all()[0]
audit_log = json.dumps(job.audit, indent=2)
assert len(models.Journal.all()) == TEST_JOURNAL_COUNT, "{}\n\n{}".format(len(models.Journal.all()),
audit_log) # json.dumps([j.data for j in models.Journal.all()], indent=2)
assert len(models.Article.all()) == 1 * TEST_ARTICLES_PER_JOURNAL, "{}\n\n{}".format(len(models.Article.all()),
audit_log) # json.dumps([a.data for a in models.Article.all()], indent=2)
assert "About to delete 25 articles in 2 batches" in audit_log
assert "Deleted 13 articles in batch 1 of 2" in audit_log
assert "Deleted 12 articles in batch 2 of 2" in audit_log
assert "Deleted 25 articles" in audit_log