Skip to content

Commit

Permalink
Fix type hint for SQLite table writer: thombashi#12
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Apr 20, 2019
1 parent 74fb67d commit 619903b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions pytablewriter/writer/binary/_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def _write_table(self):
[value_dp.data for value_dp in value_dp_list]
for value_dp_list in self._table_value_dp_matrix
],
type_hints=self.type_hints,
)
self.stream.create_table_from_tabledata(table_data)

Expand Down
1 change: 1 addition & 0 deletions requirements/test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pytablereader[excel,sqlite]>=0.25.4
pytest
simplejson
sqliteschema
tablib
termcolor
tox
41 changes: 41 additions & 0 deletions test/writer/binary/test_sqlite_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
from __future__ import absolute_import, print_function, unicode_literals

import collections
from collections import OrderedDict
from decimal import Decimal

import pytablewriter as ptw
import pytest
from pytablereader import SqliteFileLoader
from sqliteschema import SQLiteSchemaExtractor
from tabledata import TableData

from ..._common import print_test_result
Expand Down Expand Up @@ -108,6 +110,45 @@ def test_normal(self, tmpdir, table, header, value, expected):

assert actual_dump == expected_dump

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", ""),
]
),
]

@pytest.mark.parametrize(
["table", "header", "value", "expected"],
[[data.table, data.header, data.value, data.expected] for data in exception_test_data_list],
Expand Down

0 comments on commit 619903b

Please sign in to comment.