예제 #1
0
def test_nested_value(backend):
    class MyMovie(Movie):
        class Meta(Movie.Meta):
            dbref_includes = ["actor.name"]

    actor = Actor({'name': 'Robert de Niro'})
    movie = MyMovie({'actor': actor})
    actor.movies = [movie]

    backend.save(movie)
    backend.commit()

    recovered_actor = backend.get(Actor, {'pk': actor.pk})

    assert recovered_actor.movies[0]._lazy
    assert set(recovered_actor.movies[0].lazy_attributes.keys()) == set(
        ['pk', 'actor_name'])

    assert recovered_actor.movies[0].actor_name == actor.name
    assert recovered_actor.movies[0]['actor_name'] == actor.name

    assert recovered_actor.movies[0]._lazy

    #Now we request the name attribute of the actor (which is not lazy), which triggers a revert
    assert recovered_actor.movies[0].actor.name == actor.name
    assert recovered_actor.movies[0].actor['name'] == actor.name
    assert recovered_actor.movies[0]._lazy == False

    with pytest.raises(AttributeError):
        assert recovered_actor.movies[0].actor_name

    with pytest.raises(KeyError):
        assert recovered_actor.movies[0]['actor_name']
예제 #2
0
def test_query_set(backend):

    actors = [
        Actor({
            'name': 'bar',
            'is_funny': True
        }),
        Actor({
            'name': 'baz',
            'is_funny': False
        }),
        Actor({
            'name': 'baz',
            'is_funny': False
        }),
        Actor({
            'name': 'bar',
            'is_funny': False
        })
    ]

    for actor in actors:
        backend.save(actor)

    backend.commit()

    queryset = backend.filter(Actor, {'name': 'bar', 'is_funny': True})

    assert queryset.next() == actors[0]
예제 #3
0
def test_query_set(backend):

    actors = [
        Actor({
            'foo': 'bar',
            'value': 10
        }),
        Actor({
            'foo': 'baz',
            'value': 10
        }),
        Actor({
            'foo': 'baz',
            'value': 11
        }),
        Actor({
            'foo': 'bar',
            'value': 11
        })
    ]

    for actor in actors:
        backend.save(actor)

    backend.commit()

    queryset = backend.filter(Actor, {'foo': 'bar', 'value': 10})

    assert queryset.next() == actors[0]
예제 #4
0
def test_nested_value(backend):

    class MyMovie(Movie):

        class Meta(Movie.Meta):
            dbref_includes = ["actor.name"]

    actor = Actor({'name' : 'Robert de Niro'})
    movie = MyMovie({'actor' : actor})
    actor.movies = [movie]

    backend.save(movie)
    backend.commit()

    recovered_actor = backend.get(Actor,{'pk' : actor.pk})

    assert recovered_actor.movies[0]._lazy
    assert set(recovered_actor.movies[0].lazy_attributes.keys()) == set(['pk','actor_name'])

    assert recovered_actor.movies[0].actor_name == actor.name
    assert recovered_actor.movies[0]['actor_name'] == actor.name

    assert recovered_actor.movies[0]._lazy

    #Now we request the name attribute of the actor (which is not lazy), which triggers a revert
    assert recovered_actor.movies[0].actor.name == actor.name
    assert recovered_actor.movies[0].actor['name'] == actor.name
    assert recovered_actor.movies[0]._lazy == False

    with pytest.raises(AttributeError):
        assert recovered_actor.movies[0].actor_name

    with pytest.raises(KeyError):
        assert recovered_actor.movies[0]['actor_name']
예제 #5
0
def test_regex_operator(backend, small_test_data):

    backend.filter(Actor, {}).delete()
    marlon_brando = Actor({
        'name': 'Marlon Brando',
        'gross_income_m': 1.453,
        'appearances': 78,
        'is_funny': False,
        'birth_year': 1924
    })
    marlon_wayans = Actor({'name': 'Marlon Wayans'})
    backend.save(marlon_brando)
    backend.save(marlon_wayans)
    backend.commit()

    assert backend.get(Actor,
                       {'name': {
                           '$regex': r'^Marlon\s+(?!Wayans)[\w]+$'
                       }}) == marlon_brando
    assert len(backend.filter(Actor, {'name': {
        '$regex': r'^Marlon\s+.*$'
    }})) == 2
    assert len(backend.filter(Actor, {'name': {
        '$regex': r'^.*\s+Brando$'
    }})) == 1
def test_indexed_delete(backend):

    movie = Movie({'name' : 'The Godfather'})
    actor = Actor({'name' : 'Marlon Brando'})
    actor.performances = [movie]
    movie.cast = {'Don Corleone' : actor}

    movie.save(backend)
예제 #7
0
def test_nonunique_file_backend_index(file_backend):
    file_backend.create_index(Actor, fields={'yob': 1})
    actor1 = Actor({'yob': 1})
    file_backend.save(actor1)
    actor2 = Actor({'yob': 1})
    file_backend.save(actor2)
    file_backend.commit()
    assert actor1.pk != actor2.pk
예제 #8
0
def test_unique_file_backend_index(file_backend):
    file_backend.create_index(Actor, fields={'num_films': 1}, unique=True)
    actor1 = Actor({'num_films': 1})
    file_backend.save(actor1)
    actor2 = Actor({'num_films': 1})
    file_backend.save(actor2)
    with pytest.raises(NonUnique):
        file_backend.commit()
예제 #9
0
def test_indexed_delete(backend):

    movie = Movie({'name': 'The Godfather'})
    actor = Actor({'name': 'Marlon Brando'})
    actor.performances = [movie]
    movie.cast = {'Don Corleone': actor}

    movie.save(backend)
예제 #10
0
def test_delete(backend):

    stallone = Actor({'name': u'ßílvöster Ställöne'})
    arnie = Actor({'name': u'Arnöld Schwürzenöggär'})

    backend.save(stallone)
    backend.save(arnie)
    backend.commit()

    assert backend.get(Actor, {'name': stallone.name}) == stallone
    assert backend.get(Actor, {'name': arnie.name}) == arnie
예제 #11
0
def test_and_queries(backend):

    backend.save(Actor({'foo': 'bar', 'value': 10}))
    backend.save(Actor({'foo': 'baz', 'value': 10}))
    backend.save(Actor({'foo': 'baz', 'value': 11}))
    backend.save(Actor({'foo': 'bar', 'value': 11}))

    backend.commit()

    assert len(backend.filter(Actor, {'foo': 'bar'})) == 2
    assert len(backend.filter(Actor, {'value': 10})) == 2
    assert len(backend.filter(Actor, {'foo': 'bar', 'value': 10})) == 1
    assert len(backend.filter(Actor, {'foo': 'baz', 'value': 10})) == 1
    assert len(backend.filter(Actor, {'foo': 'bar', 'value': 11})) == 1
    assert len(backend.filter(Actor, {'foo': 'baz', 'value': 11})) == 1
예제 #12
0
def test_and_queries(backend):

    backend.save(Actor({'name': 'bar', 'is_funny': False}))
    backend.save(Actor({'name': 'baz', 'is_funny': False}))
    backend.save(Actor({'name': 'baz', 'is_funny': True}))
    backend.save(Actor({'name': 'bar', 'is_funny': True}))

    backend.commit()

    assert len(backend.filter(Actor, {'name': 'bar'})) == 2
    assert len(backend.filter(Actor, {'is_funny': False})) == 2
    assert len(backend.filter(Actor, {'name': 'bar', 'is_funny': False})) == 1
    assert len(backend.filter(Actor, {'name': 'baz', 'is_funny': False})) == 1
    assert len(backend.filter(Actor, {'name': 'bar', 'is_funny': False})) == 1
    assert len(backend.filter(Actor, {'name': 'baz', 'is_funny': False})) == 1
예제 #13
0
def test_update_with_dict(backend):

    if isinstance(backend, FileBackend):
        return

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    backend.save(actor)
    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    backend.update(actor, {'name': 'Ian McKellan'})

    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Ian McKellan'})) == 1

    assert actor.name == 'Ian McKellan'

    backend.update(actor, {'name': 'Roger Moore'}, update_obj=False)

    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Roger Moore'})) == 1

    assert actor.name == 'Ian McKellan'
예제 #14
0
def test_update_on_deleted_document_fails(mongodb_backend):

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    mongodb_backend.save(actor)
    mongodb_backend.commit()

    assert len(mongodb_backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    mongodb_backend.delete(actor)

    actor.name = 'Patrick Stewart'
    actor.age = 50

    with pytest.raises(actor.DoesNotExist):
        mongodb_backend.update(actor, ('name',))
예제 #15
0
def test_delete(backend):

    stallone = Actor({'name' : 'Silvester Stallone'})
    arnie = Actor({'name' : 'Arnold Schwarzenegger'})

    backend.save(stallone)
    backend.save(arnie)
    backend.commit()

    assert backend.get(Actor,{'name' : 'Silvester Stallone'}) == stallone
    assert backend.get(Actor,{'name' : 'Arnold Schwarzenegger'}) == arnie

    with pytest.raises(Actor.DoesNotExist):
        actor = backend.get(Actor,{'name' : 'Eddie Murphy'})

    with pytest.raises(Actor.MultipleDocumentsReturned):
        actor = backend.get(Actor,{})
예제 #16
0
def test_unique_file_backend_index():
    backend = file_backend(None,
                           tempfile.mkdtemp(), {'serializer_class': 'pickle'},
                           autoload_embedded=True)
    backend.create_index(Actor, fields={'num_films': 1}, unique=True)
    actor1 = Actor({'num_films': 1})
    actor1.save(backend)
    actor2 = Actor({'num_films': 1})
    actor2.save(backend)
    with pytest.raises(NonUnique):
        backend.commit()
예제 #17
0
def test_nonunique_file_backend_index():
    backend = file_backend(None,
                           tempfile.mkdtemp(), {'serializer_class': 'pickle'},
                           autoload_embedded=True)
    backend.create_index(Actor, fields={'yob': 1})
    actor1 = Actor({'yob': 1})
    actor1.save(backend)
    actor2 = Actor({'yob': 1})
    actor2.save(backend)
    backend.commit()
    assert actor1.pk != actor2.pk
예제 #18
0
def test_update_on_deleted_document_fails(backend):

    if isinstance(backend,FileBackend):
        return

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    backend.save(actor)
    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    backend.delete(actor)

    actor.name = 'Patrick Stewart'
    actor.age = 50

    with pytest.raises(actor.DoesNotExist):
        backend.update(actor, ('name',))
예제 #19
0
def test_update_on_deleted_document_fails(backend):

    if isinstance(backend, FileBackend):
        return

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    backend.save(actor)
    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    backend.delete(actor)

    actor.name = 'Patrick Stewart'
    actor.age = 50

    with pytest.raises(actor.DoesNotExist):
        backend.update(actor, ('name', ))
예제 #20
0
def test_update_by_list(mongodb_backend):

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    mongodb_backend.save(actor)
    mongodb_backend.commit()

    assert len(mongodb_backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    actor.name = 'Patrick Stewart'
    actor.age = 50

    mongodb_backend.update(actor, ('name',))

    mongodb_backend.commit()

    assert len(mongodb_backend.filter(Actor, {'name': 'Robert de Niro'})) == 0
    assert len(mongodb_backend.filter(Actor, {'name': 'Patrick Stewart'})) == 1

    assert len(mongodb_backend.filter(Actor, {'age': 50})) == 0
예제 #21
0
def test_keys_with_dots(backend):

    actor = Actor({
        'some.key.with.nasty.dots': [{
            'some.more.nasty.dots': 100
        }],
        'pk': 'test'
    })

    backend.save(actor)
    backend.commit()

    assert actor == backend.get(Actor, {'pk': 'test'})
예제 #22
0
def test_nested_value(backend):

    actor = Actor({'name' : 'Robert de Niro'})
    movie = Movie({'best_actor' : actor,'title' : 'The Godfather'})

    backend.save(actor)
    actor.movies = [movie]
    backend.save(actor)
    backend.commit()
    backend.save(movie)
    backend.commit()

    recovered_actor = backend.get(Actor,{'pk' : actor.pk})

    assert recovered_actor == actor
    assert movie in recovered_actor.movies
    assert recovered_actor.movies[0] == movie
    assert 'best_actor' in recovered_actor.movies[0]
    assert recovered_actor.movies[0].best_actor == recovered_actor

    recovered_actors = backend.filter(Actor,{'movies.title' : 'The Godfather'})
    assert len(recovered_actors) == 1
    assert actor in recovered_actors
예제 #23
0
def test_update_non_indexed_field(backend):

    if isinstance(backend, FileBackend):
        return

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    backend.save(actor)
    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    actor.name = 'Patrick Stewart'
    actor.age = 50

    backend.update(actor, ('name', 'age'))

    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 0
    assert len(backend.filter(Actor, {'name': 'Patrick Stewart'})) == 1

    assert backend.get(Actor, {'name': 'Patrick Stewart'}).age == 50
예제 #24
0
def test_update_non_indexed_field(backend):

    if isinstance(backend,FileBackend):
        return

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    backend.save(actor)
    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    actor.name = 'Patrick Stewart'
    actor.age = 50

    backend.update(actor, ('name','age'))

    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 0
    assert len(backend.filter(Actor, {'name': 'Patrick Stewart'})) == 1

    assert backend.get(Actor, {'name': 'Patrick Stewart'}).age == 50
예제 #25
0
def test_update_by_list(backend):

    if isinstance(backend,FileBackend):
        return

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    backend.save(actor)
    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    actor.name = 'Patrick Stewart'
    actor.age = 50

    backend.update(actor, ('name',))

    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 0
    assert len(backend.filter(Actor, {'name': 'Patrick Stewart'})) == 1

    #we did not update the age field...
    assert backend.get(Actor, {'name': 'Patrick Stewart'}).age == 54
예제 #26
0
def test_update_by_list(backend):

    if isinstance(backend, FileBackend):
        return

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    backend.save(actor)
    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    actor.name = 'Patrick Stewart'
    actor.age = 50

    backend.update(actor, ('name', ))

    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 0
    assert len(backend.filter(Actor, {'name': 'Patrick Stewart'})) == 1

    #we did not update the age field...
    assert backend.get(Actor, {'name': 'Patrick Stewart'}).age == 54
예제 #27
0
def test_unique_file_backend_index():
    backend = file_backend(None, tempfile.mkdtemp(),
                           {'serializer_class': 'pickle'},
                           autoload_embedded=True)
    backend.create_index(Actor, fields={'num_films': 1}, unique=True)
    actor1 = Actor({'num_films': 1})
    actor1.save(backend)
    actor2 = Actor({'num_films': 1})
    actor2.save(backend)
    with pytest.raises(NonUnique):
        backend.commit()
예제 #28
0
def test_nonunique_file_backend_index():
    backend = file_backend(None, tempfile.mkdtemp(),
                           {'serializer_class': 'pickle'},
                           autoload_embedded=True)
    backend.create_index(Actor, fields={'yob': 1})
    actor1 = Actor({'yob': 1})
    actor1.save(backend)
    actor2 = Actor({'yob': 1})
    actor2.save(backend)
    backend.commit()
    assert actor1.pk != actor2.pk
예제 #29
0
def test_update_by_list(no_autoload_mongodb_backend):
    class MyMovie(Movie):
        class Meta(Movie.Meta):
            dbref_includes = ["year"]

    actor = Actor({
        'name':
        'Robert de Niro',
        'age':
        54,
        'movies':
        [MyMovie({
            'name': 'The Godfather',
            'year': 1987,
            'rating': 'AAA'
        })]
    })

    no_autoload_mongodb_backend.save(actor)
    no_autoload_mongodb_backend.commit()

    assert len(
        no_autoload_mongodb_backend.filter(Actor,
                                           {'name': 'Robert de Niro'})) == 1

    recovered_actor = no_autoload_mongodb_backend.get(
        Actor, {'name': 'Robert de Niro'})

    assert recovered_actor.movies[0]._lazy == True
    assert recovered_actor.movies[0].lazy_attributes == {
        'year': 1987,
        'pk': actor.movies[0].pk
    }

    with pytest.raises(AttributeError):
        recovered_actor.movies[0].rating

    assert recovered_actor.movies[0].year == 1987

    recovered_actor.movies[0].load_if_lazy()

    assert recovered_actor.movies[0].rating == 'AAA'
예제 #30
0
def test_update_unset(backend):

    if isinstance(backend, FileBackend):
        return

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    backend.save(actor)
    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    backend.update(actor, unset_fields=['name'])

    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Ian McKellan'})) == 0

    recovered_actor = backend.get(Actor, {'pk': actor.pk})

    assert recovered_actor.get('name') is None
예제 #31
0
def test_update_unset_then_set(backend):

    if isinstance(backend, FileBackend):
        return

    actor = Actor({'name': 'Robert de Niro', 'age': 54})

    backend.save(actor)
    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Robert de Niro'})) == 1

    backend.update(actor, unset_fields=['name'])
    backend.update(actor, set_fields={'name': 'Patrick Stewart'})

    backend.commit()

    assert len(backend.filter(Actor, {'name': 'Patrick Stewart'})) == 1

    recovered_actor = backend.get(Actor, {'pk': actor.pk})

    assert recovered_actor.name == 'Patrick Stewart'
예제 #32
0
def test_delete(backend):

    actor = Actor({'foo': 'bar'})

    backend.save(actor)
    backend.commit()

    assert actor.foo == 'bar'

    assert backend.get(Actor, {'pk': actor.pk}).foo == 'bar'

    del actor.foo

    with pytest.raises(AttributeError):
        actor.foo

    with pytest.raises(KeyError):
        actor['foo']

    backend.save(actor)
    backend.commit()

    with pytest.raises(AttributeError):
        backend.get(Actor, {'pk': actor.pk}).foo
예제 #33
0
def test_operators(backend):

    backend.filter(Actor, {}).delete()

    marlon_brando = Actor({
        'name': 'Marlon Brando',
        'gross_income_m': 1.453,
        'appearances': 78,
        'is_funny': False,
        'birth_year': 1924
    })
    leonardo_di_caprio = Actor({
        'name': 'Leonardo di Caprio',
        'gross_income_m': 12.453,
        'appearances': 34,
        'is_funny': False,
        'birth_year': 1974
    })
    david_hasselhoff = Actor({
        'name': 'David Hasselhoff',
        'gross_income_m': 12.453,
        'appearances': 173,
        'is_funny': True,
        'birth_year': 1952
    })
    charlie_chaplin = Actor({
        'name': 'Charlie Chaplin',
        'gross_income_m': 0.371,
        'appearances': 473,
        'is_funny': True,
        'birth_year': 1889
    })

    backend.save(marlon_brando)
    backend.save(leonardo_di_caprio)
    backend.save(david_hasselhoff)
    backend.save(charlie_chaplin)

    backend.commit()

    assert len(backend.filter(Actor, {})) == 4

    for op, results in (('$gt', [david_hasselhoff]), ('$gte', [
            david_hasselhoff
    ]), ('$lt', [charlie_chaplin]), ('$lte', [charlie_chaplin])):

        query = {'$and': [{'gross_income_m': {op: 1.0}}, {'is_funny': True}]}

        assert len(backend.filter(Actor, query)) == len(results)
        assert results in backend.filter(Actor, query)

    for op, results in (('$gt', [
            david_hasselhoff, charlie_chaplin, marlon_brando
    ]), ('$gte', [marlon_brando, david_hasselhoff, charlie_chaplin
                  ]), ('$lt', [charlie_chaplin]), ('$lte', [charlie_chaplin])):

        query = {
            '$and': [
                {
                    '$or': [
                        {
                            'gross_income_m': {
                                op: 1.0
                            }
                        },
                        {
                            'birth_year': {
                                '$lt': 1900
                            }
                        },
                    ]
                },
                {
                    '$or': [
                        {
                            'is_funny': True
                        },
                        {
                            'name': 'Marlon Brando'
                        },
                    ]
                },
            ]
        }

        assert len(backend.filter(Actor, query)) == len(results)
        assert results in backend.filter(Actor, query)

    assert len(backend.filter(Actor, {'name': {
        '$ne': 'David Hasselhoff'
    }})) == 3
    assert len(backend.filter(Actor, {'name': 'David Hasselhoff'})) == 1
    assert len(
        backend.filter(
            Actor, {
                'name': {
                    '$not': {
                        '$in': [
                            'David Hasselhoff', 'Marlon Brando',
                            'Charlie Chaplin'
                        ]
                    }
                }
            })) == 1
    assert len(
        backend.filter(
            Actor, {'name': {
                '$in': ['Marlon Brando', 'Leonardo di Caprio']
            }})) == 2
예제 #34
0
def test_composite_queries(backend):

    backend.filter(Actor, {}).delete()

    backend.save(Actor({'values': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}))
    backend.save(Actor({'values': [7, 6, 5, 4, 3, 2, 1]}))
    backend.save(Actor({'values': [1, 2, 3, 4]}))
    backend.save(Actor({'values': [1, 2, 3, 4, {'foo': 'bar'}]}))
    backend.save(Actor({'values': 'foobar'}))

    backend.commit()

    for f in (lambda: True, lambda: backend.create_index(Actor, 'values')):

        assert len(backend.filter(Actor, {})) == 5
        assert len(backend.filter(Actor, {'values': [1, 2, 3, 4]})) == 1
        assert len(
            backend.filter(Actor, {'values': [1, 2, 3, 4, {
                'foo': 'bar'
            }]})) == 1
        assert len(
            backend.filter(Actor, {'values': [1, 2, 3, {
                'foo': 'bar'
            }, 4]})) == 0
        assert len(backend.filter(Actor, {'values': [1, 2, 3, 4, 5]})) == 0
        assert len(
            backend.filter(Actor,
                           {'values': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]})) == 0

        assert len(backend.filter(Actor, {'values': {
            '$all': [4, 3, 2, 1]
        }})) == 4
        assert len(
            backend.filter(Actor,
                           {'values': {
                               '$all': [4, 3, 2, 1, {
                                   'foo': 'bar'
                               }]
                           }})) == 1
        assert len(
            backend.filter(Actor, {'values': {
                '$all': [{
                    'foo': 'bar'
                }]
            }})) == 1
        assert len(
            backend.filter(Actor, {'values': {
                '$all': [4, 3, 2, 1, 14]
            }})) == 0
        assert len(
            backend.filter(
                Actor, {'values': {
                    '$all': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
                }})) == 1
        assert len(
            backend.filter(
                Actor, {
                    'values': {
                        '$in': [[1, 2, 3, 4], [7, 6, 5, 4, 3, 2, 1],
                                [1, 2, 3, 5], 'foobar']
                    }
                })) == 3
예제 #35
0
def test_basic_sorting(backend):

    backend.filter(Actor, {}).delete()

    backend.save(Actor({'birth_year': 1983}))
    backend.save(Actor({'birth_year': 1983}))
    backend.save(Actor({'birth_year': 1984}))
    backend.save(Actor({'birth_year': 1984}))
    backend.save(Actor({'birth_year': 1984}))
    backend.save(Actor({'birth_year': 1985}))
    backend.save(Actor({'birth_year': 1980}))
    backend.save(Actor({'birth_year': 1990}))
    backend.save(Actor({'birth_year': 2000}))
    backend.save(Actor({'birth_year': 2000}))
    backend.save(Actor({'birth_year': 1900}))
    backend.save(Actor({'birth_year': 1843}))
    backend.save(Actor({'birth_year': 2014}))

    backend.commit()

    actors = backend.filter(Actor, {}).sort('birth_year')
    for i in range(1, len(actors)):
        assert actors[i - 1].birth_year <= actors[i].birth_year

    actors = backend.filter(Actor, {}).sort('birth_year', -1)
    for i in range(1, len(actors)):
        assert actors[i - 1].birth_year >= actors[i].birth_year

    actors = backend.filter(Actor, {}).sort([('birth_year', -1)])
    for i in range(1, len(actors)):
        assert actors[i - 1].birth_year >= actors[i].birth_year

    actors = backend.filter(Actor, {}).sort([('birth_year', -1)])
    for i in range(1, len(actors)):
        assert actors[i - 1].birth_year >= actors[i].birth_year

    """
    Objects with missing sort keys should be returned first when
    sorting in ascending order, else last.
    """

    actor_wo_birth_year = Actor({})

    backend.save(actor_wo_birth_year)
    backend.commit()
    actors = backend.filter(Actor, {}).sort([('birth_year', 1)])
    assert actors[0] == actor_wo_birth_year
예제 #36
0
def test_basic_sorting(backend):

    backend.filter(Actor, {}).delete()

    backend.save(Actor({'birth_year': 1983}))
    backend.save(Actor({'birth_year': 1983}))
    backend.save(Actor({'birth_year': 1984}))
    backend.save(Actor({'birth_year': 1984}))
    backend.save(Actor({'birth_year': 1984}))
    backend.save(Actor({'birth_year': 1985}))
    backend.save(Actor({'birth_year': 1980}))
    backend.save(Actor({'birth_year': 1990}))
    backend.save(Actor({'birth_year': 2000}))
    backend.save(Actor({'birth_year': 2000}))
    backend.save(Actor({'birth_year': 1900}))
    backend.save(Actor({'birth_year': 1843}))
    backend.save(Actor({'birth_year': 2014}))

    backend.commit()

    actors = backend.filter(Actor, {}).sort([('birth_year', -1)])
    for i in range(1, len(actors)):
        assert actors[i - 1].birth_year >= actors[i].birth_year
    """
    Objects with missing sort keys should be returned first when
    sorting in ascending order, else last.
    """

    actor_wo_birth_year = Actor({})

    backend.save(actor_wo_birth_year)
    backend.commit()
    actors = list(backend.filter(Actor, {}).sort([('birth_year', 1)]))
    assert actor_wo_birth_year in actors
    #SQL backends can produce ambigous results depending on how NULLS FIRST is implemented
    #this varies e.g. between Postgres and SQLite (which does not even support NULLS FIRST)
    if not isinstance(backend, SqlBackend):
        assert actors[0] == actor_wo_birth_year