Skip to content

Commit

Permalink
further test coverage improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed May 15, 2015
1 parent 3d3279c commit b75536f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/test_csvz_book.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from pyexcel_io import CSVZipBook, CSVZipWriter, save_data, OrderedDict
import zipfile
from nose.tools import raises
import sys
PY2 = sys.version_info[0] == 2

Expand Down Expand Up @@ -77,6 +78,10 @@ def test_read_one_from_many_by_name(self):
[u'4.0', u'5.0', u'6.0'],
[u'7.0', u'8.0', u'9.0']
]

@raises(ValueError)
def test_read_one_from_many_by_unknown_name(self):
CSVZipBook(self.file, load_sheet_with_name="Sheet X")

def test_read_one_from_many_by_index(self):
b = CSVZipBook(self.file, load_sheet_at_index=0)
Expand All @@ -87,6 +92,10 @@ def test_read_one_from_many_by_index(self):
[u'7.0', u'8.0', u'9.0']
]

@raises(IndexError)
def test_read_one_from_many_by_unknown_index(self):
CSVZipBook(self.file, load_sheet_at_index=999)

def tearDown(self):
os.unlink(self.file)

82 changes: 81 additions & 1 deletion tests/test_sql_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
)
from pyexcel_io.sqlbook import SQLTableReader, SQLTableWriter
from sqlalchemy.orm import relationship, backref
from nose.tools import raises


engine=create_engine("sqlite:///tmp.db")
Base=declarative_base()


class Pyexcel(Base):
__tablename__='pyexcel'
id=Column(Integer, primary_key=True)
Expand Down Expand Up @@ -115,8 +118,69 @@ def test_one_table(self):
query_sets=mysession.query(Pyexcel).all()
results = from_query_sets(self.data[0], query_sets)
assert results == self.results
mysession.close()

def test_one_table_using_mapdict_as_array(self):
mysession = Session()
self.data = [
["Birth Date", "Id", "Name", "Weight"],
[datetime.date(2014, 11, 11), 0, 'Adam', 11.25],
[datetime.date(2014, 11, 12), 1, 'Smith', 12.25]
]
mapdict = ['birth', 'id', 'name', 'weight']

writer = SQLTableWriter(mysession,
[Pyexcel,self.data[0], mapdict, None])
writer.write_array(self.data[1:])
writer.close()
query_sets=mysession.query(Pyexcel).all()
results = from_query_sets(mapdict, query_sets)
assert results == self.results
mysession.close()


def test_one_table_using_mapdict_as_dict(self):
mysession = Session()
self.data = [
["Birth Date", "Id", "Name", "Weight"],
[datetime.date(2014, 11, 11), 0, 'Adam', 11.25],
[datetime.date(2014, 11, 12), 1, 'Smith', 12.25]
]
mapdict = {
"Birth Date":'birth',
"Id":'id',
"Name": 'name',
"Weight": 'weight'
}

writer = SQLTableWriter(mysession,
[Pyexcel,self.data[0], mapdict, None])
writer.write_array(self.data[1:])
writer.close()
query_sets=mysession.query(Pyexcel).all()
results = from_query_sets(['birth', 'id', 'name', 'weight'], query_sets)
assert results == self.results
mysession.close()

@raises(ValueError)
def test_invalid_parameters(self):
mysession = Session()
self.data = [
["Birth Date", "Id", "Name", "Weight"],
[datetime.date(2014, 11, 11), 0, 'Adam', 11.25],
[datetime.date(2014, 11, 12), 1, 'Smith', 12.25]
]
mapdict = {
"Birth Date":'birth',
"Id":'id',
"Name": 'name',
"Weight": 'weight'
}

SQLTableWriter(mysession,
[Pyexcel,self.data[0], mapdict, None, None])


class TestMultipleRead:
def setUp(self):
Base.metadata.drop_all(engine)
Expand Down Expand Up @@ -164,4 +228,20 @@ def test_read(self):
assert json.dumps(data) == '{"category": [["id", "name"], [1, "News"], [2, "Sports"]], "post": [["body", "category_id", "id", "pub_date", "title"], ["formal", 1, 1, "2015-01-20T23:28:29", "Title A"], ["informal", 2, 2, "2015-01-20T23:28:30", "Title B"]]}'

def tearDown(self):
self.session.close()
self.session.close()


class TestZeroRead:
def setUp(self):
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

def test_sql(self):
mysession=Session()
sheet = SQLTableReader(mysession, Pyexcel)
data = sheet.to_array()
content = []
# 'pyexcel'' here is the table name
assert data == content
mysession.close()

0 comments on commit b75536f

Please sign in to comment.