<commit_before># -*- coding: utf-8 -*-

import unittest
import os
from .context import pydatajson
from pydatajson.ckan_utils import *
from pydatajson.helpers import title_to_name
SAMPLES_DIR = os.path.join("tests", "samples")


class DatasetConversionTestCase(unittest.TestCase):

    @classmethod
    def get_sample(cls, sample_filename):
        return os.path.join(SAMPLES_DIR, sample_filename)

    @classmethod
    def setUpClass(cls):
        cls.catalog = pydatajson.DataJson(cls.get_sample('full_data.json'))
        cls.catalog_id = cls.catalog.get('identifier', title_to_name(cls.catalog['title']))
        cls.dataset = cls.catalog.datasets[0]
        cls.dataset_id = cls.dataset.get('identifier')
        cls.distributions = cls.dataset['distribution']

    def test_catalog_id_is_prepended_to_dataset_id_if_passed(self):
        package = map_dataset_to_package(self.catalog, self.dataset, 'owner', catalog_id=self.catalog_id)
        self.assertEqual(self.catalog_id + '_' + self.dataset_id, package['id'])

    def test_dataset_id_is_preserved_if_catlog_id_is_not_passed(self):
        package = map_dataset_to_package(self.catalog, self.dataset, 'owner')
        self.assertEqual(self.dataset_id, package['id'])

    def test_replicated_plain_attributes_are_corrext(self):
        package = map_dataset_to_package(self.catalog, self.dataset, 'owner', catalog_id=self.catalog_id)
        plain_replicated_attributes = [('title', 'title'),
                                       ('notes', 'description'),
                                       ('url', 'landingPage')]
        for fst, snd in plain_replicated_attributes:
            self.assertEqual(self.dataset.get(snd), package.get(fst))
        self.assertEqual('owner', package['owner_org'])

    def test_dataset_nested_replicated_attributes_stay_the_same(self):
        package = map_dataset_to_package(self.catalog, self.dataset, 'owner', catalog_id=self.catalog_id)
        contact_point_nested = [('maintainer', 'fn'),
                                ('maintainer_email', 'hasEmail')]
        for fst, snd in contact_point_nested:
            self.assertEqual(self.dataset.get('contactPoint', {}).get(snd), package.get(fst))
        publisher_nested = [('author', 'name'),
                            ('author_email', 'mbox')]
        for fst, snd in publisher_nested:
            self.assertEqual(self.dataset.get('publisher').get(snd), package.get(fst))<commit_msg>Actualizo los tests de acuerdo al nuevo comportamiento<commit_after># -*- coding: utf-8 -*-

import unittest
import os
from .context import pydatajson
from pydatajson.ckan_utils import *
from pydatajson.helpers import title_to_name
SAMPLES_DIR = os.path.join("tests", "samples")


class DatasetConversionTestCase(unittest.TestCase):

    @classmethod
    def get_sample(cls, sample_filename):
        return os.path.join(SAMPLES_DIR, sample_filename)

    @classmethod
    def setUpClass(cls):
        cls.catalog = pydatajson.DataJson(cls.get_sample('full_data.json'))
        cls.catalog_id = cls.catalog.get('identifier', title_to_name(cls.catalog['title']))
        cls.dataset = cls.catalog.datasets[0]
        cls.dataset_id = cls.dataset.get('identifier')
        cls.distributions = cls.dataset['distribution']

    def test_catalog_id_is_prepended_to_dataset_id_and_name_if_passed(self):
        package = map_dataset_to_package(self.catalog, self.dataset, 'owner', catalog_id=self.catalog_id)
        self.assertEqual(self.catalog_id + '_' + self.dataset_id, package['id'])
        self.assertEqual(title_to_name(self.catalog_id + '-' + self.dataset['title']), package['name'])

    def test_dataset_id_and_name_are_preserved_if_catalog_id_is_not_passed(self):
        package = map_dataset_to_package(self.catalog, self.dataset, 'owner')
        self.assertEqual(self.dataset_id, package['id'])
        self.assertEqual(title_to_name(self.dataset['title']), package['name'])

    def test_replicated_plain_attributes_are_corrext(self):
        package = map_dataset_to_package(self.catalog, self.dataset, 'owner', catalog_id=self.catalog_id)
        plain_replicated_attributes = [('notes', 'description'),
                                       ('url', 'landingPage')]
        for fst, snd in plain_replicated_attributes:
            self.assertEqual(self.dataset.get(snd), package.get(fst))
        self.assertEqual('owner', package['owner_org'])

    def test_dataset_nested_replicated_attributes_stay_the_same(self):
        package = map_dataset_to_package(self.catalog, self.dataset, 'owner', catalog_id=self.catalog_id)
        contact_point_nested = [('maintainer', 'fn'),
                                ('maintainer_email', 'hasEmail')]
        for fst, snd in contact_point_nested:
            self.assertEqual(self.dataset.get('contactPoint', {}).get(snd), package.get(fst))
        publisher_nested = [('author', 'name'),
                            ('author_email', 'mbox')]
        for fst, snd in publisher_nested:
            self.assertEqual(self.dataset.get('publisher').get(snd), package.get(fst))
{'fraction_non_alphanumeric': 0.1222879684418146, 'fraction_numerical': 0.0007889546351084812, 'mean_word_length': 3.5236396074933096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '739545', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1516, 'n_words': 243}