コード例 #1
0
ファイル: engine.py プロジェクト: fjarri-attic/brain
def getEngineTestParams(db_path, all_engines, all_storages):

	IN_MEMORY = 'memory' # tag for in-memory DB tests

	storages = {
		'sqlite3': [(IN_MEMORY, (None,), {}), ('file', ('test.db',),
			{'open_existing': 0, 'db_path': db_path})],
		'postgre': [('tempdb', ('tempdb',), {'open_existing': 0,
			'port': 5432, 'user': '******', 'password': ''})]
	}

	if not all_storages:
		# leave only default storages
		storages = {x: [storages[x][0]] for x in storages}

	if not all_engines:
		default_tag = brain.getDefaultEngineTag()
		storages = {default_tag: storages[default_tag]}

	res = []

	for engine_tag in storages:
		for storage_tag, args, kwds in storages[engine_tag]:
			res.append(EngineTestParams(engine_tag, storage_tag,
				(storage_tag == IN_MEMORY), args, kwds))

	return res
コード例 #2
0
ファイル: xmlrpclayer.py プロジェクト: fjarri-attic/brain
	def _constructEnginesList(self):
		"""Returns the description of constructor arguments for all available engines"""

		tags = brain.getEngineTags()
		default_tag = brain.getDefaultEngineTag()

		res = ""
		for tag in tags:
			engine_class = brain.engine.getEngineByTag(tag)
			arg_spec = tuple(inspect.getfullargspec(engine_class.__init__))
			arg_str = inspect.formatargspec(*arg_spec)
			default = " (default)" if tag == default_tag else ""
			res += tag + default + ": " + arg_str + "\n"
		return res
コード例 #3
0
ファイル: fuzz.py プロジェクト: fjarri-attic/brain
def _runTests(objects, actions, verbosity):
	"""Main test function. Create several objects and perform random actions on them."""

	# using default engine, because we are testing only DB logic here
	engine_tag = brain.getDefaultEngineTag()

	# create brain connection and fake Python-powered connection
	conn = brain.connect(None, name=None)
	fake_conn = FakeConnection()

	objs = []
	fake_objs = []
	times = {}

	# create objects
	for i in range(objects):
		data = getRandomNonTrivialData(STARTING_DEPTH)

		try:
			objs.append(conn.create(data))
		except:
			print("Error creating object: " + str(data))
			raise

		if verbosity > 2:
			print("Object " + str(i) + " created" +
				(", initial state: " + repr(data) if verbosity > 3 else ""))

		fake_objs.append(fake_conn.create(data))

	# perform test
	for c in range(actions):
		for i in range(objects):

			# copy original state in case we delete object or some error occurs
			fake_state_before = copy.deepcopy(fake_conn.read(fake_objs[i]))

			# try to read the real object from the database
			try:
				state_before = conn.read(objs[i])
			except:
				print("Error reading object " + str(i) +
					(": " + str(fake_state_before) if verbosity > 3 else ""))
				if verbosity > 3:
					conn._engine.dump()
				raise

			# create random action and test it on fake object
			action = RandomAction(state_before)
			fake_exception = None

			# some fuzz actions can lead to exceptions
			try:
				fake_result = action(fake_conn, fake_objs[i])
			except brain.BrainError as e:
				fake_exception = e

			# if object gets deleted, return its state to original
			fake_state_after = fake_conn.read(fake_objs[i])
			if fake_state_after is None:
				fake_conn.modify(fake_objs[i], [], fake_state_before)
				continue

			# try to perform action on a real object
			try:
				starting_time = time.time()
				result = action(conn, objs[i])
				action_time = time.time() - starting_time

				method = action.getMethod()
				if method not in times.keys():
					times[method] = action_time
				else:
					times[method] += action_time

			except brain.BrainError as e:
				if not (fake_exception is not None and type(e) == type(fake_exception)):
					raise
			except:
				print("Error performing action on object " + str(i) +
					": " + action.dump(verbosity))
				if verbosity > 3:
					print("State before: " + str(fake_state_before))
					conn._engine.dump()
				raise

			if verbosity > 2:
				print("Object " + str(i) + ", " + action.dump(verbosity))

			# try to read resulting object state
			try:
				state_after = conn.read(objs[i])
			except:
				print("Error reading object " + str(i) +
					((": " + str(fake_state_after)) if verbosity > 3 else ""))
				print("After action: " + action.dump(verbosity))
				if verbosity > 3:
					print("On state: " + str(fake_state_before))
					conn._engine.dump()
				raise

			# compare resulting states of real and fake objects
			if state_after != fake_state_after:
				print("Action results are different:")
				print("State before: " + repr(fake_state_before))
				print("Action: " + action.dump(verbosity))
				print("Main state after: " + repr(state_after))
				print("Fake state after: " + repr(fake_state_after))
				if verbosity > 3:
					conn._engine.dump()
				raise Exception("Functionality error")

			# compare action return values (if any)
			if result != fake_result and fake_exception is None:
				print("Action return values are different:")
				print("State before: " + repr(fake_state_before))
				print("Action: " + action.dump(verbosity))
				print("Main return value: " + repr(result))
				print("Fake return value: " + repr(fake_result))
				if verbosity > 3:
					conn._engine.dump()
				raise Exception("Functionality error")

	return times
コード例 #4
0
ファイル: xmlrpclayer.py プロジェクト: fjarri-attic/brain
	def export_getDefaultEngineTag(self):
		return brain.getDefaultEngineTag()