unittest_views.py 6.51 KB
Newer Older
1
# coding: utf-8
2
# copyright 2015-2021 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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/>.

18
import os
19
import io
20
from io import BytesIO
21
from tempfile import NamedTemporaryFile
22

Laura Médioni's avatar
Laura Médioni committed
23
from cubicweb.web import Redirect
24
from cubicweb.devtools import testlib
25

26
from cubicweb_skos.rdfio import default_graph
27

28

29
class ViewsTC(testlib.CubicWebTC):
30

31
    def setup_database(self):
32
        with self.admin_access.client_cnx() as cnx:
33
            cnx.create_entity('ConceptScheme', title=u'musique')
34
            cnx.commit()
35
36

    def test_flat_scheme_concepts_import(self):
37
        with self.admin_access.web_request() as req:
38
            scheme = req.find('ConceptScheme', title=u'musique').one()
39
40
            # simply test the form properly render and is well formed
            self.view('skos.scheme.import', rset=scheme.as_rset(), req=req, template=None)
41
            content = u'\n\nélectro\nhip-hop\nrap\njazz\nclassique\n'
42
            req.form = self.fake_form('skos.scheme.import', {
43
                'stream': ('filename.txt', BytesIO(content.encode('utf-8'))),
44
                'encoding': u'utf-8',
45
                'language_code': u'fr',
Erica Marco's avatar
Erica Marco committed
46
                'format': u'simple',
47
                'delimiter': u'tab',
Sylvain Thénault's avatar
Sylvain Thénault committed
48
            }, [(scheme, {})])
49
50
51
52
53
            # now actually tests the import, using scheme.view and not self.view which doesn't like
            # exception, even Redirect
            self.assertRaises(Redirect, scheme.view, 'skos.scheme.import')
            self.assertEqual(set(c.dc_title() for c in scheme.top_concepts),
                             set(u'électro hip-hop rap jazz classique'.split()))
Nicolas Chauvat's avatar
Nicolas Chauvat committed
54
55
            self.assertEqual(set(label.language_code for concept in scheme.top_concepts
                                 for label in concept.preferred_label),
56
                             set(['fr']))
57

Erica Marco's avatar
Erica Marco committed
58
59
    def test_lcsv_scheme_concepts_import(self):
        with self.admin_access.web_request() as req:
60
            scheme = req.find('ConceptScheme', title=u'musique').one()
Erica Marco's avatar
Erica Marco committed
61
62
63
64
            # simply test the form properly render and is well formed
            self.view('skos.scheme.import', rset=scheme.as_rset(), req=req,
                      template=None)
            fname = 'lcsv_example_shortened.csv'
65
            posted = {'stream': (fname, io.open(self.datapath(fname), 'rb')),
Erica Marco's avatar
Erica Marco committed
66
                      'encoding': u'utf-8',
67
                      'language_code': u'fr',
68
                      'delimiter': u'tab',
Erica Marco's avatar
Erica Marco committed
69
70
71
72
73
74
75
                      'format': u'lcsv'}
            req.form = self.fake_form('skos.scheme.import', posted, [(scheme, {})])
            # now actually tests the import, using scheme.view and not self.view which doesn't like
            # exception, even Redirect
            self.assertRaises(Redirect, scheme.view, 'skos.scheme.import')
        # check that the concept were added
        with self.admin_access.client_cnx() as cnx:
76
            scheme = cnx.find('ConceptScheme', title=u'musique').one()
Erica Marco's avatar
Erica Marco committed
77
78
79
80
81
82
            self.assertEqual(len(scheme.top_concepts), 2)
            concepts = cnx.find('Concept')
            self.assertEqual(len(concepts), 5)
            concept1 = cnx.find('Concept',
                                definition="Définition de l'organisation politique de l'organisme,"
                                ).one()
83
84
85
            label = concept1.preferred_label[0]
            self.assertEqual(len(concept1.preferred_label), 1)
            self.assertEqual((label.label, label.language_code), ('Vie politique', 'fr'))
Erica Marco's avatar
Erica Marco committed
86
87
88
89
90
            self.assertEqual(len(concept1.narrower_concept), 2)
            concept2 = cnx.find('Concept',
                                definition="Création volontaire ou en application de la loi").one()
            self.assertEqual(concept2.broader_concept[0], concept1)

91
92
93
94
95
96
97
98
99
    def _test_skos_view(self, vid, nb_expected_triples):
        with self.admin_access.client_cnx() as cnx:
            scheme = cnx.find('ConceptScheme', title=u'musique').one()
            scheme.add_concept(u'pop')
            with NamedTemporaryFile(delete=False) as fobj:
                try:
                    fobj.write(scheme.view(vid))
                    fobj.close()
                    graph = default_graph()
Sylvain Thénault's avatar
pep8    
Sylvain Thénault committed
100
                    graph.load('file://' + fobj.name, rdf_format='xml')
101
102
103
104
105
106
107
108
109
110
                finally:
                    os.unlink(fobj.name)
            triples = set(graph.triples())
            self.assertEqual(len(triples), nb_expected_triples, triples)

    def test_skos_primary_view(self):
        self._test_skos_view('primary.rdf', 5)

    def test_skos_list_view(self):
        self._test_skos_view('list.rdf', 2)
Erica Marco's avatar
Erica Marco committed
111

112

113
114
115
116
117
118
119
120
121
122
123
124
class RelationWidgetTC(testlib.CubicWebTC):
    """Functional test case about the relation widget."""

    def test_linkable_rset(self):
        with self.admin_access.repo_cnx() as cnx:
            lang = cnx.create_entity('Language')
            cnx.commit()
        with self.admin_access.web_request() as req:
            lang = req.entity_from_eid(lang.eid)
            req.form = {'relation': 'language_to:Concept:subject'}
            view = self.vreg['views'].select('search_related_entities', req,
                                             rset=lang.as_rset())
125
            self.assertFalse(view.linkable_rset())
126
        with self.admin_access.repo_cnx() as cnx:
Sylvain Thénault's avatar
Sylvain Thénault committed
127
128
129
            lang_to_rtype = cnx.find('CWRType', name='language_to').one()
            scheme = cnx.create_entity('ConceptScheme', title=u'languages',
                                       scheme_relation_type=lang_to_rtype)
130
131
132
133
134
135
136
137
138
            scheme.add_concept(label=u'fr')
            cnx.commit()
        with self.admin_access.web_request() as req:
            req.form = {'relation': 'language_to:Concept:subject'}
            view = self.vreg['views'].select('search_related_entities', req,
                                             rset=lang.as_rset())
            self.assertEqual(len(view.linkable_rset()), 1)


139
if __name__ == '__main__':
140
141
    from unittest import main
    main()