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

import unittest2

from openerp.addons.base_external_referentials.reference import (
        Reference,
        get_reference,
        REFERENCES)
from openerp.addons.base_external_referentials import (
        Binder)
from openerp.addons.base_external_referentials import (
        Mapper,
        TO_REFERENCE)
from openerp.addons.base_external_referentials import (
        BackendAdapter)


class test_reference(unittest2.TestCase):
    """ Test Reference """

    def setUp(self):
        self.service = 'calamitorium'

    def tearDown(self):
        REFERENCES.references.clear()

    def test_new_reference(self):
        """ Create a reference"""
        version = '1.14'
        reference = Reference(self.service, version=version)
        self.assertEqual(reference.service, self.service)
        self.assertEqual(reference.version, version)

    def test_parent(self):
        """ Bind the reference to a parent reference"""
        version = '1.14'
        reference = Reference(self.service)
        child_reference = Reference(parent=reference, version=version)
        self.assertEqual(child_reference.service, reference.service)

    def test_no_service(self):
        """ Should raise an error because no service or parent is defined"""
        with self.assertRaises(ValueError):
            reference = Reference(version='1.14')

    def test_get_reference(self):
        """ Find a reference """
        reference = Reference(self.service)
        found_ref = get_reference(self.service)
        self.assertEqual(reference, found_ref)

    def test_reference_version(self):
        """ Find a reference with a version """
        parent = Reference(self.service)
        reference = Reference(parent=parent, version='1.14')
        found_ref = get_reference(self.service, version='1.14')
        self.assertEqual(reference, found_ref)


class test_reference_register(unittest2.TestCase):
    """ Test registration of classes on the Reference"""


    def setUp(self):
        self.service = 'calamitorium'
        self.version = '1.14'
        self.parent = Reference(self.service)
        self.reference = Reference(parent=self.parent, version=self.version)

    def tearDown(self):
        REFERENCES.references.clear()
        self.reference._classes.clear()

    def test_register_class(self):
        class BenderBinder(Binder):
            model_name = 'res.users'

        self.reference.register_class(BenderBinder)
        ref = self.reference.get_class(Binder, 'res.users')
        self.assertEqual(ref, BenderBinder)

    def test_register_class_decorator(self):
        @self.reference
        class ZoidbergMapper(Mapper):
            model_name = 'res.users'
            direction = TO_REFERENCE

        ref = self.reference.get_class(Mapper, 'res.users', TO_REFERENCE)
        self.assertEqual(ref, ZoidbergMapper)

    def test_register_class_parent(self):
        """ It should get the parent's class when no class is defined"""
        @self.parent
        class FryBinder(Binder):
            model_name = 'res.users'

        ref = self.reference.get_class(Binder, 'res.users')
        self.assertEqual(ref, FryBinder)

    def test_no_register_error(self):
        """ Error when asking for a class and none is found"""
        with self.assertRaises(ValueError):
            ref = self.reference.get_class(BackendAdapter, 'res.users')

    def test_registered_classes_all(self):
        @self.reference
        class LeelaMapper(Mapper):
            model_name = 'res.users'

        @self.reference
        class FarnsworthBinder(Binder):
            model_name = 'res.users'

        @self.reference
        class NibblerBackendAdapter(BackendAdapter):
            model_name = 'res.users'

        classes = list(self.reference.registered_classes())
        self.assertItemsEqual(
                classes,
                [LeelaMapper, FarnsworthBinder, NibblerBackendAdapter])

    def test_registered_classes_filter(self):
        @self.reference
        class LeelaMapper(Mapper):
            model_name = 'res.users'

        @self.reference
        class AmyWongMapper(Mapper):
            model_name = 'res.users'

        @self.reference
        class FarnsworthBinder(Binder):
            model_name = 'res.users'

        @self.reference
        class NibblerBackendAdapter(BackendAdapter):
            model_name = 'res.users'

        classes = list(self.reference.registered_classes(Mapper))
        self.assertItemsEqual(
                classes,
                [LeelaMapper, AmyWongMapper])
<commit_msg>[FIX] tests: use the new ImportMapper and ExportMapper<commit_after># -*- coding: utf-8 -*-

import unittest2

from openerp.addons.base_external_referentials.reference import (
        Reference,
        get_reference,
        REFERENCES)
from openerp.addons.base_external_referentials import Binder
from openerp.addons.base_external_referentials import (Mapper,
                                                       ImportMapper,
                                                       ExportMapper)
from openerp.addons.base_external_referentials import BackendAdapter


class test_reference(unittest2.TestCase):
    """ Test Reference """

    def setUp(self):
        self.service = 'calamitorium'

    def tearDown(self):
        REFERENCES.references.clear()

    def test_new_reference(self):
        """ Create a reference"""
        version = '1.14'
        reference = Reference(self.service, version=version)
        self.assertEqual(reference.service, self.service)
        self.assertEqual(reference.version, version)

    def test_parent(self):
        """ Bind the reference to a parent reference"""
        version = '1.14'
        reference = Reference(self.service)
        child_reference = Reference(parent=reference, version=version)
        self.assertEqual(child_reference.service, reference.service)

    def test_no_service(self):
        """ Should raise an error because no service or parent is defined"""
        with self.assertRaises(ValueError):
            reference = Reference(version='1.14')

    def test_get_reference(self):
        """ Find a reference """
        reference = Reference(self.service)
        found_ref = get_reference(self.service)
        self.assertEqual(reference, found_ref)

    def test_reference_version(self):
        """ Find a reference with a version """
        parent = Reference(self.service)
        reference = Reference(parent=parent, version='1.14')
        found_ref = get_reference(self.service, version='1.14')
        self.assertEqual(reference, found_ref)


class test_reference_register(unittest2.TestCase):
    """ Test registration of classes on the Reference"""


    def setUp(self):
        self.service = 'calamitorium'
        self.version = '1.14'
        self.parent = Reference(self.service)
        self.reference = Reference(parent=self.parent, version=self.version)

    def tearDown(self):
        REFERENCES.references.clear()
        self.reference._classes.clear()

    def test_register_class(self):
        class BenderBinder(Binder):
            model_name = 'res.users'

        self.reference.register_class(BenderBinder)
        ref = self.reference.get_class(Binder, 'res.users')
        self.assertEqual(ref, BenderBinder)

    def test_register_class_decorator(self):
        @self.reference
        class ZoidbergMapper(ExportMapper):
            model_name = 'res.users'

        ref = self.reference.get_class(ExportMapper, 'res.users')
        self.assertEqual(ref, ZoidbergMapper)

    def test_register_class_parent(self):
        """ It should get the parent's class when no class is defined"""
        @self.parent
        class FryBinder(Binder):
            model_name = 'res.users'

        ref = self.reference.get_class(Binder, 'res.users')
        self.assertEqual(ref, FryBinder)

    def test_no_register_error(self):
        """ Error when asking for a class and none is found"""
        with self.assertRaises(ValueError):
            ref = self.reference.get_class(BackendAdapter, 'res.users')

    def test_registered_classes_all(self):
        @self.reference
        class LeelaMapper(Mapper):
            model_name = 'res.users'

        @self.reference
        class FarnsworthBinder(Binder):
            model_name = 'res.users'

        @self.reference
        class NibblerBackendAdapter(BackendAdapter):
            model_name = 'res.users'

        classes = list(self.reference.registered_classes())
        self.assertItemsEqual(
                classes,
                [LeelaMapper, FarnsworthBinder, NibblerBackendAdapter])

    def test_registered_classes_filter(self):
        @self.reference
        class LeelaMapper(ExportMapper):
            model_name = 'res.users'

        @self.reference
        class AmyWongMapper(ImportMapper):
            model_name = 'res.users'

        @self.reference
        class FarnsworthBinder(Binder):
            model_name = 'res.users'

        @self.reference
        class NibblerBackendAdapter(BackendAdapter):
            model_name = 'res.users'

        classes = list(self.reference.registered_classes(Mapper))
        self.assertItemsEqual(
                classes,
                [LeelaMapper, AmyWongMapper])

{'fraction_non_alphanumeric': 0.09541205544214032, 'fraction_numerical': 0.004727624368754701, 'mean_word_length': 2.552671755725191, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5444939', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2514, 'n_words': 517}