def test_one_not_and_lookup(): """ Test one normal and one `not` lookup. """ lookup = L('name', 'spindle') & ~L('country', 'netherlands') expected = { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, { L.NOT: { L.AND: [ { FIELD_KEY: 'country', VALUE_KEY: 'netherlands', LOOKUP_KEY: 'exact' }, ] } }, ] } assert lookup.to_dict() == expected
def test_one_not_and_lookup(): """ Test one normal and one `not` lookup. """ lookup = L('name', 'spindle') & ~L('country', 'netherlands') expected = Q(name='spindle') & ~Q(country='netherlands') _do_comparison_test(lookup, expected)
def test_dumps(): """ Test dumping of a lookup. """ # The JSON dumps is unordered so try all possible combinations. lookup = L('name', 'spindle') result = lookup.dumps() assert lookup.to_dict() == json.loads(result) assert lookup.to_dict() == L.from_json(result).to_dict()
def test_one_lookup(): """ Test one normal lookup. """ lookup = L('name', 'spindle') expected = Q(name='spindle') _do_comparison_test(lookup, expected) lookup = L('name', True, lookup=ISNULL) expected = Q(name__isnull=True) _do_comparison_test(lookup, expected)
def test_all_not_and_lookup(): """ Test all lookups with `not`. """ lookup = ~(L('name', 'spindle') & L('country', 'netherlands')) expected = { L.NOT: { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'country', VALUE_KEY: 'netherlands', LOOKUP_KEY: 'exact' }, ] } } assert lookup.to_dict() == expected lookup = ~(L('name', 'spindle') & L('country', 'netherlands') & L('status', 'awesome')) expected = { L.NOT: { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'country', VALUE_KEY: 'netherlands', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'status', VALUE_KEY: 'awesome', LOOKUP_KEY: 'exact' }, ] } } assert lookup.to_dict() == expected
def test_or_lookup(): """ Test the `or` lookup. """ lookup = L('name', 'spindle') | L('name', 'devhouse') expected = Q(name='spindle') | Q(name='devhouse') _do_comparison_test(lookup, expected) lookup = L('name', 'spindle') | L('name', 'devhouse') | L( 'name', 'devhouse spindle') expected = Q(name='spindle') | Q(name='devhouse') | Q( name='devhouse spindle') _do_comparison_test(lookup, expected)
def test_and_lookup(): """ Test the `and` lookup. """ lookup = L('name', 'spindle') & L('country', 'netherlands') expected = Q(name='spindle') & Q(country='netherlands') _do_comparison_test(lookup, expected) lookup = L('name', 'spindle') & L('country', 'netherlands') & L( 'status', 'awesome') expected = Q(name='spindle') & Q(country='netherlands') & Q( status='awesome') _do_comparison_test(lookup, expected)
def test_all_not_and_lookup(): """ Test all lookups with `not`. """ lookup = ~(L('name', 'spindle') & L('country', 'netherlands')) expected = ~(Q(name='spindle') & Q(country='netherlands')) _do_comparison_test(lookup, expected) lookup = ~(L('name', 'spindle') & L('country', 'netherlands') & L('status', 'awesome')) expected = ~(Q(name='spindle') & Q(country='netherlands') & Q(status='awesome')) _do_comparison_test(lookup, expected)
def test_one_not_lookup(): """ Test one `not` lookup. """ lookup = ~L('name', 'spindle') expected = ~Q(name='spindle') _do_comparison_test(lookup, expected)
def test_and_lookup(): """ Test the `and` lookup. """ lookup = L('name', 'spindle') & L('country', 'netherlands') expected = { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'country', VALUE_KEY: 'netherlands', LOOKUP_KEY: 'exact' }, ] } assert lookup.to_dict() == expected lookup = L('name', 'spindle') & L('country', 'netherlands') & L( 'status', 'awesome') expected = { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'country', VALUE_KEY: 'netherlands', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'status', VALUE_KEY: 'awesome', LOOKUP_KEY: 'exact' }, ] } assert lookup.to_dict() == expected
def test_or_lookup(): """ Test the `or` lookup. """ lookup = L('name', 'spindle') | L('name', 'devhouse') expected = { L.OR: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'name', VALUE_KEY: 'devhouse', LOOKUP_KEY: 'exact' }, ] } assert lookup.to_dict() == expected lookup = L('name', 'spindle') | L('name', 'devhouse') | L( 'name', 'devhouse spindle') expected = { L.OR: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'name', VALUE_KEY: 'devhouse', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'name', VALUE_KEY: 'devhouse spindle', LOOKUP_KEY: 'exact' }, ] } assert lookup.to_dict() == expected
def test_special_type_lookup(): """ Test converting lookups with the special Decimal type. """ pi = '3.141592653589793238462643383279502884197169399375' lookup = L('pi', pi) expected = Q(pi=pi) _do_comparison_test(lookup, expected)
def test_invalid_value_for_lookup(): """ Test invalid value for a lookup. """ message = 'Only True or False allowed for ISNULL lookup type.' with raises(InvalidValueException) as excinfo: L('name', 'null', lookup=ISNULL) assert str(excinfo.value) == message
def test_invalid_lookup_type(): """ Test a lookup with a invalid type. """ message = '`spindle` is not a supported lookup! Supported lookups are: %s' % LOOKUP_TYPES with raises(UnsupportedLookupException) as excinfo: L('name', 'spindle', lookup='spindle') assert str(excinfo.value) == message
def test_adding_type_to_filter(): """ Test if type is added to filter for unsupported json types. """ lookup = L('date', date(2017, 6, 6)) _filter = lookup.filters[0] _type = _filter.get(TYPE_KEY) assert _type assert _type == date.__name__
def test_duplicate_lookup(): """ Test when combining exactly the same lookups. """ lookup = L('name', 'spindle') & L('name', 'spindle') expected = { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, ] } assert lookup.to_dict() == expected lookup = L('name', 'spindle') & L('name', 'spindle') & L('name', 'spindle') assert lookup.to_dict() == expected
def test_complex_mix_lookup(): """ Test two complex mixed lookups with `not's` `and's` and `or's`. """ lookup = (~L('name', 'spindle') & L('status', 'awesome')) & (L( 'country', 'netherlands') | L('country', 'germany')) expected = (~Q(name='spindle') & Q(status='awesome')) & ( Q(country='netherlands') | Q(country='germany')) _do_comparison_test(lookup, expected) lookup = (L('name', 'spindle') & L('status', 'awesome') ) | ~(L('country', 'netherlands') | L('country', 'germany')) expected = (Q(name='spindle') & Q( status='awesome')) | ~(Q(country='netherlands') | Q(country='germany')) _do_comparison_test(lookup, expected)
def test_one_lookup(): """ Test one normal lookup. """ lookup = L('name', 'spindle') expected = { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, ] } assert lookup.to_dict() == expected
def test_one_not_lookup(): """ Test one `not` lookup. """ lookup = ~L('name', 'spindle') expected = { L.NOT: { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, ] } } assert lookup.to_dict() == expected
def test_invalid_formats(): """ Test supplying invalid formats to from_dict. """ wrong_format = {L.AND: '', L.OR: ''} message = 'Lookup root can only have 1 key %s' % wrong_format with raises(InvalidFormat) as execinfo: L.from_dict(wrong_format) assert str(execinfo.value) == message wrong_format = {L.NOT: {L.AND: '', L.OR: ''}} message = 'Lookup root can only have 1 key %s' % {L.AND: '', L.OR: ''} with raises(InvalidFormat) as execinfo: L.from_dict(wrong_format) assert str(execinfo.value) == message wrong_format = {'unknown': ''} message = 'Lookup root connector must be `%s`, `%s` or `%s` %s' % ( L.AND, L.NOT, L.OR, wrong_format) with raises(InvalidFormat) as execinfo: L.from_dict(wrong_format) assert str(execinfo.value) == message wrong = {L.AND: 'wrong', L.OR: 'format'} wrong_format = {L.AND: [wrong]} message = 'Not a lookup or node for filter %s' % wrong with raises(InvalidFormat) as execinfo: L.from_dict(wrong_format) assert str(execinfo.value) == message
def test_and_or_lookup(): """ Test lookup with the use of `and` and `or`. """ lookup = L('name', 'spindle') & L('country', 'netherlands') | L( 'status', 'awesome') expected = Q(name='spindle') & Q(country='netherlands') | Q( status='awesome') _do_comparison_test(lookup, expected) lookup = L('status', 'awesome') | L('name', 'spindle') & L('country', 'netherlands') expected = Q( status='awesome') | Q(name='spindle') & Q(country='netherlands') _do_comparison_test(lookup, expected)
def test_combining_invalid_types(): """ Test combining invalid types. """ with raises(TypeError): L('name', 'spindle') & 1337
def test_complex_mix_lookup(): """ Test two complex mixed lookups with `not's` `and's` and `or's`. """ lookup = (~L('name', 'spindle') & L('status', 'awesome')) & (L( 'country', 'netherlands') | L('country', 'germany')) expected = { L.AND: [ { L.NOT: { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, ] } }, { FIELD_KEY: 'status', VALUE_KEY: 'awesome', LOOKUP_KEY: 'exact' }, { L.OR: [ { FIELD_KEY: 'country', VALUE_KEY: 'netherlands', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'country', VALUE_KEY: 'germany', LOOKUP_KEY: 'exact' }, ] }, ] } assert lookup.to_dict() == expected lookup = (L('name', 'spindle') & L('status', 'awesome') ) | ~(L('country', 'netherlands') | L('country', 'germany')) expected = { L.OR: [ { L.AND: [ { FIELD_KEY: 'name', VALUE_KEY: 'spindle', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'status', VALUE_KEY: 'awesome', LOOKUP_KEY: 'exact' }, ] }, { L.NOT: { L.AND: [{ L.OR: [ { FIELD_KEY: 'country', VALUE_KEY: 'netherlands', LOOKUP_KEY: 'exact' }, { FIELD_KEY: 'country', VALUE_KEY: 'germany', LOOKUP_KEY: 'exact' }, ] }] } }, ] } assert lookup.to_dict() == expected