Skip to content
Browse files

Criado módulo ``utils.functions`` para melhor organização de funções …

…que apoiam de alguma maneira as tarefas relacionadas a sincronização de dados
  • Loading branch information...
1 parent 26e0490 commit f6e2e82492cf5c0093d11eb5b422c87e3cd92e5b @gustavofonseca committed Feb 28, 2013
View
132 opac/utils/functions.py
@@ -0,0 +1,132 @@
+import itertools
+
+from django.conf import settings
+
+from .sync import datacollector
+from catalog import models
+from catalog import mongomodels
+
+
+def _get_user_catalog_definitions():
+ """
+ It analyses the choices the user made, and returns a list
+ in the form:
+ [[<collection_name_slug>,], [<journal>,]]
+ """
+ collections = models.CollectionMeta.objects.members()
+
+ full_collections = []
+ journals_a_la_carte = []
+
+ for collection in collections:
+ # decide if the entire collection must be synced or only some
+ # journals.
+ if collection.journals.members().exists():
+ journals_a_la_carte = collection.journals.members()
+ else:
+ full_collections.append(collection)
+
+ return [full_collections, journals_a_la_carte]
+
+
+def _what_to_sync(managerapi_dep=datacollector.SciELOManagerAPI):
+ """
+ Returns an iterator containing all journals that must be synced
+ to build the catalog.
+
+ If the collection is marked as member, and has some
+ journals that are also marked as members, we assume
+ only these journals must be synchronized. Else,
+ sync all its journals.
+ """
+ scielo_api = managerapi_dep(settings=settings)
+ full_collections, journals_a_la_carte = _get_user_catalog_definitions()
+
+ full_collections = (c.name_slug for c in full_collections)
+ journals_a_la_carte = (j.resource_id for j in journals_a_la_carte)
+
+ return itertools.chain(
+ scielo_api.get_all_journals(*full_collections),
+ scielo_api.get_journals(*journals_a_la_carte)
+ )
+
+
+def _what_have_changed(managerapi_dep=datacollector.SciELOManagerAPI):
+ """
+ Returns an iterator containing all journals that must be created
+ or updated in order to keep the catalog updated.
+ """
+ scielo_api = managerapi_dep(settings=settings)
+ full_collections, journals_a_la_carte = _get_user_catalog_definitions()
+
+ data = scielo_api.get_changes()
+
+ changed = identify_changes(data, full_collections, journals_a_la_carte)
+
+ return itertools.chain(
+ scielo_api.get_issues(*changed['issues']),
+ scielo_api.get_journals(*changed['journals'])
+ )
+
+
+def _list_issues_uri(journal_meta, journal_dep=mongomodels.Journal):
+ # TODO: This instantiation logic must be at Journal.get_journal
+ journal_data = journal_dep.objects.find_one({'id': journal_meta.resource_id})
+ journal_doc = journal_dep(**journal_data)
+ return (issue.resource_uri for issue in journal_doc.list_issues())
+
+
+def identify_changes(changes,
+ collections,
+ journals,
+ list_issues_uri_dep=_list_issues_uri):
+ """
+ Returns a dict where the keys are ``journals`` and ``issues``
+ both containing a list of ``object_uri`` that must be
+ synced.
+
+ ``changes`` is an iterable where each element is an
+ entry in changes API.
+
+ ``collections`` is an iterable of collections
+ that must have all its journals synced.
+
+ ``journals`` is an iterable of journals that must
+ be synced.
+ """
+ journals_list = []
+ issues_list = []
+
+ # list uris from all journals and its issues
+ for j in journals:
+ journals_list.append(j.resource_uri)
+ issues_list.append(list_issues_uri_dep(j))
+
+ collections_uris = set(c.resource_uri for c in collections)
+ journals_uris = set(journals_list)
+ issues_uris = set(itertools.chain(*issues_list))
+
+ changed_journals = set()
+ changed_issues = set()
+
+ for change_rec in changes:
+ _collection_uri = change_rec.get('collection_uri')
+ _object_uri = change_rec.get('object_uri')
+
+ if _collection_uri in collections_uris:
+ # identify the endpoint to know how to classify the uri
+ endpoint = [seg for seg in _object_uri.split('/') if seg][-2]
+ if endpoint == 'journals':
+ changed_journals.add(_object_uri)
+ elif endpoint == 'issues':
+ changed_issues.add(_object_uri)
+ else:
+ continue
+ elif _object_uri in journals_uris:
+ changed_journals.add(_object_uri)
+ elif _object_uri in issues_uris:
+ changed_issues.add(_object_uri)
+ else:
+ continue
+
+ return {'journals': list(changed_journals), 'issues': list(changed_issues)}
View
22 opac/utils/sync/datacollector.py
@@ -70,7 +70,7 @@ def fetch_data(self, endpoint,
else:
err_count = 0
- def iter_docs(self, endpoint, collection=None):
+ def iter_docs(self, endpoint, collection=None, **kwargs):
"""
Iterates over all documents of a given endpoint and collection.
@@ -87,13 +87,13 @@ def iter_docs(self, endpoint, collection=None):
offset = 0
limit = ITEMS_PER_REQUEST
- qry_params = {'limit': limit}
+ kwargs.update({'limit': limit})
if collection:
- qry_params.update({'collection': collection})
+ kwargs.update({'collection': collection})
while True:
- qry_params.update({'offset': offset})
- doc = self.fetch_data(endpoint, **qry_params)
+ kwargs.update({'offset': offset})
+ doc = self.fetch_data(endpoint, **kwargs)
for obj in doc['objects']:
# we are interested only in non-trashed items.
@@ -135,4 +135,14 @@ def get_all_collections(self):
return self.iter_docs('collections')
def get_changes(self, since=0):
- return self.iter_docs('changes')
+ return self.iter_docs('changes', since=since)
+
+ def get_issues(self, *issues):
+ """
+ Get all the given issues
+
+ ``issues`` is an arbitrary number of string values
+ of resource_ids.
+ """
+ for i in issues:
+ yield self.fetch_data('issues', resource_id=i)
View
110 opac/utils/tasks.py
@@ -1,70 +1,11 @@
-import itertools
-
from django.conf import settings
from celery import task
from .sync import datacollector
from .sync import dataloader
from .sync import pipes
+from . import functions
from catalog import models
-from catalog import mongomodels
-
-
-def _get_user_catalog_definitions():
- """
- It analyses the choices the user made, and returns a list
- in the form:
- [[<collection_name_slug>,], [<journal>,]]
- """
- collections = models.CollectionMeta.objects.members()
-
- full_collections = []
- journals_a_la_carte = []
-
- for collection in collections:
- # decide if the entire collection must be synced or only some
- # journals.
- if collection.journals.members().exists():
- journals_a_la_carte = collection.journals.members()
- else:
- full_collections.append(collection)
-
- return [full_collections, journals_a_la_carte]
-
-
-def _what_to_sync(managerapi_dep=datacollector.SciELOManagerAPI):
- """
- Returns an iterator containing all journals that must be synced
- to build the catalog.
-
- If the collection is marked as member, and has some
- journals that are also marked as members, we assume
- only these journals must be synchronized. Else,
- sync all its journals.
- """
- scielo_api = managerapi_dep(settings=settings)
- full_collections, journals_a_la_carte = _get_user_catalog_definitions()
-
- full_collections = (c.name_slug for c in full_collections)
- journals_a_la_carte = (j.resource_id for j in journals_a_la_carte)
-
- return itertools.chain(
- scielo_api.get_all_journals(*full_collections),
- scielo_api.get_journals(*journals_a_la_carte)
- )
-
-
-def _what_have_changed(managerapi_dep=datacollector.SciELOManagerAPI):
- """
- Returns an iterator containing all journals that must be created
- or updated in order to keep the catalog updated.
- """
- scielo_api = managerapi_dep(settings=settings)
- full_collections, journals_a_la_carte = _get_user_catalog_definitions()
-
- data = scielo_api.get_changes()
-
- return identify_changes(data, full_collections, journals_a_la_carte)
@task(name='utils.tasks.build_catalog')
@@ -80,7 +21,7 @@ def build_catalog():
pipes.PNormalizeJournalTitle,
pipes.PCleanup)
- data = _what_to_sync()
+ data = functions._what_to_sync()
transformed_data = ppl.run(data)
marreta = dataloader.Marreta(settings=settings)
@@ -129,50 +70,3 @@ def sync_journals_meta(managerapi_dep=datacollector.SciELOManagerAPI):
name=col.get('title', ''),
collection=cols_memo[collection],
)
-
-
-def _list_issues_uri(journal_meta, journal_dep=mongomodels.Journal):
- # TODO: This instantiation logic must be at Journal.get_journal
- journal_data = journal_dep.objects.find_one({'id': journal_meta.resource_id})
- journal_doc = journal_dep(**journal_data)
- return (issue.resource_uri for issue in journal_doc.list_issues())
-
-
-def identify_changes(changes,
- collections,
- journals,
- list_issues_uri_dep=_list_issues_uri):
- """
- Returns a list of ``object_uri`` that must be
- synced.
-
- ``changes`` is an iterable where each element is an
- entry in changes API.
-
- ``collections`` is an iterable of collections
- that must have all its journals synced.
-
- ``journals`` is an iterable of journals that must
- be synced.
- """
- journals_list = []
- issues_list = []
-
- # list uris from all journals and its issues
- for j in journals:
- journals_list.append(j.resource_uri)
- issues_list.append(list_issues_uri_dep(j))
-
- collections_uris = (c.resource_uri for c in collections)
- journals_uris = journals_list
- issues_uris = itertools.chain(*issues_list)
- super_set = set().union(collections_uris, journals_uris, issues_uris)
-
- changed = set()
- for change_rec in changes:
- if change_rec.get('collection_uri') in super_set or (
- change_rec.get('object_uri') in super_set):
-
- changed.add(change_rec.get('object_uri'))
-
- return list(changed)
View
3 opac/utils/tests/tests_datacollector.py
@@ -1,8 +1,5 @@
# coding: utf-8
import mocker
-from django.test import TestCase
-
-from catalog.test import modelfactories
class SciELOManagerAPITests(mocker.MockerTestCase):
View
333 opac/utils/tests/tests_functions.py
@@ -0,0 +1,333 @@
+# coding: utf-8
+import mocker
+
+from django.test import TestCase
+
+from catalog.test import modelfactories
+from utils.functions import (
+ _what_to_sync,
+)
+
+
+class WhatToSyncTests(mocker.MockerTestCase, TestCase):
+
+ def test_sync_entire_collection_if_journals_are_not_specified(self):
+ mocker_scieloapi = self.mocker.mock()
+
+ mocker_scieloapi(settings=mocker.ANY)
+ self.mocker.result(mocker_scieloapi)
+
+ mocker_scieloapi.get_all_journals(u'saude-publica')
+ self.mocker.result([{'acronym': 'aiss'}])
+
+ mocker_scieloapi.get_journals(*[])
+ self.mocker.result([])
+
+ self.mocker.replay()
+
+ c = modelfactories.CollectionMetaFactory.create(is_member=True)
+ modelfactories.JournalMetaFactory.create(is_member=False, collection=c)
+
+ to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
+
+ self.assertEqual(len(to_sync), 1)
+ self.assertEqual(to_sync[0], {'acronym': 'aiss'})
+
+ def test_sync_detached_journals_if_they_are_specified(self):
+ mocker_scieloapi = self.mocker.mock()
+
+ mocker_scieloapi(settings=mocker.ANY)
+ self.mocker.result(mocker_scieloapi)
+
+ mocker_scieloapi.get_all_journals(*[])
+ self.mocker.result([])
+
+ mocker_scieloapi.get_journals(u'1')
+ self.mocker.result([{'acronym': 'aiss'}])
+
+ self.mocker.replay()
+
+ c = modelfactories.CollectionMetaFactory.create(is_member=True)
+ modelfactories.JournalMetaFactory.create(is_member=True, collection=c)
+ modelfactories.JournalMetaFactory.create(is_member=False, collection=c)
+
+ to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
+
+ self.assertEqual(len(to_sync), 1)
+ self.assertEqual(to_sync[0], {'acronym': 'aiss'})
+
+ def test_sync_many_detached_journals_if_they_are_specified(self):
+ mocker_scieloapi = self.mocker.mock()
+
+ mocker_scieloapi(settings=mocker.ANY)
+ self.mocker.result(mocker_scieloapi)
+
+ mocker_scieloapi.get_all_journals(*[])
+ self.mocker.result([])
+
+ mocker_scieloapi.get_journals(u'1', u'1')
+ self.mocker.result([{'acronym': 'aiss'}, {'acronym': 'aiss'}])
+
+ self.mocker.replay()
+
+ c = modelfactories.CollectionMetaFactory.create(is_member=True)
+ modelfactories.JournalMetaFactory.create(is_member=True, collection=c)
+ modelfactories.JournalMetaFactory.create(is_member=True, collection=c)
+
+ to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
+
+ self.assertEqual(len(to_sync), 2)
+ self.assertEqual(to_sync[0], {'acronym': 'aiss'})
+ self.assertEqual(to_sync[1], {'acronym': 'aiss'})
+
+ def test_sync_nothing_if_nothing_is_specified(self):
+ mocker_scieloapi = self.mocker.mock()
+
+ mocker_scieloapi(settings=mocker.ANY)
+ self.mocker.result(mocker_scieloapi)
+
+ mocker_scieloapi.get_all_journals(*[])
+ self.mocker.result([])
+
+ mocker_scieloapi.get_journals(*[])
+ self.mocker.result([])
+
+ self.mocker.replay()
+
+ c = modelfactories.CollectionMetaFactory.create(is_member=False)
+ modelfactories.JournalMetaFactory.create(is_member=False, collection=c)
+ modelfactories.JournalMetaFactory.create(is_member=False, collection=c)
+
+ to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
+
+ self.assertEqual(len(to_sync), 0)
+
+ def test_ignore_journals_marked_as_members_if_the_journal_is_not(self):
+ mocker_scieloapi = self.mocker.mock()
+
+ mocker_scieloapi(settings=mocker.ANY)
+ self.mocker.result(mocker_scieloapi)
+
+ mocker_scieloapi.get_all_journals(*[])
+ self.mocker.result([])
+
+ mocker_scieloapi.get_journals(*[])
+ self.mocker.result([])
+
+ self.mocker.replay()
+
+ c = modelfactories.CollectionMetaFactory.create(is_member=False)
+ modelfactories.JournalMetaFactory.create(is_member=True, collection=c)
+
+ to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
+
+ self.assertEqual(len(to_sync), 0)
+
+
+class ChangesIdentificationTests(mocker.MockerTestCase, TestCase):
+
+ def test_identify_journals_given_collections(self):
+ from utils.functions import identify_changes
+
+ changes = [
+ {
+ "changed_at": "2013-01-23T15:11:33.409478",
+ "collection_uri": "/api/v1/collections/1/",
+ "event_type": "updated",
+ "object_uri": "/api/v1/journals/31/",
+ "resource_uri": "/api/v1/changes/8/",
+ "seq": 8
+ },
+ {
+ "changed_at": "2013-01-23T15:12:33.409478",
+ "collection_uri": "/api/v1/collections/2/",
+ "event_type": "added",
+ "object_uri": "/api/v1/issues/2840/",
+ "resource_uri": "/api/v1/changes/2/",
+ "seq": 9
+ },
+ ]
+
+ c = modelfactories.CollectionMetaFactory.create()
+
+ docs = identify_changes(changes, collections=[c], journals=[])
+
+ self.assertEqual(len(docs['journals']), 1)
+ self.assertEqual(docs['journals'][0], "/api/v1/journals/31/")
+
+ def test_identify_journals_given_journals(self):
+ from utils.functions import identify_changes
+
+ mocker_list_issues = self.mocker.mock()
+ mocker_list_issues(mocker.ANY)
+ self.mocker.result([u'/api/v1/issues/1/'])
+ self.mocker.replay()
+
+ changes = [
+ {
+ "changed_at": "2013-01-23T15:11:33.409478",
+ "collection_uri": "/api/v1/collections/1/",
+ "event_type": "updated",
+ "object_uri": "/api/v1/journals/31/",
+ "resource_uri": "/api/v1/changes/8/",
+ "seq": 8
+ },
+ {
+ "changed_at": "2013-01-23T15:12:33.409478",
+ "collection_uri": "/api/v1/collections/2/",
+ "event_type": "updated",
+ "object_uri": "/api/v1/journals/1/",
+ "resource_uri": "/api/v1/changes/2/",
+ "seq": 9
+ },
+ ]
+
+ j = modelfactories.JournalMetaFactory.create()
+
+ docs = identify_changes(changes,
+ collections=[],
+ journals=[j],
+ list_issues_uri_dep=mocker_list_issues)
+
+ self.assertEqual(len(docs['journals']), 1)
+ self.assertEqual(docs['journals'][0], "/api/v1/journals/1/")
+
+ def test_identify_journals_given_journals_and_collections(self):
+ from utils.functions import identify_changes
+
+ mocker_list_issues = self.mocker.mock()
+ mocker_list_issues(mocker.ANY)
+ self.mocker.result([u'/api/v1/issues/1/'])
+ self.mocker.replay()
+
+ changes = [
+ {
+ "changed_at": "2013-01-23T15:11:33.409478",
+ "collection_uri": "/api/v1/collections/1/",
+ "event_type": "updated",
+ "object_uri": "/api/v1/journals/31/",
+ "resource_uri": "/api/v1/changes/8/",
+ "seq": 8
+ },
+ {
+ "changed_at": "2013-01-23T15:12:33.409478",
+ "collection_uri": "/api/v1/collections/2/",
+ "event_type": "updated",
+ "object_uri": "/api/v1/journals/1/",
+ "resource_uri": "/api/v1/changes/2/",
+ "seq": 9
+ },
+ {
+ "changed_at": "2013-01-23T15:13:33.409478",
+ "collection_uri": "/api/v1/collections/2/",
+ "event_type": "updated",
+ "object_uri": "/api/v1/journals/2/",
+ "resource_uri": "/api/v1/changes/3/",
+ "seq": 10
+ },
+ ]
+
+ c = modelfactories.CollectionMetaFactory.create(
+ resource_uri="/api/v1/collections/2/"
+ )
+ c2 = modelfactories.CollectionMetaFactory.create(
+ resource_uri="/api/v1/collections/1/"
+ )
+ j = modelfactories.JournalMetaFactory.create(
+ collection=c,
+ resource_uri="/api/v1/journals/1/"
+ )
+
+ docs = identify_changes(changes,
+ collections=[c2],
+ journals=[j],
+ list_issues_uri_dep=mocker_list_issues)
+
+ self.assertEqual(len(docs['journals']), 2)
+ self.assertIn("/api/v1/journals/31/", docs['journals'])
+ self.assertIn("/api/v1/journals/1/", docs['journals'])
+
+ def test_membership_is_irrelevant(self):
+ from utils.functions import identify_changes
+
+ mocker_list_issues = self.mocker.mock()
+
+ mocker_list_issues(mocker.ANY)
+ self.mocker.result([u'/api/v1/issues/1/'])
+
+ mocker_list_issues(mocker.ANY)
+ self.mocker.result([])
+
+ self.mocker.replay()
+
+ changes = [
+ {
+ "changed_at": "2013-01-23T15:11:33.409478",
+ "collection_uri": "/api/v1/collections/1/",
+ "event_type": "updated",
+ "object_uri": "/api/v1/journals/1/",
+ "resource_uri": "/api/v1/changes/8/",
+ "seq": 8
+ },
+ {
+ "changed_at": "2013-01-23T15:12:33.409478",
+ "collection_uri": "/api/v1/collections/2/",
+ "event_type": "added",
+ "object_uri": "/api/v1/journals/2/",
+ "resource_uri": "/api/v1/changes/2/",
+ "seq": 9
+ },
+ ]
+
+ j = modelfactories.JournalMetaFactory.create(
+ is_member=True,
+ resource_uri=u'/api/v1/journals/1/')
+ j2 = modelfactories.JournalMetaFactory.create(
+ is_member=False,
+ resource_uri=u'/api/v1/journals/2/')
+
+ docs = identify_changes(changes,
+ collections=[],
+ journals=[j, j2],
+ list_issues_uri_dep=mocker_list_issues)
+
+ self.assertEqual(len(docs['journals']), 2)
+
+ def test_identify_issue_changes(self):
+ from utils.functions import identify_changes
+
+ mocker_list_issues = self.mocker.mock()
+ mocker_list_issues(mocker.ANY)
+ self.mocker.result([u'/api/v1/issues/1/'])
+
+ self.mocker.replay()
+
+ journal_doc = modelfactories.JournalFactory.build()
+ journal_meta = modelfactories.JournalMetaFactory.create()
+
+ changes = [
+ {
+ "changed_at": "2013-01-23T15:11:33.409478",
+ "collection_uri": "/api/v1/collections/1/",
+ "event_type": "updated",
+ "object_uri": "/api/v1/journals/2/",
+ "resource_uri": "/api/v1/changes/8/",
+ "seq": 8
+ },
+ {
+ "changed_at": "2013-01-23T15:12:33.409478",
+ "collection_uri": "/api/v1/collections/1/",
+ "event_type": "updated",
+ "object_uri": "/api/v1/issues/1/",
+ "resource_uri": "/api/v1/changes/2/",
+ "seq": 9
+ },
+ ]
+
+ docs = identify_changes(changes,
+ collections=[],
+ journals=[journal_meta],
+ list_issues_uri_dep=mocker_list_issues)
+
+ self.assertEqual(len(docs['issues']), 1)
+ self.assertEqual(docs['issues'][0], "/api/v1/issues/1/")
View
325 opac/utils/tests/tests_tasks.py
@@ -6,127 +6,11 @@
from catalog import models
from catalog.test import modelfactories
from utils.tasks import (
- _what_to_sync,
sync_collections_meta,
sync_journals_meta,
)
-class WhatToSyncTests(mocker.MockerTestCase, TestCase):
-
- def test_sync_entire_collection_if_journals_are_not_specified(self):
- mocker_scieloapi = self.mocker.mock()
-
- mocker_scieloapi(settings=mocker.ANY)
- self.mocker.result(mocker_scieloapi)
-
- mocker_scieloapi.get_all_journals(u'saude-publica')
- self.mocker.result([{'acronym': 'aiss'}])
-
- mocker_scieloapi.get_journals(*[])
- self.mocker.result([])
-
- self.mocker.replay()
-
- c = modelfactories.CollectionMetaFactory.create(is_member=True)
- modelfactories.JournalMetaFactory.create(is_member=False, collection=c)
-
- to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
-
- self.assertEqual(len(to_sync), 1)
- self.assertEqual(to_sync[0], {'acronym': 'aiss'})
-
- def test_sync_detached_journals_if_they_are_specified(self):
- mocker_scieloapi = self.mocker.mock()
-
- mocker_scieloapi(settings=mocker.ANY)
- self.mocker.result(mocker_scieloapi)
-
- mocker_scieloapi.get_all_journals(*[])
- self.mocker.result([])
-
- mocker_scieloapi.get_journals(u'1')
- self.mocker.result([{'acronym': 'aiss'}])
-
- self.mocker.replay()
-
- c = modelfactories.CollectionMetaFactory.create(is_member=True)
- modelfactories.JournalMetaFactory.create(is_member=True, collection=c)
- modelfactories.JournalMetaFactory.create(is_member=False, collection=c)
-
- to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
-
- self.assertEqual(len(to_sync), 1)
- self.assertEqual(to_sync[0], {'acronym': 'aiss'})
-
- def test_sync_many_detached_journals_if_they_are_specified(self):
- mocker_scieloapi = self.mocker.mock()
-
- mocker_scieloapi(settings=mocker.ANY)
- self.mocker.result(mocker_scieloapi)
-
- mocker_scieloapi.get_all_journals(*[])
- self.mocker.result([])
-
- mocker_scieloapi.get_journals(u'1', u'1')
- self.mocker.result([{'acronym': 'aiss'}, {'acronym': 'aiss'}])
-
- self.mocker.replay()
-
- c = modelfactories.CollectionMetaFactory.create(is_member=True)
- modelfactories.JournalMetaFactory.create(is_member=True, collection=c)
- modelfactories.JournalMetaFactory.create(is_member=True, collection=c)
-
- to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
-
- self.assertEqual(len(to_sync), 2)
- self.assertEqual(to_sync[0], {'acronym': 'aiss'})
- self.assertEqual(to_sync[1], {'acronym': 'aiss'})
-
- def test_sync_nothing_if_nothing_is_specified(self):
- mocker_scieloapi = self.mocker.mock()
-
- mocker_scieloapi(settings=mocker.ANY)
- self.mocker.result(mocker_scieloapi)
-
- mocker_scieloapi.get_all_journals(*[])
- self.mocker.result([])
-
- mocker_scieloapi.get_journals(*[])
- self.mocker.result([])
-
- self.mocker.replay()
-
- c = modelfactories.CollectionMetaFactory.create(is_member=False)
- modelfactories.JournalMetaFactory.create(is_member=False, collection=c)
- modelfactories.JournalMetaFactory.create(is_member=False, collection=c)
-
- to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
-
- self.assertEqual(len(to_sync), 0)
-
- def test_ignore_journals_marked_as_members_if_the_journal_is_not(self):
- mocker_scieloapi = self.mocker.mock()
-
- mocker_scieloapi(settings=mocker.ANY)
- self.mocker.result(mocker_scieloapi)
-
- mocker_scieloapi.get_all_journals(*[])
- self.mocker.result([])
-
- mocker_scieloapi.get_journals(*[])
- self.mocker.result([])
-
- self.mocker.replay()
-
- c = modelfactories.CollectionMetaFactory.create(is_member=False)
- modelfactories.JournalMetaFactory.create(is_member=True, collection=c)
-
- to_sync = list(_what_to_sync(managerapi_dep=mocker_scieloapi))
-
- self.assertEqual(len(to_sync), 0)
-
-
class SyncCollectionsMetaTests(mocker.MockerTestCase, TestCase):
def test_data_is_loaded_if_all_goes_fine(self):
@@ -280,212 +164,3 @@ def test_bypass_journals_with_non_member_collections(self):
sync_journals_meta(managerapi_dep=mocker_scieloapi)
self.assertEqual(models.JournalMeta.objects.count(), 0)
-
-
-class ChangesIdentificationTests(mocker.MockerTestCase, TestCase):
-
- def test_identify_journals_given_collections(self):
- from utils.tasks import identify_changes
-
- changes = [
- {
- "changed_at": "2013-01-23T15:11:33.409478",
- "collection_uri": "/api/v1/collections/1/",
- "event_type": "updated",
- "object_uri": "/api/v1/journals/31/",
- "resource_uri": "/api/v1/changes/8/",
- "seq": 8
- },
- {
- "changed_at": "2013-01-23T15:12:33.409478",
- "collection_uri": "/api/v1/collections/2/",
- "event_type": "added",
- "object_uri": "/api/v1/issues/2840/",
- "resource_uri": "/api/v1/changes/2/",
- "seq": 9
- },
- ]
-
- c = modelfactories.CollectionMetaFactory.create()
-
- docs = identify_changes(changes, collections=[c], journals=[])
-
- self.assertEqual(len(docs), 1)
- self.assertEqual(docs[0], "/api/v1/journals/31/")
-
- def test_identify_journals_given_journals(self):
- from utils.tasks import identify_changes
-
- mocker_list_issues = self.mocker.mock()
- mocker_list_issues(mocker.ANY)
- self.mocker.result([u'/api/v1/issues/1/'])
- self.mocker.replay()
-
- changes = [
- {
- "changed_at": "2013-01-23T15:11:33.409478",
- "collection_uri": "/api/v1/collections/1/",
- "event_type": "updated",
- "object_uri": "/api/v1/journals/31/",
- "resource_uri": "/api/v1/changes/8/",
- "seq": 8
- },
- {
- "changed_at": "2013-01-23T15:12:33.409478",
- "collection_uri": "/api/v1/collections/2/",
- "event_type": "updated",
- "object_uri": "/api/v1/journals/1/",
- "resource_uri": "/api/v1/changes/2/",
- "seq": 9
- },
- ]
-
- j = modelfactories.JournalMetaFactory.create()
-
- docs = identify_changes(changes,
- collections=[],
- journals=[j],
- list_issues_uri_dep=mocker_list_issues)
-
- self.assertEqual(len(docs), 1)
- self.assertEqual(docs[0], "/api/v1/journals/1/")
-
- def test_identify_journals_given_journals_and_collections(self):
- from utils.tasks import identify_changes
-
- mocker_list_issues = self.mocker.mock()
- mocker_list_issues(mocker.ANY)
- self.mocker.result([u'/api/v1/issues/1/'])
- self.mocker.replay()
-
- changes = [
- {
- "changed_at": "2013-01-23T15:11:33.409478",
- "collection_uri": "/api/v1/collections/1/",
- "event_type": "updated",
- "object_uri": "/api/v1/journals/31/",
- "resource_uri": "/api/v1/changes/8/",
- "seq": 8
- },
- {
- "changed_at": "2013-01-23T15:12:33.409478",
- "collection_uri": "/api/v1/collections/2/",
- "event_type": "updated",
- "object_uri": "/api/v1/journals/1/",
- "resource_uri": "/api/v1/changes/2/",
- "seq": 9
- },
- {
- "changed_at": "2013-01-23T15:13:33.409478",
- "collection_uri": "/api/v1/collections/2/",
- "event_type": "updated",
- "object_uri": "/api/v1/journals/2/",
- "resource_uri": "/api/v1/changes/3/",
- "seq": 10
- },
- ]
-
- c = modelfactories.CollectionMetaFactory.create(
- resource_uri="/api/v1/collections/2/"
- )
- c2 = modelfactories.CollectionMetaFactory.create(
- resource_uri="/api/v1/collections/1/"
- )
- j = modelfactories.JournalMetaFactory.create(
- collection=c,
- resource_uri="/api/v1/journals/1/"
- )
-
- docs = identify_changes(changes,
- collections=[c2],
- journals=[j],
- list_issues_uri_dep=mocker_list_issues)
-
- self.assertEqual(len(docs), 2)
- self.assertIn("/api/v1/journals/31/", docs)
- self.assertIn("/api/v1/journals/1/", docs)
-
- def test_membership_is_irrelevant(self):
- from utils.tasks import identify_changes
-
- mocker_list_issues = self.mocker.mock()
-
- mocker_list_issues(mocker.ANY)
- self.mocker.result([u'/api/v1/issues/1/'])
-
- mocker_list_issues(mocker.ANY)
- self.mocker.result([])
-
- self.mocker.replay()
-
- changes = [
- {
- "changed_at": "2013-01-23T15:11:33.409478",
- "collection_uri": "/api/v1/collections/1/",
- "event_type": "updated",
- "object_uri": "/api/v1/journals/1/",
- "resource_uri": "/api/v1/changes/8/",
- "seq": 8
- },
- {
- "changed_at": "2013-01-23T15:12:33.409478",
- "collection_uri": "/api/v1/collections/2/",
- "event_type": "added",
- "object_uri": "/api/v1/journals/2/",
- "resource_uri": "/api/v1/changes/2/",
- "seq": 9
- },
- ]
-
- j = modelfactories.JournalMetaFactory.create(
- is_member=True,
- resource_uri=u'/api/v1/journals/1/')
- j2 = modelfactories.JournalMetaFactory.create(
- is_member=False,
- resource_uri=u'/api/v1/journals/2/')
-
- docs = identify_changes(changes,
- collections=[],
- journals=[j, j2],
- list_issues_uri_dep=mocker_list_issues)
-
- self.assertEqual(len(docs), 2)
-
- def test_identify_issue_changes(self):
- from utils.tasks import identify_changes
-
- mocker_list_issues = self.mocker.mock()
- mocker_list_issues(mocker.ANY)
- self.mocker.result([u'/api/v1/issues/1/'])
-
- self.mocker.replay()
-
- journal_doc = modelfactories.JournalFactory.build()
- journal_meta = modelfactories.JournalMetaFactory.create()
-
- changes = [
- {
- "changed_at": "2013-01-23T15:11:33.409478",
- "collection_uri": "/api/v1/collections/1/",
- "event_type": "updated",
- "object_uri": "/api/v1/journals/2/",
- "resource_uri": "/api/v1/changes/8/",
- "seq": 8
- },
- {
- "changed_at": "2013-01-23T15:12:33.409478",
- "collection_uri": "/api/v1/collections/1/",
- "event_type": "updated",
- "object_uri": "/api/v1/issues/1/",
- "resource_uri": "/api/v1/changes/2/",
- "seq": 9
- },
- ]
-
- docs = identify_changes(changes,
- collections=[],
- journals=[journal_meta],
- list_issues_uri_dep=mocker_list_issues)
-
- self.assertEqual(len(docs), 1)
- self.assertEqual(docs[0], "/api/v1/issues/1/")

0 comments on commit f6e2e82

Please sign in to comment.
Something went wrong with that request. Please try again.