Skip to content

Commit

Permalink
Adds some tests for create_store()
Browse files Browse the repository at this point in the history
  • Loading branch information
remram44 committed Sep 27, 2013
1 parent 3d2b7f0 commit 90316bb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
8 changes: 6 additions & 2 deletions file_archive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,16 @@ def create_store(path):
if os.path.exists(path):
if not os.path.isdir(path) or os.listdir(path):
raise CreationError("Path is not a directory or is not empty")
exists = True
else:
exists = False
try:
os.mkdir(path)
if not exists:
os.mkdir(path)
os.mkdir(os.path.join(path, 'objects'))
except OSError, e:
raise CreationError("Could not create directories: %s: %s" % (
e.__class__.__name__))
e.__class__.__name__, e.message))
MetadataStore.create_db(os.path.join(path, 'database'))

def close(self):
Expand Down
Empty file added tests/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions tests/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import sys
try:
import unittest2 as unittest
except ImportError:
import unittest


top_level = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
start_dir = os.path.join(top_level, 'tests')
if not top_level in sys.path:
sys.path.insert(0, top_level)


sys.path.append(start_dir)


class Program(unittest.TestProgram):
def createTests(self):
if self.testNames is None:
self.test = self.testLoader.discover(
start_dir=start_dir,
pattern='test_*.py',
top_level_dir=top_level)
else:
self.test = self.testLoader.loadTestsFromNames(self.testNames)

Program()
48 changes: 48 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import contextlib
import os
import shutil
import tempfile

try:
import unittest2 as unittest
except ImportError:
import unittest

from file_archive import FileStore
from file_archive.errors import CreationError


@contextlib.contextmanager
def temp_dir(make=True):
path = tempfile.mkdtemp(prefix='test_file_archive_')
try:
if make:
yield path
else:
yield path + 'internal'
finally:
shutil.rmtree(path)


class TestCreate(unittest.TestCase):
"""Tests the creation of a new file store on disk.
"""
def test_create(self):
with temp_dir(False) as d:
FileStore.create_store(d)
self.assertTrue(os.path.isdir(d))
self.assertTrue(os.path.isfile(os.path.join(d, 'database')))
with temp_dir(True) as d:
FileStore.create_store(d)
self.assertTrue(os.path.isfile(os.path.join(d, 'database')))

def test_create_nonempty(self):
with temp_dir() as d:
with open(os.path.join(d, 'somefile'), 'wb') as fp:
fp.write("I'm not empty\n")
with self.assertRaises(CreationError):
FileStore.create_store(d)
with temp_dir() as d:
FileStore.create_store(d)
with self.assertRaises(CreationError):
FileStore.create_store(d)

0 comments on commit 90316bb

Please sign in to comment.