def setUp(self):
        """Create some publications with tags."""
        self.owner =  User.objects.create_user(username='******', password='******', email='*****@*****.**')
        research_area = ResearchArea(title='AI', description='Artificial Intelligence')
        research_area.save()
        self.owner.profile.research_areas.add(research_area)
        self.author = Author(name='An author', email="*****@*****.**")
        self.author.save()
        self.tag1 = Tag(name = 'AI', description = "Artificial Intelligence")
        self.tag1.save()
        self.tag2 = Tag(name = 'Parallel Programming', description = "Parallel Programming")
        self.tag2.save()

        self.keyword = Keyword(keyword="AI")
        self.keyword.save()
        self.keyword2 = Keyword(keyword="Parallel")
        self.keyword2.save()

        self.pub1 = Publication(title='Pub1', doi='10.1.1.10')
        self.pub1.owner = self.owner
        self.pub1.save()
        self.pub1.authors.add(self.author)
        self.pub1.keywords.add(self.keyword)
        self.pub1.keywords.add(self.keyword2)
        self.pub1.tags.add(self.tag1)
        self.pub2 = Publication(title='Pub2', doi='10.1.1.10')
        self.pub2.owner = self.owner
        self.pub2.save()
        self.pub2.authors.add(self.author)
        self.pub2.tags.add(self.tag1)
        self.pub2.tags.add(self.tag2)
class DecoratorTests(unittest.TestCase):

    def setUp(self):
        self.owner = User.objects.create_user(username='******',
                email='*****@*****.**', password='******')
        self.publication = Publication(title='Some Publication')

        self.publication.title = "Some publication"
        self.publication.owner = self.owner
        self.publication.save()

        self.decorator = OaiPmhDecorator()

    def tearDown(self):
        User.objects.all().delete()
        Publication.objects.all().delete()

    @unittest.skip("Takes too long to run - CiteSeer query")
    def test_valid_doi(self):
        self.publication.doi = '10.1.1.119.2204'
        self.decorator.decorate_publication(self.publication)
        self.assertNotEqual(self.publication.decorated, None)

    def test_invalid_doi(self):
        self.publication.doi = 'wrongdoi'
        self.assertRaises(InvalidDataException, self.decorator.decorate_publication, self.publication)
    def setUp(self):
        self.user = User.objects.create_user(username="******", password="******", email="*****@*****.**")
        self.user2 = User.objects.create_user(username="******", password="******", email="*****@*****.**")
        self.xml_inserter = XmlInserter()

        self.author = Author.objects.create(name="Test", address="123-Stree", affiliation="Heriot-Watt", email="*****@*****.**")
        self.author.save()

        self.tag = Tag.objects.create(name="AI", description="Artificial Intelligence")
        self.tag.save()

        self.keyword = Keyword.objects.create(keyword="AI")
        self.keyword.save()

        self.publication = Publication(title="A publication")
        self.publication.owner = self.user
        self.publication.save()
        self.publication.authors.add(self.author)

        self.comment = Comment.objects.create(title="Comment", text="Commento", publication=self.publication)
        self.comment.user = self.user2
        self.comment.save()
        self.vote = Vote(comment=self.comment)
        self.vote.votetype = 0
        self.vote.caster = self.user
        self.vote.save()

        self.template = PeerReviewTemplate()
        self.template.template_text = "Some text"
        self.template.save()
    def setUp(self):
        self.owner = User.objects.create_user(username='******',
                email='*****@*****.**', password='******')
        self.publication = Publication(title='Some Publication')

        self.publication.title = "Some publication"
        self.publication.owner = self.owner
        self.publication.save()

        self.decorator = OaiPmhDecorator()
class SearchLogicTests(unittest.TestCase):

    def setUp(self):
        """Create some publications with tags."""
        self.owner =  User.objects.create_user(username='******', password='******', email='*****@*****.**')
        research_area = ResearchArea(title='AI', description='Artificial Intelligence')
        research_area.save()
        self.owner.profile.research_areas.add(research_area)
        self.author = Author(name='An author', email="*****@*****.**")
        self.author.save()
        self.tag1 = Tag(name = 'AI', description = "Artificial Intelligence")
        self.tag1.save()
        self.tag2 = Tag(name = 'Parallel Programming', description = "Parallel Programming")
        self.tag2.save()

        self.keyword = Keyword(keyword="AI")
        self.keyword.save()
        self.keyword2 = Keyword(keyword="Parallel")
        self.keyword2.save()

        self.pub1 = Publication(title='Pub1', doi='10.1.1.10')
        self.pub1.owner = self.owner
        self.pub1.save()
        self.pub1.authors.add(self.author)
        self.pub1.keywords.add(self.keyword)
        self.pub1.keywords.add(self.keyword2)
        self.pub1.tags.add(self.tag1)
        self.pub2 = Publication(title='Pub2', doi='10.1.1.10')
        self.pub2.owner = self.owner
        self.pub2.save()
        self.pub2.authors.add(self.author)
        self.pub2.tags.add(self.tag1)
        self.pub2.tags.add(self.tag2)

    def tearDown(self):
        """docstring for tearDown"""
        User.objects.all().delete()
        Author.objects.all().delete()
        Tag.objects.all().delete()
        Publication.objects.all().delete()

    def test_search_publications_by_keyword(self):
        keyword_query = {'keyword': 'AI'}
        publications = search_publications(keyword_terms=keyword_query)
        self.assertListEqual([self.pub1], list(publications))

    def test_search_publication_by_author(self):
        author_query = {'name': 'author'}
        publications = search_publications(author_terms=author_query)
        self.assertListEqual([self.pub1, self.pub2], list(publications))

    def test_search_publications_by_tag(self):
        tag_query = {'name': 'parallel'}
        publications = search_publications(tag_terms=tag_query)
        self.assertListEqual([self.pub2], list(publications))

    def test_search_publications_by_title(self):
        publication_query = {'title': '1'}
        publications = search_publications(publication_terms=publication_query)
        self.assertListEqual([self.pub1], list(publications))

    def test_search_publications_by_title_and_doi(self):
        publication_query = {'title': '1', 'doi': '10.1.1.10', 'searchtype': 'and'}
        publications = search_publications(publication_terms=publication_query)
        self.assertListEqual([self.pub1], list(publications))

    def test_search_publications_by_title_or_doi(self):
        publication_query = {'title': '1', 'doi': '10.1.1.10', 'searchtype': 'or'}
        publications = search_publications(publication_terms=publication_query)
        self.assertListEqual([self.pub1, self.pub2], list(publications))

    def test_search_publications_by_title_and_doi_with_querydict(self):
        publication_query = QueryDict('title=1&doi=10.1.1.10&searchtype=and')
        publications = search_publications(publication_terms=publication_query)
        self.assertListEqual([self.pub1], list(publications))

    def test_search_without_matching_query(self):
        publication_query = {'title': 'no in database'}
        publications = search_publications(publication_terms=publication_query)
        self.assertListEqual([], list(publications))

    def test_get_recommended_publications(self):
        publications = get_related_publications(self.pub1)
        self.assertListEqual([self.pub2], list(publications))

    def test_recommended_tags(self):
        tags = get_related_tags(self.tag1)
        result = [self.tag2]
        self.assertEqual(result, tags)

    def test_recommended_keyword(self):
        keywords = get_related_keywords(self.keyword)
        result = [self.keyword2]
        self.assertListEqual(result, keywords)

    def test_related_users(self):
        users = get_related_users_for_publication(self.pub1)
        self.assertEqual([self.owner], users)
Exemplo n.º 6
0
    def modify_publication(self, data, publication_id=None, requester=None):
        """Create or modify a publication object according to the specified value."""
        try:
            parsed_data = self._parse_xml_to_dict(data)
            if publication_id:
                publication = Publication.objects.get(id=publication_id)
            else:
                publication = Publication()
            publication.address = parsed_data['address']
            publication.booktitle = parsed_data['booktitle']
            publication.chapter = parsed_data['chapter']
            publication.edition = parsed_data['edition']
            publication.editor = parsed_data['editor']
            publication.how_published = parsed_data['howpublished']
            publication.institution = parsed_data['institution']
            publication.isbn = parsed_data['isbn']
            publication.journal = parsed_data['journal']
            publication.number = parsed_data['number']
            publication.organization = parsed_data['organization']
            publication.pages = parsed_data['pages']
            publication.publisher = parsed_data['publisher']
            curr_review = publication.review_status
            new_review = parsed_data['review_status']
            if publication_id:
                # Only relevant when changing
                if curr_review != new_review:
                    if not requester or not access.validate_editor_for_publication(requester, publication):
                        raise InvalidDataException("Only an editor can change the review status")
            publication.review_status = parsed_data['review_status']
            publication.series = parsed_data['series']
            publication.publication_type = parsed_data['publicationtype']
            publication.volume = parsed_data['volume']
            publication.title = parsed_data['title']
            publication.month = parsed_data['month']
            publication.note = parsed_data['note']
            publication.year = parsed_data['year']
            owner_id = self._get_id_from_atom_link(parsed_data['owner'])
            xml_owner = User.objects.get(id=owner_id)
            if publication_id:
                current_owner = User.objects.get(id=publication.owner.id)
                if requester:
                    if current_owner == requester:
                        publication.owner = requester
                    else:
                        raise InvalidDataException("Only the owner can change the publication.")
                else:
                    publication.owner = xml_owner
            else:
                if requester:
                    publication.owner = requester
                elif xml_owner:
                    publication.owner = xml_owner 
            publication.save()
            for a in parsed_data['authors']:
                author_id = self._get_id_from_atom_link(a)
                if author_id:
                    author = Author.objects.get(id=author_id)
                    publication.authors.add(author)
            for t in parsed_data['tags']:
                tag_id = self._get_id_from_atom_link(t)
                if tag_id:
                    tag = Tag.objects.get(id=tag_id)
                    publication.tags.add(tag)
            for r in parsed_data['referencematerials']:
                material_id = self._get_id_from_atom_link(r)
                if material_id:
                    material = ReferenceMaterial.objects.get(id=material_id)
                    publication.referencematerial_set.add(material)
            for f in parsed_data['fields']:
                key = f.tag
                furtherfield, created = FurtherField.objects.get_or_create(key=key, publication=publication)
            publication.save()
            return publication
        except User.DoesNotExist:
            raise InvalidDataException("The user provided does not exist (id: %s)"
                    % (owner_id))
        except Author.DoesNotExist:
            raise InvalidDataException("The author provided does not exist (id: %s)"
                    % (author_id))
        except Tag.DoesNotExist:

            raise InvalidDataException("The tag provided does not exist (id: %s)"
                    % (tag_id))
        except ReferenceMaterial.DoesNotExist:
            raise InvalidDataException("The reference material provided does not exist (id: %s)"
                    % (material_id))
        except KeyError, e:
            logger.error(e)
            raise InvalidDataException("The data sent was missing an attribute!")
class InserterTests(unittest.TestCase):

    def setUp(self):
        self.user = User.objects.create_user(username="******", password="******", email="*****@*****.**")
        self.user2 = User.objects.create_user(username="******", password="******", email="*****@*****.**")
        self.xml_inserter = XmlInserter()

        self.author = Author.objects.create(name="Test", address="123-Stree", affiliation="Heriot-Watt", email="*****@*****.**")
        self.author.save()

        self.tag = Tag.objects.create(name="AI", description="Artificial Intelligence")
        self.tag.save()

        self.keyword = Keyword.objects.create(keyword="AI")
        self.keyword.save()

        self.publication = Publication(title="A publication")
        self.publication.owner = self.user
        self.publication.save()
        self.publication.authors.add(self.author)

        self.comment = Comment.objects.create(title="Comment", text="Commento", publication=self.publication)
        self.comment.user = self.user2
        self.comment.save()
        self.vote = Vote(comment=self.comment)
        self.vote.votetype = 0
        self.vote.caster = self.user
        self.vote.save()

        self.template = PeerReviewTemplate()
        self.template.template_text = "Some text"
        self.template.save()

    def tearDown(self):
        User.objects.all().delete()
        
    def test_modify_existing_user(self):
        user = User.objects.get(username="******")
        new_user = self.xml_inserter.modify_user(user_xml, user_id=user.id)
        self.assertEqual(user.id, new_user.id)

    def test_xml_user_parser(self):
        """Attempts to parse a user from xml."""
        user = self.xml_inserter.modify_user(user_xml)
        self.assertEqual(user.username, "test")
        self.assertEqual(user.email, '*****@*****.**')
        self.assertNotEqual(user.password, 'test')

    def test_insert_user_invalid_email(self):
        """Attempts to insert a user with an invalid email."""
        user = self.xml_inserter.modify_user(invalid_email_user_xml)

    def test_insert_comment(self):
        xml = comment_xml % (self.publication.id, self.user.id, self.vote.id)
        comment = self.xml_inserter.modify_comment(xml, user_id=self.user.id)
        self.assertNotEqual(comment.vote_set, None)

    def test_insert_publication_from_xml(self):
        xml = publication_xml % (self.user.id, self.author.id, self.comment.id, self.tag.id)
        publication = self.xml_inserter.modify_publication(xml)
        self.assertEqual(publication.authors.get(), self.author)
        self.assertEqual(publication.tags.get(), self.tag)
        self.assertEqual(publication.owner, self.user)

    def test_change_publication_review_status_non_editor(self):
        xml = publication_xml % (self.user.id, self.author.id, self.comment.id, self.tag.id)
        with self.assertRaises(InvalidDataException):
            self.xml_inserter.modify_publication(xml,
                self.publication.id, self.user)

    def test_insert_peer_review_template_from_xml(self):
        xml = template_xml
        text = """A template.

        With linebreaks.

        An stuff like that."""
        template = self.xml_inserter.modify_peerreviewtemplate(xml)
        self.assertEqual(template.template_text, text)

    def test_insert_peer_review_from_xml(self):
        xml = peerreview_xml % (self.user.id, self.publication.id, self.template.id)
        peerreview = self.xml_inserter.modify_peerreview(xml)
        self.assertEqual(peerreview.peer_reviewer, self.user)
        self.assertEqual(peerreview.publication, self.publication)
        self.assertEqual(peerreview.template, self.template)
        self.assertEqual(peerreview.title, 'The reviewing of stuff.')

    def test_insert_author_from_xml(self):
        xml = author_xml
        author = self.xml_inserter.modify_author(xml)
        self.assertEqual(author.name, 'Jack')
        self.assertEqual(author.address, '123 Funroad')

    def test_insert_rating_from_xml(self):
        xml = rating_xml % (self.publication.id)
        rating = self.xml_inserter.modify_rating(xml)
        self.assertEqual(rating.rating, '5')

    def test_insert_papergroup_from_xml(self):
        xml = papergroup_xml % (self.user.id, self.user2.id, self.tag.id, self.publication.id)
        papergroup = self.xml_inserter.modify_papergroup(xml)
        self.assertEqual(papergroup.description, 'Papergroup of nature.')
        self.assertEqual(papergroup.title, 'Nature papergroup.')
        self.assertEqual(papergroup.blind_review, '1')
        self.assertEqual(papergroup.publications.all()[0], self.publication)

    def test_insert_papergroup_without_publication(self):
        xml = papergroup_no_publication_xml % (self.user.id, self.user2.id, self.tag.id)
        papergroup = self.xml_inserter.modify_papergroup(xml)
        self.assertEqual(papergroup.editors.all()[0], self.user)
        self.assertEqual(papergroup.referees.all()[0], self.user2)
        self.assertEqual(papergroup.tags.all()[0], self.tag)

    def test_insert_papergroup_invalid_editor(self):
        xml = papergroup_no_publication_xml % ('999', self.user2.id, self.tag.id)
        self.assertRaises(InvalidDataException, self.xml_inserter.modify_papergroup, xml)

    def test_insert_upvote_for_comment(self):
        xml = vote_xml % (self.user.id, self.comment.id)
        vote = self.xml_inserter.modify_vote(xml)
        self.assertEqual('upvote', vote.votetype)
        self.assertEqual(vote.caster, self.user)
        self.assertEqual(self.comment, vote.comment)
        self.assertEqual(self.user2.profile.esteem.value, 20)

    def test_insert_downvote_for_comment(self):
        xml = downvote_xml % (self.user.id, self.comment.id)
        vote = self.xml_inserter.modify_vote(xml)
        self.assertEqual('downvote', vote.votetype)
        self.assertEqual(vote.caster, self.user)
        self.assertEqual(self.comment, vote.comment)

    def test_insert_keyword_from_xml(self):
        xml = keyword_xml
        keyword = self.xml_inserter.modify_keyword(xml)
        self.assertEqual(keyword.keyword, "Some keyword.")

    def test_modify_keyword_from_xml(self):
        xml = keyword_xml
        kw = Keyword.objects.create(keyword='test')
        keyword = self.xml_inserter.modify_keyword(xml, kw.id)
        self.assertEqual(keyword.keyword, 'Some keyword.')