# Path: ckanext/dcatapit/mapping.py
# def themes_to_aggr_json(themes: list) -> str:
#     aggr = []
#     for name in themes or []:
#         name = name.split('/')[-1]  # if it's a URL, only take the final name
#         aggr.append({'theme': name.upper(), 'subthemes': []})
#     return json.dumps(aggr)
# 
# def theme_aggr_to_theme_uris(aggregated_themes: list) -> list:
#     return [theme_name_to_uri(agg.get('theme')) for agg in aggregated_themes]
# 
# def theme_names_to_uris(names: list) -> list:
#     return [theme_name_to_uri(name) for name in names]
# 
# def theme_name_to_uri(theme_name: str) -> str:
#     if theme_name.startswith('http'):
#         log.warning(f'Theme name "{theme_name}" is already a URI')
#         return theme_name
#     return THEME_BASE_URI + theme_name.upper()
# 
# Path: ckanext/dcatapit/schema.py
# FIELD_THEMES_AGGREGATE = 'themes_aggregate'


from unittest import TestCase
from uuid import uuid4
from ckan.tests.helpers import call_action
from ckanext.dcatapit.mapping import themes_to_aggr_json, theme_aggr_to_theme_uris, theme_names_to_uris, \
    theme_name_to_uri
from ckanext.dcatapit.schema import FIELD_THEMES_AGGREGATE
import json
import nose
import pytest
import ckan.tests.factories as factories
import ckanext.dcatapit.plugin as plugin

        created = call_action('package_create', {'defer_commit': False}, **src)
        reloaded = call_action('package_show', context=__class__.context, id=created['id'])

        for pkg in (created, reloaded):
            self._ensure_aggr(pkg, [__class__.DEFAULT_THEME])
            self._ensure_themes(pkg, [__class__.DEFAULT_THEME])

    def test_package_no_theme_aggr(self):
        src = self._get_dataset()
        src.pop('extras')

        created = call_action('package_create', {'defer_commit': False}, **src)
        reloaded = call_action('package_show', context=__class__.context, id=created['id'])

        for pkg in (created, reloaded):
            self._ensure_aggr(pkg, [__class__.DEFAULT_AGGR])
            self._ensure_themes(pkg, [__class__.DEFAULT_AGGR])

    def test_package_theme_aggr(self):
        src = self._get_dataset()

        created = call_action('package_create', {'defer_commit': False}, **src)
        reloaded = call_action('package_show', context=__class__.context, id=created['id'])

        for pkg in (created, reloaded):
            self._ensure_aggr(pkg, [__class__.DEFAULT_AGGR])
            self._ensure_themes(pkg, [__class__.DEFAULT_AGGR])

    def _ensure_aggr(self, pkg, expected_themes: list):

# Path: ckanext/dcatapit/mapping.py
# def themes_to_aggr_json(themes: list) -> str:
#     aggr = []
#     for name in themes or []:
#         name = name.split('/')[-1]  # if it's a URL, only take the final name
#         aggr.append({'theme': name.upper(), 'subthemes': []})
#     return json.dumps(aggr)
# 
# def theme_aggr_to_theme_uris(aggregated_themes: list) -> list:
#     return [theme_name_to_uri(agg.get('theme')) for agg in aggregated_themes]
# 
# def theme_names_to_uris(names: list) -> list:
#     return [theme_name_to_uri(name) for name in names]
# 
# def theme_name_to_uri(theme_name: str) -> str:
#     if theme_name.startswith('http'):
#         log.warning(f'Theme name "{theme_name}" is already a URI')
#         return theme_name
#     return THEME_BASE_URI + theme_name.upper()
# 
# Path: ckanext/dcatapit/schema.py
# FIELD_THEMES_AGGREGATE = 'themes_aggregate'


# Path: ckanext/dcatapit/tests/test_plugin.py
from unittest import TestCase
from uuid import uuid4
from ckan.tests.helpers import call_action
from ckanext.dcatapit.mapping import themes_to_aggr_json, theme_aggr_to_theme_uris, theme_names_to_uris, \
    theme_name_to_uri
from ckanext.dcatapit.schema import FIELD_THEMES_AGGREGATE
import json
import nose
import pytest
import ckan.tests.factories as factories
import ckanext.dcatapit.plugin as plugin


        created = call_action('package_create', {'defer_commit': False}, **src)
        reloaded = call_action('package_show', context=__class__.context, id=created['id'])

        for pkg in (created, reloaded):
            self._ensure_aggr(pkg, [__class__.DEFAULT_THEME])
            self._ensure_themes(pkg, [__class__.DEFAULT_THEME])

    def test_package_no_theme_aggr(self):
        src = self._get_dataset()
        src.pop('extras')

        created = call_action('package_create', {'defer_commit': False}, **src)
        reloaded = call_action('package_show', context=__class__.context, id=created['id'])

        for pkg in (created, reloaded):
            self._ensure_aggr(pkg, [__class__.DEFAULT_AGGR])
            self._ensure_themes(pkg, [__class__.DEFAULT_AGGR])

    def test_package_theme_aggr(self):
        src = self._get_dataset()

        created = call_action('package_create', {'defer_commit': False}, **src)
        reloaded = call_action('package_show', context=__class__.context, id=created['id'])

        for pkg in (created, reloaded):
            self._ensure_aggr(pkg, [__class__.DEFAULT_AGGR])
            self._ensure_themes(pkg, [__class__.DEFAULT_AGGR])

    def _ensure_aggr(self, pkg, expected_themes: list):

        self.assertIn(FIELD_THEMES_AGGREGATE, pkg.keys(), 'Aggregate not found')