<commit_before>import unittest

from ppp_datamodel import Missing, Triple, Resource, Sentence, List
from ppp_datamodel import Intersection, JsonldResource
from ppp_datamodel.communication import Request, TraceItem, Response
from ppp_libmodule.tests import PPPTestCase
from ppp_hal import app

class TestDefinition(PPPTestCase(app)):
    config_var = 'PPP_HAL_CONFIG'
    config = '''{"apis": ["http://api.archives-ouvertes.fr/search/"],
                "memcached": {"servers": ["127.0.0.1"], "timeout": 10}}'''

    def testSearchAuthors(self):
        q = Request('1', 'en', Triple(
            Resource('A Hierarchical Resource Reservation Algorithm for Network Enabled Servers of the french department of research.'),
            Resource('author'),
            Missing()))
        r = self.request(q)
        self.assertEqual(len(r), 1, r)
        self.assertIsInstance(r[0].tree, List)
        self.assertEqual({x.value for x in r[0].tree.list}, {
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>'})
        self.assertIn(JsonldResource('<NAME>', graph={'@id': '<NAME>'}),
                r[0].tree.list)

    def testSearchPapers(self):
        q = Request('1', 'en', Triple(
            Missing(),
            Resource('author'),
            Resource('<NAME>')))
        r = self.request(q)
        self.assertEqual(len(r), 1, r)
        self.assertIsInstance(r[0].tree, List)
        self.assertIn('Deployment of a hierarchical middleware',
                {x.value for x in r[0].tree.list})

    def testIntersection(self):
        q = Request('1', 'en', Intersection([
            Triple(
                Resource('A Hierarchical Resource Reservation Algorithm for Network Enabled Servers of the french department of research.'),
                Resource('author'),
                Missing()),
            Triple(
                Resource('Deployment of a hierarchical middleware'),
                Resource('author'),
                Missing())]))
        q.__class__.from_dict(q.as_dict())
        r = self.request(q)
        self.assertEqual(len(r), 1, r)
        self.assertEqual({x.value for x in r[0].tree.list},
                {'<NAME>', '<NAME>'})

    def testRecursive(self):
        q = Request('1', 'en', Triple(
            Triple(
                Resource('The first cycles in an evolving graph'),
                Resource('author'),
                Missing()),
            Resource('birth date'),
            Missing()))
        r = self.request(q)
        self.assertEqual(len(r), 1, r)
        self.assertIsInstance(r[0].tree, Triple)
        self.assertIsInstance(r[0].tree.subject, List)
        self.assertEqual(r[0].tree.predicate, Resource('birth date'))
        self.assertIn(
                '<NAME>',
                {x.value for x in r[0].tree.subject.list})

    def testNotTooLarge(self):
        q = Request('1', 'en', Triple(
            Resource('<NAME>'),
            Resource('author'),
            Missing()))<commit_msg>Add tests for Jsonld output.<commit_after>import unittest

from ppp_datamodel import Missing, Triple, Resource, Sentence, List
from ppp_datamodel import Intersection, JsonldResource
from ppp_datamodel.communication import Request, TraceItem, Response
from ppp_libmodule.tests import PPPTestCase
from ppp_hal import app

class TestDefinition(PPPTestCase(app)):
    config_var = 'PPP_HAL_CONFIG'
    config = '''{"apis": ["http://api.archives-ouvertes.fr/search/"],
                "memcached": {"servers": ["127.0.0.1"], "timeout": 10}}'''

    def testSearchAuthors(self):
        q = Request('1', 'en', Triple(
            Resource('A Hierarchical Resource Reservation Algorithm for Network Enabled Servers of the french department of research.'),
            Resource('author'),
            Missing()))
        r = self.request(q)
        self.assertEqual(len(r), 1, r)
        self.assertIsInstance(r[0].tree, List)
        self.assertEqual({x.value for x in r[0].tree.list}, {
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>'})
        self.assertIn(JsonldResource('<NAME>', graph={'@id': '<NAME>'}),
                r[0].tree.list)

    def testSearchPapers(self):
        q = Request('1', 'en', Triple(
            Missing(),
            Resource('author'),
            Resource('<NAME>')))
        r = self.request(q)
        self.assertEqual(len(r), 1, r)
        self.assertIsInstance(r[0].tree, List)
        self.assertIn('Deployment of a hierarchical middleware',
                {x.value for x in r[0].tree.list})

    def testIntersection(self):
        q = Request('1', 'en', Intersection([
            Triple(
                Resource('A Hierarchical Resource Reservation Algorithm for Network Enabled Servers of the french department of research.'),
                Resource('author'),
                Missing()),
            Triple(
                Resource('Deployment of a hierarchical middleware'),
                Resource('author'),
                Missing())]))
        q.__class__.from_dict(q.as_dict())
        r = self.request(q)
        self.assertEqual(len(r), 1, r)
        self.assertEqual({x.value for x in r[0].tree.list},
                {'<NAME>', '<NAME>'})
        ec = JsonldResource('EC', graph={'@id': '<NAME>'})
        fd = JsonldResource('FD', graph={'@id': '<NAME>'})
        self.assertIn(r[0].tree.list, ([ec, fd], [fd, ec]))

    def testRecursive(self):
        q = Request('1', 'en', Triple(
            Triple(
                Resource('The first cycles in an evolving graph'),
                Resource('author'),
                Missing()),
            Resource('birth date'),
            Missing()))
        r = self.request(q)
        self.assertEqual(len(r), 1, r)
        self.assertIsInstance(r[0].tree, Triple)
        self.assertIsInstance(r[0].tree.subject, List)
        self.assertEqual(r[0].tree.predicate, Resource('birth date'))
        self.assertIn(
                '<NAME>',
                {x.value for x in r[0].tree.subject.list})
        self.assertIn(JsonldResource('DK', graph={'@id': '<NAME>'}),
                r[0].tree.subject.list)

    def testNotTooLarge(self):
        q = Request('1', 'en', Triple(
            Resource('<NAME>'),
            Resource('author'),
            Missing()))
{'fraction_non_alphanumeric': 0.14849445324881141, 'fraction_numerical': 0.008874801901743265, 'mean_word_length': 2.177744209466264, 'pattern_counts': {'":': 8, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1511664', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1768, 'n_words': 424}