def test_normal_add_primary_key_column(self, tmpdir):
        p = tmpdir.join("tmp.db")
        con = SimpleSQLite(str(p), "w")

        table_name = "table1"
        con.create_table_from_data_matrix(
            table_name=table_name,
            attr_names=["AA", "BB"],
            data_matrix=[["a", 11], ["bb", 12]],
            add_primary_key_column=True,
        )
        assert con.select_as_tabledata(table_name) == TableData(
            table_name=table_name, headers=["id", "AA", "BB"], rows=[[1, "a", 11], [2, "bb", 12]]
        )
        assert con.schema_extractor.fetch_table_schema(table_name).primary_key == "id"

        table_name = "table2"
        con.create_table_from_data_matrix(
            table_name=table_name,
            attr_names=["AA", "BB"],
            data_matrix=[["a", 11], ["bb", 12]],
            primary_key="pkey",
            add_primary_key_column=True,
        )
        assert con.select_as_tabledata(table_name) == TableData(
            table_name=table_name, headers=["pkey", "AA", "BB"], rows=[[1, "a", 11], [2, "bb", 12]]
        )
        assert con.schema_extractor.fetch_table_schema(table_name).primary_key == "pkey"
Beispiel #2
0
    def test_normal_add_primary_key_column(self, tmpdir):
        p = tmpdir.join("tmp.db")
        con = SimpleSQLite(str(p), "w")

        table_name = "table1"
        con.create_table_from_data_matrix(
            table_name=table_name,
            attr_names=["AA", "BB"],
            data_matrix=[["a", 11], ["bb", 12]],
            add_primary_key_column=True,
        )
        assert con.select_as_tabledata(table_name) == TableData(
            table_name=table_name,
            headers=["id", "AA", "BB"],
            rows=[[1, "a", 11], [2, "bb", 12]])
        assert con.schema_extractor.fetch_table_schema(
            table_name).primary_key == "id"

        table_name = "table2"
        con.create_table_from_data_matrix(
            table_name=table_name,
            attr_names=["AA", "BB"],
            data_matrix=[["a", 11], ["bb", 12]],
            primary_key="pkey",
            add_primary_key_column=True,
        )
        assert con.select_as_tabledata(table_name) == TableData(
            table_name=table_name,
            headers=["pkey", "AA", "BB"],
            rows=[[1, "a", 11], [2, "bb", 12]])
        assert con.schema_extractor.fetch_table_schema(
            table_name).primary_key == "pkey"
    def test_normal_type_hint_header(self):
        runner = CliRunner()
        basename = "type_hint_header"
        file_path = "{}.csv".format(basename)
        db_path = "{}.sqlite".format(basename)

        with runner.isolated_filesystem():
            with open(file_path, "w") as f:
                f.write(
                    dedent("""\
                        "a text","b integer","c real"
                        1,"1","1.1"
                        2,"2","1.2"
                        3,"3","1.3"
                        """))
                f.flush()

            result = runner.invoke(
                cmd, ["--type-hint-header", "-o", db_path, "file", file_path])
            print_traceback(result)
            assert result.exit_code == ExitCode.SUCCESS

            con = SimpleSQLite(db_path, "r")
            tbldata = con.select_as_tabledata(basename)
            assert tbldata.headers == ["a text", "b integer", "c real"]
            assert tbldata.rows == [("1", 1, 1.1), ("2", 2, 1.2),
                                    ("3", 3, 1.3)]
    def test_normal_type_hint_header(self):
        runner = CliRunner()
        basename = "type_hint_header"
        file_path = "{}.csv".format(basename)
        db_path = "{}.sqlite".format(basename)

        with runner.isolated_filesystem():
            with open(file_path, "w") as f:
                f.write(
                    dedent(
                        """\
                        "a text","b integer","c real"
                        1,"1","1.1"
                        2,"2","1.2"
                        3,"3","1.3"
                        """
                    )
                )
                f.flush()

            result = runner.invoke(cmd, ["--type-hint-header", "-o", db_path, "file", file_path])
            print_traceback(result)
            assert result.exit_code == ExitCode.SUCCESS

            con = SimpleSQLite(db_path, "r")
            tbldata = con.select_as_tabledata(basename)
            assert tbldata.headers == ["a text", "b integer", "c real"]
            assert tbldata.rows == [("1", 1, 1.1), ("2", 2, 1.2), ("3", 3, 1.3)]
Beispiel #5
0
    def test_normal_type_hint_header(self):
        url = "https://example.com/type_hint_header.csv"
        responses.add(
            responses.GET,
            url,
            body=dedent("""\
                "a text","b integer","c real"
                1,"1","1.1"
                2,"2","1.2"
                3,"3","1.3"
                """),
            content_type="text/plain; charset=utf-8",
            status=200,
        )
        runner = CliRunner()

        with runner.isolated_filesystem():
            result = runner.invoke(
                cmd, ["--type-hint-header", "-o", self.db_path, "url", url])
            print_traceback(result)
            assert result.exit_code == ExitCode.SUCCESS

            con = SimpleSQLite(self.db_path, "r")
            table_names = list(
                set(con.fetch_table_names()) -
                set([SourceInfo.get_table_name()]))

            # table name may change test execution order
            tbldata = con.select_as_tabledata(table_names[0])

            assert tbldata.headers == ["a text", "b integer", "c real"]
            assert tbldata.rows == [("1", 1, 1.1), ("2", 2, 1.2),
                                    ("3", 3, 1.3)]
    def test_normal_type_hint_header(self):
        url = "https://example.com/type_hint_header.csv"
        responses.add(
            responses.GET,
            url,
            body=dedent(
                """\
                "a text","b integer","c real"
                1,"1","1.1"
                2,"2","1.2"
                3,"3","1.3"
                """
            ),
            content_type="text/plain; charset=utf-8",
            status=200,
        )
        runner = CliRunner()

        with runner.isolated_filesystem():
            result = runner.invoke(cmd, ["--type-hint-header", "-o", self.db_path, "url", url])
            print_traceback(result)
            assert result.exit_code == ExitCode.SUCCESS

            con = SimpleSQLite(self.db_path, "r")
            table_names = list(set(con.fetch_table_names()) - set([SourceInfo.get_table_name()]))

            # table name may change test execution order
            tbldata = con.select_as_tabledata(table_names[0])

            assert tbldata.headers == ["a text", "b integer", "c real"]
            assert tbldata.rows == [("1", 1, 1.1), ("2", 2, 1.2), ("3", 3, 1.3)]
    def test_normal_type_hint_header(self):
        text = dedent("""\
            "a text","b integer","c real"
            1,"1","1.1"
            2,"2","1.2"
            3,"3","1.3"
            """)
        runner = CliRunner()

        with runner.isolated_filesystem():
            result = runner.invoke(
                cmd,
                ["--type-hint-header", "-o", self.db_path, "stdin", "csv"],
                input=text)
            print_traceback(result)
            assert result.exit_code == ExitCode.SUCCESS

            con = SimpleSQLite(self.db_path, "r")
            table_names = list(
                set(con.fetch_table_names()) - {SourceInfo.get_table_name()})

            # table name may change test execution order
            tbldata = con.select_as_tabledata(table_names[0])

            assert tbldata.headers == ["a text", "b integer", "c real"]
            assert tbldata.rows == [("1", 1, 1.1), ("2", 2, 1.2),
                                    ("3", 3, 1.3)]
    def test_normal_no_type_inference(self):
        runner = CliRunner()
        basename = "no_type_inference"
        file_path = "{}.csv".format(basename)
        db_path = "{}.sqlite".format(basename)

        with runner.isolated_filesystem():
            with open(file_path, "w") as f:
                f.write(
                    dedent(
                        """\
                        "a","b"
                        11,"xyz"
                        22,"abc"
                        """
                    )
                )
                f.flush()

            result = runner.invoke(cmd, ["--no-type-inference", "-o", db_path, "file", file_path])
            print_traceback(result)
            assert result.exit_code == ExitCode.SUCCESS

            con = SimpleSQLite(db_path, "r")
            tbldata = con.select_as_tabledata(basename)
            assert tbldata.headers == ["a", "b"]
            assert tbldata.rows == [("11", "xyz"), ("22", "abc")]
    def test_normal_no_type_inference(self):
        runner = CliRunner()
        basename = "no_type_inference"
        file_path = "{}.csv".format(basename)
        db_path = "{}.sqlite".format(basename)

        with runner.isolated_filesystem():
            with open(file_path, "w") as f:
                f.write(
                    dedent("""\
                        "a","b"
                        11,"xyz"
                        22,"abc"
                        """))
                f.flush()

            result = runner.invoke(
                cmd, ["--no-type-inference", "-o", db_path, "file", file_path])
            print_traceback(result)
            assert result.exit_code == ExitCode.SUCCESS

            con = SimpleSQLite(db_path, "r")
            tbldata = con.select_as_tabledata(basename)
            assert tbldata.headers == ["a", "b"]
            assert tbldata.rows == [("11", "xyz"), ("22", "abc")]
Beispiel #10
0
    def test_normal(self, con, tmpdir):
        dump_path = str(tmpdir.join("dump.db"))
        con.dump(dump_path)
        con_dump = SimpleSQLite(dump_path, "r")

        assert con.fetch_num_records(
            TEST_TABLE_NAME) == con_dump.fetch_num_records(TEST_TABLE_NAME)
        assert con.select_as_tabledata(
            TEST_TABLE_NAME) == con_dump.select_as_tabledata(TEST_TABLE_NAME)
    def test_normal(self, con, tmpdir):
        dump_path = str(tmpdir.join("dump.db"))
        con.dump(dump_path)
        con_dump = SimpleSQLite(dump_path, "r")

        assert con.fetch_num_records(TEST_TABLE_NAME) == con_dump.fetch_num_records(TEST_TABLE_NAME)
        assert con.select_as_tabledata(TEST_TABLE_NAME) == con_dump.select_as_tabledata(
            TEST_TABLE_NAME
        )
Beispiel #12
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)
Beispiel #15
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)
    def test_normal_format_ssv(self):
        db_path = "test_ssv.sqlite"
        runner = CliRunner()

        with runner.isolated_filesystem():
            file_path = valid_ssv_file()
            result = runner.invoke(cmd, ["-o", db_path, "file", file_path, "--format", "ssv"])
            print_traceback(result)

            assert result.exit_code == ExitCode.SUCCESS

            con = SimpleSQLite(db_path, "r")
            data = con.select_as_tabledata(table_name="ssv")
            expected = (
                "table_name=ssv, "
                "headers=[USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME, COMMAND], "
                "cols=11, rows=5"
            )

            assert str(data) == expected
    def test_normal_format_ssv(self):
        db_path = "test_ssv.sqlite"
        runner = CliRunner()

        with runner.isolated_filesystem():
            file_path = valid_ssv_file()
            result = runner.invoke(
                cmd, ["-o", db_path, "file", file_path, "--format", "ssv"])
            print_traceback(result)

            assert result.exit_code == ExitCode.SUCCESS

            con = SimpleSQLite(db_path, "r")
            data = con.select_as_tabledata(table_name="ssv")
            expected = (
                "table_name=ssv, "
                "headers=[USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME, COMMAND], "
                "cols=11, rows=5")

            assert str(data) == expected