Пример #1
0
    def test_to_sqlite3_namedtuple(self):
        elements = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]

        # test namedtuple with the same order as column
        with sqlite3.connect(':memory:') as conn:
            user = collections.namedtuple('user', ['id', 'name'])

            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

            table_name = 'user'
            seq(elements).map(lambda u: user(u[0], u[1])).to_sqlite3(
                conn, table_name)
            result = seq.sqlite3(conn, 'SELECT id, name FROM user;').to_list()
            self.assertListEqual(elements, result)

        # test namedtuple with different order
        with sqlite3.connect(':memory:') as conn:
            user = collections.namedtuple('user', ['name', 'id'])

            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

            table_name = 'user'
            seq(elements).map(lambda u: user(u[1], u[0])).to_sqlite3(
                conn, table_name)
            result = seq.sqlite3(conn, 'SELECT id, name FROM user;').to_list()
            self.assertListEqual(elements, result)
Пример #2
0
    def test_to_sqlite3_namedtuple(self):
        elements = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]

        # test namedtuple with the same order as column
        with sqlite3.connect(':memory:') as conn:
            user = collections.namedtuple('user', ['id', 'name'])

            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

            table_name = 'user'
            seq(elements).map(lambda u: user(u[0], u[1])).to_sqlite3(conn, table_name)
            result = seq.sqlite3(conn, 'SELECT id, name FROM user;').to_list()
            self.assertListEqual(elements, result)

        # test namedtuple with different order
        with sqlite3.connect(':memory:') as conn:
            user = collections.namedtuple('user', ['name', 'id'])

            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

            table_name = 'user'
            seq(elements).map(lambda u: user(u[1], u[0])).to_sqlite3(conn, table_name)
            result = seq.sqlite3(conn, 'SELECT id, name FROM user;').to_list()
            self.assertListEqual(elements, result)
Пример #3
0
    def test_to_sqlite3_tuples(self):
        conn = sqlite3.connect(':memory:')
        c = conn.cursor()
        c.execute('''CREATE TABLE demo (color TEXT, value REAL)''')
        conn.commit()

        seq(('red', 1), ('green', 2), ('blue', 3)).to_sqlite3(conn, 'INSERT INTO demo (color, value) VALUES (?, ?)')

        res = seq.sqlite3(conn, 'SELECT * FROM demo')
        assert res == [('red', 1), ('green', 2), ('blue', 3)]
Пример #4
0
    def test_to_sqlite3_dicts(self):
        conn = sqlite3.connect(':memory:')
        c = conn.cursor()
        c.execute('''CREATE TABLE demo (color TEXT, value REAL)''')
        conn.commit()

        seq({'color': 'red', 'value': 1}, {'color': 'green', 'value': 2}, {'color': 'blue', 'value': 3}) \
            .to_sqlite3(conn, 'demo')

        res = seq.sqlite3(conn, 'SELECT * FROM demo')
        assert res == [('red', 1), ('green', 2), ('blue', 3)]
Пример #5
0
    def test_to_sqlite3_tuple(self):
        elements = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]

        with sqlite3.connect(':memory:') as conn:
            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

            table_name = 'user'
            seq(elements).to_sqlite3(conn, table_name)
            result = seq.sqlite3(conn, 'SELECT id, name FROM user;').to_list()
            self.assertListEqual(elements, result)
Пример #6
0
    def test_to_sqlite3_query(self):
        elements = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]

        with sqlite3.connect(':memory:') as conn:
            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

            insert_sql = 'INSERT INTO user (id, name) VALUES (?, ?)'
            seq(elements).to_sqlite3(conn, insert_sql)
            result = seq.sqlite3(conn, 'SELECT id, name FROM user;').to_list()
            self.assertListEqual(elements, result)
Пример #7
0
    def test_to_sqlite3_dict(self):
        elements = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]

        with sqlite3.connect(':memory:') as conn:
            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

            table_name = 'user'
            seq(elements).map(lambda x: {'id': x[0], 'name': x[1]}).to_sqlite3(conn, table_name)
            result = seq.sqlite3(conn, 'SELECT id, name FROM user;').to_list()
            self.assertListEqual(elements, result)
Пример #8
0
    def test_to_sqlite3_query(self):
        elements = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]

        with sqlite3.connect(':memory:') as conn:
            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

            insert_sql = 'INSERT INTO user (id, name) VALUES (?, ?)'
            seq(elements).to_sqlite3(conn, insert_sql)
            result = seq.sqlite3(conn, 'SELECT id, name FROM user;').to_list()
            self.assertListEqual(elements, result)
Пример #9
0
    def test_to_sqlite3_file(self):
        tmp_path = 'functional/test/data/tmp/test.db'

        with sqlite3.connect(tmp_path) as conn:
            conn.execute('DROP TABLE IF EXISTS user;')
            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

        insert_sql = 'INSERT INTO user (id, name) VALUES (?, ?)'
        elements = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]

        seq(elements).to_sqlite3(tmp_path, insert_sql)
        result = seq.sqlite3(tmp_path, 'SELECT id, name FROM user;').to_list()
        self.assertListEqual(elements, result)
Пример #10
0
    def test_to_sqlite3_file(self):
        tmp_path = 'functional/test/data/tmp/test.db'

        with sqlite3.connect(tmp_path) as conn:
            conn.execute('DROP TABLE IF EXISTS user;')
            conn.execute('CREATE TABLE user (id INT, name TEXT);')
            conn.commit()

        insert_sql = 'INSERT INTO user (id, name) VALUES (?, ?)'
        elements = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]

        seq(elements).to_sqlite3(tmp_path, insert_sql)
        result = seq.sqlite3(tmp_path, 'SELECT id, name FROM user;').to_list()
        self.assertListEqual(elements, result)
Пример #11
0
    def test_sqlite3(self):
        db_file = 'functional/test/data/test_sqlite3.db'

        # test failure case
        with self.assertRaises(ValueError):
            seq.sqlite3(1, 'SELECT * from user').to_list()

        # test select from file path
        query_0 = 'SELECT id, name FROM user;'
        result_0 = seq.sqlite3(db_file, query_0).to_list()
        expected_0 = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]
        self.assertListEqual(expected_0, result_0)

        # test select from connection
        with sqlite3.connect(db_file) as conn:
            result_0_1 = seq.sqlite3(conn, query_0).to_list()
            self.assertListEqual(expected_0, result_0_1)

        # test select from cursor
        with sqlite3.connect(db_file) as conn:
            cursor = conn.cursor()
            result_0_2 = seq.sqlite3(cursor, query_0).to_list()
            self.assertListEqual(expected_0, result_0_2)

        # test connection with kwds
        result_0_3 = seq.sqlite3(db_file, query_0, timeout=30).to_list()
        self.assertListEqual(expected_0, result_0_3)

        # test order by
        result_1 = seq.sqlite3(
            db_file, 'SELECT id, name FROM user ORDER BY name;').to_list()
        expected_1 = [(2, 'Jack'), (3, 'Jane'), (4, 'Stephan'), (1, 'Tom')]
        self.assertListEqual(expected_1, result_1)

        # test query with params
        result_2 = seq.sqlite3(db_file,
                               'SELECT id, name FROM user WHERE id = ?;',
                               parameters=(1, )).to_list()
        expected_2 = [(1, 'Tom')]
        self.assertListEqual(expected_2, result_2)
Пример #12
0
    def test_sqlite3(self):
        db_file = 'functional/test/data/test_sqlite3.db'

        # test failure case
        with self.assertRaises(ValueError):
            seq.sqlite3(1, 'SELECT * from user').to_list()

        # test select from file path
        query_0 = 'SELECT id, name FROM user;'
        result_0 = seq.sqlite3(db_file, query_0).to_list()
        expected_0 = [(1, 'Tom'), (2, 'Jack'), (3, 'Jane'), (4, 'Stephan')]
        self.assertListEqual(expected_0, result_0)

        # test select from connection
        with sqlite3.connect(db_file) as conn:
            result_0_1 = seq.sqlite3(conn, query_0).to_list()
            self.assertListEqual(expected_0, result_0_1)

        # test select from cursor
        with sqlite3.connect(db_file) as conn:
            cursor = conn.cursor()
            result_0_2 = seq.sqlite3(cursor, query_0).to_list()
            self.assertListEqual(expected_0, result_0_2)

        # test connection with kwds
        result_0_3 = seq.sqlite3(db_file, query_0, timeout=30).to_list()
        self.assertListEqual(expected_0, result_0_3)

        # test order by
        result_1 = seq.sqlite3(db_file,
                               'SELECT id, name FROM user ORDER BY name;').to_list()
        expected_1 = [(2, 'Jack'), (3, 'Jane'), (4, 'Stephan'), (1, 'Tom')]
        self.assertListEqual(expected_1, result_1)

        # test query with params
        result_2 = seq.sqlite3(db_file,
                               'SELECT id, name FROM user WHERE id = ?;',
                               parameters=(1,)).to_list()
        expected_2 = [(1, 'Tom')]
        self.assertListEqual(expected_2, result_2)
Пример #13
0
def get_line_table(db_path: str) -> LineTable:
    line_table_rows = seq.sqlite3(
        db_path, 'SELECT * FROM line').map(lambda x: LineTableRow(*x))
    return line_table_rows
Пример #14
0
def get_context_table(db_path: str) -> ContextTable:
    context_table_rows = seq.sqlite3(
        db_path, 'SELECT * FROM context').map(lambda x: ContextTableRow(*x))
    return context_table_rows
Пример #15
0
def get_file_table(db_path: str) -> FileTable:
    file_table_rows = seq.sqlite3(
        db_path, 'SELECT * FROM file').map(lambda x: FileTableRow(*x))
    return file_table_rows