forked from pyexcel/pyexcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sql.py
106 lines (94 loc) · 3.04 KB
/
test_sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import datetime
from textwrap import dedent
from unittest import TestCase
from nose.tools import eq_, raises
import pyexcel as pe
from .db import Base, Pyexcel, Session, engine
class TestSQL(TestCase):
def setUp(self):
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
p1 = Pyexcel(
id=0, name="Adam", weight=11.25, birth=datetime.date(2014, 11, 11)
)
session = Session()
session.add(p1)
p1 = Pyexcel(
id=1, name="Smith", weight=12.25, birth=datetime.date(2014, 11, 12)
)
session.add(p1)
session.commit()
def test_sql(self):
sheet = pe.get_sheet(session=Session(), table=Pyexcel)
content = dedent(
"""
pyexcel:
+------------+----+-------+--------+
| birth | id | name | weight |
+------------+----+-------+--------+
| 2014-11-11 | 0 | Adam | 11.25 |
+------------+----+-------+--------+
| 2014-11-12 | 1 | Smith | 12.25 |
+------------+----+-------+--------+"""
).strip("\n")
self.assertEqual(str(sheet), content)
def test_sql_sheet(self):
sheet = pe.get_sheet(
session=Session(),
table=Pyexcel,
sheet_name="custom sheet",
export_columns=["weight", "birth"],
)
content = dedent(
"""
custom sheet:
+--------+------------+
| weight | birth |
+--------+------------+
| 11.25 | 2014-11-11 |
+--------+------------+
| 12.25 | 2014-11-12 |
+--------+------------+"""
).strip("\n")
self.assertEqual(str(sheet), content)
class TestEmptyTable:
def setUp(self):
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
def test_empty_table(self):
sheet = pe.get_sheet(session=Session(), table=Pyexcel)
assert sheet is not None
@raises(Exception)
def test_save_as(self):
data = [
["birth", "name", "weight"],
[datetime.date(2017, 1, 11), "Adam", 3.0],
]
session = Session()
pe.save_as(array=data, dest_session=session, dest_table=Pyexcel)
sheet = pe.get_sheet(session=session, table=Pyexcel)
eq_(str(sheet), "")
def test_save_as_1(self):
data = [
["birth", "name", "weight"],
[datetime.date(2017, 1, 11), "Adam", 3.0],
]
session = Session()
pe.save_as(
array=data,
name_columns_by_row=0,
dest_session=session,
dest_table=Pyexcel,
)
sheet = pe.get_sheet(session=session, table=Pyexcel)
print(sheet)
content = dedent(
"""
pyexcel:
+------------+----+------+--------+
| birth | id | name | weight |
+------------+----+------+--------+
| 2017-01-11 | 1 | Adam | 3.0 |
+------------+----+------+--------+"""
).strip("\n")
eq_(str(sheet), content)