Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #58

Merged
merged 6 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ beautifulsoup4
DataProperty>=0.13.0
jsonschema
pathvalidate>=0.9.3
pytablereader>=0.6.3
pytablereader>=0.6.5
path.py
six
xlrd
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

setuptools.setup(
name="SimpleSQLite",
version="0.6.6",
version="0.6.7",
url="https://github.com/thombashi/SimpleSQLite",

author="Tsuyoshi Hombashi",
Expand Down
4 changes: 0 additions & 4 deletions simplesqlite/_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ def append_table(con_src, con_dst, table_name):
:raises simplesqlite.TableNotFoundError:
|raises_verify_table_existence|
:raises ValueError: If attribute of the table is different from each other.

.. seealso::

:py:meth:`simplesqlite.SimpleSQLite.create_table_with_data`
"""

con_src.verify_table_existence(table_name)
Expand Down
49 changes: 41 additions & 8 deletions simplesqlite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,13 @@ def get_attribute_name_list(self, table_name):

self.verify_table_existence(table_name)

query = "SELECT * FROM '{:s}'".format(table_name)
query = u"SELECT * FROM '{:s}'".format(table_name)
result = self.execute_query(query, logging.getLogger().findCaller())

return self.__get_list_from_fetch(result.description)
return [
dataproperty.to_unicode(attr)
for attr in self.__get_list_from_fetch(result.description)
]

def get_attr_type(self, table_name):
"""
Expand Down Expand Up @@ -543,9 +546,9 @@ def get_attribute_type_list(self, table_name):
self.verify_table_existence(table_name)

attribute_name_list = self.get_attribute_name_list(table_name)
query = "SELECT DISTINCT {:s} FROM '{:s}'".format(
",".join([
"TYPEOF({:s})".format(SqlQuery.to_attr_str(attribute))
query = u"SELECT DISTINCT {:s} FROM '{:s}'".format(
u",".join([
u"TYPEOF({:s})".format(SqlQuery.to_attr_str(attribute))
for attribute in attribute_name_list]),
table_name)
result = self.execute_query(query, logging.getLogger().findCaller())
Expand Down Expand Up @@ -998,6 +1001,8 @@ def create_table_with_data(
"""
Create a table if not exists. And insert data into the created table.

Alias of :py:meth:`~.create_table_from_data_matrix`.

:param str table_name: Table name to create.
:param list attribute_name_list: List of attribute names of the table.
:param data_matrix: Data to be inserted into the table.
Expand All @@ -1017,9 +1022,37 @@ def create_table_with_data(
:py:meth:`.create_index_list`
"""

self.create_table_from_data_matrix(
table_name, attribute_name_list, data_matrix, index_attribute_list)

def create_table_from_data_matrix(
self, table_name, attr_name_list, data_matrix,
index_attr_list=None):
"""
Create a table if not exists. And insert data into the created table.

:param str table_name: Table name to create.
:param list attr_name_list: List of attribute names of the table.
:param data_matrix: Data to be inserted into the table.
:type data_matrix: List of |dict|/|namedtuple|/|list|/|tuple|
:param tuple index_attr_list:
List of attribute names of create indices.
:raises simplesqlite.InvalidTableNameError:
|raises_validate_table_name|
:raises simplesqlite.InvalidAttributeNameError:
|raises_validate_attr_name|
:raises ValueError: If the ``data_matrix`` is empty.

.. seealso::

:py:meth:`.create_table`
:py:meth:`.insert_many`
:py:meth:`.create_index_list`
"""

self.__create_table_from_tabledata(
ptr.TableData(table_name, attribute_name_list, data_matrix),
index_attribute_list)
ptr.TableData(table_name, attr_name_list, data_matrix),
index_attr_list)

def create_table_from_tabledata(self, tabledata, index_attr_list=None):
"""
Expand Down Expand Up @@ -1254,7 +1287,7 @@ def __verify_value_matrix(field_list, value_matrix):
" header: {} {}\n".format(len(field_list), field_list) +
" # of miss match line: {} ouf of {}\n".format(
len(miss_match_idx_list), len(value_matrix)) +
" e.g. value at line={}, len={}: {}\n".format(
" e.g. value at line={}, col-size={}: {}\n".format(
miss_match_idx_list[0],
len(sample_miss_match_list), sample_miss_match_list)
)
Expand Down
37 changes: 27 additions & 10 deletions test/test_simplesqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,13 @@ def test_normal(self, con, value, expected):

@pytest.mark.parametrize(["value", "expected"], [
["not_exist_table", TableNotFoundError],
[None, TableNotFoundError],
[None, InvalidTableNameError],
])
def test_null(self, con, value, expected):
def test_null_table(self, con, value, expected):
with pytest.raises(expected):
con.get_attribute_name_list(value)

def test_null(self, con_null):
def test_null_con(self, con_null):
with pytest.raises(NullDatabaseConnectionError):
con_null.get_attribute_name_list("not_exist_table")

Expand Down Expand Up @@ -811,6 +811,7 @@ def test_normal(
table_name=table_name)
result_matrix = result.fetchall()
assert len(result_matrix) == 3

print("expected: {}".format(expected_attr))
print("actual: {}".format(con.get_attr_type(table_name)))
assert con.get_attr_type(table_name) == expected_attr
Expand Down Expand Up @@ -849,7 +850,7 @@ def test_null(self, con_null):

class Test_SimpleSQLite_create_table_from_tabledata:

@pytest.mark.parametrize(["value"], [
@pytest.mark.parametrize(["value", "expected"], [
[
ptr.TableData(
"json1",
Expand All @@ -860,11 +861,29 @@ class Test_SimpleSQLite_create_table_from_tabledata:
{u'attr_a': 3, u'attr_b': 120.9,
u'attr_c': u'ccc'},
]
)
),
[(1, 4.0, u'a'), (2, 2.1, u'bb'), (3, 120.9, u'ccc')]
],
[
ptr.TableData(
"multibyte_char",
["姓", "名", "生年月日", "郵便番号", "住所", "電話番号"],
[
["山田", "太郎", "2001/1/1", "100-0002",
"東京都千代田区皇居外苑", "03-1234-5678"],
["山田", "次郎", "2001/1/2", "251-0036",
"神奈川県藤沢市江の島1丁目", "03-9999-9999"],
]
),
[
("山田", "太郎", "2001/1/1", "100-0002",
"東京都千代田区皇居外苑", "03-1234-5678"),
("山田", "次郎", "2001/1/2", "251-0036",
"神奈川県藤沢市江の島1丁目", "03-9999-9999"),
]
],
])
def test_normal(
self, tmpdir, value):
def test_normal(self, tmpdir, value, expected):
p_db = tmpdir.join("tmp.db")

con = SimpleSQLite(str(p_db), "w")
Expand All @@ -876,9 +895,7 @@ def test_normal(

result = con.select(select="*", table_name=value.table_name)
result_matrix = result.fetchall()
assert len(result_matrix) == 3
assert result_matrix == [
(1, 4.0, u'a'), (2, 2.1, u'bb'), (3, 120.9, u'ccc')]
assert result_matrix == expected


class Test_SimpleSQLite_create_table_from_csv:
Expand Down