Exemplo n.º 1
0
 def setUp(self):
     self.tempdir = mkdtemp()
     self.fs = DataStoreFS(self.tempdir)
Exemplo n.º 2
0
class TestDataStoreFS(TestCase):
    def setUp(self):
        self.tempdir = mkdtemp()
        self.fs = DataStoreFS(self.tempdir)

    def tearDown(self):
        rmtree(self.tempdir)

    @contextmanager
    def assert_tempdir_stays_empty(self):
        yield

        files_in_temp = list(os.listdir(os.path.join(self.tempdir, '__temp')))
        self.assertTrue(
            not files_in_temp,
            "we do not expect files in temp dir, found %s" % files_in_temp
        )

    def create_file(self, path, content):
        with open(os.path.join(self.tempdir, path), mode='w') as outf:
            outf.write(content)

    def create_empty_file(self, path):
        self.create_file(path, '')

    def test_it_list_files(self):
        self.create_empty_file('b')
        self.create_empty_file('c')
        self.assertEquals(2, len(list(self.fs.listfiles(''))))

    def test_it_list_top_dirs(self):
        dirpath = os.path.join(self.tempdir, 'd')

        os.mkdir(dirpath)
        self.assertEquals(set([
            os.path.join(self.tempdir, '__temp'),
            dirpath,
        ]), set(self.fs.listdirs()))

    def test_it_opens_file_for_reading(self):
        sentinel = 'abcdefg'
        self.create_file('b', sentinel)

        with self.fs.open_for_reading('b') as inf:
            self.assertEquals(sentinel, inf.read())

    def test_it_creates_files(self):
        sentinel = 'abcdefg'

        with self.assert_tempdir_stays_empty():
            with self.fs.open_new_file('c') as outf:
                outf.write(sentinel)

        with self.fs.open_for_reading('c') as inf:
            self.assertEquals(sentinel, inf.read())


    def test_it_create_files_iff_they_dont_exist_yet(self):
        with self.assert_tempdir_stays_empty():
            with self.fs.open_new_file('c') as outf:
                outf.write('A')

        has_entered = []

        def create_again():
            with self.fs.open_new_file('c') as outf:
                has_entered.append(True)

        with self.assert_tempdir_stays_empty():
            self.assertRaises(FileAlreadyExistsException, create_again)
        self.assertTrue(has_entered)