-
Notifications
You must be signed in to change notification settings - Fork 18
/
tests.py
206 lines (170 loc) · 6.52 KB
/
tests.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from unittest import TestCase
from nose.tools import assert_equals, assert_true
from google_spreadsheet.api import SpreadsheetAPI
from test_settings import (GOOGLE_SPREADSHEET_USER,
GOOGLE_SPREADSHEET_PASSWORD,
GOOGLE_SPREADSHEET_SOURCE,
GOOGLE_SPREADSHEET_KEY,
GOOGLE_WORKSHEET_KEY,
COLUMN_NAME,
COLUMN_UNIQUE_VALUE)
class TestSpreadsheetAPI(TestCase):
"""Test Google Spreadsheet API
Test Class for Google Spreadsheet API wrapper.
"""
def setUp(self):
"""Set Up.
Initialize the Amazon API wrapper. The following values:
* GOOGLE_SPREADSHEET_USER
* GOOGLE_SPREADSHEET_PASSWORD
* GOOGLE_SPREADSHEET_SOURCE
* GOOGLE_SPREADSHEET_KEY
* GOOGLE_WORKSHEET_KEY
* COLUMN_NAME
* COLUMN_UNIQUE_VALUE
Are imported from a custom file named: 'test_settings.py'
"""
self.spreadsheet = SpreadsheetAPI(GOOGLE_SPREADSHEET_USER,
GOOGLE_SPREADSHEET_PASSWORD, GOOGLE_SPREADSHEET_SOURCE)
def test_list_spreadsheets(self):
"""Test List Spreadsheets.
Tests the list spreadsheets method by calling it and testing that at
least one result was returned.
"""
sheets = self.spreadsheet.list_spreadsheets()
assert_true(len(sheets))
def test_list_worksheets(self):
"""Test List Worksheets.
Tests the list worksheets method by calling it and testing that at
least one result was returned.
"""
sheets = self.spreadsheet.list_worksheets(GOOGLE_SPREADSHEET_KEY)
assert_true(len(sheets))
def test_get_worksheet(self):
"""Test Get Worksheet.
Tests the get worksheet method by calling it and testing that a
result was returned.
"""
sheet = self.spreadsheet.get_worksheet(GOOGLE_SPREADSHEET_KEY,
GOOGLE_WORKSHEET_KEY)
assert_true(sheet)
class TestWorksheet(TestCase):
"""Test Worksheet Class
Test Class for Worksheet.
"""
def setUp(self):
"""Set Up.
Initialize the Amazon API wrapper. The following values:
* GOOGLE_SPREADSHEET_USER
* GOOGLE_SPREADSHEET_PASSWORD
* GOOGLE_SPREADSHEET_SOURCE
* GOOGLE_SPREADSHEET_KEY
* GOOGLE_WORKSHEET_KEY
* COLUMN_NAME
* COLUMN_UNIQUE_VALUE
Are imported from a custom file named: 'test_settings.py'
"""
self.spreadsheet = SpreadsheetAPI(GOOGLE_SPREADSHEET_USER,
GOOGLE_SPREADSHEET_PASSWORD, GOOGLE_SPREADSHEET_SOURCE)
self.sheet = self.spreadsheet.get_worksheet(GOOGLE_SPREADSHEET_KEY,
GOOGLE_WORKSHEET_KEY)
def test_get_rows(self):
"""Test Get Rows.
"""
rows = self.sheet.get_rows()
assert_true(len(rows))
def test_update_row_by_index(self):
"""Test Update Rows By Index.
First gets all rows, than updates last row.
"""
rows = self.sheet.get_rows()
row_index = len(rows) - 1
new_row = rows[0]
row = self.sheet.update_row_by_index(row_index, new_row)
del row['__rowid__']
del new_row['__rowid__']
assert_equals(row, new_row)
def test_update_row_by_id(self):
"""Test Update Rows By ID.
First gets all rows, than updates last row.
"""
rows = self.sheet.get_rows()
new_row = rows[0]
row = self.sheet.update_row(new_row)
assert_equals(row, new_row)
def test_insert_delete_row(self):
"""Test Insert and Delete Row.
First gets all rows, than inserts a new row, finally deletes the new
row.
"""
rows = self.sheet.get_rows()
num_rows = len(rows)
new_row = rows[0]
self.sheet.insert_row(new_row)
insert_rows = self.sheet.get_rows()
assert_equals(len(insert_rows), num_rows + 1)
self.sheet._flush_cache()
insert_rows = self.sheet.get_rows()
assert_equals(len(insert_rows), num_rows + 1)
self.sheet.delete_row_by_index(num_rows)
delete_rows = self.sheet.get_rows()
assert_equals(len(delete_rows), num_rows)
assert_equals(delete_rows[-1], rows[-1])
self.sheet._flush_cache()
delete_rows = self.sheet.get_rows()
assert_equals(len(delete_rows), num_rows)
assert_equals(delete_rows[-1], rows[-1])
def test_delete_by_id(self):
"""Test Delete Row By ID.
First gets all rows, than inserts a new row, finally deletes the new
row by ID.
"""
rows = self.sheet.get_rows()
num_rows = len(rows)
new_row = rows[0]
new_row = self.sheet.insert_row(new_row)
insert_rows = self.sheet.get_rows()
assert_equals(len(insert_rows), num_rows + 1)
self.sheet._flush_cache()
insert_rows = self.sheet.get_rows()
assert_equals(len(insert_rows), num_rows + 1)
self.sheet.delete_row(new_row)
delete_rows = self.sheet.get_rows()
assert_equals(len(delete_rows), num_rows)
assert_equals(delete_rows[-1], rows[-1])
self.sheet._flush_cache()
delete_rows = self.sheet.get_rows()
assert_equals(len(delete_rows), num_rows)
assert_equals(delete_rows[-1], rows[-1])
def test_delete_all_rows(self):
"""Tests deleting of all rows in the sheet
"""
# first retrieve rows and store in memory to re-add after test
rows = self.sheet.get_rows()
self.sheet.delete_all_rows()
assert_equals(len(self.sheet.get_rows()), 0)
# add back the rows that were there so the other tests still pass
for row in rows:
self.sheet.insert_row(row)
def test_query(self):
"""Test Query.
Filter rows by a unique column vlaue.
"""
rows = self.sheet.get_rows(
query='{0} = {1}'.format(COLUMN_NAME, COLUMN_UNIQUE_VALUE))
assert_equals(len(rows), 1)
def test_sort(self):
"""Test Sort.
Sort ascending and descending.
"""
rows = self.sheet.get_rows(
order_by='column:{0}'.format(COLUMN_NAME), reverse='false')
assert_true(rows)
def test_filter(self):
"""Test Filter.
Tests filter in memory.
"""
filtered_rows = self.sheet.get_rows(
filter_func=lambda row: row[COLUMN_NAME] == unicode(
COLUMN_UNIQUE_VALUE))
assert_equals(1, len(filtered_rows))