Skip to content

Commit 0d6293e

Browse files
committed
custom filename in write_to_csv + some codestyle from pycharm
1 parent 0222439 commit 0d6293e

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

html_table_extractor/extractor.py

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

72-
def write_to_csv(self, path='.'):
73-
with open(os.path.join(path, 'output.csv'), 'w') as csv_file:
72+
def write_to_csv(self, path='.', filename='output.csv'):
73+
with open(os.path.join(path, filename), 'w') as csv_file:
7474
table_writer = csv.writer(csv_file)
7575
for row in self._output:
7676
table_writer.writerow(row)

tests/tests.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
# -*- coding: utf-8 -*-
33

44
import unittest
5+
from unittest.mock import patch, mock_open
6+
57
from bs4 import BeautifulSoup
8+
69
from html_table_extractor.extractor import Extractor
710

811

@@ -132,5 +135,34 @@ def test_return_list(self):
132135
)
133136

134137

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

0 commit comments

Comments
 (0)