Skip to content

Commit d35450e

Browse files
committed
test cases written
1 parent d58479b commit d35450e

File tree

10 files changed

+326
-33
lines changed

10 files changed

+326
-33
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.pyc
2+
*~
3+
.coverage

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: python
2+
notifications:
3+
email: false
4+
env:
5+
global:
6+
python:
7+
- 2.6
8+
- 2.7
9+
- 3.3
10+
- 3.4
11+
- pypy
12+
install:
13+
- pip install git+https://github.com/chfw/pyexcel-io.git
14+
- pip install git+https://github.com/chfw/pyexcel.git
15+
- pip install -r tests/requirements.txt
16+
script:
17+
make test
18+
after_success:
19+
coveralls

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ or clone it and install it::
3030
Dependencies
3131
============
3232

33+
* pyexcel

pyexcel_webio/__init__.py

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ExcelInput(object):
2424
2525
The source could be from anywhere, memory or file system
2626
"""
27-
def load_single_sheet(self, file_name, sheet_name=None, **keywords):
27+
def load_single_sheet(self, sheet_name=None, **keywords):
2828
"""Abstract method
2929
3030
:param form_field_name: the file field name in the html form for file upload
@@ -36,7 +36,7 @@ def load_single_sheet(self, file_name, sheet_name=None, **keywords):
3636
"""
3737
raise NotImplementedError("Please implement this function")
3838

39-
def load_book(self, file_name, **keywords):
39+
def load_book(self, **keywords):
4040
"""Abstract method
4141
4242
:param form_field_name: the file field name in the html form for file upload
@@ -45,7 +45,7 @@ def load_book(self, file_name, **keywords):
4545
"""
4646
raise NotImplementedError("Please implement this function")
4747

48-
def get_sheet(self, file_name, sheet_name=None, **keywords):
48+
def get_sheet(self, sheet_name=None, **keywords):
4949
"""
5050
Get a :class:`Sheet` instance from the file
5151
@@ -56,9 +56,9 @@ def get_sheet(self, file_name, sheet_name=None, **keywords):
5656
:param keywords: additional key words
5757
:returns: A sheet object
5858
"""
59-
return self.load_single_sheet(file_name, sheet_name, **keywords)
59+
return self.load_single_sheet(sheet_name=sheet_name, **keywords)
6060

61-
def get_array(self, file_name, sheet_name=None, **keywords):
61+
def get_array(self, sheet_name=None, **keywords):
6262
"""
6363
Get a list of lists from the file
6464
@@ -69,10 +69,13 @@ def get_array(self, file_name, sheet_name=None, **keywords):
6969
:param keywords: additional key words
7070
:returns: A list of lists
7171
"""
72-
sheet = self.get_sheet(file_name, sheet_name, **keywords)
73-
return sheet.to_array()
72+
sheet = self.get_sheet(sheet_name=sheet_name, **keywords)
73+
if sheet:
74+
return sheet.to_array()
75+
else:
76+
return None
7477

75-
def get_dict(self, file_name, sheet_name=None, name_columns_by_row=0, name_rows_by_column=-1, **keywords):
78+
def get_dict(self, sheet_name=None, name_columns_by_row=0, **keywords):
7679
"""Get a dictionary from the file
7780
7881
:param form_field_name: the file field name in the html form for file upload
@@ -82,12 +85,13 @@ def get_dict(self, file_name, sheet_name=None, name_columns_by_row=0, name_rows_
8285
:param keywords: additional key words
8386
:returns: A dictionary
8487
"""
85-
sheet = self.load_single_sheet(file_name, sheet_name,
86-
name_columns_by_row=name_columns_by_row,
87-
name_rows_by_column=name_rows_by_column, **keywords)
88-
return sheet.to_dict()
88+
sheet = self.load_single_sheet(sheet_name=sheet_name, name_columns_by_row=name_columns_by_row, **keywords)
89+
if sheet:
90+
return sheet.to_dict()
91+
else:
92+
return None
8993

90-
def get_records(self, file_name, sheet_name=None, name_columns_by_row=0, name_rows_by_column=-1, **keywords):
94+
def get_records(self, sheet_name=None, name_columns_by_row=0, **keywords):
9195
"""Get a list of records from the file
9296
9397
:param form_field_name: the file field name in the html form for file upload
@@ -97,52 +101,78 @@ def get_records(self, file_name, sheet_name=None, name_columns_by_row=0, name_ro
97101
:param keywords: additional key words
98102
:returns: A list of records
99103
"""
100-
sheet = self.load_single_sheet(file_name, sheet_name,
101-
name_columns_by_row=name_columns_by_row,
102-
name_rows_by_column=name_rows_by_column, **keywords)
103-
return sheet.to_records()
104+
sheet = self.load_single_sheet(sheet_name=sheet_name, name_columns_by_row=name_columns_by_row, **keywords)
105+
if sheet:
106+
return sheet.to_records()
107+
else:
108+
return None
104109

105-
def get_book(self, file_name, **keywords):
110+
def save_to_database(self, session=None, table=None, sheet_name=None, name_columns_by_row=0, **keywords):
111+
sheet = self.load_single_sheet(sheet_name=sheet_name, name_columns_by_row=name_columns_by_row, **keywords)
112+
sheet.save_to_database(session, table)
113+
114+
def get_book(self, **keywords):
106115
"""Get a instance of :class:`Book` from the file
116+
107117
:param form_field_name: the file field name in the html form for file upload
108118
:param keywords: additional key words
109119
:returns: A instance of :class:`Book`
110120
"""
111-
return self.load_book(file_name, **keywords)
121+
return self.load_book(**keywords)
112122

113-
def get_book_dict(self, file_name, **keywords):
123+
def get_book_dict(self, **keywords):
114124
"""Get a dictionary of two dimensional array from the file
115125
116126
:param form_field_name: the file field name in the html form for file upload
117127
:param keywords: additional key words
118128
:returns: A dictionary of two dimensional arrays
119129
"""
120-
book = self.get_book(file_name, **keywords)
121-
return book.to_dict()
130+
book = self.load_book(**keywords)
131+
if book:
132+
return book.to_dict()
133+
else:
134+
return None
135+
136+
def save_book_to_database(self, session, tables, **keywords):
137+
book = self.load_book(**keywords)
138+
book.save_to_database(session, tables)
139+
122140

123141
def dumpy_func(content, content_type=None, status=200):
124142
return None
125143

144+
126145
ExcelResponse = dumpy_func
127146

128-
def make_response(pyexcel_instance, file_type, status=200):
147+
148+
def make_response(pyexcel_instance, file_type, status=200, **keywords):
129149
io = BytesIO()
130-
pyexcel_instance.save_to_memory(file_type, io)
150+
pyexcel_instance.save_to_memory(file_type, io, **keywords)
131151
io.seek(0)
132152
return ExcelResponse(io.read(), content_type=FILE_TYPE_MIME_TABLE[file_type], status=status)
133153

134154

135-
def make_response_from_array(array, file_type, status=200):
136-
return make_response(pe.Sheet(array), file_type, status)
155+
def make_response_from_array(array, file_type, status=200, **keywords):
156+
return make_response(pe.Sheet(array), file_type, status, **keywords)
137157

138158

139-
def make_response_from_dict(adict, file_type, status=200):
140-
return make_response(pe.load_from_dict(adict), file_type, status)
159+
def make_response_from_dict(adict, file_type, status=200, **keywords):
160+
return make_response(pe.load_from_dict(adict), file_type, status, **keywords)
161+
162+
163+
def make_response_from_records(records, file_type, status=200, **keywords):
164+
return make_response(pe.load_from_records(records), file_type, status, **keywords)
165+
166+
167+
def make_response_from_book_dict(adict, file_type, status=200, **keywords):
168+
return make_response(pe.Book(adict), file_type, status, **keywords)
141169

142170

143-
def make_response_from_records(records, file_type, status=200):
144-
return make_response(pe.load_from_records(records), file_type, status)
171+
def make_response_from_a_table(session, table, file_type, status=200, **keywords):
172+
sheet = pe.get_sheet(session=session, table=table, **keywords)
173+
return make_response(sheet, file_type, status, **keywords)
145174

146175

147-
def make_response_from_book_dict(adict, file_type, status=200):
148-
return make_response(pe.Book(adict), file_type, status)
176+
def make_response_from_tables(session, tables, file_type, status=200, **keywords):
177+
book = pe.get_book(session=session, tables=tables, **keywords)
178+
return make_response(book, file_type, status, **keywords)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
author="C. W.",
1818
version='0.0.1',
1919
author_email="wangc_2011@hotmail.com",
20-
url="https://github.com/chfw/flask-pyexcel",
20+
url="https://github.com/chfw/pyexcel-webio",
2121
description='A generic web extension for pyexcel.',
2222
install_requires=dependencies,
2323
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),

test.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nosetests --with-cov --with-doctest --doctest-extension=.rst

test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nosetests --rednose --with-cov --with-doctest --doctest-extension=.rst

tests/db.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from sqlalchemy import create_engine
2+
from sqlalchemy.ext.declarative import declarative_base
3+
from sqlalchemy import Column , Integer, String, Float, Date
4+
from sqlalchemy.orm import sessionmaker
5+
6+
engine=create_engine("sqlite:///tmp.db")
7+
Base=declarative_base()
8+
9+
class Signature(Base):
10+
__tablename__="signature"
11+
X=Column(Integer, primary_key=True)
12+
Y=Column(Integer)
13+
Z=Column(Integer)
14+
15+
class Signature2(Base):
16+
__tablename__="signature2"
17+
A=Column(Integer, primary_key=True)
18+
B=Column(Integer)
19+
C=Column(Integer)
20+
21+
Session=sessionmaker(bind=engine)
22+

tests/requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
nose
2+
rednose
3+
nose-cov
4+
python-coveralls
5+
coverage
6+
pyexcel-xls

0 commit comments

Comments
 (0)