def test_can_register_via_decorator_using_custom_name(self): registry = persisting_theory.Registry() @registry.register(name="custom_name") def something(): pass self.assertEqual(registry.get('custom_name'), something)
def test_can_register_data_to_registry(self): data = "something" registry = persisting_theory.Registry() registry.register(data, name="key") self.assertEqual(len(registry), 1) self.assertEqual(registry.get("key"), data)
def test_can_register_class_and_function_via_decorator(self): registry = persisting_theory.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_infer_name_from_class_function_and_instance(self): registry = persisting_theory.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")
target = TargetSerializer() class Meta: model = models.UserFilter fields = ["uuid", "target", "creation_date"] read_only_fields = ["uuid", "creation_date"] def validate(self, data): target = data.pop("target") if target["type"] == "artist": data["target_artist"] = target["obj"] return data state_serializers = persisting_theory.Registry() class DescriptionStateMixin(object): def get_description(self, o): if o.description: return o.description.text TAGS_FIELD = serializers.ListField(source="get_tags") @state_serializers.register(name="music.Artist") class ArtistStateSerializer(DescriptionStateMixin, serializers.ModelSerializer): tags = TAGS_FIELD