MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_collectionclasses

Method test_collectionclasses

test/orm/test_attributes.py:859–949  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

857 instrumentation.unregister_class(Foo)
858
859 def test_collectionclasses(self):
860 class Foo:
861 pass
862
863 instrumentation.register_class(Foo)
864 _register_attribute(
865 Foo, "collection", uselist=True, typecallable=set, useobject=True
866 )
867 assert attributes.manager_of_class(Foo).is_instrumented("collection")
868 assert isinstance(Foo().collection, set)
869 attributes._unregister_attribute(Foo, "collection")
870 assert not attributes.manager_of_class(Foo).is_instrumented(
871 "collection"
872 )
873 try:
874 _register_attribute(
875 Foo,
876 "collection",
877 uselist=True,
878 typecallable=dict,
879 useobject=True,
880 )
881 assert False
882 except sa_exc.ArgumentError as e:
883 assert (
884 str(e) == "Type InstrumentedDict must elect an appender "
885 "method to be a collection class"
886 )
887
888 class MyDict(dict):
889 @collection.appender
890 def append(self, item):
891 self[item.foo] = item
892
893 @collection.remover
894 def remove(self, item):
895 del self[item.foo]
896
897 _register_attribute(
898 Foo,
899 "collection",
900 uselist=True,
901 typecallable=MyDict,
902 useobject=True,
903 )
904 assert isinstance(Foo().collection, MyDict)
905 attributes._unregister_attribute(Foo, "collection")
906
907 class MyColl:
908 pass
909
910 try:
911 _register_attribute(
912 Foo,
913 "collection",
914 uselist=True,
915 typecallable=MyColl,
916 useobject=True,

Callers

nothing calls this directly

Calls 4

is_instrumentedMethod · 0.80
manager_of_classMethod · 0.80
_register_attributeFunction · 0.70
FooClass · 0.70

Tested by

no test coverage detected