git » nmdb » commit e2e8928

tests/python*: Make a single version of the tests, compatible with 2 and 3

author Alberto Bertogli
2010-04-14 21:27:49 UTC
committer Alberto Bertogli
2010-04-14 22:31:50 UTC
parent e3bc3600b620556541c8058a363877c40770eda1

tests/python*: Make a single version of the tests, compatible with 2 and 3

This patch unifies python 2 and 3 tests into one directory with both tests
being compatible with both versions.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

tests/python/README +4 -0
tests/python/random1-cache.py +21 -16
tests/python/random1.py +28 -25
tests/python3/README +0 -4
tests/python3/random1-cache.py +0 -129
tests/python3/random1.py +0 -154

diff --git a/tests/python/README b/tests/python/README
new file mode 100644
index 0000000..3252f9f
--- /dev/null
+++ b/tests/python/README
@@ -0,0 +1,4 @@
+
+The tests in this directory are written to be compatible with both Python 2
+and 3. When making a change, please make sure they continue to be.
+
diff --git a/tests/python/random1-cache.py b/tests/python/random1-cache.py
index 0e95dc5..cdfa898 100755
--- a/tests/python/random1-cache.py
+++ b/tests/python/random1-cache.py
@@ -28,9 +28,9 @@ def checked(f):
 			return f(k, *args, **kwargs)
 		except:
 			if k in history:
-				print history[k]
+				print(history[k])
 			else:
-				print 'No history for key', k
+				print('No history for key', k)
 			raise
 	newf.__name__ = f.__name__
 	return newf
@@ -56,7 +56,7 @@ def get(k):
 
 	l = ldb[k]
 	if l != n:
-		raise Mismatch, (n, l)
+		raise Mismatch((n, l))
 	history[k].append((get, k))
 	return True
 
@@ -71,7 +71,7 @@ def delete(k):
 
 def find_missing():
 	misses = 0
-	for k in ldb.keys():
+	for k in list(ldb.keys()):
 		if not get(k):
 			misses += 1
 	return misses
@@ -83,8 +83,15 @@ def getrand():
 
 
 if __name__ == '__main__':
+	# We want to always use a generator range(), which has different names
+	# in Python 2 and 3, so isolate the code from this hack
+	if sys.version_info[0] == 2:
+		gen_range = xrange
+	else:
+		gen_range = range
+
 	if len(sys.argv) < 2:
-		print 'Use: random1-cache.py number_of_keys [key_prefix]'
+		print('Use: random1-cache.py number_of_keys [key_prefix]')
 		sys.exit(1)
 
 	nkeys = int(sys.argv[1])
@@ -94,18 +101,18 @@ if __name__ == '__main__':
 		key_prefix = ''
 
 	# fill all the keys
-	print 'populate'
-	for i in xrange(nkeys):
+	print('populate')
+	for i in gen_range(nkeys):
 		set(key_prefix + str(getrand()), getrand())
 
-	print 'missing', find_missing()
+	print('missing', find_missing())
 
-	lkeys = ldb.keys()
+	lkeys = list(ldb.keys())
 
 	# operate on them a bit
-	print 'random operations'
+	print('random operations')
 	operations = ('set', 'get', 'delete')
-	for i in xrange(min(len(lkeys), nkeys / 2)):
+	for i in gen_range(min(len(lkeys), nkeys // 2)):
 		op = choice(operations)
 		k = choice(lkeys)
 		if op == 'set':
@@ -116,14 +123,12 @@ if __name__ == '__main__':
 			delete(k)
 			lkeys.remove(k)
 
-	print 'missing', find_missing()
+	print('missing', find_missing())
 
-	print 'delete'
+	print('delete')
 	for k in lkeys:
 		delete(k)
 
-	print 'missing', find_missing()
-
-	sys.exit(0)
+	print('missing', find_missing())
 
 
diff --git a/tests/python/random1.py b/tests/python/random1.py
index 172b4b5..e0cd5c2 100755
--- a/tests/python/random1.py
+++ b/tests/python/random1.py
@@ -28,9 +28,9 @@ def checked(f):
 			return f(k, *args, **kwargs)
 		except:
 			if k in history:
-				print history[k]
+				print(history[k])
 			else:
-				print 'No history for key', k
+				print('No history for key', k)
 			raise
 	newf.__name__ = f.__name__
 	return newf
@@ -50,7 +50,7 @@ def get(k):
 	n = ndb[k]
 	l = ldb[k]
 	if l != n:
-		raise Mismatch, (n, l)
+		raise Mismatch((n, l))
 	history[k].append((get, k))
 	return n
 
@@ -65,7 +65,7 @@ def cas(k, ov, nv):
 	prel = ldb[k]
 	pren = ndb[k]
 	n = ndb.cas(k, ov, nv)
-	if not ldb.has_key(k):
+	if k not in ldb:
 		l = 0
 	elif ldb[k] == ov:
 		ldb[k] = nv
@@ -73,10 +73,10 @@ def cas(k, ov, nv):
 	else:
 		l = 1
 	if n != l:
-		print k, ldb[k], ndb[k]
-		print prel, pren
-		print history[k]
-		raise Mismatch, (n, l)
+		print(k, ldb[k], ndb[k])
+		print(prel, pren)
+		print(history[k])
+		raise Mismatch((n, l))
 	history[k].append((cas, k, ov, nv))
 	return n
 
@@ -87,12 +87,12 @@ def check():
 			n = ndb[k]
 			l = ldb[k]
 		except:
-			print history[k]
-			raise Mismatch, (n, l)
+			print(history[k])
+			raise Mismatch((n, l))
 
 		if n != n:
-			print history[k]
-			raise Mismatch, (n, l)
+			print(history[k])
+			raise Mismatch((n, l))
 
 
 # Use integers because the normal random() generates floating point numbers,
@@ -102,8 +102,15 @@ def getrand():
 
 
 if __name__ == '__main__':
+	# We want to always use a generator range(), which has different names
+	# in Python 2 and 3, so isolate the code from this hack
+	if sys.version_info[0] == 2:
+		gen_range = xrange
+	else:
+		gen_range = range
+
 	if len(sys.argv) < 2:
-		print 'Use: random1.py number_of_keys [key_prefix]'
+		print('Use: random1.py number_of_keys [key_prefix]')
 		sys.exit(1)
 
 	nkeys = int(sys.argv[1])
@@ -113,16 +120,16 @@ if __name__ == '__main__':
 		key_prefix = ''
 
 	# fill all the keys
-	print 'populate'
-	for i in xrange(nkeys):
+	print('populate')
+	for i in gen_range(nkeys):
 		set(key_prefix + str(getrand()), getrand())
 
-	lkeys = ldb.keys()
+	lkeys = list(ldb.keys())
 
 	# operate on them a bit
-	print 'random operations'
+	print('random operations')
 	operations = ('set', 'delete', 'cas0', 'cas1')
-	for i in xrange(nkeys / 2):
+	for i in gen_range(nkeys // 2):
 		op = choice(operations)
 		k = choice(lkeys)
 		if op == 'set':
@@ -137,18 +144,14 @@ if __name__ == '__main__':
 			# successful cas
 			cas(k, ldb[k], getrand())
 
-	print 'check'
+	print('check')
 	check()
 
-	print 'delete'
+	print('delete')
 	for k in lkeys:
 		delete(k)
 
-	print 'check'
+	print('check')
 	check()
 
-	sys.exit(0)
-
-
-
 
diff --git a/tests/python3/README b/tests/python3/README
deleted file mode 100644
index 9f857b7..0000000
--- a/tests/python3/README
+++ /dev/null
@@ -1,4 +0,0 @@
-
-These tests are identical to the ones in tests/python, except they've been
-modified to work under Python 3 (and obviously use the Python 3 bindings).
-
diff --git a/tests/python3/random1-cache.py b/tests/python3/random1-cache.py
deleted file mode 100755
index f5e4ac2..0000000
--- a/tests/python3/random1-cache.py
+++ /dev/null
@@ -1,129 +0,0 @@
-#!/usr/bin/env python
-
-import sys
-import nmdb
-from random import randint, choice
-
-
-class Mismatch (Exception):
-	pass
-
-
-# network db
-ndb = nmdb.Cache()
-ndb.add_tipc_server()
-ndb.add_tcp_server('localhost')
-ndb.add_udp_server('localhost')
-
-# local db
-ldb = {}
-
-# history of each key
-history = {}
-
-# check decorator
-def checked(f):
-	def newf(k, *args, **kwargs):
-		try:
-			return f(k, *args, **kwargs)
-		except:
-			if k in history:
-				print(history[k])
-			else:
-				print('No history for key', k)
-			raise
-	newf.__name__ = f.__name__
-	return newf
-
-
-# operations
-@checked
-def set(k, v):
-	ndb[k] = v
-	ldb[k] = v
-	if k not in history:
-		history[k] = []
-	history[k].append((set, k, v))
-
-@checked
-def get(k):
-	try:
-		n = ndb[k]
-	except KeyError:
-		del ldb[k]
-		del history[k]
-		return 0
-
-	l = ldb[k]
-	if l != n:
-		raise Mismatch((n, l))
-	history[k].append((get, k))
-	return True
-
-@checked
-def delete(k):
-	del ldb[k]
-	try:
-		del ndb[k]
-	except KeyError:
-		pass
-	history[k].append((delete, k))
-
-def find_missing():
-	misses = 0
-	for k in list(ldb.keys()):
-		if not get(k):
-			misses += 1
-	return misses
-
-# Use integers because the normal random() generates floating point numbers,
-# and they can mess up comparisons because of architecture details.
-def getrand():
-	return randint(0, 1000000000000000000)
-
-
-if __name__ == '__main__':
-	if len(sys.argv) < 2:
-		print('Use: random1-cache.py number_of_keys [key_prefix]')
-		sys.exit(1)
-
-	nkeys = int(sys.argv[1])
-	if len(sys.argv) > 2:
-		key_prefix = sys.argv[2]
-	else:
-		key_prefix = ''
-
-	# fill all the keys
-	print('populate')
-	for i in range(nkeys):
-		set(key_prefix + str(getrand()), getrand())
-
-	print('missing', find_missing())
-
-	lkeys = list(ldb.keys())
-
-	# operate on them a bit
-	print('random operations')
-	operations = ('set', 'get', 'delete')
-	for i in range(min(len(lkeys), nkeys // 2)):
-		op = choice(operations)
-		k = choice(lkeys)
-		if op == 'set':
-			set(k, getrand())
-		elif op == 'get':
-			get(k)
-		elif op == 'delete':
-			delete(k)
-			lkeys.remove(k)
-
-	print('missing', find_missing())
-
-	print('delete')
-	for k in lkeys:
-		delete(k)
-
-	print('missing', find_missing())
-
-	sys.exit(0)
-
-
diff --git a/tests/python3/random1.py b/tests/python3/random1.py
deleted file mode 100755
index ae18bc6..0000000
--- a/tests/python3/random1.py
+++ /dev/null
@@ -1,154 +0,0 @@
-#!/usr/bin/env python
-
-import sys
-import nmdb
-from random import randint, choice
-
-
-class Mismatch (Exception):
-	pass
-
-
-# network db
-ndb = nmdb.DB()
-ndb.add_tipc_server()
-ndb.add_tcp_server('localhost')
-ndb.add_udp_server('localhost')
-
-# local db
-ldb = {}
-
-# history of each key
-history = {}
-
-# check decorator
-def checked(f):
-	def newf(k, *args, **kwargs):
-		try:
-			return f(k, *args, **kwargs)
-		except:
-			if k in history:
-				print(history[k])
-			else:
-				print('No history for key', k)
-			raise
-	newf.__name__ = f.__name__
-	return newf
-
-
-# operations
-@checked
-def set(k, v):
-	ndb[k] = v
-	ldb[k] = v
-	if k not in history:
-		history[k] = []
-	history[k].append((set, k, v))
-
-@checked
-def get(k):
-	n = ndb[k]
-	l = ldb[k]
-	if l != n:
-		raise Mismatch((n, l))
-	history[k].append((get, k))
-	return n
-
-@checked
-def delete(k):
-	del ndb[k]
-	del ldb[k]
-	history[k].append((delete, k))
-
-@checked
-def cas(k, ov, nv):
-	prel = ldb[k]
-	pren = ndb[k]
-	n = ndb.cas(k, ov, nv)
-	if k not in ldb:
-		l = 0
-	elif ldb[k] == ov:
-		ldb[k] = nv
-		l = 2
-	else:
-		l = 1
-	if n != l:
-		print(k, ldb[k], ndb[k])
-		print(prel, pren)
-		print(history[k])
-		raise Mismatch((n, l))
-	history[k].append((cas, k, ov, nv))
-	return n
-
-
-def check():
-	for k in ldb.keys():
-		try:
-			n = ndb[k]
-			l = ldb[k]
-		except:
-			print(history[k])
-			raise Mismatch((n, l))
-
-		if n != n:
-			print(history[k])
-			raise Mismatch((n, l))
-
-
-# Use integers because the normal random() generates floating point numbers,
-# and they can mess up comparisons because of architecture details.
-def getrand():
-	return randint(0, 1000000000000000000)
-
-
-if __name__ == '__main__':
-	if len(sys.argv) < 2:
-		print('Use: random1.py number_of_keys [key_prefix]')
-		sys.exit(1)
-
-	nkeys = int(sys.argv[1])
-	if len(sys.argv) > 2:
-		key_prefix = sys.argv[2]
-	else:
-		key_prefix = ''
-
-	# fill all the keys
-	print('populate')
-	for i in range(nkeys):
-		set(key_prefix + str(getrand()), getrand())
-
-	lkeys = list(ldb.keys())
-
-	# operate on them a bit
-	print('random operations')
-	operations = ('set', 'delete', 'cas0', 'cas1')
-	for i in range(nkeys // 2):
-		op = choice(operations)
-		k = choice(lkeys)
-		if op == 'set':
-			set(k, getrand())
-		elif op == 'delete':
-			delete(k)
-			lkeys.remove(k)
-		elif op == 'cas0':
-			# unsucessful cas
-			cas(k, -1, -1)
-		elif op == 'cas1':
-			# successful cas
-			cas(k, ldb[k], getrand())
-
-	print('check')
-	check()
-
-	print('delete')
-	for k in lkeys:
-		delete(k)
-
-	print('check')
-	check()
-
-	sys.exit(0)
-
-
-
-