예제 #1
0
    def test_count_where(self, conn):
        repo = Repository(conn, User)
        users = repo.fetch_all()
        assert len(users) == repo.count_where([(User.id, '>', 0)])

        assert repo.count_where([(User.name, 'bilbo')]) == 1
        assert repo.count_where([(User.name, 'John')]) == 0
예제 #2
0
    def test_fetch(self, conn):
        repo = Repository(conn, User)
        users = repo.fetch_all()
        assert len(users) == len(rows_users)
        ids = []
        names = []
        for u in users:
            ids.append(u.id)
            names.append(u.name)

        users = repo.fetch(repo.select().where(User.id, '>', -1))
        assert users is not None
        assert len(users) == len(ids)
        for u in users:
            assert u.id in ids

        # test empty result query
        users = repo.fetch(repo.select().where(User.id, '=', -1))
        assert type(users) is list
        assert len(users) == 0

        # test different record class
        users = repo.fetch(repo.select().where(User.id, '>', -1), cls=UserName)
        assert users is not None
        assert len(users) == len(ids)
        for u in users:
            assert isinstance(u, UserName)
            assert u.name in names
예제 #3
0
 def test_valid_pk(self, conn):
     repo = Repository(conn, User)
     users = repo.fetch_all()
     assert type(users) is list
     assert len(users) == len(rows_users)
     for r in users:
         assert repo.valid_pk(r.id) is True
     assert repo.valid_pk(-1) is False
예제 #4
0
    def test_fetch_pk(self, conn):
        repo = Repository(conn, User)
        users = repo.fetch_all()
        assert len(users) == len(rows_users)
        for u in users:
            record = repo.fetch_pk(u.id)
            assert record is not None
            assert record.asdict() == u.asdict()

        record = repo.fetch_pk(-1)
        assert record is None
예제 #5
0
 def test_exists(self, conn):
     repo = Repository(conn, User)
     users = repo.fetch_all()
     first = None
     for r in users:
         # existing name with different id is False
         assert repo.exists(User.name, r.name, r.id) is False
         if first is None:
             first = r.name
         else:
             # existing name with another id is True
             assert repo.exists(User.name, first, r.id) is True
예제 #6
0
    def test_fetch_one(self, conn):
        repo = Repository(conn, User)
        users = repo.fetch_all()
        assert len(users) == len(rows_users)
        for u in users:
            record = repo.fetch_one(repo.select().where(User.id, '=', u.id))
            assert record is not None
            assert record.asdict() == u.asdict()

        # if not found, returns None
        record = repo.fetch_one(repo.select().where(User.id, '=', -1))
        assert record is None
예제 #7
0
    def test_update_where(self, conn):
        repo = Repository(conn, User)
        users = repo.fetch_all()
        u = users.pop()  # lets just use the last entry

        # test exception on empty where clauses
        with pytest.raises(RepositoryError):
            repo.update_where(User(name='Pocoyo'), [])
        with pytest.raises(RepositoryError):
            repo.update_where(User(name='Pocoyo'), [()])

        repo.update_where(User(name='Pocoyo'), [(User.login, '=', u.login)])
        record = repo.fetch_pk(u.id)
        assert record.name == 'Pocoyo'
        assert record.login == u.login
예제 #8
0
 def test_fetchall(self, conn):
     repo = Repository(conn, User)
     users = repo.fetch_all()
     assert type(users) is list
     assert len(users) == len(rows_users)
     for r in users:
         assert isinstance(r, User)
         assert r.id is not None and type(r.id) is int
         assert r.name is not None and type(r.name) is str and len(
             r.name) > 0
         assert r.email is not None and type(r.email) is str and len(
             r.email) > 0
         assert r.active is not None
         if isinstance(conn, PgConnection):
             assert type(r.active) is bool
         elif isinstance(conn, Sqlite3Connection):
             assert type(r.active) is int
예제 #9
0
    def test_fetch_by_field(self, conn):
        repo = Repository(conn, User)
        users = repo.fetch_all()
        assert len(users) == len(rows_users)
        for u in users:
            # fetch all columns
            records = repo.fetch_by_field(User.id, u.id)
            assert len(records) == 1
            assert records.pop().asdict() == u.asdict()

            # fetch single column
            records = repo.fetch_by_field(User.id, u.id, cols=[User.name])
            assert len(records) == 1
            record = records.pop().asdict()
            assert len(record) == 1
            assert record['name'] == u.name

        # fetch non-existing record
        records = repo.fetch_by_field(User.id, -1)
        assert len(records) == 0
예제 #10
0
    def test_list(self, conn):
        repo = Repository(conn, User)
        users = repo.fetch_all()

        qry = repo.select().order(User.id)
        total, rows = repo.list(qry, 1)
        assert total == len(users)
        assert len(rows) == 1
        assert rows[0].name == 'aragorn'

        total, rows = repo.list(qry, 1, 1)
        assert total == len(users)
        assert len(rows) == 1
        assert rows[0].name == 'bilbo'

        qry = repo.select().order(User.id)
        total, rows = repo.list(qry, 2, 2)
        assert total == len(users)
        assert len(rows) == 2
        assert rows[0].name == 'samwise'
        assert rows[1].name == 'gandalf'
예제 #11
0
 def test_count(self, conn):
     repo = Repository(conn, User)
     users = repo.fetch_all()
     assert len(users) == repo.count()
예제 #12
0
 def test_map_result_id(self, conn):
     repo = Repository(conn, User)
     users = repo.map_result_id(repo.fetch_all())
     assert len(users) == len(rows_users)
     for id, record in users.items():
         assert id == record.id