コード例 #1
0
 def setUp(self):
     
     args = {"transaction_management":"999"}
     args_string = shlex.quote(json.dumps(args))
     
     self.test_subject = HintFactoryPlugin(123,456,None,args_string)
     self.test_subject.hint_db = self.test_subject.mongo.test_hpit.hpit_hints
コード例 #2
0
 def test_constructor(self):
     """
     HintFactoryPlugin.__init__() Test plan:
         -ensure that logger set to none
         -ensure hf is instance of SimpleHintFactory
     """
     
     args = {"transaction_management":"999"}
     args_string = shlex.quote(json.dumps(args))
     
     hf = HintFactoryPlugin(1,1,None,args_string)
     hf.logger.should.equal(None)
     isinstance(hf.hf,SimpleHintFactory).should.equal(True)
コード例 #3
0
class TestHintFactoryPlugin(unittest.TestCase):
    def setUp(self):
        
        args = {"transaction_management":"999"}
        args_string = shlex.quote(json.dumps(args))
        
        self.test_subject = HintFactoryPlugin(123,456,None,args_string)
        self.test_subject.hint_db = self.test_subject.mongo.test_hpit.hpit_hints
    
    def tearDown(self):
        self.test_subject = None
        client = MongoClient()
        client.drop_database("test_hpit")
        
    def test_constructor(self):
        """
        HintFactoryPlugin.__init__() Test plan:
            -ensure that logger set to none
            -ensure hf is instance of SimpleHintFactory
        """
        
        args = {"transaction_management":"999"}
        args_string = shlex.quote(json.dumps(args))
        
        hf = HintFactoryPlugin(1,1,None,args_string)
        hf.logger.should.equal(None)
        isinstance(hf.hf,SimpleHintFactory).should.equal(True)
    
    def test_init_problem_callback(self):
        """
        HintFactoryPlugin.init_problem_callback() Test plan:
            - try without start state or goal problem, should respond withe error
            - mock hf.create_or_get_problem_node, should be called with message values
            - if returns false, response should be not ok
            - if returns true, response should be ok
        """
        
        self.test_subject.hf.create_or_get_problem_node = MagicMock(return_value = False)
        self.test_subject.send_response = MagicMock()
        
        msg = {"message_id":"1"}
        self.test_subject.init_problem_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "error": "hf_init_problem requires a 'start_state' and 'goal_problem'",
                "status":"NOT_OK"
            })
        msg["start_state"] = "2 + 2 = 4"
        self.test_subject.init_problem_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "error": "hf_init_problem requires a 'start_state' and 'goal_problem'",
                "status":"NOT_OK"
            })
        
        msg["goal_problem"] = "4 = 4"
        self.test_subject.init_problem_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "status": "NOT_OK",
                "error":"Unknown error when attempting to create or get problem state"
            })
        
        self.test_subject.hf.create_or_get_problem_node = MagicMock(return_value = True)
        self.test_subject.init_problem_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "status": "OK",
            })
    
    def test_delete_problem_callback(self):
        """
        HintFactoryPlugin.delete_problem_callback() Test plan:
            - pass without state, should relpy with error
            - invalid state, should respond error
            - mock delete problem true, should respond ok
            - mock delete problem false, should respond not ok
        """
        self.test_subject.send_response = MagicMock()
        self.test_subject.hf.delete_problem = MagicMock(return_value=True)

        msg = {"message_id":"1"}
        self.test_subject.delete_problem_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "error": "hf_delete_problem requires a 'state'",
            "status":"NOT_OK"         
        })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = 4
        self.test_subject.delete_problem_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"NOT_OK",
            "error":"message's 'state' parameter should be a dict",       
        })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = dict(HintFactoryState(problem="2 + 2 = 4"))
        self.test_subject.delete_problem_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"OK",     
        })
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.delete_problem.return_value=False
        self.test_subject.delete_problem_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"NOT_OK",
            "error":"unable to delete problem",     
        })
        self.test_subject.send_response.reset_mock()
        
        
    def test_delete_state_callback(self):
        """
        HintFactoryPlugin.delete_state_callback() Test plan:
            - pass without state, should respond error
            - pass with invalid state, should respond error
            - mock delete node to return None, should reply not ok
            - mock delete node to raise exceptino, should reply not ok with exception
            - mock delete node to return True, should reply ok
        """
        self.test_subject.send_response = MagicMock()
        self.test_subject.hf.delete_node = MagicMock(return_value=None)
        
        msg = {"message_id":"1"}
        self.test_subject.delete_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "error": "hf_delete_state requires a 'state'",
            "status":"NOT_OK"         
        })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = 4
        self.test_subject.delete_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"NOT_OK",
            "error":"message's 'state' parameter should be a dict",       
        })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = dict(HintFactoryState(problem="2 + 2 = 4"))
        self.test_subject.delete_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"NOT_OK",      
        })
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.delete_node = MagicMock(side_effect=StateDoesNotExistException("State does not exist"))
        self.test_subject.delete_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "status":"NOT_OK",
                "error":"State does not exist",
            })
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.delete_node = MagicMock(return_value=True)
        self.test_subject.delete_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "status":"OK",
            })
        self.test_subject.send_response.reset_mock()
        
        
    def test_push_state_callback(self):
        """
        HintFactoryPlugin.push_state_callback() Test plan:
            - send without state, should return error
            - put in bogus state, should return error
            - if valid state, make sure push_node and send_response called correctly
        """
        
        self.test_subject.send_response = MagicMock()
        self.test_subject.hf.push_node = MagicMock()
        
        msg = {"message_id":"1"}
        self.test_subject.push_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "error": "hf_push_state requires a 'state'",
                "status":"NOT_OK"
            })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = 4
        self.test_subject.push_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "status":"NOT_OK",
                "error":"message's 'state' parameter should be a dict",
            })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = HintFactoryState(problem="2 + 2 = 4")
        self.test_subject.push_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "status":"NOT_OK",
                "error":"message's 'state' parameter should be a dict",
            })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = dict(HintFactoryState(problem="2 + 2 = 4"))
        self.test_subject.push_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "status":"NOT_OK",
                "error":"State must have at least one step"
            })
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.push_node = MagicMock(side_effect=StateDoesNotExistException("State does not exist"))
        hf = HintFactoryState(problem="2 + 2 = 4")
        hf.append_step("simplify","4=4")
        msg["state"]=  dict(hf)
        self.test_subject.push_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "status":"NOT_OK",
                "error":"State does not exist",
            })
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.push_node = MagicMock(return_value = 4)
        hf = HintFactoryState(problem="2 + 2 = 4")
        hf.append_step("simplify","4=4")
        msg["state"]= dict(hf)
        self.test_subject.push_state_callback(msg)
        self.test_subject.send_response.assert_called_with("1", {
                "status":"OK",
            })
        self.test_subject.send_response.reset_mock()
        
    def test_hint_exists_callback(self):
        """
        HintFactoryPlugin.hint_exists_callback() Test plan:
            - mock hint_exists, mock send_response
            - pass a bogus state, should respond with error
            - if hint exists, should return exists, if not, should return no
        """
        self.test_subject.send_response = MagicMock()
        self.test_subject.hf.hint_exists = MagicMock(return_value = False)
        
        msg = {"message_id":"1"}
        self.test_subject.hint_exists_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "error": "hf_hint_exists requires a 'state'",
            "status":"NOT_OK"      
        })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = 4
        self.test_subject.hint_exists_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"NOT_OK",
            "error":"message's 'state' parameter should be a dict",
        })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = dict(HintFactoryState(problem="2 + 2 = 4"))
        self.test_subject.hint_exists_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"OK",
            "exists":"NO"
        })
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.hint_exists = MagicMock(side_effect=StateDoesNotExistException("state does not exist"))
        msg["state"] = dict(HintFactoryState(problem="2 + 2 = 4"))
        self.test_subject.hint_exists_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"NOT_OK",
            "error":"state does not exist"
        })
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.hint_exists = MagicMock(return_value=True)
        self.test_subject.hint_exists_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"OK",
            "exists":"YES"
        })
        self.test_subject.send_response.reset_mock()
    
    def test_get_hint_callback(self):
        """
        HintFactoryPlugin.get_hint_callback() Test plan:
            - mock get_hint, send_response
            - pass a bogus state, no state, shoudl respond with error
            - if hint false, should return not exists, otherwise fine
        """
        self.test_subject.send_response = MagicMock()
        self.test_subject.hf.get_hint = MagicMock(return_value=False)
        
        msg = {"message_id":"1"}
        self.test_subject.get_hint_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "error": "hf_get_hint requires a 'state'",
            "status":"NOT_OK",
        })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = 4
        self.test_subject.get_hint_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"NOT_OK",
            "error":"message's 'state' parameter should be a dict",
        })
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = dict(HintFactoryState(problem="2 + 2 = 4"))
        self.test_subject.get_hint_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"OK",
            "exists":"NO"
        })
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.get_hint = MagicMock(side_effect=HintDoesNotExistException("hint does not exist"))
        self.test_subject.get_hint_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"NOT_OK",
            "error":"hint does not exist"
        })
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.get_hint = MagicMock(return_value={"hint_text":"hint text","hint_result":"hint result"})
        self.test_subject.get_hint_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"OK",
            "exists":"YES",
            "hint_text": "hint text",
            "hint_result": "hint result",
        })
        self.test_subject.send_response.reset_mock()
        
        #student model stuff
        msg["student_id"] = "123"
        self.test_subject.hf.get_hint = MagicMock(return_value={"hint_text":"hint text","hint_result":"hint result"})
        self.test_subject.get_hint_callback(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "status":"OK",
            "exists":"YES",
            "hint_text": "hint text",
            "hint_result": "hint result"
        })
        self.test_subject.hint_db.find({"student_id":"123","hint_text":"hint text","hint_result":"hint result","state": dict(HintFactoryState(problem="2 + 2 = 4"))}).count().should.equal(1)
        self.test_subject.send_response.reset_mock()
        
        #duplicate records?
        self.test_subject.get_hint_callback(msg)
        self.test_subject.hint_db.find({}).count().should.equal(1)
        self.test_subject.send_response.reset_mock()
        
        self.test_subject.hf.get_hint = MagicMock(return_value={"hint_text":"hint text 2","hint_result":"hint result"})
        self.test_subject.get_hint_callback(msg)
        self.test_subject.hint_db.find({}).count().should.equal(2)
        self.test_subject.send_response.reset_mock()
        
        msg["state"] = dict(HintFactoryState(problem="2 + 3 = 5"))
        self.test_subject.get_hint_callback(msg)
        self.test_subject.hint_db.find({}).count().should.equal(3)
        self.test_subject.send_response.reset_mock()
        
    def test_transaction_callback_method(self):
        """
        HintFactoryPlugin.transaction_callback_method() Test plan:
            - if outcome does not exist, should reply with nothing
            - if outcome not hint, should reply with nothing
            - if state does not exist, should reply with nothing
            - if state not a state, should reply with nothing
            - if hint exists, should reply with true
            - if hint does not exist, should reply with nothing
        """
        
        self.test_subject.send_response = MagicMock()
        self.test_subject.hf.get_hint = MagicMock(return_value=False)
        
        #invalid orig id
        msg = {"message_id":"1","orig_entity_id":"2","sender_entity_id":"888"}
        self.test_subject.transaction_callback_method(msg)
        self.test_subject.send_response.assert_called_with("1",{"error" : "Access denied","responder":"hf",})
        self.test_subject.send_response.reset_mock()
        
        #no args
        msg = {"message_id":"1","orig_entity_id":"2","sender_entity_id":"999"}
        self.test_subject.transaction_callback_method(msg)
        self.test_subject.send_response.assert_called_with("1",{"error": "'outcome' is not present for hint factory transaction.","responder":"hf"})
        self.test_subject.send_response.reset_mock()
        
        #outcome not hint
        msg["outcome"] = "not hint"
        self.test_subject.transaction_callback_method(msg)
        self.test_subject.send_response.assert_called_with("1",{"error": "'outcome' is not 'hint' for hint factory transaction.","responder":"hf"})
        self.test_subject.send_response.reset_mock()
        
        #outcome normal, no state
        msg["outcome"] = "hint"
        self.test_subject.transaction_callback_method(msg)
        self.test_subject.send_response.assert_called_with("1",{"error": "'state' required for hint factory transactions.","responder":"hf"})
        self.test_subject.send_response.reset_mock()
        
        #state invalid
        msg["state"] = "foo"
        self.test_subject.transaction_callback_method(msg)
        self.test_subject.send_response.assert_called_with("1",{"error": "'state' is invalid for hint factory transaction.","responder":"hf"})
        self.test_subject.send_response.reset_mock()
        
        #good state, hint does not exist
        msg["state"] = dict(HintFactoryState(problem="2 + 2 = 4"))
        self.test_subject.transaction_callback_method(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "hint_exists":False,
            "hint_text":"",
            "responder":"hf"
        })
        
        #good state, hint does exist
        self.test_subject.hf.get_hint = MagicMock(return_value="this is a hint")
        self.test_subject.transaction_callback_method(msg)
        self.test_subject.send_response.assert_called_with("1",{
            "hint_exists":True,
            "hint_text":"this is a hint",
            "responder":"hf"
        })