tests.py 4.45 KB
Newer Older
Eliot Berriot's avatar
Eliot Berriot committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import unittest 
import test_registries
import registries

registries.meta_registry.look_into = "test_registries"

TEST_APPS = (
    "app1",
    "app2",
)

class RegistryTest(unittest.TestCase):

    def test_can_infer_name_from_class_function_and_instance(self):
        registry = registries.Registry()

        def something():
            pass

        class MyClass:
            pass

        self.assertEqual(registry.get_object_name(something), "something")
        self.assertEqual(registry.get_object_name(MyClass), "MyClass")

        with self.assertRaises(ValueError):
            self.assertEqual(registry.get_object_name(MyClass()), "MyClass")

    def test_can_register_data_to_registry(self):

        data = "something"
        registry = registries.Registry()
        registry.register(data, name="key")

        self.assertEqual(len(registry), 1)
        self.assertEqual(registry.get("key"), data)

40 41 42 43 44 45 46 47 48 49 50 51 52 53
    def test_can_restric_registered_data(self):

        class RestrictedRegistry(registries.Registry):
            def validate(self, obj):
                """Only accept integer values"""
                return isinstance(obj, int)

        registry = RestrictedRegistry()

        registry.register(12, name="twelve")
        with self.assertRaises(ValueError):
            registry.register("not an int", name="not an int")


Eliot Berriot's avatar
Eliot Berriot committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    def test_can_register_class_and_function_via_decorator(self):
        registry = registries.Registry()

        @registry.register
        class ToRegister:
            pass

        self.assertEqual(registry.get('ToRegister'), ToRegister)

        @registry.register
        def something():
            pass

        self.assertEqual(registry.get('something'), something)
            
    def test_can_register_via_decorator_using_custom_name(self):
        registry = registries.Registry()

        @registry.register(name="custom_name")
        def something():
            pass

        self.assertEqual(registry.get('custom_name'), something)

    def test_registry_can_autodiscover(self):

        registry = test_registries.awesome_people
        registry.autodiscover(apps=TEST_APPS)

        self.assertEqual(len(registry), 2)
        self.assertNotEqual(registry.get('AlainDamasio', None), None)
        self.assertNotEqual(registry.get('FrederikPeeters', None), None)

        registry.clear()

89 90 91 92 93
    def test_autodiscover_raises_an_error_if_there_is_an_error_in_imported_module(self):
        with self.assertRaises(NameError):
            registry = test_registries.awesome_people
            registry.autodiscover(apps=('buggy_app',))

Eliot Berriot's avatar
Eliot Berriot committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    def test_meta_registry_can_autodiscovering_registries_and_trigger_their_autodiscover_method(self):

        registry = registries.meta_registry
        registry.autodiscover(apps=TEST_APPS)

        self.assertEqual(len(registry), 2)
        self.assertEqual(registry.get('awesome_people'), test_registries.awesome_people)
        self.assertEqual(registry.get('vegetable_registry'), test_registries.vegetable_registry)

        registry = test_registries.awesome_people
        self.assertEqual(len(registry), 2)
        self.assertNotEqual(registry.get('AlainDamasio', None), None)
        self.assertNotEqual(registry.get('FrederikPeeters', None), None)

        registry = test_registries.vegetable_registry
        self.assertEqual(len(registry), 2)
        self.assertNotEqual(registry.get('Potato', None), None)
        self.assertNotEqual(registry.get('Ketchup', None), None)

113 114 115 116 117 118 119 120 121 122 123 124 125
    def test_can_manipulate_data_before_registering(self):

        class ModifyData(registries.Registry):
            def prepare_data(self, obj):
                return "hello " + obj

        r = ModifyData()

        r.register("eliot", name="eliot")
        r.register("roger", name="roger")

        self.assertEqual(r.get('eliot'), "hello eliot")
        self.assertEqual(r.get('roger'), "hello roger")
Eliot Berriot's avatar
Eliot Berriot committed
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    def test_can_manipulate_key_before_registering(self):

        class ModifyKey(registries.Registry):
            def prepare_name(self, obj, key=None):
                return "custom_key " + obj.name

        r = ModifyKey()

        class N:
            def __init__(self, name):
                self.name = name

        n1 = N(name="eliot")
        n2 = N(name="alain")
        r.register(n1)
        r.register(n2)

        self.assertEqual(r.get('custom_key eliot'), n1)
        self.assertEqual(r.get('custom_key alain'), n2)
Eliot Berriot's avatar
Eliot Berriot committed
146 147
if __name__ == '__main__':
    unittest.main()