def test_normal_file(
        self,
        tmpdir,
        json_text,
        filename,
        table_name,
        expected_table_name,
        expected_attr_names,
        expected_data_matrix,
    ):
        p_db = tmpdir.join("tmp.db")
        p_json = tmpdir.join(filename)

        with open(str(p_json), "w") as f:
            f.write(json_text)

        con = SimpleSQLite(str(p_db), "w")
        con.create_table_from_json(str(p_json), table_name)

        assert con.fetch_table_names() == [expected_table_name]
        assert expected_attr_names == con.fetch_attr_names(expected_table_name)

        result = con.select(select="*", table_name=expected_table_name)
        result_matrix = result.fetchall()
        assert len(result_matrix) == 3
        assert result_matrix == expected_data_matrix
Example #2
0
    def test_normal_file(
        self,
        tmpdir,
        json_text,
        filename,
        table_name,
        expected_table_name,
        expected_attr_names,
        expected_data_matrix,
    ):
        p_db = tmpdir.join("tmp.db")
        p_json = tmpdir.join(filename)

        with open(str(p_json), "w") as f:
            f.write(json_text)

        con = SimpleSQLite(str(p_db), "w")
        try:
            con.create_table_from_json(str(p_json), table_name)
        except ImportError:
            pytest.skip("requires pytablereader")

        assert con.fetch_table_names() == [expected_table_name]
        assert expected_attr_names == con.fetch_attr_names(expected_table_name)

        result = con.select(select="*", table_name=expected_table_name)
        result_matrix = result.fetchall()
        assert len(result_matrix) == 3
        assert result_matrix == expected_data_matrix
    def test_normal_empty_header(self, tmpdir, table_name, attr_names, data_matrix, expected):
        p = tmpdir.join("tmp.db")
        con = SimpleSQLite(str(p), "w")

        con.create_table_from_data_matrix(table_name, attr_names, data_matrix)

        assert con.fetch_attr_names(table_name) == expected
def main():
    file_path = "sample_data_multi.json"

    # create sample data file ---
    with open(file_path, "w") as f:
        f.write("""{
            "table_a" : [
                {"attr_b": 4, "attr_c": "a", "attr_a": 1},
                {"attr_b": 2.1, "attr_c": "bb", "attr_a": 2},
                {"attr_b": 120.9, "attr_c": "ccc", "attr_a": 3}
            ],
            "table_b" : [
                {"a": 1, "b": 4},
                {"a": 2 },
                {"a": 3, "b": 120.9}
            ]
        }""")

    # create table ---
    con = SimpleSQLite("sample.sqlite", "w")
    con.create_table_from_json(file_path)

    # output ---
    for table_name in con.fetch_table_names():
        print("table: " + table_name)
        print(con.fetch_attr_names(table_name))
        result = con.select(select="*", table_name=table_name)
        for record in result.fetchall():
            print(record)
        print()
Example #5
0
    def test_normal_empty_header(self, tmpdir, table_name, attr_names,
                                 data_matrix, expected):
        p = tmpdir.join("tmp.db")
        con = SimpleSQLite(str(p), "w")

        con.create_table_from_data_matrix(table_name, attr_names, data_matrix)

        assert con.fetch_attr_names(table_name) == expected
    def test_normal_number_header(self, tmpdir):
        p = tmpdir.join("tmp.db")
        con = SimpleSQLite(str(p), "w")
        table_name = "numbers"
        attr_names = [1, 123456789]
        data_matrix = [[1, 2], [1, 2]]
        expected = ["1", "123456789"]

        con.create_table_from_data_matrix(table_name, attr_names, data_matrix)

        assert con.fetch_attr_names(table_name) == expected
    def test_normal_symbol_header(self, tmpdir):
        p = tmpdir.join("tmp.db")
        con = SimpleSQLite(str(p), "w")
        table_name = "symbols"
        attr_names = ["a!bc#d$e%f&gh(i)j", "k@l[m]n{o}p;q:r_s.t/u"]
        data_matrix = [{"ABCD>8.5": "aaa", "ABCD<8.5": 0}, {"ABCD>8.5": "bbb", "ABCD<8.5": 9}]
        expected = ["a!bc#d$e%f&gh(i)j", "k@l[m]n{o}p;q:r_s.t/u"]

        con.create_table_from_data_matrix(table_name, attr_names, data_matrix)

        assert con.fetch_attr_names(table_name) == expected
Example #8
0
    def test_normal_number_header(self, tmpdir):
        p = tmpdir.join("tmp.db")
        con = SimpleSQLite(str(p), "w")
        table_name = "numbers"
        attr_names = [1, 123456789]
        data_matrix = [[1, 2], [1, 2]]
        expected = ["1", "123456789"]

        con.create_table_from_data_matrix(table_name, attr_names, data_matrix)

        assert con.fetch_attr_names(table_name) == expected
Example #9
0
    def test_normal(self, tmpdir, value, type_hints, expected):
        p_db = tmpdir.join("tmp.db")

        con = SimpleSQLite(str(p_db), "w")
        con.create_table_from_tabledata(value)

        assert con.fetch_table_names() == [value.table_name]
        assert con.fetch_attr_names(value.table_name) == value.headers

        actual = con.select_as_tabledata(columns=value.headers,
                                         table_name=value.table_name,
                                         type_hints=type_hints)
        assert actual.value_matrix == expected
    def test_normal(self, tmpdir, value, type_hints, expected):
        p_db = tmpdir.join("tmp.db")

        con = SimpleSQLite(str(p_db), "w")
        con.create_table_from_tabledata(value)

        assert con.fetch_table_names() == [value.table_name]
        assert con.fetch_attr_names(value.table_name) == value.headers

        actual = con.select_as_tabledata(
            columns=value.headers, table_name=value.table_name, type_hints=type_hints
        )
        assert actual.value_matrix == expected
    def test_normal(self, tmpdir, value, expected):
        p_db = tmpdir.join("tmp.db")

        con = SimpleSQLite(str(p_db), "w")
        con.create_table_from_tabledata(value)

        assert con.fetch_table_names() == [value.table_name]
        assert con.fetch_attr_names(value.table_name) == value.headers

        result = con.select(select="*", table_name=value.table_name)
        result_matrix = result.fetchall()
        assert result_matrix == expected

        actual = con.select_as_tabledata(columns=value.headers, table_name=value.table_name)
        assert actual.equals(value)
Example #12
0
    def test_normal(self, tmpdir, value, expected):
        p_db = tmpdir.join("tmp.db")

        con = SimpleSQLite(str(p_db), "w")
        con.create_table_from_tabledata(value)

        assert con.fetch_table_names() == [value.table_name]
        assert con.fetch_attr_names(value.table_name) == value.headers

        result = con.select(select="*", table_name=value.table_name)
        result_matrix = result.fetchall()
        assert result_matrix == expected

        actual = con.select_as_tabledata(columns=value.headers,
                                         table_name=value.table_name)
        assert actual.equals(value)
Example #13
0
    def test_normal_symbol_header(self, tmpdir):
        p = tmpdir.join("tmp.db")
        con = SimpleSQLite(str(p), "w")
        table_name = "symbols"
        attr_names = ["a!bc#d$e%f&gh(i)j", "k@l[m]n{o}p;q:r_s.t/u"]
        data_matrix = [{
            "ABCD>8.5": "aaa",
            "ABCD<8.5": 0
        }, {
            "ABCD>8.5": "bbb",
            "ABCD<8.5": 9
        }]
        expected = ["a!bc#d$e%f&gh(i)j", "k@l[m]n{o}p;q:r_s.t/u"]

        con.create_table_from_data_matrix(table_name, attr_names, data_matrix)

        assert con.fetch_attr_names(table_name) == expected
def main():
    with open("sample_data.csv", "w") as f:
        f.write("\n".join([
            '"attr_a","attr_b","attr_c"', '1,4,"a"', '2,2.1,"bb"',
            '3,120.9,"ccc"'
        ]))

    # create table ---
    con = SimpleSQLite("sample.sqlite", "w")
    con.create_table_from_csv("sample_data.csv")

    # output ---
    table_name = "sample_data"
    print(con.fetch_attr_names(table_name))
    result = con.select(select="*", table_name=table_name)
    for record in result.fetchall():
        print(record)
Example #15
0
    def to_table_data(self):
        from simplesqlite import SimpleSQLite
        from simplesqlite.query import AttrList

        con = SimpleSQLite(self._source_data, "r")

        for table in con.fetch_table_names():
            self.__table_name = table

            attr_names = con.fetch_attr_names(table)
            data_matrix = con.select(select=AttrList(attr_names),
                                     table_name=table).fetchall()

            yield TableData(
                table,
                attr_names,
                data_matrix,
                dp_extractor=self._loader.dp_extractor,
                type_hints=self._extract_type_hints(attr_names),
            )
    def test_normal_text(
        self,
        tmpdir,
        csv_text,
        table_name,
        attr_names,
        expected_table_name,
        expected_attr_names,
        expected_data_matrix,
    ):
        p_db = tmpdir.join("tmp.db")

        con = SimpleSQLite(str(p_db), "w")
        con.create_table_from_csv(csv_text, table_name, attr_names)

        assert con.fetch_table_names() == [expected_table_name]
        assert expected_attr_names == con.fetch_attr_names(expected_table_name)

        result = con.select(select="*", table_name=expected_table_name)
        result_matrix = result.fetchall()
        assert len(result_matrix) == 3
        assert result_matrix == expected_data_matrix
# create sample data file ---
with open(file_path, "w") as f:
    f.write(
        """{
        "table_a" : [
            {"attr_b": 4, "attr_c": "a", "attr_a": 1},
            {"attr_b": 2.1, "attr_c": "bb", "attr_a": 2},
            {"attr_b": 120.9, "attr_c": "ccc", "attr_a": 3}
        ],
        "table_b" : [
            {"a": 1, "b": 4},
            {"a": 2 },
            {"a": 3, "b": 120.9}
        ]
    }"""
    )

# create table ---
con = SimpleSQLite("sample.sqlite", "w")
con.create_table_from_json(file_path)

# output ---
for table_name in con.fetch_table_names():
    print("table: " + table_name)
    print(con.fetch_attr_names(table_name))
    result = con.select(select="*", table_name=table_name)
    for record in result.fetchall():
        print(record)
    print()