def test_stmt_in_context_stmt(self): stmt_guid = str(uuid.uuid4()) existing_stmt = Statement.Statement(json.dumps({'statement_id':stmt_guid, 'actor':{'objectType':'Agent','mbox':'*****@*****.**'}, 'verb': {"id":"verb/url/outer"},"object": {'id':'activityy16'}})) guid = str(uuid.uuid4()) stmt = Statement.Statement(json.dumps({'actor':{'objectType':'Agent','mbox':'*****@*****.**'}, 'verb': {"id":"verb/url"},"object": {'id':'activity16'}, 'context':{'registration': guid, 'contextActivities': {'other': {'id': 'NewActivityID'}}, 'revision': 'foo', 'platform':'bar','language': 'en-US', 'statement': {'id': stmt_guid}}})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) context = models.context.objects.get(id=stmt.statement.context.id) stmt_ref = models.StatementRef(ref_id=stmt_guid) neststmt = models.statement.objects.get(statement_id=stmt_ref.ref_id) st = models.statement.objects.get(id=stmt.statement.id) self.assertEqual(st.stmt_object.id, activity.id) self.assertEqual(st.context.id, context.id) self.assertEqual(context.registration, guid) self.assertEqual(context.revision, 'foo') self.assertEqual(context.platform, 'bar') self.assertEqual(context.language, 'en-US') self.assertEqual(neststmt.verb.verb_id, "verb/url/outer")
def statements_post(req_dict): stmtResponses = [] if isinstance(req_dict['body'], str): try: req_dict['body'] = ast.literal_eval(req_dict['body']) except: req_dict['body'] = json.loads(req_dict['body']) if not type(req_dict['body']) is list: stmt = Statement.Statement(req_dict['body'], auth=req_dict['user']).statement stmtResponses.append(str(stmt.statement_id)) else: try: for st in req_dict['body']: stmt = Statement.Statement(st, auth=req_dict['user']).statement stmtResponses.append(str(stmt.statement_id)) except Exception, e: for stmt_id in stmtResponses: try: models.statement.objects.get( statement_id=stmt_id).delete() except models.statement.DoesNotExist: pass # stmt already deleted raise e
def test_context_ext_stmt(self): guid = str(uuid.uuid4()) stmt = Statement.Statement(json.dumps({'actor':{'objectType':'Agent','mbox':'*****@*****.**'}, 'verb': {"id":"verb/url"},"object": {'id':'activity16'}, 'context':{'registration': guid, 'contextActivities': {'other': {'id': 'NewActivityID'}}, 'revision': 'foo', 'platform':'bar','language': 'en-US', 'extensions':{'k1': 'v1', 'k2': 'v2'}}})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) context = models.context.objects.get(id=stmt.statement.context.id) extList = models.context_extensions.objects.values_list().filter(context=context) extKeys = [ext[1] for ext in extList] extVals = [ext[2] for ext in extList] context_activities = stmt.statement.context.contextActivities.all() self.assertEqual(stmt.statement.verb.verb_id, "verb/url") self.assertEqual(stmt.statement.stmt_object.id, activity.id) self.assertEqual(stmt.statement.context.id, context.id) st = models.statement.objects.get(id=stmt.statement.id) self.assertEqual(st.stmt_object.id, activity.id) self.assertEqual(st.context.id, context.id) self.assertEqual(context.registration, guid) self.assertEqual(context_activities[0].key, 'other') self.assertEqual(context_activities[0].context_activity, 'NewActivityID') self.assertEqual(context.revision, 'foo') self.assertEqual(context.platform, 'bar') self.assertEqual(context.language, 'en-US') self.assertIn('k1', extKeys) self.assertIn('k2', extKeys) self.assertIn('v1', extVals) self.assertIn('v2', extVals)
def test_context_stmt(self): guid = str(uuid.uuid4()) stmt = Statement.Statement(json.dumps({'actor':{'objectType':'Agent','mbox':'*****@*****.**'}, 'verb': {"id":"verb/url"},"object": {'id':'activity15'}, 'context':{'registration': guid, 'contextActivities': {'other': {'id': 'NewActivityID'}, 'grouping':{'id':'GroupID'}}, 'revision': 'foo', 'platform':'bar', 'language': 'en-US'}})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) context = models.context.objects.get(id=stmt.statement.context.id) context_activities = stmt.statement.context.contextActivities.all() self.assertEqual(stmt.statement.verb.verb_id, "verb/url") self.assertEqual(stmt.statement.stmt_object.id, activity.id) self.assertEqual(stmt.statement.context.id, context.id) st = models.statement.objects.get(id=stmt.statement.id) self.assertEqual(st.stmt_object.id, activity.id) self.assertEqual(st.context.id, context.id) self.assertEqual(context_activities[1].key, 'grouping') self.assertEqual(context_activities[1].context_activity, 'GroupID') self.assertEqual(context_activities[0].key, 'other') self.assertEqual(context_activities[0].context_activity, 'NewActivityID') self.assertEqual(context.registration, guid) self.assertEqual(context.revision, 'foo') self.assertEqual(context.platform, 'bar') self.assertEqual(context.language, 'en-US')
def test_model_authoritative_set(self): stmt = Statement.Statement(json.dumps({"actor":{"name":"tom","mbox":"mailto:[email protected]"}, 'verb': {"id":"verb/url"}, "object": {"id":"activity"}})) self.assertTrue(models.statement.objects.get(pk=stmt.statement.pk).authoritative) stmt2 = Statement.Statement(json.dumps({"actor":{"name":"tom","mbox":"mailto:[email protected]"}, 'verb': {"id":"verb/url"}, "object": {"id":"activity"}})) self.assertTrue(models.statement.objects.get(pk=stmt2.statement.pk).authoritative) self.assertFalse(models.statement.objects.get(pk=stmt.statement.pk).authoritative) stmt3 = Statement.Statement(json.dumps({"actor":{"name":"tom","mbox":"mailto:[email protected]"}, 'verb': {"id":"verb/url"}, "object": {"id":"activity2"}})) self.assertTrue(models.statement.objects.get(pk=stmt3.statement.pk).authoritative) self.assertTrue(models.statement.objects.get(pk=stmt2.statement.pk).authoritative) self.assertFalse(models.statement.objects.get(pk=stmt.statement.pk).authoritative)
def test_given_stmtID_stmt(self): stmt = Statement.Statement(json.dumps({"statement_id":"blahID", "actor":{"objectType":"Agent","mbox": "*****@*****.**"}, "verb":{"id": "http://adlnet.gov/expapi/verbs/created","display": {"en-US":"created", "en-GB":"made"}}, "object":{"id":"http://example.adlnet.gov/tincan/example/simplestatement"}})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) verb = models.Verb.objects.get(id=stmt.statement.verb.id) actor = models.agent.objects.get(id=stmt.statement.actor.id) gb_lang = verb.display.all()[0] us_lang = verb.display.all()[1] self.assertEqual(gb_lang.key, 'en-GB') self.assertEqual(gb_lang.value, 'made') self.assertEqual(us_lang.key, 'en-US') self.assertEqual(us_lang.value, 'created') self.assertEqual(activity.activity_id, "http://example.adlnet.gov/tincan/example/simplestatement") self.assertEqual(actor.mbox, "*****@*****.**") self.assertEqual(verb.verb_id, "http://adlnet.gov/expapi/verbs/created") st = models.statement.objects.get(statement_id="blahID") self.assertEqual(st.stmt_object.id, activity.id) self.assertEqual(st.verb.id, verb.id)
def test_substatement_as_object(self): guid = str(uuid.uuid4()) stmt = Statement.Statement(json.dumps({'actor':{'objectType':'Agent','mbox':'*****@*****.**'}, 'verb': {"id":"verb/url"}, 'object':{'objectType':'SubStatement', 'actor':{'objectType':'Agent','mbox':'*****@*****.**'},'verb': {"id":"verb/url/nest"}, 'object': {'objectType':'activity', 'id':'testex.com'}, 'result':{'completion': True, 'success': True, 'response': 'kicked'}, 'context':{'registration': guid, 'contextActivities': {'other': {'id': 'NewActivityID'}},'revision': 'foo', 'platform':'bar', 'language': 'en-US', 'extensions':{'k1': 'v1', 'k2': 'v2'}}}})) outer_stmt = models.statement.objects.get(id=stmt.statement.id) sub_stmt = models.SubStatement.objects.get(id=outer_stmt.stmt_object.id) sub_obj = models.activity.objects.get(id=sub_stmt.stmt_object.id) sub_act = models.agent.objects.get(id=sub_stmt.actor.id) sub_con = models.context.objects.get(id=sub_stmt.context.id) sub_res = models.result.objects.get(id=sub_stmt.result.id) self.assertEqual(outer_stmt.verb.verb_id, "verb/url") self.assertEqual(outer_stmt.actor.mbox, '*****@*****.**') self.assertEqual(sub_stmt.verb.verb_id, "verb/url/nest") self.assertEqual(sub_obj.activity_id, 'testex.com') self.assertEqual(sub_act.mbox, '*****@*****.**') self.assertEqual(sub_con.registration, guid) self.assertEqual(sub_res.response, 'kicked')
def test_stmt_ref_as_object(self): stmt = Statement.Statement(json.dumps({"actor":{"objectType":"Agent","mbox": "*****@*****.**"}, "verb":{"id": "http://adlnet.gov/expapi/verbs/created","display": {"en-US":"created"}}, "object":{"id":"http://example.adlnet.gov/tincan/example/simplestatement"}, "statement_id":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"})) stmt2 = Statement.Statement(json.dumps({"actor":{"name":"Example Admin", "mbox":"*****@*****.**"}, 'verb': {"id":"http://adlnet.gov/expapi/verbs/attempted"}, 'object': {'objectType':'StatementRef', 'id': "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}})) stmts = models.statement.objects.all() stmt_refs = models.StatementRef.objects.filter(ref_id="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") self.assertEqual(len(stmt_refs), 1) self.assertEqual(stmt_refs[0].ref_id, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") self.assertEqual(len(stmts), 2)
def test_existing_stmtID_stmt(self): stmt = Statement.Statement(json.dumps({"statement_id":"blahID","verb":{"id":"verb/url", "display":{"en-US":"myverb"}}, "object": {"id":"activity"}, "actor":{"objectType":"Agent", "mbox":"*****@*****.**"}})) self.assertRaises(ParamConflict, Statement.Statement, json.dumps({"statement_id":"blahID", "verb":{"id":"verb/url","display":{"en-US":"myverb"}},"object": {'id':'activity2'}, "actor":{"objectType":"Agent", "mbox":"*****@*****.**"}}))
def test_voided_stmt(self): stmt = Statement.Statement(json.dumps({"actor":{"objectType":"Agent","mbox": "*****@*****.**"}, "verb":{"id": "http://adlnet.gov/expapi/verbs/created","display": {"en-US":"created"}}, "object":{"id":"http://example.adlnet.gov/tincan/example/simplestatement"}})) st_id = stmt.statement.statement_id st_model = models.statement.objects.get(statement_id=st_id) self.assertEqual(st_model.voided, False) stmt2 = Statement.Statement(json.dumps({"actor":{"name":"Example Admin", "mbox":"*****@*****.**"}, 'verb': {"id":"http://adlnet.gov/expapi/verbs/voided"}, 'object': {'objectType':'StatementRef', 'id': str(st_id)}})) st_model = models.statement.objects.get(statement_id=st_id) self.assertEqual(st_model.voided, True) stmt_ref = models.StatementRef.objects.get(ref_id=str(st_id)) self.assertEqual(stmt_ref.object_type, 'StatementRef')
def test_voided_wrong_type(self): stmt = Statement.Statement(json.dumps({"actor":{"objectType":"Agent","mbox": "*****@*****.**"}, "verb":{"id": "http://adlnet.gov/expapi/verbs/created","display": {"en-US":"created"}}, "object":{"id":"http://example.adlnet.gov/tincan/example/simplestatement"}})) st_id = stmt.statement.statement_id self.assertRaises(ParamError, Statement.Statement, json.dumps({"actor":{"name":"Example Admin", "mbox":"*****@*****.**"}, 'verb': {"id":"http://adlnet.gov/expapi/verbs/voided"}, 'object': {'objectType':'Statement', 'id': str(st_id)}}))
def test_agent_as_object(self): guid = str(uuid.uuid4()) stmt = Statement.Statement(json.dumps({'object':{'objectType':'Agent', 'name': 'lulu', 'openid':'luluid'}, 'verb': {"id":"verb/url"},'actor':{'objectType':'Agent','mbox':'*****@*****.**'}})) st = models.statement.objects.get(id=stmt.statement.id) agent = models.agent.objects.get(id=stmt.statement.stmt_object.id) self.assertEqual(agent.name, 'lulu') self.assertEqual(agent.openid, 'luluid')
def test_actor_with_context_stmt(self): stmt_guid = str(uuid.uuid4()) existing_stmt = Statement.Statement(json.dumps({'statement_id':stmt_guid, 'actor':{'objectType':'Agent','mbox':'*****@*****.**'}, 'verb': {"id":"verb/url/outer"},"object": {'id':'activityy16'}})) guid = str(uuid.uuid4()) stmt = Statement.Statement(json.dumps({'actor':{'objectType':'Agent', 'name': 'steve', 'mbox':'*****@*****.**'}, 'verb': {"id":"verb/url"},"object": {'id':'activity18'},'context':{'registration': guid, 'instructor': {'objectType':'Agent','name':'jon','mbox':'*****@*****.**'}, 'contextActivities': {'other': {'id': 'NewActivityID1'}}, 'revision': 'foob', 'platform':'bard', 'language': 'en-US', 'statement': {'id':stmt_guid}}})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) context = models.context.objects.get(id=stmt.statement.context.id) conactor = models.agent.objects.get(id=stmt.statement.context.instructor.id) stmt_ref = models.StatementRef(ref_id=stmt_guid) neststmt = models.statement.objects.get(statement_id=stmt_ref.ref_id) st = models.statement.objects.get(id=stmt.statement.id) context_activities = stmt.statement.context.contextActivities.all() self.assertEqual(st.stmt_object.id, activity.id) self.assertEqual(st.context.id, context.id) self.assertEqual(st.context.instructor.id, conactor.id) self.assertEqual(st.verb.verb_id, "verb/url" ) self.assertEqual(context.registration, guid) self.assertEqual(context_activities[0].key, 'other') self.assertEqual(context_activities[0].context_activity, 'NewActivityID1') self.assertEqual(context.revision, 'foob') self.assertEqual(context.platform, 'bard') self.assertEqual(context.language, 'en-US') self.assertEqual(neststmt.verb.verb_id, "verb/url/outer") self.assertEqual(conactor.objectType, 'Agent') self.assertEqual(conactor.name, 'steve') self.assertEqual(conactor.mbox, '*****@*****.**')
def test_minimum_stmt(self): stmt = Statement.Statement(json.dumps({"actor":{"objectType":"Agent","mbox": "*****@*****.**"}, "verb":{"id": "http://adlnet.gov/expapi/verbs/created","display": {"en-US":"created"}}, "object":{"id":"http://example.adlnet.gov/tincan/example/simplestatement"}})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) verb = models.Verb.objects.get(id=stmt.statement.verb.id) actor = models.agent.objects.get(id=stmt.statement.actor.id) self.assertEqual(activity.activity_id, "http://example.adlnet.gov/tincan/example/simplestatement") self.assertEqual(actor.mbox, "*****@*****.**") self.assertEqual(verb.verb_id, "http://adlnet.gov/expapi/verbs/created")
def test_result_score_stmt(self): time = str(datetime.now()) stmt = Statement.Statement(json.dumps({"actor":{'objectType':'Agent','name':'jon','mbox':'*****@*****.**'}, 'verb': {"id":"verb/url"},"object": {'id':'activity14'}, "result": {'score':{'scaled':.95}, 'completion': True, 'success': True, 'response': 'yes', 'duration': time, 'extensions':{'key1': 'value1', 'key2':'value2'}}})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) result = models.result.objects.get(id=stmt.statement.result.id) score = models.score.objects.get(id=stmt.statement.result.score.id) actor = models.agent.objects.get(id=stmt.statement.actor.id) extList = models.result_extensions.objects.values_list().filter(result=result) extKeys = [ext[1] for ext in extList] extVals = [ext[2] for ext in extList] self.assertEqual(stmt.statement.verb.verb_id, "verb/url") self.assertEqual(stmt.statement.stmt_object.id, activity.id) self.assertEqual(stmt.statement.result.id, result.id) self.assertEqual(stmt.statement.actor.id, actor.id) st = models.statement.objects.get(id=stmt.statement.id) self.assertEqual(st.stmt_object.id, activity.id) self.assertEqual(st.result.id, result.id) self.assertEqual(st.actor.id, actor.id) self.assertEqual(result.completion, True) self.assertEqual(result.success, True) self.assertEqual(result.response, 'yes') self.assertEqual(result.duration, time) self.assertEqual(result.score.id, score.id) self.assertEqual(score.scaled, .95) self.assertEqual(activity.activity_id, 'activity14') self.assertEqual(actor.name, 'jon') self.assertEqual(actor.mbox, '*****@*****.**') self.assertIn('key1', extKeys) self.assertIn('key2', extKeys) self.assertIn('value1', extVals) self.assertIn('value2', extVals)
def test_string_result_stmt(self): stmt = Statement.Statement(json.dumps({'actor':{'objectType':'Agent','mbox':'*****@*****.**'}, 'verb': {"id":"verb/url"},"object": {"id":"activity11"}, "result": "This is a string."})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) result = models.result.objects.get(id=stmt.statement.result.id) ext = models.result_extensions.objects.get(result=result) self.assertEqual(stmt.statement.verb.verb_id, "verb/url") self.assertEqual(stmt.statement.stmt_object.id, activity.id) self.assertEqual(stmt.statement.result.id, result.id) st = models.statement.objects.get(id=stmt.statement.id) self.assertEqual(st.stmt_object.id, activity.id) self.assertEqual(st.result.id, result.id) self.assertEqual(ext.key, 'resultString') self.assertEqual(ext.value, 'This is a string.')
def statements_get(req_dict): # If statementId is in req_dict then it is a single get if 'statementId' in req_dict: statementId = req_dict['statementId'] # Try to retrieve stmt, if DNE then return empty else return stmt info st = Statement.Statement(statement_id=statementId, get=True, auth=req_dict['user']) stmt_data = st.statement.object_return() return HttpResponse(stream_response_generator(stmt_data), mimetype="application/json", status=200) # If statementId is not in req_dict then it is a complex GET else: statementResult = {} stmtList = retrieve_statement.complexGet(req_dict) statementResult = retrieve_statement.buildStatementResult( req_dict.copy(), stmtList) return HttpResponse(stream_response_generator(statementResult), mimetype="application/json", status=200)
def test_result_stmt(self): time = str(datetime.now()) stmt = Statement.Statement(json.dumps({'actor':{'objectType':'Agent','mbox':'*****@*****.**'}, 'verb': {"id":"verb/url"},"object": {'id':'activity12'}, "result": {'completion': True, 'success': True, 'response': 'kicked', 'duration': time}})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) result = models.result.objects.get(id=stmt.statement.result.id) self.assertEqual(stmt.statement.verb.verb_id, "verb/url") self.assertEqual(stmt.statement.stmt_object.id, activity.id) self.assertEqual(stmt.statement.result.id, result.id) st = models.statement.objects.get(id=stmt.statement.id) self.assertEqual(st.stmt_object.id, activity.id) self.assertEqual(st.result.id, result.id) self.assertEqual(result.completion, True) self.assertEqual(result.success, True) self.assertEqual(result.response, 'kicked') self.assertEqual(result.duration, time)
def test_group_stmt(self): ot = "Group" name = "the group SMT" mbox = "mailto:[email protected]" members = [{"name":"agentA","mbox":"mailto:[email protected]"}, {"name":"agentB","mbox":"mailto:[email protected]"}] testagent = json.dumps({"objectType":ot, "name":name, "mbox":mbox,"member":members}) stmt = Statement.Statement(json.dumps({"actor":testagent, 'verb': {"id":"verb/url"},"object": {"id":"activity5", "objectType": "Activity"}})) activity = models.activity.objects.get(id=stmt.statement.stmt_object.id) actor = models.agent.objects.get(id=stmt.statement.actor.id) self.assertEqual(stmt.statement.verb.verb_id, "verb/url") self.assertEqual(stmt.statement.stmt_object.id, activity.id) self.assertEqual(stmt.statement.actor.id, actor.id) st = models.statement.objects.get(id=stmt.statement.id) self.assertEqual(st.stmt_object.id, activity.id) self.assertEqual(st.actor.id, actor.id) self.assertEqual(actor.name, name) self.assertEqual(actor.mbox, mbox)
def complexGet(req_dict): args = {} language = None # Set language if one if 'language' in req_dict: language = req_dict['language'] user = None if 'user' in req_dict: user = req_dict['user'] # Parse out params into single dict try: the_dict = req_dict['body'] if isinstance(the_dict, str): try: the_dict = ast.literal_eval(the_dict) except: the_dict = json.loads(the_dict) except KeyError: the_dict = req_dict # If want ordered by ascending if 'ascending' in the_dict: if the_dict['ascending']: ascending = True # Cycle through the_dict and set since and until params for k,v in the_dict.items(): if k.lower() == 'since': date_object = convertToUTC(v) args['stored__gt'] = date_object elif k.lower() == 'until': date_object = convertToUTC(v) args['stored__lte'] = date_object # If searching by activity or actor if 'object' in the_dict: objectData = the_dict['object'] obj = parse_incoming_object(objectData, args) if obj: args['stmt_object'] = obj # If searching by verb if 'verb' in the_dict: verb_id = the_dict['verb'] verb = models.Verb.objects.filter(verb_id=verb_id) if verb: args['verb'] = verb # If searching by registration if 'registration' in the_dict: uuid = str(the_dict['registration']) cntx = models.context.objects.get(registration=uuid) args['context'] = cntx # If searching by actor if 'actor' in the_dict: actorData = the_dict['actor'] actor = parse_incoming_actor(actorData) if actor: args['actor'] = actor # If searching by instructor if 'instructor' in the_dict: instData = the_dict['instructor'] inst = parse_incoming_instructor(instData) if inst: args['context__in'] = inst # there's a default of true if not 'authoritative' in the_dict or str(the_dict['authoritative']).upper() == 'TRUE': args['authoritative'] = True limit = 0 # If want results limited if 'limit' in the_dict: limit = int(the_dict['limit']) sparse = True # If want sparse results if 'sparse' in the_dict: if not type(the_dict['sparse']) is bool: if the_dict['sparse'].lower() == 'false': sparse = False else: sparse = the_dict['sparse'] ascending = False # Set stored param based on ascending if ascending: stored_param = 'stored' else: stored_param = '-stored' stmt_list = retrieve_stmts_from_db(the_dict, limit, stored_param, args) full_stmt_list = [] # For each stmt convert to our Statement class and retrieve all json for stmt in stmt_list: st = Statement.Statement(statement_id=stmt.statement_id, get=True, auth=user) full_stmt_list.append(st.get_full_statement_json(sparse, language)) return full_stmt_list
def statements_put(req_dict): req_dict['body']['statement_id'] = req_dict['statementId'] stmt = Statement.Statement(req_dict['body'], auth=req_dict['user']).statement return HttpResponse("No Content", status=204)
def test_no_registration_context_stmt(self): # expect the LRS to assign a context registration uuid stmt = Statement.Statement(json.dumps({'actor':{'objectType':'Agent','mbox':'*****@*****.**'},"verb":{"id":"verb/url"},"object": {'id':'activity14'}, 'context': {'contextActivities': {'other': {'id': 'NewActivityID'}}}})).statement context = models.context.objects.get(id=stmt.context.id) self.assertIsNotNone(context.registration)