Skip to content

Commit

Permalink
add unittest for trigger_by_id
Browse files Browse the repository at this point in the history
  • Loading branch information
philipkcl committed Oct 27, 2023
1 parent 105c086 commit 8bda6d6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 25 deletions.
8 changes: 8 additions & 0 deletions doajtest/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import shutil
from contextlib import contextmanager
from glob import glob
from unittest import TestCase

Expand Down Expand Up @@ -205,6 +206,13 @@ def _make_and_push_test_context(self, path="/", acc=None):

return ctx

@contextmanager
def _make_and_push_test_context_manager(self, path="/", acc=None):
ctx = self._make_and_push_test_context(path=path, acc=acc)
yield ctx

ctx.pop()


def diff_dicts(d1, d2, d1_label='d1', d2_label='d2', print_unchanged=False):
"""
Expand Down
95 changes: 70 additions & 25 deletions doajtest/unit/test_withdraw_reinstate.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from doajtest.helpers import DoajTestCase
from doajtest.fixtures import JournalFixtureFactory, ArticleFixtureFactory, ApplicationFixtureFactory
from flask_login import current_user
import time

from doajtest.fixtures import JournalFixtureFactory, ArticleFixtureFactory, ApplicationFixtureFactory
from doajtest.helpers import DoajTestCase
from portality import models, constants
from portality.tasks.journal_in_out_doaj import SetInDOAJBackgroundTask, change_in_doaj

import time

class TestWithdrawReinstate(DoajTestCase):

Expand All @@ -15,7 +14,6 @@ def setUp(self):
def tearDown(self):
super(TestWithdrawReinstate, self).tearDown()


@staticmethod
def make_account():
account = models.Account.make_account(email="[email protected]", username="admin", name="admin",
Expand Down Expand Up @@ -71,7 +69,8 @@ def test_02_reinstate_task(self):

pissn = j.bibjson().get_identifiers(j.bibjson().P_ISSN)
eissn = j.bibjson().get_identifiers(j.bibjson().E_ISSN)
asource = ArticleFixtureFactory.make_article_source(pissn=pissn[0], eissn=eissn[0], with_id=False, in_doaj=False)
asource = ArticleFixtureFactory.make_article_source(pissn=pissn[0], eissn=eissn[0], with_id=False,
in_doaj=False)
a = models.Article(**asource)
a.save()
articles.append(a.id)
Expand All @@ -97,23 +96,7 @@ def test_03_withdraw(self):
acc.set_name("testuser")
ctx = self._make_and_push_test_context(acc=acc)

sources = JournalFixtureFactory.make_many_journal_sources(10, in_doaj=True)
ids = []
articles = []
for source in sources:
source["admin"]["current_application"] = ""
j = models.Journal(**source)
j.save()
ids.append(j.id)

pissn = j.bibjson().get_identifiers(j.bibjson().P_ISSN)
eissn = j.bibjson().get_identifiers(j.bibjson().E_ISSN)
asource = ArticleFixtureFactory.make_article_source(pissn=pissn[0], eissn=eissn[0], with_id=False)
a = models.Article(**asource)
a.save()
articles.append(a.id)

time.sleep(1)
articles, ids = make_journals_for_withdraw()

change_in_doaj(ids, False)

Expand Down Expand Up @@ -144,7 +127,8 @@ def test_04_reinstate(self):

pissn = j.bibjson().get_identifiers(j.bibjson().P_ISSN)
eissn = j.bibjson().get_identifiers(j.bibjson().E_ISSN)
asource = ArticleFixtureFactory.make_article_source(pissn=pissn[0], eissn=eissn[0], with_id=False, in_doaj=False)
asource = ArticleFixtureFactory.make_article_source(pissn=pissn[0], eissn=eissn[0], with_id=False,
in_doaj=False)
a = models.Article(**asource)
a.save()
articles.append(a.id)
Expand Down Expand Up @@ -187,4 +171,65 @@ def test_05_withdraw_with_ur(self):
assert j.is_in_doaj() is False

ur = models.Application.pull(application.id)
assert ur.application_status == constants.APPLICATION_STATUS_REJECTED, "application status: {}, expected: {}".format(ur.application_status, constants.APPLICATION_STATUS_REJECTED)
assert ur.application_status == constants.APPLICATION_STATUS_REJECTED, "application status: {}, expected: {}".format(
ur.application_status, constants.APPLICATION_STATUS_REJECTED)

def test_06_withdraw_with_trigger_by_jid(self):
with self._make_and_push_test_context_manager(acc=self.make_account()):
_, journal_ids = make_journals_for_withdraw()

trigger_by_jid = journal_ids[0]
change_in_doaj(journal_ids, False, trigger_by_jid=trigger_by_jid)
assert_journals_withdrawn(journal_ids, trigger_by_jid)

def test_07_withdraw_without_trigger_by_jid(self):
with self._make_and_push_test_context_manager(acc=self.make_account()):
_, journal_ids = make_journals_for_withdraw()

trigger_by_jid = None
change_in_doaj(journal_ids, False, trigger_by_jid=trigger_by_jid)
assert_journals_withdrawn(journal_ids, trigger_by_jid)


def make_test_account():
acc = models.Account()
acc.set_name("testuser")
return acc


def make_journals_for_withdraw():
sources = JournalFixtureFactory.make_many_journal_sources(10, in_doaj=True)
ids = []
articles = []
for source in sources:
source["admin"]["current_application"] = ""
j = models.Journal(**source)
j.save()
ids.append(j.id)

pissn = j.bibjson().get_identifiers(j.bibjson().P_ISSN)
eissn = j.bibjson().get_identifiers(j.bibjson().E_ISSN)
asource = ArticleFixtureFactory.make_article_source(pissn=pissn[0], eissn=eissn[0], with_id=False)
a = models.Article(**asource)
a.save()
articles.append(a.id)
time.sleep(1)
return articles, ids


def has_auto_msg(j):
return any(['Journal automatically withdrawn due to' in n['note'] for n in j.notes])


def assert_journals_withdrawn(journal_ids, trigger_by_jid):
for id in journal_ids:
j = models.Journal.pull(id)
if trigger_by_jid is None:
assert not has_auto_msg(j)
else:
if id == trigger_by_jid:
assert not has_auto_msg(j)
else:
assert has_auto_msg(j)

assert j.is_in_doaj() is False

0 comments on commit 8bda6d6

Please sign in to comment.