Beispiel #1
0
class StoreTest(unittest.TestCase):

    prefix = "test"
    postfix = "file"
    tmp_dir = tempfile.gettempdir()
    lines_per_file = 50

    def setUp(self):
        self.store = AutoSplittingFile(self.tmp_dir, self.lines_per_file,
                                       self.prefix, self.postfix)

    def tearDown(self):
        self.store.close()

    def test_file_opening_closing(self):
        self.assertIsNone(self.store._file)
        name = self.store.generate_file_path()
        self.store.open(name)
        self.assertIsNotNone(self.store._file)
        self.assertFalse(self.store._file.closed)
        self.assertEqual(name, self.store._file.name)
        self.store.close()
        self.assertIsNone(self.store._file)

    def test_write(self):
        some_text = "test_write"
        self.store.write(some_text)
        file = self.store._file.name
        self.store.close()
        with gzip.open(file, 'rt') as f:
            self.assertEqual(some_text, f.readline().strip())

    def test_multiple_writes(self):
        some_text = "test_multiple_writes"
        times = min(10, self.lines_per_file - 1)
        name = self.store.generate_file_path()
        self.store.open(name)
        for i in range(times):
            self.assertEqual(i, self.store._stored_matches)
            self.store.write(some_text)
            self.assertEqual(name, self.store._file.name)
            self.assertEqual(i + 1, self.store._stored_matches)

        self.store.close()
        with gzip.open(name, 'rt') as f:
            i = 0
            for line in f.readlines():
                self.assertEqual(some_text, line.strip())
                i += 1
            self.assertEqual(times, i)

    def test_multi_file_writing(self):
        some_text = "test_multi_file_writing"
        self.store.open(self.store.generate_file_path())
        file = self.store._file.name
        written_lines = 55
        for i in range(self.lines_per_file):
            self.store.write(some_text)
            self.assertEqual(file, self.store._file.name)

        self.store.write(some_text)
        self.assertEqual(1, self.store._stored_matches)
        second_file = self.store._file.name
        for i in range(written_lines - self.lines_per_file - 1):
            self.assertEqual(i + 1, self.store._stored_matches)
            self.store.write(some_text)
            self.assertEqual(second_file, self.store._file.name)

        self.assertNotEqual(file, second_file)
        self.store.close()
        with gzip.open(file, 'rt') as f:
            i = 0
            for line in f.readlines():
                self.assertEqual(some_text, line.strip())
                i += 1
            self.assertEqual(self.lines_per_file, i)

        with gzip.open(second_file, 'rt') as f:
            i = 0
            for line in f.readlines():
                self.assertEqual(some_text, line.strip())
                i += 1
            self.assertEqual(written_lines - self.lines_per_file, i)
Beispiel #2
0
class StoreTest(unittest.TestCase):

    prefix = "test"
    postfix = "file"
    tmp_dir = tempfile.gettempdir()
    lines_per_file = 50

    def setUp(self):
        self.store = AutoSplittingFile(self.tmp_dir, self.lines_per_file, self.prefix, self.postfix)

    def tearDown(self):
        self.store.close()

    def test_file_opening_closing(self):
        self.assertIsNone(self.store._file)
        name = self.store.generate_file_path()
        self.store.open(name)
        self.assertIsNotNone(self.store._file)
        self.assertFalse(self.store._file.closed)
        self.assertEqual(name, self.store._file.name)
        self.store.close()
        self.assertIsNone(self.store._file)

    def test_write(self):
        some_text = "test_write"
        self.store.write(some_text)
        file = self.store._file.name
        self.store.close()
        with gzip.open(file, 'rt') as f:
            self.assertEqual(some_text, f.readline().strip())

    def test_multiple_writes(self):
        some_text = "test_multiple_writes"
        times = min(10, self.lines_per_file -1)
        name = self.store.generate_file_path()
        self.store.open(name)
        for i in range(times):
            self.assertEqual(i, self.store._stored_matches)
            self.store.write(some_text)
            self.assertEqual(name, self.store._file.name)
            self.assertEqual(i+1, self.store._stored_matches)

        self.store.close()
        with gzip.open(name, 'rt') as f:
            i = 0
            for line in f.readlines():
                self.assertEqual(some_text, line.strip())
                i += 1
            self.assertEqual(times,i)

    def test_multi_file_writing(self):
        some_text = "test_multi_file_writing"
        self.store.open(self.store.generate_file_path())
        file = self.store._file.name
        written_lines = 55
        for i in range(self.lines_per_file):
            self.store.write(some_text)
            self.assertEqual(file, self.store._file.name)

        self.store.write(some_text)
        self.assertEqual(1, self.store._stored_matches)
        second_file= self.store._file.name
        for i in range(written_lines - self.lines_per_file -1):
            self.assertEqual(i+1, self.store._stored_matches)
            self.store.write(some_text)
            self.assertEqual(second_file, self.store._file.name)

        self.assertNotEqual(file, second_file)
        self.store.close()
        with gzip.open(file, 'rt') as f:
            i = 0
            for line in f.readlines():
                self.assertEqual(some_text, line.strip())
                i += 1
            self.assertEqual(self.lines_per_file, i)

        with gzip.open(second_file, 'rt') as f:
            i = 0
            for line in f.readlines():
                self.assertEqual(some_text, line.strip())
                i += 1
            self.assertEqual(written_lines-self.lines_per_file, i)
Beispiel #3
0
 def setUp(self):
     self.store = AutoSplittingFile(self.tmp_dir, self.lines_per_file,
                                    self.prefix, self.postfix)
Beispiel #4
0
 def setUp(self):
     self.store = AutoSplittingFile(self.tmp_dir, self.lines_per_file, self.prefix, self.postfix)