Skip to content

Commit

Permalink
Merge pull request #40 from thombashi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
thombashi committed Aug 11, 2016
2 parents 4d552de + 82ccbe7 commit dfeafc4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -23,7 +23,7 @@

setuptools.setup(
name="SimpleSQLite",
version="0.4.5",
version="0.4.6",
url="https://github.com/thombashi/SimpleSQLite",
bugtrack_url="https://github.com/thombashi/SimpleSQLite/issues",

Expand Down
13 changes: 11 additions & 2 deletions simplesqlite/core.py
Expand Up @@ -994,9 +994,9 @@ def create_table_with_data(
:py:meth:`.create_index_list`
"""

validate_table_name(table_name)

self.validate_access_permission(["w", "a"])
validate_table_name(table_name)
self.__validate_attr_name_list(attribute_name_list)

if dataproperty.is_empty_sequence(data_matrix):
raise ValueError("input data is null: '{:s} ({:s})'".format(
Expand Down Expand Up @@ -1185,6 +1185,15 @@ def __verify_sqlite_db_file(self, database_path):
connection = sqlite3.connect(database_path)
connection.close()

@staticmethod
def __validate_attr_name_list(attr_name_list):
if dataproperty.is_empty_sequence(attr_name_list):
raise ValueError("attribute name list is empty")

for attr_name in attr_name_list:
if dataproperty.is_empty_string(attr_name):
raise ValueError("attribute name includes an empty string")

@staticmethod
def __verify_value_matrix(field_list, value_matrix):
"""
Expand Down
46 changes: 34 additions & 12 deletions simplesqlite/loader/spreadsheet/gsloader.py
Expand Up @@ -13,6 +13,7 @@
from ..error import InvalidDataError
from ..data import TableData
from .core import SpreadSheetLoader
from ..._func import connect_sqlite_db_mem


class GoogleSheetsTableLoader(SpreadSheetLoader):
Expand Down Expand Up @@ -93,16 +94,22 @@ def load(self):
gc = gspread.authorize(credentials)
for worksheet in gc.open(self.title).worksheets():
self._worksheet = worksheet
self.__all_values = worksheet.get_all_values()
self.__all_values = [row for row in worksheet.get_all_values()]

if self._is_empty_sheet():
continue

self.__strip_empty_col()
try:
self.__strip_empty_col()
except ValueError:
continue

value_matrix = self.__all_values[self._get_start_row_idx():]
header_list = value_matrix[0]
record_list = value_matrix[1:]
try:
header_list = value_matrix[0]
record_list = value_matrix[1:]
except IndexError:
continue

yield TableData(self.make_table_name(), header_list, record_list)

Expand All @@ -127,16 +134,31 @@ def _validate_title(self):
raise ValueError("spreadsheet title is empty")

def __strip_empty_col(self):
col_idx = 0
t_value_matrix = zip(*self.__all_values)

for col_value_list in t_value_matrix:
from ...sqlquery import SqlQuery

con = connect_sqlite_db_mem()

tmp_table_name = "tmp"
header_list = [
"a{:d}".format(i)
for i in range(len(self.__all_values[0]))
]
con.create_table_with_data(
tmp_table_name, header_list, self.__all_values)
for col_idx, header in enumerate(header_list):
result = con.select(
select=SqlQuery.to_attr_str(header), table_name=tmp_table_name)
if any([
dataproperty.is_not_empty_string(value)
for value in col_value_list
dataproperty.is_not_empty_string(record[0])
for record in result.fetchall()
]):
break

col_idx += 1
strip_header_list = header_list[col_idx:]
if dataproperty.is_empty_sequence(strip_header_list):
raise ValueError()

self.__all_values = zip(*t_value_matrix[col_idx:])
result = con.select(
select=",".join(SqlQuery.to_attr_str_list(strip_header_list)),
table_name=tmp_table_name)
self.__all_values = result.fetchall()
23 changes: 23 additions & 0 deletions test/test_simplesqlite.py
Expand Up @@ -748,6 +748,29 @@ def test_normal(
assert len(result_matrix) == 3
assert con.get_attr_type(table_name) == expected_attr

@pytest.mark.parametrize(
[
"attr_name_list", "data_matrix",
"index_attr_list", "expected",
],
[
[
[""], [["a"], ["bb"], ["ccc"]],
[], ValueError,
],
]
)
def test_exception_empty_header(
self, tmpdir, attr_name_list, data_matrix, index_attr_list,
expected):
p = tmpdir.join("tmp.db")
con = SimpleSQLite(str(p), "w")
table_name = TEST_TABLE_NAME

with pytest.raises(expected):
con.create_table_with_data(
table_name, attr_name_list, data_matrix, index_attr_list)

def test_null(self, con_null):
with pytest.raises(NullDatabaseConnectionError):
con_null.create_table_with_data(
Expand Down

0 comments on commit dfeafc4

Please sign in to comment.