Skip to content

Commit 15bf083

Browse files
authored
Merge pull request #10 from fobiasmog/master
Custom filename for write_to_csv
2 parents d153d82 + dcb2577 commit 15bf083

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

html_table_extractor/extractor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def parse(self):
7171
def return_list(self):
7272
return self._output
7373

74-
def write_to_csv(self, path='.'):
75-
with open(os.path.join(path, 'output.csv'), 'w') as csv_file:
74+
def write_to_csv(self, path='.', filename='output.csv'):
75+
with open(os.path.join(path, filename), 'w') as csv_file:
7676
table_writer = csv.writer(csv_file)
7777
for row in self._output:
7878
table_writer.writerow(row)

tests/tests.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33

4+
import sys
45
import unittest
6+
7+
if sys.version_info[0] < 3:
8+
import mock
9+
else:
10+
from unittest import mock
11+
512
from bs4 import BeautifulSoup
13+
614
from html_table_extractor.extractor import Extractor
715

816

@@ -132,5 +140,34 @@ def test_return_list(self):
132140
)
133141

134142

143+
@mock.patch('csv.writer')
144+
@mock.patch('html_table_extractor.extractor.open')
145+
class TestWriteToCsv(unittest.TestCase):
146+
def setUp(self):
147+
html = """
148+
<table>
149+
<tr>
150+
<td>1</td>
151+
<td>2</td>
152+
</tr>
153+
<tr>
154+
<td>3</td>
155+
<td>4</td>
156+
</tr>
157+
</table>
158+
"""
159+
self.extractor = Extractor(html)
160+
self.extractor.parse()
161+
mock.mock_open()
162+
163+
def test_write_to_csv_default(self, csv_mock, _):
164+
self.extractor.write_to_csv()
165+
csv_mock.assert_called_with('./output.csv', 'w')
166+
167+
def test_write_to_csv_custom_path_and_filename(self, csv_mock, _):
168+
self.extractor.write_to_csv(path='/test/path', filename='testfile.csv')
169+
csv_mock.assert_called_with('/test/path/testfile.csv', 'w')
170+
171+
135172
if __name__ == '__main__':
136-
unittest.main()
173+
unittest.main()

0 commit comments

Comments
 (0)