def test_ToJSON(self): group = Group(**{'member': [{'name': 'test'}, {'name': 'test2'}]}) self.assertEqual( group.to_json(), '{"member": [{"name": "test", "objectType": "Agent"}, ' '{"name": "test2", "objectType": "Agent"}], "objectType": "Group"}' )
def test_InitAll(self): ctx = Context( registration=uuid.uuid4(), instructor=Group(member=[Agent(name='instructorGroupMember')]), team=Group(member=[Agent(name='teamGroupMember')]), context_activities=ContextActivities(category=Activity( id='contextActivityCategory')), revision='revision', platform='platform', language='en-US', statement=StatementRef(id='016699c6-d600-48a7-96ab-86187498f16f'), extensions=Extensions({'extensions': 'extend!'})) self.ctxVerificationHelper(ctx)
def test_InitGroupActor(self): statement = Statement(actor=Group(member=[Agent(name='test')])) self.assertIsNone(statement.id) self.assertIsNone(statement.verb) self.assertIsNone(statement.object) self.assertIsNone(statement.timestamp) self.assertIsNone(statement.stored) self.assertIsNone(statement.authority) self.groupVerificationHelper(statement.actor)
def test_ToJSONFromJSON(self): group = Group.from_json( '{"member":[{"name":"test"}, {"name":"test2"}]}') self.assertIsInstance(group.member[0], Agent) self.assertEqual( group.to_json(), '{"member": [{"name": "test", "objectType": "Agent"}, ' '{"name": "test2", "objectType": "Agent"}], "objectType": "Group"}' )
def _create_actor(): """Create xAPI actor""" student_id = "https://www.lip6.fr/mocah/invalidURI/student-number:" + student_hash partner_id = "https://www.lip6.fr/mocah/invalidURI/partner-number:" + partner_hash StudentAgent = Agent(openid=student_id, name="student") PartnerAgent = Agent(openid=partner_id, name="partner") AgentGroup = Group(member=[StudentAgent, PartnerAgent], name=student_hash + "||" + partner_hash) # print(student_hash, partner_hash) return AgentGroup
def setUp(self): self.endpoint = lrs_properties.endpoint self.version = lrs_properties.version self.username = lrs_properties.username self.password = lrs_properties.password self.lrs = RemoteLRS( version=self.version, endpoint=self.endpoint, username=self.username, password=self.password, ) self.agent = Agent(mbox="mailto:[email protected]") self.agent2 = Agent(mbox="mailto:[email protected]") self.verb = Verb(id="http://adlnet.gov/expapi/verbs/experienced", display=LanguageMap({"en-US": "experienced"})) self.group = Group(member=[self.agent, self.agent2]) self.activity = Activity( id="http://tincanapi.com/TinCanPython/Test/Unit/0", definition=ActivityDefinition()) self.activity.definition.type = "http://id.tincanapi.com/activitytype/unit-test" self.activity.definition.name = LanguageMap({"en-US": "Python Tests"}) self.activity.definition.description = LanguageMap( {"en-US": "Unit test in the test suite for the Python library"}) self.activity.object_type = 'Activity' self.parent = Activity(id="http://tincanapi.com/TinCanPython/Test", definition=ActivityDefinition()) self.parent.definition.type = "http://id.tincanapi.com/activitytype/unit-test-suite" self.parent.definition.name = LanguageMap({"en-US": "Python Tests"}) self.parent.definition.description = LanguageMap( {"en-US": "Unit test in the test suite for the Python library"}) self.parent.object_type = 'Activity' self.statement_ref = StatementRef(id=uuid.uuid4()) self.context = Context(registration=uuid.uuid4(), statement=self.statement_ref) # self.context.context_activities = ContextActivities(parent=[self.parent]) self.score = Score(raw=97, scaled=0.97, max=100, min=0) self.result = Result(score=self.score, success=True, completion=True, duration="PT120S") self.substatement = SubStatement( actor=self.agent, verb=self.verb, object=self.activity, )
def socialmedia_builder(verb, platform, account_name, account_homepage, object_type, object_id, message, tags=[], parent_object_type=None, parent_id=None, rating=None, instructor_name=None, instructor_email=None, team_name=None, course_code=None, account_email=None, user_name=None, timestamp=None): verbmapper = {'created': 'http://activitystrea.ms/schema/1.0/create', 'shared': 'http://activitystrea.ms/schema/1.0/share', 'liked': 'http://activitystrea.ms/schema/1.0/like', 'rated': 'http://id.tincanapi.com/verb/rated', 'commented': 'http://adlnet.gov/expapi/verbs/commented'} objectmapper = {'Note': 'http://activitystrea.ms/schema/1.0/note', 'Tag': 'http://id.tincanapi.com/activitytype/tag', 'Article': 'http://activitystrea.ms/schema/1.0/article', 'Video': 'http://activitystrea.ms/schema/1.0/video'} agentaccount = AgentAccount(name=account_name, home_page=account_homepage) actor = Agent(account=agentaccount) if (account_email is not None): actor.mbox = account_email if (user_name is not None): actor.name = user_name verb_obj = Verb(id=verbmapper[verb],display=LanguageMap({'en-US': verb})) #message = message.decode('utf-8').encode('ascii', 'ignore') #message.decode('utf-8').replace(u"\u2018", "'").replace(u"\u2019", "'").replace(u"\u2013", "-").replace(u"\ud83d", " ").replace(u"\ude09", " ").replace(u"\u00a0l", " ").replace(u"\ud83d", " ").replace(u"\u2026", " ").replace(u"\ude09", " ").replace(u"\u00a0"," ") object = Activity( id=object_id, object_type=object_type, definition=ActivityDefinition( name=LanguageMap({'en-US': message}), type=objectmapper[object_type] ), ) taglist = [] for tag in tags: tagobject = Activity( id='http://id.tincanapi.com/activity/tags/tincan', object_type='Activity', definition=ActivityDefinition( name=LanguageMap({'en-US': tag}), type=objectmapper['Tag'] ), ) taglist.append(tagobject) parentlist = [] if (verb in ['liked','shared','commented','rated']): parentobject = Activity( id=parent_id, object_type=parent_object_type, ) parentlist.append(parentobject) courselist = [] if (course_code is not None): courseobject = Activity( id=course_code, object_type='Course', definition=ActivityDefinition(type="http://adlnet.gov/expapi/activities/course") ) courselist.append(courseobject) instructor = None if (instructor_name is not None): instructor=Agent(name=instructor_name,mbox=instructor_email) team = None if (team_name is not None): team = Group(Agent(name=team_name), object_type='Group') result = None if (rating is not None): rating_as_float = float(rating) result = Result(score=Score(raw=rating_as_float)) context = Context( registration=uuid.uuid4(), platform=platform, instructor=instructor, team=team, context_activities=ContextActivities(other=ActivityList(taglist),parent=ActivityList(parentlist),grouping=ActivityList(courselist)) ) statement = statement_builder(actor, verb_obj, object, context, result, timestamp) return statement
def test_ToJSONFromJSON(self): group = Group.from_json('{"member":[{"name":"test"}, {"name":"test2"}]}') self.assertIsInstance(group.member[0], Agent) self.assertEqual(group.to_json(), '{"member": [{"name": "test", "objectType": "Agent"}, {"name": "test2", "objectType": "Agent"}], "objectType": "Group"}')
def test_AddMemberAnon(self): group = Group() group.addmember({"name":"test"}) self.assertIsInstance(group.member[0], Agent)
def test_FromJSONmember(self): group = Group.from_json('''{"member":[{"name":"test"}]}''') for k in group.member: self.assertIsInstance(k, Agent)
def test_FromJSONExceptionEmpty(self): with self.assertRaises(ValueError): group = Group.from_json('')
def test_AddMember(self): group = Group() group.addmember(Agent(name='test')) self.assertIsInstance(group.member[0], Agent)
def test_InitGroupObject(self): substatement = SubStatement(object=Group(member=[Agent(name='test')])) self.groupVerificationHelper(substatement.object)
def test_InitGroupActor(self): substatement = SubStatement(actor=Group(member=[Agent(name='test')])) self.groupVerificationHelper(substatement.actor)
def test_FromJSONExceptionBadJSON(self): with self.assertRaises(ValueError): Group.from_json('{"bad JSON"}')
def test_AddMemberAnon(self): group = Group() group.addmember({"name": "test"}) self.assertIsInstance(group.member[0], Agent)
def test_InitEmpty(self): group = Group() self.assertEqual(group.member, [])
def test_InitUnpack(self): obj = {"member": [{"name": "test"}]} group = Group(**obj) self.assertIsInstance(group.member[0], Agent)
def test_InitObjectType(self): group = Group(object_type='Group') self.assertEqual(group.object_type, 'Group') self.assertEqual(group.member, [])
def test_FromJSONEmptyObject(self): group = Group.from_json('{}') self.assertEquals(group.member, [])
def test_InitMember(self): group = Group(member=[Agent(name='test')]) self.assertIsInstance(group.member[0], Agent)
def test_FromJSONExceptionBadJSON(self): with self.assertRaises(ValueError): group = Group.from_json('{"bad JSON"}')
def test_InitMemberAnon(self): group = Group(member=[{"name": "test"}]) self.assertIsInstance(group.member[0], Agent)
def test_FromJSONExceptionEmpty(self): with self.assertRaises(ValueError): Group.from_json('')
def test_ToJSON(self): group = Group(**{'member':[{'name':'test'}, {'name':'test2'}]}) self.assertEqual(group.to_json(), '{"member": [{"name": "test", "objectType": "Agent"}, {"name": "test2", "objectType": "Agent"}], "objectType": "Group"}')
def test_FromJSONEmptyObject(self): group = Group.from_json('{}') self.assertEqual(group.member, [])
def socialmedia_builder(statement_id, verb, platform, account_name, account_homepage, object_type, object_id, message, tags=[], parent_object_type=None, parent_id=None, rating=None, instructor_name=None, instructor_email=None, team_name=None, unit=None, account_email=None, user_name=None, timestamp=None, other_contexts=[]): agentaccount = AgentAccount(name=account_name, home_page=account_homepage) actor = Agent(account=agentaccount) # XAPI statements can only have one of: AgentAccount, mbox, mboxsha1 or Openid #if (account_email is not None): # actor.mbox = account_email #if (user_name is not None): # actor.name = user_name verb_obj = Verb(id=xapi_settings.get_verb_iri(verb), display=LanguageMap({'en-US': verb})) #message = message.decode('utf-8').encode('ascii', 'ignore') #message.decode('utf-8').replace(u"\u2018", "'").replace(u"\u2019", "'").replace(u"\u2013", "-").replace(u"\ud83d", " ").replace(u"\ude09", " ").replace(u"\u00a0l", " ").replace(u"\ud83d", " ").replace(u"\u2026", " ").replace(u"\ude09", " ").replace(u"\u00a0"," ") object = Activity( id=object_id, object_type=object_type, definition=ActivityDefinition( name=LanguageMap({'en-US': message}), type=xapi_settings.get_object_iri(object_type)), ) taglist = [] for tag in tags: tagobject = Activity( id='http://id.tincanapi.com/activity/tags/tincan', object_type='Activity', definition=ActivityDefinition( name=LanguageMap({'en-US': tag}), type=xapi_settings.get_object_iri('Tag')), ) taglist.append(tagobject) # Add "other" in contextActivities other_contexts_list = [] for other in other_contexts: other_context_obj = Activity( id=other['obj_id'], object_type=other['obj_type'], definition=ActivityDefinition(name=LanguageMap( {'en-US': other['definition']['name']}), type=other['definition']['type']), ) other_contexts_list.append(other_context_obj) taglist.extend(other_contexts_list) parentlist = [] if (verb in ['liked', 'shared', 'commented', 'rated']): #recipe specific config parentobject = Activity( id=parent_id, object_type=parent_object_type, ) parentlist.append(parentobject) elif (platform == 'GitHub' or platform.lower() == 'trello'): parentobject = Activity( id=parent_id, object_type=parent_object_type, ) parentlist.append(parentobject) courselist = [] if unit is not None: courseobject = Activity( id="http://adlnet.gov/expapi/activities/course", object_type='Course', definition=ActivityDefinition( name=LanguageMap({'en-US': unit.code}), description=LanguageMap({ 'en-US': "A course/unit of learning hosted on the CLAToolkit" }))) courselist.append(courseobject) instructor = None if (instructor_name is not None): instructor = Agent(name=instructor_name, mbox=instructor_email) team = None if (team_name is not None): team = Group(Agent(name=team_name), object_type='Group') result = None if (rating is not None): rating_as_float = float(rating) result = Result(score=Score(raw=rating_as_float)) context = Context(registration=uuid.uuid4(), platform=platform, instructor=instructor, team=team, context_activities=ContextActivities( other=ActivityList(taglist), parent=ActivityList(parentlist), grouping=ActivityList(courselist))) # Xapi spec requires that the learning provider SHOULD provide the authority # Authority is a group with Agent as oauth consumer app where name is token url and homepage is consumer_key account = AgentAccount(name=unit.get_lrs_key(), home_page=unit.get_lrs_access_token_url()) authority = Group(Agent(account=account)) statement = statement_builder(statement_id, actor, verb_obj, object, context, result, authority, timestamp) return statement