def main(): writer = pytablewriter.SqliteTableWriter() # create the first table writer.table_name = "example" writer.headers = ["int", "float", "str", "bool", "mix", "time"] writer.value_matrix = [ [0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"], [2, "-2.23", "foo", False, None, "2017-12-23 12:34:51+0900"], [3, 0, "bar", "true", "inf", "2017-03-03 22:44:55+0900"], [-10, -9.9, "", "FALSE", "nan", "2017-01-01 00:00:00+0900"], ] writer.dump("sample.sqlite", close_after_write=False) # write the second table writer.table_name = "Timezone" writer.headers = [ "zone_id", "abbreviation", "time_start", "gmt_offset", "dst", ] writer.value_matrix = [ ["1", "CEST", "1017536400", "7200", "1"], ["1", "CEST", "1048986000", "7200", "1"], ["1", "CEST", "1080435600", "7200", "1"], ["1", "CEST", "1111885200", "7200", "1"], ["1", "CEST", "1143334800", "7200", "1"], ] writer.dump("sample.sqlite") writer.close()
def test_normal_multi_table(self, tmpdir): test_filepath = str(tmpdir.join("test.sqlite")) data_list = [ TableData( table_name="first", header_list=["ha1", "hb1", "hc1"], row_list=[[1.0, 2.0, 3.0], [11.0, 12.0, 13.0]], ), TableData( table_name="second", header_list=["ha2", "hb2", "hc2"], row_list=[[11.0, 12.0, 13.0], [1.0, 2.0, 3.0]], ), ] writer = ptw.SqliteTableWriter() for data in data_list: writer.from_tabledata(data) writer.dump(test_filepath, close_after_write=False) writer.close() count = 0 for data, expected in zip(data_list, SqliteFileLoader(test_filepath).load()): assert data == expected count += 1 assert count == 2
def test_normal_type_hints(self, tmpdir): test_file_path = str(tmpdir.join("test.sqlite")) writer = ptw.SqliteTableWriter() writer.open(test_file_path) writer.table_name = "hoge" writer.headers = ["a", "b"] writer.value_matrix = [[1, 2], [11, 12]] writer.type_hints = [ptw.String] writer.write_table() writer.close() schema = SQLiteSchemaExtractor( test_file_path).fetch_database_schema_as_dict() assert schema[writer.table_name] == [ OrderedDict([ ("Field", "a"), ("Index", False), ("Type", "TEXT"), ("Null", "YES"), ("Key", ""), ("Default", "NULL"), ("Extra", ""), ]), OrderedDict([ ("Field", "b"), ("Index", False), ("Type", "INTEGER"), ("Null", "YES"), ("Key", ""), ("Default", "NULL"), ("Extra", ""), ]), ]
def test_smoke_empty(self, tmpdir, table, header, value, expected): writer = ptw.SqliteTableWriter() writer.open(":memory:") writer.table_name = table writer.headers = header writer.value_matrix = value writer.write_table()
def test_exception(self, tmpdir, table, header, value, expected): writer = ptw.SqliteTableWriter() writer.open(":memory:") writer.table_name = table writer.headers = header writer.value_matrix = value with pytest.raises(expected): writer.write_table()
def test_exception(self, tmpdir, table, header, value, expected): test_file_path = tmpdir.join("test.sqlite") writer = ptw.SqliteTableWriter() writer.open(str(test_file_path)) writer.table_name = table writer.header_list = header writer.value_matrix = value with pytest.raises(expected): writer.write_table()
def test_normal_single_table(self, tmpdir): test_filepath = str(tmpdir.join("test.sqlite")) data = TableData( "tablename", ["ha", "hb", "hc"], [[1.0, 2.0, 3.0], [11.0, 12.0, 13.0], [1.0, 2.0, 3.0]] ) writer = ptw.SqliteTableWriter() writer.from_tabledata(data) writer.dump(test_filepath) for expected in SqliteFileLoader(test_filepath).load(): assert data == expected
def test_normal(self, tmpdir, table, header, value, expected): test_file_path = tmpdir.join("test.sqlite") writer = ptw.SqliteTableWriter() writer.open(str(test_file_path)) writer.table_name = table writer.headers = header writer.value_matrix = value writer.iteration_length = len(value) writer.write_table_iter() writer.close() for table_data in SqliteFileLoader(str(test_file_path)).load(): assert table_data == expected
def test_normal(self, tmpdir, table, header, value, expected): test_file_path = tmpdir.join("test.sqlite") writer = ptw.SqliteTableWriter() writer.open(str(test_file_path)) writer.table_name = table writer.headers = header writer.value_matrix = value writer.write_table() writer.close() for table_data in SqliteFileLoader(str(test_file_path)).load(): expected_dump = ptw.dump_tabledata(expected) actual_dump = ptw.dump_tabledata(table_data) print_test_result(expected=expected_dump, actual=actual_dump) assert actual_dump == expected_dump
def test_normal(self, tmpdir, table, header, value, expected): test_file_path = tmpdir.join("test.sqlite") writer = ptw.SqliteTableWriter() writer.open(str(test_file_path)) writer.table_name = table writer.header_list = header writer.value_matrix = value writer.write_table() writer.close() loader = ptr.SqliteFileLoader(str(test_file_path)) for table_data in loader.load(): expected_dump = ptw.dump_tabledata(expected) actual_dump = ptw.dump_tabledata(table_data) print("[expected]\n{}".format(expected_dump)) print("[actual]\n{}".format(actual_dump)) assert actual_dump == expected_dump
def test_exception(self, tmpdir): writer = ptw.SqliteTableWriter() writer.open(":memory:") with pytest.raises(NotImplementedError): writer.dumps()
#!/usr/bin/env python # encoding: utf-8 """ .. codeauthor:: Tsuyoshi Hombashi <*****@*****.**> """ import pytablewriter writer = pytablewriter.SqliteTableWriter() writer.open("sample.sqlite") # create the first table writer.table_name = "example" writer.header_list = ["int", "float", "str", "bool", "mix", "time"] writer.value_matrix = [ [0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"], [2, "-2.23", "foo", False, None, "2017-12-23 12:34:51+0900"], [3, 0, "bar", "true", "inf", "2017-03-03 22:44:55+0900"], [-10, -9.9, "", "FALSE", "nan", "2017-01-01 00:00:00+0900"], ] writer.write_table() # write the second table writer.table_name = "Timezone" writer.header_list = [ "zone_id", "abbreviation", "time_start", "gmt_offset", "dst", ]