Esempio n. 1
0
def test_update():
    Car.delete_all()
    c = Car.update(make=u'ford', year=2010, model=u'test')
    assert Car.count() == 1
    Car.update(c.id, year=2011)
    assert Car.count() == 1
    assert c.year == 2011
Esempio n. 2
0
 def setup_class(cls):
     cls.ta = TestApp(ag.wsgi_test_app)
     Car.delete_all()
     Car.add(**{
         'make': u'chevy',
         'model': u'cav',
         'year': 2010
     })
Esempio n. 3
0
def test_count_and_delete_all():
    Car.delete_all()
    Car.add(**{'make': u'test', 'model': u'count', 'year': 2010})
    Car.add(**{'make': u'test', 'model': u'count', 'year': 2009})
    Car.add(**{'make': u'test', 'model': u'count2', 'year': 2010})
    assert Car.count() == 3
    assert Car.count_by(model=u'count') == 2
    assert Car.count_where(Car.model == u'count') == 2
    eq_(Car.delete_all(), 3)
Esempio n. 4
0
def test_get_by_and_where():
    Car.delete_all()
    Car.add(**{'make': u'chevy', 'model': u'astro', 'year': 1993})
    c = Car.get_by(make=u'chevy', model=u'astro', year=1993)
    assert c.model == 'astro'

    c = Car.get_where(Car.make == u'chevy', Car.year < 2000)
    assert c.model == 'astro'

    c = Car.get_where(Car.make == u'chevy', Car.year > 2000)
    assert c is None
Esempio n. 5
0
def test_delete_where():
    Car.delete_all()
    Car.add(**{'make': u'test', 'model': u'count', 'year': 2010})
    Car.add(**{'make': u'test', 'model': u'count', 'year': 2009})
    Car.add(**{'make': u'test', 'model': u'count2', 'year': 2010})

    # two clauses
    assert Car.delete_where(Car.model == u'count', Car.year == 2009) == 1
    assert Car.count() == 2

    # one clause
    assert Car.delete_where(Car.model == u'count2') == 1
Esempio n. 6
0
def test_edit():
    Car.delete_all()
    c1 = Car.add(**{'make': u'test', 'model': u'count', 'year': 2008})
    cid = c1.id
    Car.edit(c1.id, make=u'ford', year=2010)
    db.sess.remove()
    c = Car.first()
    assert c.make == 'ford'
    assert c.model == 'count'
    assert c.year == 2010
    c1 = Car.edit(year=2011, id=cid)
    assert c.make == 'ford'
    assert c.model == 'count'
    assert c.year == 2011

    try:
        c1 = Car.edit(year=2011)
        assert False
    except ValueError:
        pass
Esempio n. 7
0
def test_query_attribute():
    Car.delete_all()
    c = Car.add(**{'make': u'test', 'model': u'count', 'year': 2010})
    assert Car.query().count() == 1

    # default usage should return all attributes
    c = Car.query().first()
    assert isinstance(c, Car)
    assert c.id
    assert c.make
    assert c.model

    # sending attribute names should give us a recordset with just those
    # attributes
    res = Car.query('id', 'make').first()
    assert not isinstance(res, Car)
    assert res.id
    assert res.make
    try:
        assert res.model
        assert False, 'expected exception'
    except AttributeError:
        pass
Esempio n. 8
0
def test_lists_pairs_firsts():
    Car.delete_all()
    c1 = Car.add(**{'make': u'test', 'model': u'count', 'year': 2008})
    c2 = Car.add(**{'make': u'test', 'model': u'count', 'year': 2009})
    c3 = Car.add(**{'make': u'test', 'model': u'count2', 'year': 2010})

    result = Car.list()
    assert len(result) == 3
    assert result[2] is c3

    result = Car.list_by(model=u'count2')
    assert len(result) == 1
    assert result[0] is c3

    result = Car.list_where(Car.model == u'count2')
    assert len(result) == 1
    assert result[0] is c3

    # with order_by clauses
    result = Car.list(order_by=Car.year.desc())
    assert result[2] is c1

    # multiple values for order_by
    result = Car.list(order_by=(Car.model, Car.year.desc()))
    assert result[0] is c2, result

    # with order by
    result = Car.list_by(model=u'count', order_by=Car.year.desc())
    assert result[0] is c2

    # with order by
    result = Car.list_where(Car.model == u'count', order_by=Car.year.desc())
    assert result[0] is c2

    # with extra arg
    try:
        Car.list_where(Car.model == u'count',
                       order_by=Car.year.desc(),
                       erroneous='foo')
        assert False
    except ValueError:
        pass

    ###
    #   test pairs
    ###
    expect = [
        (c1.id, c1.year),
        (c2.id, c2.year),
        (c3.id, c3.year),
    ]
    result = Car.pairs('id:year')
    eq_(expect, result)

    expect = [
        (c1.model, c1.year),
        (c2.model, c2.year),
        (c3.model, c3.year),
    ]
    result = Car.pairs('model:year')
    eq_(expect, result)

    expect = [
        (c3.model, c3.year),
        (c2.model, c2.year),
        (c1.model, c1.year),
    ]
    result = Car.pairs('model:year', order_by=Car.year.desc())
    eq_(expect, result)

    expect = [
        (c2.model, c2.year),
        (c1.model, c1.year),
    ]
    result = Car.pairs_by('model:year',
                          model=u'count',
                          order_by=Car.year.desc())
    eq_(expect, result)

    result = Car.pairs_where('model:year',
                             Car.model == u'count',
                             order_by=Car.year.desc())
    eq_(expect, result)

    result = Car.pairs_where('model:year',
                             Car.model == u'we-need-an-empty-list',
                             order_by=Car.year.desc())
    eq_([], result)

    ###
    #   test firsts
    ###
    c = Car.first()
    assert c is c1

    c = Car.first(order_by=Car.year.desc())
    assert c is c3

    c = Car.first_by(model=u'count2')
    assert c is c3

    c = Car.first_by(model=u'count', order_by=Car.year.desc())
    assert c is c2

    c = Car.first_where(Car.model == u'count2')
    assert c is c3

    c = Car.first_where(Car.model == u'count', order_by=Car.year.desc())
    assert c is c2

    c = Car.first_by(model=u'nothere')
    assert c is None

    try:
        c = Car.first_where(Car.model == u'count2', erronous='foo')
    except ValueError:
        pass