示例#1
0
def test_invalid_value():
    request = Struct(method='GET', GET=Data(**{'query': 'bazaar=asd'}))
    # noinspection PyTypeChecker
    query2 = Query(request=request, variables=[Variable.integer(name='bazaar', value_to_q=lambda variable, op, value_string_or_f: None)])
    with pytest.raises(QueryException) as e:
        query2.to_q()
    assert 'Unknown value "asd" for variable "bazaar"' in str(e)
示例#2
0
def setup():
    from tri.query import Variable
    from django.db.models import IntegerField, FloatField, TextField, BooleanField, AutoField, CharField, CommaSeparatedIntegerField, DateField, DateTimeField, DecimalField, EmailField, URLField, TimeField, ForeignKey, ManyToOneRel, ManyToManyField, ManyToManyRel

    def foreign_key_factory(model_field, **kwargs):
        setdefaults(kwargs, dict(
            choices=model_field.foreign_related_fields[0].model.objects.all()
        ))
        kwargs['model'] = model_field.foreign_related_fields[0].model
        return Variable.choice_queryset(**kwargs)

    # The order here is significant because of inheritance structure. More specific must be below less specific.
    global _variable_factory_by_django_field_type
    _variable_factory_by_django_field_type = OrderedDict([
        (CharField, lambda model_field, **kwargs: Variable(**kwargs)),
        (URLField, lambda model_field, **kwargs: Variable.url(**kwargs)),
        (TimeField, lambda model_field, **kwargs: Variable.time(**kwargs)),
        (EmailField, lambda model_field, **kwargs: Variable.email(**kwargs)),
        (DecimalField, lambda model_field, **kwargs: Variable.decimal(**kwargs)),
        (DateField, lambda model_field, **kwargs: Variable.date(**kwargs)),
        (DateTimeField, lambda model_field, **kwargs: Variable.datetime(**kwargs)),
        (CommaSeparatedIntegerField, lambda model_field, **kwargs: Variable.comma_separated(parent_field=Variable.integer(**kwargs))),
        (BooleanField, lambda model_field, **kwargs: Variable.boolean(**kwargs)),
        (TextField, lambda model_field, **kwargs: Variable.text(**kwargs)),
        (FloatField, lambda model_field, **kwargs: Variable.float(**kwargs)),
        (IntegerField, lambda model_field, **kwargs: Variable.integer(**kwargs)),
        (AutoField, lambda model_field, **kwargs: Variable.integer(**setdefaults(kwargs, dict(show=False)))),
        (ManyToOneRel, None),
        (ManyToManyField, lambda model_field, **kwargs: Variable.multi_choice_queryset(**setdefaults(kwargs, dict(choices=model_field.rel.to._default_manager.all())))),
        (ManyToManyRel, None),
        # (ManyToManyRel_Related, None),
        (ForeignKey, foreign_key_factory),
    ])
示例#3
0
 def foreign_key_factory(model_field, **kwargs):
     setdefaults(kwargs, dict(
         choices=model_field.foreign_related_fields[0].model.objects.all()
     ))
     kwargs['model'] = model_field.foreign_related_fields[0].model
     return Variable.choice_queryset(**kwargs)
示例#4
0
def test_invalid_form_data():
    query2 = Query(request=Struct(method='GET', GET=Data(**{'bazaar': 'asds'})), variables=[Variable.integer(name='bazaar', attr='quux__bar__bazaar', gui__show=True)])
    # noinspection PyTypeChecker
    assert query2.to_query_string() == ''
    # noinspection PyTypeChecker
    assert repr(query2.to_q()) == repr(Q())