File tree Expand file tree Collapse file tree 2 files changed +35
-3
lines changed Expand file tree Collapse file tree 2 files changed +35
-3
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 22# -*- coding: utf-8 -*-
33
44import unittest
5+ from unittest .mock import patch , mock_open
6+
57from bs4 import BeautifulSoup
8+
69from 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+
135167if __name__ == '__main__' :
136- unittest .main ()
168+ unittest .main ()
You can’t perform that action at this time.
0 commit comments