예제 #1
0
파일: test_fs.py 프로젝트: cecedille1/Sett
class TestLineReplacer(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.patch_open = mock.patch('sett.utils.fs.open', create=True)

    def setUp(self):
        self.lr = LineReplacer('abc.txt')
        self.Opn = self.patch_open.start()
        self.opn = self.Opn.return_value
        self.opn.readlines.return_value = ['abc', 'def', 'ghi']

    def tearDown(self):
        self.patch_open.stop()

    def test_open(self):
        self.lr.open()
        self.Opn.assert_called_once_with('abc.txt', 'r+', encoding='utf-8')

    def test_iterator(self):
        self.lr.open()
        lst = list(self.lr)
        self.assertEqual(lst, [(0, 'abc'), (1, 'def'), (2, 'ghi')])

    def test_close(self):
        self.lr.open()
        self.lr.close()
        self.opn.assert_has_calls([
            mock.call.seek(0),
            mock.call.truncate(),
            mock.call.writelines(['abc', 'def', 'ghi']),
            mock.call.close(),
        ])

    def test_replace(self):
        with self.lr:
            self.lr.replace('def', 'fed')
        self.opn.writelines.assert_called_once_with(['abc', 'fed', 'ghi'])
예제 #2
0
파일: test_fs.py 프로젝트: cecedille1/Sett
 def setUp(self):
     self.lr = LineReplacer('abc.txt')
     self.Opn = self.patch_open.start()
     self.opn = self.Opn.return_value
     self.opn.readlines.return_value = ['abc', 'def', 'ghi']