def test_no_transaction(self): class MockLocalLoader(LocalBusinessContextLoader): def load(self) -> Set[BusinessContext]: return set() class MockRemoteLoader(RemoteLoader): def load_contexts( self, identifiers: Set[Identifier]) -> Set[BusinessContext]: return set() registry = Registry(MockLocalLoader(), RemoteBusinessContextLoader(MockRemoteLoader())) with self.assertRaises(TransactionNotInProgress) as exception: registry.save_or_update_context(None) self.assertTrue(exception is not None)
def test_apply_post_conditions_filter_object_field(self): registry = Registry(MockLocalLoader(), RemoteBusinessContextLoader(MockRemoteLoader())) weaver = Weaver(registry) operation_context = OperationContext('user.create') operation_context.set_output(User('John Doe', '*****@*****.**')) weaver.apply_post_conditions(operation_context) self.assertEqual('John Doe', operation_context.get_output().name) self.assertEqual(None, operation_context.get_output().email)
def test_initialization(self): auth_logged_in_identifier = Identifier("auth", "userLoggedIn") user_create_identifier = Identifier("user", "create") precondition_1 = Precondition( 'precondition_1', Constant(value=True, type=ExpressionType.BOOL)) precondition_2 = Precondition( 'precondition_2', Constant(value=False, type=ExpressionType.BOOL)) post_condition_1 = PostCondition( 'post_condition_1', PostConditionType.FILTER_OBJECT_FIELD, 'user', Constant(value=True, type=ExpressionType.BOOL)) post_condition_2 = PostCondition( 'post_condition_2', PostConditionType.FILTER_OBJECT_FIELD, 'user', Constant(value=True, type=ExpressionType.BOOL)) auth_logged_in = BusinessContext(auth_logged_in_identifier, set(), {precondition_1}, {post_condition_1}) user_create = BusinessContext(user_create_identifier, {auth_logged_in_identifier}, {precondition_2}, {post_condition_2}) class MockLocalLoader(LocalBusinessContextLoader): def load(self) -> Set[BusinessContext]: return {user_create} class MockRemoteLoader(RemoteLoader): _contexts = {auth_logged_in_identifier: auth_logged_in} def load_contexts( self, identifiers: Set[Identifier]) -> Set[BusinessContext]: result = set() for identifier in identifiers: if identifier in self._contexts: result.add(self._contexts[identifier]) return result registry = Registry(MockLocalLoader(), RemoteBusinessContextLoader(MockRemoteLoader())) context = registry.get_context_by_identifier(user_create_identifier) self.assertTrue(precondition_1 in context.preconditions) self.assertTrue(precondition_2 in context.preconditions) self.assertTrue(post_condition_1 in context.post_conditions) self.assertTrue(post_condition_2 in context.post_conditions)
def test_apply_post_conditions_filter_list_of_objects(self): registry = Registry(MockLocalLoader(), RemoteBusinessContextLoader(MockRemoteLoader())) weaver = Weaver(registry) operation_context = OperationContext('product.listAll') operation_context.set_output([Product('Toothpaste', 123.45, False), Product('Toothbrush', 123.45, True)]) weaver.apply_post_conditions(operation_context) self.assertEqual(1, len(operation_context.get_output())) self.assertEqual('Toothpaste', operation_context.get_output()[0].name) self.assertEqual(None, operation_context.get_output()[0].buying_price)
def test_rollback(self): class MockLocalLoader(LocalBusinessContextLoader): def load(self) -> Set[BusinessContext]: return set() class MockRemoteLoader(RemoteLoader): def load_contexts( self, identifiers: Set[Identifier]) -> Set[BusinessContext]: return set() registry = Registry(MockLocalLoader(), RemoteBusinessContextLoader(MockRemoteLoader())) registry.begin_transaction() registry.save_or_update_context( BusinessContext(Identifier('user.validEmail'), set(), set(), set())) registry.rollback_transaction() self.assertEquals(0, len(registry.get_all_contexts()))
def test_communication(self): auth_logged_in_identifier = Identifier("auth", "userLoggedIn") user_create_identifier = Identifier("user", "create") user_valid_email_identifier = Identifier("user", "validEmail") properties_not_null = Precondition( name='propertiesNotNull', condition=LogicalAnd( IsNotNull( VariableReference(name='email', type=ExpressionType.STRING)), IsNotNull( VariableReference(name='name', type=ExpressionType.STRING)))) hide_user_email = PostCondition( name='hideUserEmail', type=PostConditionType.FILTER_OBJECT_FIELD, reference_name='email', condition=Constant(True, type=ExpressionType.BOOL)) auth_logged_in = BusinessContext(auth_logged_in_identifier, set(), set(), set()) user_valid_email = BusinessContext(user_valid_email_identifier, set(), {properties_not_null}, set()) user_create = BusinessContext( user_create_identifier, {auth_logged_in_identifier, user_valid_email_identifier}, set(), {hide_user_email}) class MockLocalLoader(LocalBusinessContextLoader): def load(self) -> Set[BusinessContext]: return {user_create, user_valid_email} class MockRemoteLoader(RemoteLoader): _contexts = {auth_logged_in_identifier: auth_logged_in} def load_contexts( self, identifiers: Set[Identifier]) -> Set[BusinessContext]: result = set() for identifier in identifiers: if identifier in self._contexts: result.add(self._contexts[identifier]) return result server_registry = Registry( MockLocalLoader(), RemoteBusinessContextLoader(MockRemoteLoader())) port = 5561 host = 'localhost' server = ServerThread(registry=server_registry, sleep_interval=1, port=port) server.start() time.sleep(1) # Give server time to initialize retrieved = retrieve_contexts({user_create_identifier}, host, port) self.assertEqual(1, len(retrieved)) received_context = retrieved.pop() self.assertEqual(user_create_identifier, received_context.identifier) self.assertEqual(2, len(received_context.included_contexts)) self.assertEqual(1, len(received_context.preconditions)) self.assertEqual(1, len(received_context.post_conditions)) retrieved = retrieve_all_contexts(host, port) self.assertEqual(2, len(retrieved)) server.stop() server.join()
def test_evaluate_preconditions(self): registry = Registry(MockLocalLoader(), RemoteBusinessContextLoader(MockRemoteLoader())) weaver = Weaver(registry) operation_context = OperationContext('user.create') self.assertRaises(BusinessRulesCheckFailed, weaver.evaluate_preconditions, operation_context)