unittest_sobjects.py 5.45 KB
Newer Older
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
# coding: utf-8
# copyright 2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

from cubicweb.devtools.testlib import CubicWebTC

class SKOSXMLImportTC(CubicWebTC):
    test_db_id = 'xmlparser'

    @classmethod
    def pre_setup_database(cls, cnx, config):
        url = u'file://%s' % cls.datapath('siaf_matieres_shortened.xml')
        cnx.create_entity('CWSource', name=u'mythesaurus', type=u'datafeed', parser=u'rdf.skos',
                          url=url)
        cnx.commit()

30
    def check_siaf_shortened(self, source_name):
31
32
33
34
35
36
        with self.admin_access.client_cnx() as cnx:
            scheme = cnx.execute('ConceptScheme X').one()
            self.assertEqual(scheme.title, u"Thésaurus-matières pour l'indexation des archives locales")
            self.assertEqual(scheme.description, u"""Le Thésaurus pour la description et l'indexation des archives locales s'applique à tous les fonds d'archives locales, publiques et privées, anciennes, modernes et contemporaines. Il a valeur réglementaire pour l’ensemble des services d’archives territoriales – régionales, départementales et communales. Il se compose du thésaurus-matières, essentiellement réservé aux expressions illustrant la notion d'objet mais accueillant aussi des termes liés à des attributions essentielles des producteurs d'archives (par exemple : police, fiscalité, aide sociale), ainsi que trois listes d'autorité ("Actions", "Typologie documentaire", "Contexte historique") contenant des descripteurs qui ne sont pas par eux-mêmes des termes d'indexation mais qu'on associera à un ou plusieurs descripteurs du thésaurus, si le contexte documentaire l'exige.""")
            self.assertEqual(scheme.description_format, u"text/plain")
            self.assertEqual(scheme.cwuri, u'http://data.culture.fr/thesaurus/resource/ark:/67717/Matiere')
37
            self.assertEqual(scheme.cw_source[0].name, source_name)
38
            top_concepts = dict((c.cwuri, c) for c in scheme.top_concepts)
39
40
            # 11 original top concepts + 1 because of missing broader concept
            self.assertEqual(len(top_concepts), 12)
41
            concept = top_concepts['http://data.culture.fr/thesaurus/resource/ark:/67717/T1-503']
42
            self.assertEqual(concept.cw_source[0].name, source_name)
43
44
45
46
47
48
49
50
            self.assertEqual(len(concept.pref_label), 1)
            self.assertEqual(len(concept.alt_label), 0)
            self.assertEqual(len(concept.hidden_label), 0)
            narrow_concepts = dict((c.cwuri, c) for c in concept.narrower_concept)
            self.assertEqual(len(narrow_concepts), 2)
            label = concept.pref_label[0]
            # XXX support skos-xl
            self.assertEqual(label.cwuri, u'http://data.culture.fr/thesaurus/resource/ark:/67717/T1-503#pref_label_of8c179857731ea1dbfc9d152ba4338eda')
51
            self.assertEqual(label.cw_source[0].name, source_name)
52
            self.assertEqual(label.label, u'communications')
53
            self.failIf(cnx.execute('Any L WHERE NOT EXISTS(L pref_label_of X) AND NOT EXISTS(L alt_label_of Y) AND NOT EXISTS(L hidden_label_of Z)'))
54

55
56
    def test_datafeed_source(self):
        dfsource = self.repo.sources_by_uri['mythesaurus']
57
58
59
60
        # Disable raise_on_error as the "shortened" input file is not
        # complete. Though, check the content of the import with
        # `check_siaf_shortened` subsequently.
        roe = False
61
62
        # test creation upon initial pull
        with self.repo.internal_cnx() as cnx:
63
            stats = dfsource.pull_data(cnx, force=True, raise_on_error=roe)
64
        self.check_siaf_shortened(u'mythesaurus')
65
66
        # test update upon subsequent pull
        with self.repo.internal_cnx() as cnx:
67
            stats = dfsource.pull_data(cnx, force=True, raise_on_error=roe)
68
69
70
71
72
73

    def test_service(self):
        with self.admin_access.repo_cnx() as cnx:
            cnx.call_service('rdf.skos.import',
                             stream=open(self.datapath('siaf_matieres_shortened.xml')))
        self.check_siaf_shortened(u'system')
74
75
76
77
78

    def test_oddities(self):
        with self.admin_access.repo_cnx() as cnx:
            cnx.call_service('rdf.skos.import',
                             stream=open(self.datapath('oddities.xml')))
79
            scheme = cnx.execute('ConceptScheme X WHERE X cwuri "http://data.culture.fr/thesaurus/Matiere"').one()
80
81
82
            self.assertEqual(scheme.dc_title(), scheme.cwuri)
            concept = scheme.reverse_in_scheme[0]
            self.assertEqual(concept.label(), u'communications')
83
            self.assertEqual(len(concept.in_scheme), 2)
84
85
            concept = cnx.execute('Concept X WHERE X cwuri "http://logilab.fr/thesaurus/test/c3"').one()
            self.assertEqual(len(concept.broader_concept), 2)
86
87
88
89
90


if __name__ == '__main__':
    from logilab.common.testlib import unittest_main
    unittest_main()