File tree Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 11#!/usr/bin/python
22# -*- coding: utf-8 -*-
33
4+ import sys
45import unittest
6+
7+ if sys .version_info [0 ] < 3 :
8+ import mock
9+ else :
10+ from unittest import mock
11+
512from bs4 import BeautifulSoup
13+
614from 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+
135172if __name__ == '__main__' :
136- unittest .main ()
173+ unittest .main ()
You can’t perform that action at this time.
0 commit comments