Skip to content
Browse files

more nicer unittest

  • Loading branch information...
1 parent 75b91d5 commit 78418aa632b4bee7242884ad49bf9462bf428a10 aloneroad committed Jan 5, 2011
Showing with 56 additions and 89 deletions.
  1. +6 −6 pymogile/client.py
  2. +5 −0 pymogile/{http.py → file.py}
  3. +1 −7 setup.py
  4. +9 −2 tests/test_backend.py
  5. +35 −74 tests/test_client.py
View
12 pymogile/client.py
@@ -7,8 +7,8 @@
This module is a client library for the MogileFS distributed file system
"""
from pymogile.backend import Backend
-from pymogile.exceptions import MogileFSError, MogileFSError
-from pymogile.http import NewHttpFile, ClientHttpFile
+from pymogile.exceptions import MogileFSError
+from pymogile.file import NewHttpFile, ClientHttpFile
class Client(object):
def __init__(self, domain, trackers, readonly=False):
@@ -24,13 +24,13 @@ def add_hook(self, hookname, *args):
pass
def add_backend_hook(self):
- pass
+ raise NotImplementedError()
def errstr(self):
- pass
+ raise NotImplementedError()
def errcode(self):
- pass
+ raise NotImplementedError()
@property
@@ -70,7 +70,7 @@ def new_file(self, key, cls=None, largefile=False, content_length=0,
if not res:
return None
- # [ (devid,path), (devid,path), ... ]
+ # [(devid, path), (devid, path),... ]
dests = []
# determine old vs. new format to populate destinations
if 'dev_count' not in res:
View
5 pymogile/http.py → pymogile/file.py
@@ -17,6 +17,7 @@ def get_content_length(response):
except (TypeError, ValueError):
return 0
+
class HttpFile(object):
def __init__(self, mg, fid, key, cls, create_close_arg=None):
self.mg = mg
@@ -240,6 +241,7 @@ def tell(self):
return False
return self._pos
+
class NewHttpFile(HttpFile):
def __init__(self, path, devid, backup_dests=None,
mg=None, fid=None, cls=None, key=None,
@@ -253,6 +255,9 @@ def __init__(self, path, devid, backup_dests=None,
self._paths = [(devid, path)] + list(backup_dests)
self._is_closed = 0
+ def paths(self):
+ return self._paths
+
def read(self, n= -1):
return self._fp.read(n)
View
8 setup.py
@@ -20,11 +20,5 @@
license='GPL',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
include_package_data=True,
- zip_safe=False,
- install_requires=[
- # -*- Extra requirements: -*-
- ],
- entry_points="""
- # -*- Entry points: -*-
- """
+ zip_safe=False
)
View
11 tests/test_backend.py
@@ -9,22 +9,29 @@ class TestBackend(unittest.TestCase):
def setUp(self):
self.backend = Backend(['127.0.0.1:7001'])
- def test_do_request_host_not_exist(self):
+ def test_do_request_trackers_not_exist(self):
backend = Backend(["127.0.0.1:7011", "127.0.0.1:7012"])
try:
backend.do_request("get_domains")
except MogileFSError:
pass
else:
assert False
+
+ def test_do_request_one_tracker_down(self):
+ backend = Backend(["127.0.0.1:7001", "127.0.0.1:7012"])
+ try:
+ backend.do_request("get_domains")
+ except MogileFSError:
+ assert False
def test_do_request(self):
res = self.backend.do_request("get_domains")
assert res
def test_do_request_cmd_not_exist(self):
try:
- self.backend.do_request("spameggham")
+ self.backend.do_request("asdfkljweioav")
except MogileFSError:
pass
else:
View
109 tests/test_client.py
@@ -90,24 +90,12 @@ def test_new_file_unexisting_class(self):
client = Client(TEST_NS, HOSTS)
key = 'test_file_%s_%s' % (random.random(), time.time())
- try:
- client.new_file(key, cls='spam')
- except MogileFSError:
- pass
- else:
- assert False
-
+ self.assertRaises(MogileFSError, client.new_file, key, 'unexisting')
def test_new_file_unexisting_domain(self):
- client = Client('spamdomain', HOSTS)
-
+ client = Client('unexisting_domain', HOSTS)
key = 'test_file_%s_%s' % (random.random(), time.time())
- try:
- client.new_file(key)
- except MogileFSError, e:
- pass
- else:
- assert False
+ self.assertRaises(MogileFSError, client.new_file, key)
def test_closed_file(self):
client = Client(TEST_NS, HOSTS)
@@ -116,33 +104,11 @@ def test_closed_file(self):
fp.write("spam")
fp.close()
- try:
- fp.write("egg")
- except:
- pass
- else:
- assert False, "operation not permitted to closed file"
+ self.assertRaises(ValueError, fp.write, "egg")
+ self.assertRaises(ValueError, fp.read)
+ self.assertRaises(ValueError, fp.seek, 0)
+ self.assertRaises(ValueError, fp.tell)
- try:
- fp.read()
- except:
- pass
- else:
- assert False, "operation not permitted to closed file"
-
- try:
- fp.seek(0)
- except:
- pass
- else:
- assert False, "operation not permitted to closed file"
-
- try:
- fp.tell()
- except:
- pass
- else:
- assert False, "operation not permitted to closed file"
# def test_readonly_file(self):
# client = Client(TEST_NS, HOSTS)
@@ -164,11 +130,11 @@ def test_seek(self):
fp = client.new_file(key)
fp.write("SPAM")
fp.seek(1)
- assert fp.tell() == 1
+ self.assertEqual(fp.tell(), 1)
+
fp.write("p")
fp.close()
-
- assert client.get_file_data(key) == "SpAM"
+ self.assertEqual(client.get_file_data(key), "SpAM")
def test_seek_negative(self):
client = Client(TEST_NS, HOSTS)
@@ -177,11 +143,11 @@ def test_seek_negative(self):
fp = client.new_file(key)
fp.write("SPAM")
fp.seek(-10)
- assert fp.tell() == 0
+ self.assertEqual(fp.tell(), 0)
+
fp.write("s")
fp.close()
-
- assert client.get_file_data(key) == "sPAM"
+ self.assertEqual(client.get_file_data(key), "sPAM")
def test_seek_read(self):
client = Client(TEST_NS, HOSTS)
@@ -191,28 +157,26 @@ def test_seek_read(self):
fp = client.read_file(key)
fp.seek(1)
- assert fp.tell() == 1
+ self.assertEqual(fp.tell(), 1)
+
content = fp.read(3)
-
assert content == "123"
- assert fp.tell() == 4
-
+ self.assertEqual(fp.tell(), 4)
def test_rename(self):
client = Client(TEST_NS, HOSTS)
key = 'test_file_%s_%s' % (random.random(), time.time())
client.new_file(key).write(key)
paths = client.get_paths(key)
- assert paths
+ self.assertTrue(paths)
newkey = 'test_file2_%s_%s' % (random.random(), time.time())
client.rename(key, newkey)
paths = client.get_paths(newkey)
- assert paths
+ self.assertTrue(paths)
content = client.get_file_data(newkey)
assert content == key
-
def test_rename_dupliate_key(self):
client = Client(TEST_NS, HOSTS)
@@ -223,7 +187,6 @@ def test_rename_dupliate_key(self):
client.store_content(key2, key2)
self.assertEqual(client.rename(key1, key2), False)
-
def test_store_file(self):
client = Client(TEST_NS, HOSTS)
@@ -232,54 +195,50 @@ def test_store_file(self):
data = ''.join(random.choice("0123456789") for _ in xrange(8192 * 2))
fp = StringIO(data)
length = client.store_file(key, fp)
- assert length == len(data)
+ self.assertEqual(length, len(data))
content = client.get_file_data(key)
- assert content == data
-
+ self.assertEqual(content, data)
def test_store_content(self):
client = Client(TEST_NS, HOSTS)
key = 'test_file_%s_%s' % (random.random(), time.time())
data = ''.join(random.choice("0123456789") for _ in xrange(8192 * 2))
length = client.store_content(key, data)
- assert length == len(data)
+ self.assertEqual(length, len(data))
content = client.get_file_data(key)
- assert content == data
-
+ self.assertEqual(content, data)
def test_read_file(self):
client = Client(TEST_NS, HOSTS)
key = 'test_file_%s_%s' % (random.random(), time.time())
client.store_content(key, key)
fp = client.read_file(key)
- assert fp is not None
- assert key == fp.read()
-
+ self.assertNotEqual(fp, None)
+ self.assertEqual(key, fp.read())
def test_delete(self):
client = Client(TEST_NS, HOSTS)
key = 'test_file_%s_%s' % (random.random(), time.time())
client.new_file(key).write("SPAM")
paths = client.get_paths(key)
- assert paths
+ self.assertTrue(paths)
client.delete(key)
paths = client.get_paths(key)
- assert not paths
-
+ self.assertFalse(paths)
def test_mkcol(self):
client = Client(TEST_NS, HOSTS)
for x in xrange(0, 10):
key = 'test_file_%s_%s_%d' % (random.random(), time.time(), x)
client.new_file(key).write("SPAM%s" % x)
paths = client.get_paths(key)
- assert paths
+ self.assertTrue(paths)
# def test_edit_file(self):
@@ -299,7 +258,6 @@ def test_mkcol(self):
#
# assert cl.get_file_data(key) == "sPaM"
-
def test_file_like_object(self):
client = Client(TEST_NS, HOSTS)
key = 'test_file_%s_%s' % (random.random(), time.time())
@@ -309,17 +267,20 @@ def test_file_like_object(self):
fp.seek(0)
line = fp.readline()
- assert line == "spam\n"
+ self.assertEqual(line, "spam\n")
line = fp.readline()
- assert line == "egg\n"
+
+ self.assertEqual(line, "egg\n")
line = fp.readline()
- assert line == "ham\n"
+
+ self.assertEqual(line, "ham\n")
+
line = fp.readline()
- assert line == ''
+ self.assertEqual(line, '')
fp.seek(0)
lines = fp.readlines()
- assert lines == ["spam\n", "egg\n", "ham\n"]
+ self.assertEqual(lines, ["spam\n", "egg\n", "ham\n"])
fp.close()

0 comments on commit 78418aa

Please sign in to comment.
Something went wrong with that request. Please try again.