Пример #1
0
 def test_one_db_session(self):
     c = Car.first()
     assert c.make
     self.ta.get('/')
     try:
         assert c.make
         assert False, 'expected DetachedInstanceError'
     except DetachedInstanceError:
         pass
Пример #2
0
    def test_split_db_sessions(self):
        raise SkipTest
        """ something weird happens when this test runs in that
        test_model.py:TestFKs.test_fk_prevent_parent_update() fails
        when running against PGSQL (at least)
        """
        wsgiapp = make_wsgi('SplitSessionsTest')
        ta = TestApp(wsgiapp)
        run_tasks('clear-db')
        run_tasks('init-db:~test')
        r = ta.get('/')
        assert 'Index Page' in r

        c = Car.first()
        assert c.make
        ta.get('/')
        assert c.make
Пример #3
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
Пример #4
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