# 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/>.

"""Tests for cubicweb-datacat schema."""


from cubicweb import ValidationError
from cubicweb.devtools.testlib import CubicWebTC

from utils import create_file


class DatasetRelationsTC(CubicWebTC):
    """Test case for Dataset entity type relations."""

    def test_dataset_dcat_theme_relation(self):
        """Check that constraint for ``theme`` relation on Dataset entity type is enforced."""
        with self.admin_access.repo_cnx() as cnx:
            # A concept scheme with a concept, and we link a catalog to this scheme
            scheme = cnx.create_entity('ConceptScheme', title=u'My Thesaurus')
            label = cnx.create_entity('Label', label=u'My Concept', kind=u'preferred',
                                      label_of=cnx.create_entity('Concept', in_scheme=scheme))
            concept = label.label_of[0]
            cat = cnx.create_entity('DataCatalog', title=u'My Catalog', theme_taxonomy=scheme)
            cnx.commit()
            cnx.create_entity('Dataset', title=u'My Dataset', in_catalog=cat, dcat_theme=concept)
            cnx.commit()
            # Another scheme not related to the catalog
            scheme2 = cnx.create_entity('ConceptScheme', title=u'My Second Thesaurus')
            label2 = cnx.create_entity('Label', label=u'My Second Concept', kind=u'preferred',
                                       label_of=cnx.create_entity('Concept', in_scheme=scheme2))
            concept2 = label2.label_of[0]
            cnx.commit()
            with self.assertRaises(ValidationError) as cm:
                cnx.create_entity('Dataset', title=u'My Second Dataset', in_catalog=cat,
                                  dcat_theme=concept2)
                cnx.commit()
                self.assertEqual(cm.exception.msg,
                                 "Theme must belong to the dataset's catalog vocabulary")
            # To have a theme, a dataset must be in a catalog
            with self.assertRaises(ValidationError) as cm:
                # No in_catalog
                cnx.create_entity('Dataset', title=u'My Third Dataset', dcat_theme=concept)
                cnx.commit()
                self.assertEqual(cm.exception.msg,
                                 "Theme must belong to the dataset's catalog vocabulary")


class ResourceFeedTC(CubicWebTC):

    def test_script_mimetype_constraints(self):
        """Check MIME type constraints on validation/transformation_script
        relations.
        """
        with self.admin_access.repo_cnx() as cnx:
            ds = cnx.create_entity('Dataset', title=u'ds')
            cnx.create_entity('Script', name=u'csv',
                              accepted_format=u'text/csv',
                              implemented_by=create_file(cnx, 'pass'))
            cnx.create_entity('Script', name=u'plain',
                              accepted_format=u'text/plain',
                              implemented_by=create_file(cnx, 'pass'))
            cnx.create_entity('Script', name=u'allpurpose',
                              implemented_by=create_file(cnx, 'pass'))
            cnx.create_entity('ResourceFeed', url=u'a/b/c',
                              data_format=u'text/plain',
                              resource_feed_of=ds)
            cnx.commit()
            with self.assertRaises(ValidationError) as cm:
                cnx.execute('SET R validation_script S WHERE S name "csv"')
                cnx.commit()
            self.assertEqual(cm.exception.errors,
                             {'validation_script-subject': (
                                 u'script does not handle resource feed data format')})
            cnx.rollback()
            cnx.execute('SET R validation_script S WHERE S name "plain"')
            cnx.commit()
            cnx.execute('SET R validation_script S WHERE S name "allpurpose"')
            cnx.commit()


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