def char_field_factory(model_field, **_): if not model_field.choices: return Shortcut(call_target__attribute='text') return Shortcut( call_target__attribute='choice', choices=[x[0] for x in model_field.choices], )
def test_shortcut_call_target_attribute(): class Foo: @classmethod def foo(cls): return cls assert Shortcut(call_target__attribute='foo', call_target__cls=Foo)() is Foo assert isinstance(Shortcut(call_target__cls=Foo)(), Foo)
def field_char_field_factory(model_field, **_): if not model_field.choices: return Shortcut(call_target__attribute='text') display_name_by_choice = dict(model_field.choices) return Shortcut(call_target__attribute='choice', choices=[x[0] for x in model_field.choices], choice_display_name_formatter=lambda choice, **_: display_name_by_choice[choice])
def test_class_shortcut__shortcut_stack(): class MyFoo: @classmethod @class_shortcut def shortcut(cls, call_target): return call_target() @classmethod @class_shortcut(call_target__attribute='shortcut') def shortcut2(cls, call_target, **kwargs): return call_target(**kwargs) middle = Shortcut(call_target=MyFoo.shortcut2) class MyOtherFoo(MyFoo): @classmethod @class_shortcut(call_target=middle) def shortcut3(cls, call_target, **kwargs): return call_target(**kwargs) assert MyOtherFoo().shortcut2().__tri_declarative_shortcut_stack == [ 'shortcut2', 'shortcut' ] assert MyOtherFoo().shortcut3().__tri_declarative_shortcut_stack == [ 'shortcut3', 'shortcut2', 'shortcut' ]
def test_is_shortcut(): t = Namespace(x=1) assert not is_shortcut(t) s = Shortcut(x=1) assert isinstance(s, Namespace) assert is_shortcut(s)
def test_namespace_shortcut_overwrite_backward(): assert Namespace( Namespace(x=Namespace(y__z=1, y__zz=2)), Namespace(x=Shortcut(a__b=3)), ) == Namespace( x__a__b=3, x__y__z=1, x__y__zz=2, )
def test_shortcut_chaining(): def endpoint(**kwargs): return kwargs foo = Shortcut( call_target=endpoint, tag='foo', ) bar = Shortcut( call_target=foo, bar=1, # these two will get popped off by Namespace.__call__, let's make sure they are! call_target__cls='randomcrap', call_target__attribute='randomcrap', ) assert bar() == dict(tag='foo', bar=1)
def register_filter_factory(django_field_class, *, shortcut_name=MISSING, factory=MISSING): assert shortcut_name is not MISSING or factory is not MISSING if factory is MISSING: factory = Shortcut(call_target__attribute=shortcut_name) _filter_factory_by_django_field_type[django_field_class] = factory
def test_nested_namespace_overriding_and_calling(): @dispatch def f(extra): return extra.foo foo = Shortcut( call_target=f, extra__foo='asd', ) assert foo(extra__foo='qwe') == 'qwe'
def test_refinable_object_complete_example(): def f_(p=11): return p class Foo(RefinableObject): a = Refinable() b = Refinable() @dispatch( b='default_b', ) def __init__(self, **kwargs): self.non_refinable = 17 super(Foo, self).__init__(**kwargs) @staticmethod @dispatch(f=Namespace(call_target=f_)) @refinable def c(f): """ c docstring """ return f() @staticmethod @shortcut @dispatch(call_target=f_) def shortcut_to_f(call_target): return call_target() @shortcut @dispatch(call_target=Foo) def shortcut_to_foo(call_target): return call_target() Foo.shortcut_to_foo = staticmethod(shortcut_to_foo) Foo.q = Shortcut(call_target=Foo, b='refined_by_shortcut_b') with pytest.raises(TypeError): Foo(non_refinable=1) assert Foo().a is None assert Foo(a=1).a == 1 # refinable function with dispatch assert Foo().c() == 11 assert Foo().c(f__p=13) == 13 assert Foo(c=lambda p: 77).c(12321312312) == 77
def test_class_shortcut_shortcut(): class Foo: def __init__(self, **kwargs): self.kwargs = kwargs @classmethod @class_shortcut(x=17) def shortcut1(cls, call_target=None, **kwargs): return call_target(**kwargs) Foo.shortcut2 = Shortcut( y=42, call_target__cls=Foo, call_target__attribute='shortcut1', ) assert Foo.shortcut1().kwargs == {'x': 17} assert Foo.shortcut2().kwargs == {'x': 17, 'y': 42}
def test_generate_docs(): def some_callable(): pass class Foo(RefinableObject): """docstring for Foo""" name = Refinable() description = Refinable() some_other_thing = Refinable() @dispatch( name='foo-name', description=lambda foo, bar: 'qwe', some_other_thing=some_callable, ) def __init__(self): """ :param name: description of the name field """ super(Foo, self).__init__() @staticmethod @refinable def refinable_func(field, instance, value): pass @classmethod @class_shortcut def shortcut1(cls): return cls() @classmethod @class_shortcut( description='fish' ) def shortcut2(cls, call_target): """shortcut2 docstring""" return call_target() @classmethod @class_shortcut( description=lambda foo: 'qwe' # TODO: This is currently not shown in the documentation output, but it should be! ) def shortcut3(cls, call_target): """ shortcut3 docstring :param call_target: something something call_target """ return call_target() Foo.shortcut4 = Shortcut( call_target=Foo, name='baz', description='qwe', ) (actual_filename, actual_doc), = list(_generate_rst_docs(classes=[Foo])) assert actual_filename == '/Foo.rst' expected_doc = """ Foo === docstring for Foo Refinable members ----------------- * `description` * `name` description of the name field * `refinable_func` * `some_other_thing` Defaults ^^^^^^^^ * `description` * `lambda foo, bar: 'qwe'` * `name` * `foo-name` * `some_other_thing` * `tests.test_docs.some_callable` Shortcuts --------- `shortcut1` ^^^^^^^^^^^ `shortcut2` ^^^^^^^^^^^ shortcut2 docstring `shortcut3` ^^^^^^^^^^^ shortcut3 docstring :param call_target: something something call_target `shortcut4` ^^^^^^^^^^^ """ assert actual_doc.strip() == expected_doc.strip()
def setup_db_compat_django(): from tri_table import register_column_factory try: # noinspection PyUnresolvedReferences from django.db.models import IntegerField, FloatField, TextField, BooleanField, AutoField, CharField, DateField, DateTimeField, DecimalField, EmailField, TimeField, ForeignKey, ManyToOneRel, ManyToManyField, ManyToManyRel except ImportError: pass else: # The order here is significant because of inheritance structure. More specific must be below less specific. register_column_factory(CharField, Shortcut(call_target__attribute='text')) register_column_factory(TimeField, Shortcut(call_target__attribute='time')) register_column_factory(EmailField, Shortcut(call_target__attribute='email')) register_column_factory(DecimalField, Shortcut(call_target__attribute='decimal')) register_column_factory(DateField, Shortcut(call_target__attribute='date')) register_column_factory(DateTimeField, Shortcut(call_target__attribute='datetime')) register_column_factory(BooleanField, Shortcut(call_target__attribute='boolean')) register_column_factory(TextField, Shortcut(call_target__attribute='text')) register_column_factory(FloatField, Shortcut(call_target__attribute='float')) register_column_factory(IntegerField, Shortcut(call_target__attribute='integer')) register_column_factory( AutoField, Shortcut(call_target__attribute='integer', show=False)) register_column_factory(ManyToOneRel, None) register_column_factory( ManyToManyField, Shortcut(call_target__attribute='many_to_many')) register_column_factory(ManyToManyRel, None) register_column_factory(ForeignKey, Shortcut(call_target__attribute='foreign_key'))
def test_generate_docs(): def some_callable(): pass # pragma: no cover class Foo(RefinableObject): """ docstring for Foo """ name = Refinable() description = Refinable() some_other_thing = Refinable() empty_string_default = Refinable() @dispatch( name='foo-name', description=lambda foo, bar: 'qwe', some_other_thing=some_callable, empty_string_default='', ) def __init__(self): """ :param name: description of the name field """ super(Foo, self).__init__() # pragma: no cover @staticmethod @refinable def refinable_func(field, instance, value): pass # pragma: no cover @classmethod @class_shortcut def shortcut1(cls): return cls() # pragma: no cover @classmethod @class_shortcut(description='fish') def shortcut2(cls, call_target): """shortcut2 docstring""" return call_target() # pragma: no cover @classmethod @class_shortcut(description=lambda foo: 'qwe') def shortcut3(cls, call_target): """ shortcut3 docstring :param call_target: something something call_target """ return call_target() # pragma: no cover Foo.shortcut4 = Shortcut( call_target=Foo, name='baz', description='qwe', ) (actual_filename, actual_doc), = list(_generate_rst_docs(classes=[Foo])) assert actual_filename == '/Foo.rst' expected_doc = """ Foo === Base class: `RefinableObject` docstring for Foo Refinable members ----------------- * `description` * `empty_string_default` * `name` description of the name field * `refinable_func` * `some_other_thing` Defaults ^^^^^^^^ * `description` * `lambda foo, bar: 'qwe'` * `empty_string_default` * `""` * `name` * `foo-name` * `some_other_thing` * `iommi.docs__tests.some_callable` Shortcuts --------- `shortcut1` ^^^^^^^^^^^ `shortcut2` ^^^^^^^^^^^ shortcut2 docstring Defaults ++++++++ * `description` * `fish` `shortcut3` ^^^^^^^^^^^ shortcut3 docstring :param call_target: something something call_target Defaults ++++++++ * `description` * `lambda foo: 'qwe'` `shortcut4` ^^^^^^^^^^^ Defaults ++++++++ * `call_target` * `iommi.docs__tests.Foo` * `name` * `baz` * `description` * `qwe` """ assert actual_doc.strip() == expected_doc.strip()
def setup_db_compat_django(): from iommi.form import register_field_factory from iommi.query import register_variable_factory from iommi.table import register_column_factory from django.db.models import ( AutoField, BooleanField, CharField, DateField, DateTimeField, DecimalField, EmailField, FileField, FloatField, ForeignKey, IntegerField, ManyToManyField, ManyToManyRel, ManyToOneRel, TextField, TimeField, URLField, UUIDField, ) # The order here is significant because of inheritance structure. More specific must be below less specific. register_factory(CharField, shortcut_name='text') register_factory(UUIDField, shortcut_name='text') register_factory(TimeField, shortcut_name='time') register_factory(EmailField, shortcut_name='email') register_factory(DecimalField, shortcut_name='decimal') register_factory(DateField, shortcut_name='date') register_factory(DateTimeField, shortcut_name='datetime') register_factory(FloatField, shortcut_name='float') register_factory(IntegerField, shortcut_name='integer') register_factory(AutoField, factory=Shortcut(call_target__attribute='integer', include=False)) register_factory(ManyToOneRel, factory=None) register_factory(ManyToManyRel, factory=None) register_factory(ManyToManyField, shortcut_name='many_to_many') register_factory(ForeignKey, shortcut_name='foreign_key') # Column specific register_column_factory(BooleanField, shortcut_name='boolean') register_column_factory(TextField, shortcut_name='text') # Variable specific register_variable_factory(URLField, shortcut_name='url') register_variable_factory(BooleanField, shortcut_name='boolean') register_variable_factory(TextField, shortcut_name='text') # Field specific register_field_factory(URLField, shortcut_name='url') register_field_factory(BooleanField, factory=lambda model_field, **kwargs: (Shortcut(call_target__attribute='boolean') if not model_field.null else Shortcut( call_target__attribute='boolean_tristate'))) register_field_factory(TextField, shortcut_name='textarea') register_field_factory(FileField, shortcut_name='file')
def test_namespace_shortcut_overwrite_backward(): actual = Namespace(Namespace(x=Namespace(y__z=1, y__zz=2)), Namespace(x=Shortcut(a__b=3))) expected = Namespace(x__a__b=3, x__y__z=1, x__y__zz=2) assert expected == actual
class Foo: a = Shortcut(x=1)
def test_retain_shortcut_type(): assert isinstance(Shortcut(foo=Shortcut()).foo, Shortcut) assert isinstance(Shortcut(foo=Shortcut(bar=Shortcut())).foo.bar, Shortcut) assert Shortcut(foo__bar__q=1, foo=Shortcut(bar=Shortcut())).foo.bar.q == 1
class Foo(object): a = Shortcut(x=1)
def setup_db_compat_django(): from iommi.form import register_field_factory from iommi.query import register_filter_factory from iommi.table import register_column_factory from django.db.models import ( AutoField, BinaryField, BooleanField, CharField, DateField, DateTimeField, DecimalField, EmailField, FileField, FilePathField, FloatField, ForeignKey, GenericIPAddressField, ImageField, IntegerField, ManyToManyField, ManyToManyRel, ManyToOneRel, TextField, TimeField, URLField, UUIDField, ) def char_field_factory(model_field, **_): if not model_field.choices: return Shortcut(call_target__attribute='text') display_name_by_choice = dict(model_field.choices) return Shortcut( call_target__attribute='choice', choices=[x[0] for x in model_field.choices], choice_display_name_formatter=lambda choice, **_: display_name_by_choice[choice] ) # The order here is significant because of inheritance structure. More specific must be below less specific. register_factory(CharField, factory=char_field_factory) register_factory(UUIDField, shortcut_name='text') register_factory(TimeField, shortcut_name='time') register_factory(EmailField, shortcut_name='email') register_factory(DecimalField, shortcut_name='decimal') register_factory(DateField, shortcut_name='date') register_factory(DateTimeField, shortcut_name='datetime') register_factory(FloatField, shortcut_name='float') register_factory(IntegerField, shortcut_name='integer') register_factory(FileField, shortcut_name='file') register_factory(AutoField, factory=Shortcut(call_target__attribute='integer', include=False)) register_factory(ManyToOneRel, factory=None) register_factory(ManyToManyRel, factory=None) register_factory(ManyToManyField, shortcut_name='many_to_many') register_factory(ForeignKey, shortcut_name='foreign_key') register_factory(GenericIPAddressField, shortcut_name='text') register_factory(FilePathField, shortcut_name='text') register_factory(BinaryField, factory=None) # Column specific register_column_factory(BooleanField, shortcut_name='boolean') register_column_factory(TextField, shortcut_name='text') # Filter specific register_filter_factory(URLField, shortcut_name='url') register_filter_factory(BooleanField, shortcut_name='boolean') register_filter_factory(TextField, shortcut_name='text') # Field specific register_field_factory(ImageField, shortcut_name='image') register_field_factory(URLField, shortcut_name='url') register_field_factory( BooleanField, factory=lambda model_field, **kwargs: ( Shortcut(call_target__attribute='boolean') if not model_field.null else Shortcut(call_target__attribute='boolean_tristate') ) ) register_field_factory(TextField, shortcut_name='textarea') register_field_factory(FileField, shortcut_name='file')
def test_namespace_shortcut_overwrite(): assert Namespace( Namespace(x=Shortcut(y__z=1, y__zz=2), ), Namespace(x=Namespace(a__b=3), ), ) == Namespace(x__a__b=3, )