Skip to content
Snippets Groups Projects
Select Git revision
  • 4326795d3cda2833ed79273bee1050152787fd24
  • master default protected
  • add-frequency-to-form
  • dev protected
  • ckan-2.11.0
  • add-package-custom-fields
  • fix-adding-datasets-for-users-and-editors
  • add-auth-subroute
  • 71-migrate-custom-fields-to-ckanext-scheming
  • add-author-maintainer-information
  • fix-inline-flex-btns
  • fix-known-spatial-uri-validation
  • py3
  • 47-aktuelle-resource-einer-collection-wird-nicht-mehr-gefunden
  • 10-eingabe-der-dct-accrualperiodicity-in-weboberflache
  • v1.3
  • 2.5.3
  • 2.5.2
  • 2.5.1
  • 2.5.0
  • 2.4.7
  • 2.4.6
  • 2.4.5
  • 2.4.4
  • 2.4.3
  • 2.4.2
  • 2.4.1
  • 2.4.0
  • 2.3.1
  • 2.3.0
  • 2.2.0
  • 2.1.0
  • 2.0.0
  • 1.4.3
  • 1.4.2
  • 1.4.1
36 results

test_helpers_tpsh.py

Blame
  • test_helpers_tpsh.py 7.29 KiB
    # encoding: utf-8
    
    from collections import namedtuple, OrderedDict
    from mock import patch
    from ckan.common import config
    from ckanext.odsh.tests_tpsh.resources import org_dicts
    import unittest
    
    
    from ckanext.odsh.helpers_tpsh import (
        map_dct_type_to_ckan_type,
        map_ckan_type_to_dct_type,
        correct_missing_relationship,
        get_language_of_package,
        get_address_org,
        load_json_to_ordered_dict,
    )
    
    
    class TestMatchDctTypeToCkanType(unittest.TestCase):
        
        def test_it_returns_collection(self):
            dct_type = 'http://dcat-ap.de/def/datasetTypes/collection'
            ckan_type = map_dct_type_to_ckan_type(dct_type)
            expected_ckan_type = 'collection'
            assert ckan_type ==  expected_ckan_type
        
        def test_it_returns_none_for_unknown_type(self):
            assert map_dct_type_to_ckan_type('some unknown type') == None
    
    
    class TestMatchCkanTypeToDctType(unittest.TestCase):
        
        def test_it_returns_url_for_collection(self):
            ckan_type = 'collection'
            dct_type = map_ckan_type_to_dct_type(ckan_type)
            expected_dct_type = 'http://dcat-ap.de/def/datasetTypes/collection'
            assert dct_type == expected_dct_type
    
    
    FakePackageRelationship = namedtuple(
        'FakePackageRelationship',
        '''
        object_package_id
        subject_package_id
        id
        type
        '''
    )
    
    class Test_correct_missing_relationship(unittest.TestCase):
        def setUp(self):
            self.relationships_from_model = [
                FakePackageRelationship(
                    object_package_id='object package id',
                    subject_package_id='subject package id',
                    id='id',
                    type='type'
                )
            ]
    
        def test_it_does_not_modify_pkg_dict_if_no_model_relationships(self):
            relationships_from_model = []
            original_pkg_dict = {'id': 'some_id'}
            pkg_dict = dict(original_pkg_dict)
            correct_missing_relationship(
                pkg_dict, relationships_from_model
            )
            assert pkg_dict ==  original_pkg_dict
        
        def test_it_does_not_modify_pkg_dict_if_relationships_already_in_dict(self):
            original_pkg_dict = {
                'type': 'dataset',
                'relationships_as_subject': [
                    {
                        '__extras': {
                            'object_package_id': 'original object package id', 
                            'subject_package_id': 'original subject package id'
                        }, 
                        'comment': '', 
                        'id': 'original id', 
                        'type': 'original type'
                    }
                ]
            }
            pkg_dict = dict(original_pkg_dict)
            correct_missing_relationship(
                pkg_dict, self.relationships_from_model
            )
            assert pkg_dict ==  original_pkg_dict
        
        def test_it_does_not_modify_pkg_dict_if_type_collection(self):
            original_pkg_dict = {
                'type': 'collection',
                'relationships_as_subject': [
                    {
                        '__extras': {
                            'object_package_id': 'original object package id', 
                            'subject_package_id': 'original subject package id'
                        }, 
                        'comment': '', 
                        'id': 'original id', 
                        'type': 'original type'
                    }
                ]
            }
            pkg_dict = dict(original_pkg_dict)
            correct_missing_relationship(
                pkg_dict, self.relationships_from_model
            )
            assert pkg_dict ==  original_pkg_dict
        
        def test_it_adds_relationships_if_not_already_in_dict(self):
            pkg_dict = {
                'type': 'dataset',
                'relationships_as_subject': []
            }
            correct_missing_relationship(
                pkg_dict, self.relationships_from_model
            )
            expected_pkg_dict = {
                'type': 'dataset',
                'relationships_as_subject': [
                    {
                        '__extras': {
                            'object_package_id': 'object package id', 
                            'subject_package_id': 'subject package id'
                        }, 
                        'comment': '', 
                        'id': 'id', 
                        'type': 'type'
                    }
                ]
            }
    
            from_relationships = lambda d, key: d.get('relationships_as_subject')[0].get(key)
            from_extras = lambda d, key: d.get('relationships_as_subject')[0].get('__extras').get(key)
            
            # assert
            assert pkg_dict.get('type') ==  'dataset'
            
            for key in ('id', 'type'):
                assert from_relationships(pkg_dict, key) is not None
                assert from_relationships(pkg_dict, key) == from_relationships(expected_pkg_dict, key)
    
            for key in ('object_package_id', 'subject_package_id'):
                assert pkg_dict is not None
                assert from_extras(pkg_dict, key) == from_extras(expected_pkg_dict, key)
    
        
    class Test_get_language_of_package(unittest.TestCase):
        def setUp(self):
            config.update({'ckanext.odsh.language_mapping': '/usr/lib/ckan/default/src/ckanext-odsh/ckanext/odsh/resources/language_mapping.json'})
        
        def tearDown(self):
            config.clear()
    
        def test_it_returns_Englisch(self):
            test_package = {
                    'id': 'language_test',
                    'extras': [
                        {'key': 'language', 'value': 'http://publications.europa.eu/resource/authority/language/ENG'},
                    ]
                }
            assert get_language_of_package(test_package) == 'Englisch'
        
        def test_it_returns_None_if_language_id_not_in_dict(self):
            test_package = {
                    'id': 'language_test',
                    'extras': [
                        {'key': 'language', 'value': 'tlhIngan Hol'},
                    ]
                }
            assert get_language_of_package(test_package) ==  None
        
        def test_it_returns_None_if_language_not_in_pkg_dict(self):
            test_package = {}
            assert get_language_of_package(test_package) ==  None
        
    
    class Test_get_address_org(unittest.TestCase):
        def test_it_returns_address_for_org_with_address(self):
            organization = org_dicts.organization_with_address
            address = get_address_org(organization)
            assert address.get('location') ==  'Müllerdorf'
            assert address.get('person') ==  'Michael Müller'
            assert address.get('mail') ==  'mueller@mueller.de'
            assert address.get('street') ==  'Müllergasse 10'
            assert address.get('telephone') ==  '040 123456'
            assert address.get('web') ==  'http://mueller.de'
    
        def test_it_returns_empty_dict_if_called_via_organization_new(self):
            organization = dict()
            address = get_address_org(organization)
            assert type(address) is dict
            assert len(address) ==  0
    
    
    class Test_load_json_to_ordered_dict(unittest.TestCase):
        def setUp(self):
            json_str = '{"A": 1, "B": 2, "D": 3, "C":4, "E": 0}'
            self.result = load_json_to_ordered_dict(json_str)
        
        def test_it_does_not_crash(self):
            pass
    
        def test_it_returns_ordered_dict(self):
            assert type(self.result) is OrderedDict
        
        def test_it_preserves_order_of_keys(self):
            keys = list(self.result.keys())
            assert keys == ['A', 'B', 'D', 'C', 'E']
        
        def test_it_preserves_order_of_values(self):
            values = list(self.result.values())
            assert values == [1, 2, 3, 4, 0]