Esempio n. 1
0
    def test_register_invalid(self):
        """Register invalid serializer"""
        class Thing(object):
            pass
        invalid1 = Thing()
        invalid2 = Thing()
        setattr(invalid2, 'load', lambda x: x)

        with self.assertRaises(AttributeError):
            manager.register('bork', invalid1)
        with self.assertRaises(AttributeError):
            manager.register('bork', invalid2)
Esempio n. 2
0
    def test_custom_cache_serializer(self):
        """Custom cache serializer"""
        data = {'key1': 'value1'}

        class MySerializer(object):
            @classmethod
            def load(self, file_obj):
                return json.load(file_obj)

            @classmethod
            def dump(self, obj, file_obj):
                return json.dump(obj, file_obj, indent=2)

        manager.register('spoons', MySerializer)
        self.assertFalse(os.path.exists(self.wf.cachefile('test.spoons')))
        self.wf.cache_serializer = 'spoons'
        self.wf.cache_data('test', data)
        self.assertTrue(os.path.exists(self.wf.cachefile('test.spoons')))
        self.assertEqual(data, self.wf.cached_data('test'))
Esempio n. 3
0
    def test_register_unregister(self):
        """Register/unregister serializers"""
        serializers = {}
        for name in self.serializers:
            serializer = manager.serializer(name)
            self._is_serializer(serializer)

        for name in self.serializers:
            serializer = manager.unregister(name)
            self._is_serializer(serializer)
            serializers[name] = serializer

        for name in self.serializers:
            self.assertEqual(manager.serializer(name), None)

        for name in self.serializers:
            with self.assertRaises(ValueError):
                manager.unregister(name)

        for name in self.serializers:
            serializer = serializers[name]
            manager.register(name, serializer)