Ejemplo n.º 1
0
def test_find_user_by_id():
    with database.Database() as db:
        user_two = db.execute("select id, name from users where id = %(id)s", {
            "id": 2
        }).fetchone()
        assert user_two.id == 2
        assert user_two.name == "User 2"
Ejemplo n.º 2
0
def test_find_all_users():
    with database.Database() as db:
        users = db.execute("select id, name from users").fetchall()
        assert len(users) == 4
        assert users[0].id == 1
        assert users[1].name == "User 2"
        cursor = db.execute("select id, name from users")
        for u in cursor:
            assert u.id is not None
Ejemplo n.º 3
0
def test_update_user_whereall():
    with database.Database() as db:
        try:
            db.update("users").set("name", "Usuario 3").whereall({
                "id":
                1,
                "name":
                "User1"
            }).execute()
        except () as e:
            assert e is None
Ejemplo n.º 4
0
def test_paging_complex_query():
    with database.Database() as db:
        page = db.paging("select * from users where id < %(id)s", {'id': 10},
                         0, 3)
        assert type(page) == database.Page
        assert len(page.data) == 3
        assert not page.last_page
        page = db.paging("select * from users where id < %(id)s", {'id': 10},
                         1, 3)
        assert type(page) == database.Page
        assert len(page.data) == 1
        assert page.last_page
Ejemplo n.º 5
0
def test_group_by():
    with database.Database() as db:
        result = db.select("users") \
            .fields('gender sexo', 'count(1) contagem') \
            .where('id', 10, '<') \
            .group_by('gender') \
            .order_by('contagem desc') \
            .execute() \
            .fetchall()
        assert result[0].sexo == 'M'
        assert result[0].contagem == 3
        assert result[1].sexo == 'F'
        assert result[1].contagem == 1
Ejemplo n.º 6
0
def test_insert_users():
    with database.Database() as db:
        db.insert("users") \
            .set("id", 1) \
            .set("name", "User 1") \
            .set("birth", "2018-03-20") \
            .set("gender", "M") \
            .execute()
        db.insert("users").set("id", 2) \
            .set("name", "User 2") \
            .set("birth", "2018-02-20") \
            .set("gender", "F") \
            .execute()
        db.insert("users") \
            .setall({"id": 3,  "name": "User 3", "birth": "2018-01-20"}) \
            .set("gender", "M") \
            .execute()
        db.insert("users") \
            .setall({"id": 4,  "name": "Usuario 4", "birth": "2017-01-20"}) \
            .set("gender", "M") \
            .execute()
Ejemplo n.º 7
0
def test_paging_user_by_select():
    with database.Database() as db:
        page = db.select("users") \
            .fields('id', 'name') \
            .where("id", "10", "<") \
            .order_by('id') \
            .paging(0, 3)
        assert type(page) == database.Page
        assert len(page.data) == 3
        assert page.data[0].id == 1
        assert page.data[0].name is not None
        try:
            page.data[0].birth
            assert False  # falhou!
        except Exception as e:
            assert isinstance(e, AttributeError)
        assert not page.last_page
        page = db.select("users").where("id", "10", "<").paging(1, 3)
        assert type(page) == database.Page
        assert len(page.data) == 1
        assert page.last_page
Ejemplo n.º 8
0
def test_find_user_by_select_without_where():
    with database.Database() as db:
        users = db.select("users").execute().fetchall()
        assert len(users) == 4
Ejemplo n.º 9
0
def test_paging_user_by_select_without_where():
    with database.Database() as db:
        page = db.select("users").paging(1, 2)
        assert type(page) == database.Page
        assert len(page.data) == 2
Ejemplo n.º 10
0
def test_update_user():
    with database.Database() as db:
        c = db.update("users").set("name", "Usuario 3").where("id",
                                                              3).execute()
        assert c.cursor.rowcount == 1
Ejemplo n.º 11
0
def test_find_user_by_select():
    with database.Database() as db:
        users = db.select("users").where("name", "User%",
                                         "like").execute().fetchall()
        assert len(users) == 2
Ejemplo n.º 12
0
def test_create_table_users():
    with database.Database() as db:
        db.execute("drop table if exists users")
        db.execute(
            "create table if not exists users(id int primary key, name varchar(255), birth date, gender char)"
        )
Ejemplo n.º 13
0
def test_truncate_table():
    with database.Database() as db:
        db.execute("truncate table users")
Ejemplo n.º 14
0
def test_find_two_users():
    with database.Database() as db:
        users = db.execute("select id, name from users").fetchmany(2)
        assert len(users) == 2
        assert users[0].id == 1
        assert users[1].name == "User 2"
Ejemplo n.º 15
0
def test_find_user_by_id_named_query():
    with database.Database() as db:
        user_three = db.execute("find-user-by-id", {"id": 3}).fetchone()
        assert user_three.id == 3
        assert user_three.name == "Usuario 3"
Ejemplo n.º 16
0
def test_delete_user_by_id():
    with database.Database() as db:
        db.delete("users").where("id", 3).execute()
        assert len(db.execute("select id from users").fetchall()) == 3
Ejemplo n.º 17
0
def test_select_without_result():
    with database.Database() as db:
        u = db.select('users').where('1', '0',
                                     constant=True).execute().fetchone()
        assert u is None
Ejemplo n.º 18
0
def test_delete_user_like_name():
    with database.Database() as db:
        db.delete("users").where("name", "Use%", "like").execute()
        assert len(db.execute("select id from users").fetchall()) == 1