예제 #1
0
 def testPiazzaToUserIntIdMapping(self):
     importer = PiazzaImporter('unittest', # MySQL user
                               '',         # MySQL pwd
                               'unittest', # MySQL db
                               None,       # MySQL table
                               'data/test_PiazzaContent.json', # JSON Piazza content file path
                               usersFileName='data/test_PiazzaUsers.json', # JSON Piazza user info file path
                               unittesting=True)
     importer.importJsonUsersFromPiazzaZip('data/test_PiazzaUsers.json')        
     self.assertEqual(210129, importer.idPiazza2UserIntId('hqyjmaplhAK'))
     self.assertEqual(211516, importer.idPiazza2UserIntId('hc19qkoyc9C'))
예제 #2
0
    def testUsersImport(self):
        # Get an importer with minimal initialization;
        # accomplished by the 'unittesting=True':
        importer = PiazzaImporter('unittest', # MySQL user
                                  '',         # MySQL pwd
                                  'unittest', # MySQL db
                                  None,       # MySQL table
                                  'data/test_PiazzaContent.json', # JSON Piazza content file path
                                  usersFileName='data/test_PiazzaUsers.json', # JSON Piazza user info file path
                                  unittesting=True)

        importer.importJsonUsersFromPiazzaZip('data/test_PiazzaUsers.json')
        
        # Test retrieval of one PiazzaUser instance by Piazza uid.
        hc19qkoyc9C_UserObj = PiazzaImporter.usersByPiazzaId['hc19qkoyc9C']

        # To make this test work even if the underlying database
        # does not have a mapping from the LTI to user int,
        # force correctness, but at least check for correct type:
        if type(hc19qkoyc9C_UserObj) != int:
            assert("A PiazzaUser's user_int_id must be an integer.")
        
        truth = [(u'asks', 0), 
                 (u'views', 7), 
                 ('ext_id', u'47bf69315b7391dace7ccbc344690969'), 
                 ('piazza_id', u'hc19qkoyc9C'), 
                 (u'posts', 0), 
                 (u'days', 7), 
                 (u'answers', 0),
                 ('anon_screen_name', 'anon_screen_name_redacted'),
                 ('user_int_id', hc19qkoyc9C_UserObj['user_int_id']),
                 ]
        
        self.assertItemsEqual(truth, hc19qkoyc9C_UserObj.items())
        
        # Check that PiazzaImporter created the proper number of PiazzaUser
        # instances. It should equal the number of top level JSON 
        # structs in the test file for this test method:
        with open('data/test_PiazzaUsers.json', 'r') as fd:
            numJsonLines = len(json.load(fd))
        self.assertEqual(numJsonLines, len(importer))
        
        hc19qkoyc9C_UserObj.keys()
예제 #3
0
 def testUnmapablePiazzaId(self):
     postObj = PiazzaPost({'id' : 'badPiazzaId', 'foo' : 10, 'bar' : 20})
     anon = PiazzaImporter.makeUnknowAnonScreenName(postObj['id'])
     self.assertEqual('unknown_piazza_mapping_badPiazzaId', anon)
예제 #4
0
    def testContentLoadingToMemory(self):
        
        piazzaImporter = PiazzaImporter('unittest',       # MySQL user 
                                        '',               # MySQL pwd
                                        'unittest',       # MySQL db
                                        'piazza_content', # MySQL table
                                        'data/test_PiazzaContent.json', # Test file from Piazza
                                        'data/test_PiazzaUsers.json',
                                        mappingFile='data/test_AccountMappingInput.csv')


        firstObj = piazzaImporter[0]
        self.assertEqual('XJ8LO55EUbrCAcMyzSbP3w==', firstObj['oid'])
        secondObj = piazzaImporter[1]
        self.assertEqual('9OOUV8g_SqbPDuRldhHUhw==', secondObj['oid'])
        self.assertEqual('D7ObrIJu2xZS-vCNSH9RqQ==', firstObj['children'][0]['oid'])

        anon_screen_name = firstObj['anon_screen_name']
        self.assertEqual('8caf8996ed242c081908e29e134f93f075343e4f', anon_screen_name)
        
        
        #   --- Getting Subject --- 
        
        # Getting subject from a PiazzaPost instance:
        subject = firstObj['subject']
        self.assertEqual('wireshark shows same packet sent twice', subject)
        
        # Getting subject from a PiazzaPost OID:
        subject = piazzaImporter.getSubject(firstObj)
        self.assertEqual('wireshark shows same packet sent twice', subject)
        
        # Getting subject from a PiazzaPost JSON object:
        subject = piazzaImporter.getSubject(firstObj.nameValueDict)
        self.assertEqual('wireshark shows same packet sent twice', subject)
        
        # Getting the subject of the first post:
        subject = piazzaImporter[0]['subject']
        self.assertEqual('wireshark shows same packet sent twice', subject)
        
        #   --- Getting Content --- 

        # Getting content from a PiazzaPost instance:
        content = piazzaImporter.getContent(secondObj)
        self.assertEqual('<p>Just wanted to pop in and say hello just as class starts.', content)
        
        # Getting content from a PiazzaPost OID:
        content = piazzaImporter.getContent(secondObj['oid'])
        self.assertEqual('<p>Just wanted to pop in and say hello just as class starts.', content)
        
        # Get first post's content (i.e. body):
        content = piazzaImporter[0]['content']
        self.assertEqual('<p>I tried to use', content[0:17])
        
        # Get first post's content (i.e. body):
        content = piazzaImporter[0]['body']    # For compatibility with OpenEdX forum
        self.assertEqual('<p>I tried to use', content[0:17])

        
        # Get first post's tags:
        tags = piazzaImporter[0]['tags']
        self.assertEqual([u'lectures',u'student'], tags)
        
        # First post's Piazze UID:
        piazzaUID = piazzaImporter[0]['id']
        self.assertEqual('hr7xjaytsC8', piazzaUID)
        
        # First post's status:
        status = piazzaImporter[0]['status']
        self.assertEqual('active', status)
        
        # First post's 'no answer followup':
        noAnswerFollowup = piazzaImporter[0]['no_answer_followup']
        self.assertEqual(0, noAnswerFollowup)
        
        # First post's creation date:
        cDate = piazzaImporter[0]['created']
        self.assertEqual('2014-01-26T10:08:18Z', cDate)
        
        # First post's creation date using get()
        cDate = piazzaImporter[0].get('created', '0000-00-00T00:00:00')
        self.assertEqual('2014-01-26T10:08:18Z', cDate)
        
        # Keys of a PiazzaPost object:
        postObjKeys = piazzaImporter[0].keys()
        postObjKeys.sort()
        trueKeys =  ['anon_screen_name', u'change_log', u'children', u'config', u'created', u'folders', u'history', u'id', u'no_answer', u'no_answer_followup', u'nr', 'oid', u'status', u'tag_endorse_arr', u'tag_good', u'tag_good_arr', u'tags', u'type', u'unique_views']

        self.assertEqual(trueKeys, postObjKeys)
        
        # First post's type:
        theType = piazzaImporter[0]['type']
        self.assertEqual('question', theType)
        
        # First post's good-tags-array of anon_screen_name:
        tagGoodArr = piazzaImporter[0]['tag_good_arr']
        self.assertEqual(['8491933cf7fd48668da31fdcddc1e55a3fdb120b'], tagGoodArr)
        # Synonym for tag_good_arr: good_tag:
        tagGoodArr = piazzaImporter[0]['good_tags']
        self.assertEqual(['8491933cf7fd48668da31fdcddc1e55a3fdb120b'], tagGoodArr)
        
        # First endorse-tags array:
        tagEndorseArr = piazzaImporter[0]['tag_endorse_arr']
        self.assertEqual(['ac79b0b077dd8c44d9ea6dfac1f08e6cd0ba29ea'], tagEndorseArr)
        # Synonym for tag_endorse_arr: endorse_tags:
        tagEndorseArr = piazzaImporter[0]['endorse_tags']
        self.assertEqual(['ac79b0b077dd8c44d9ea6dfac1f08e6cd0ba29ea'], tagEndorseArr)
        
        
        # Second post's number of up-votes received:
        numVotes = piazzaImporter[1]['no_upvotes'] # second JSON obj!
        self.assertEqual(2, numVotes)
        
        # Second post's number of answers received:
        numAnswers = piazzaImporter[1]['no_answer'] # second JSON obj!
        self.assertEqual(0, numAnswers)
        
        # Second post's number of followup answers received:
        numAnswers = piazzaImporter[1]['no_answer_followup'] # second JSON obj!
        self.assertEqual(0, numAnswers)
        
        # Second post's 'is anonymous post':
        anonLevel = piazzaImporter[1]['anon']
        self.assertEqual('no', anonLevel)
        
        # First post's bucket name:
        try:
            piazzaImporter[0]['bucket_name']
            self.fail('Obj 0 has no bucket name; should be KeyError')
        except KeyError:
            pass

        # Second post's bucket name:
        bucketName = piazzaImporter[1]['bucket_name'] # second JSON obj!
        self.assertEqual('Week 1/19 - 1/25', bucketName)

        # Second post's 'updated' field:
        updated = piazzaImporter[1]['updated'] # second JSON obj!
        self.assertEqual('2014-01-22T06:55:56Z', updated)

        # First post's 'folders' (names) array:
        folders = piazzaImporter[0]['folders']
        self.assertEqual(['lectures'], folders)

        # Test idPiazza2Anon():
        self.assertEqual('8caf8996ed242c081908e29e134f93f075343e4f', piazzaImporter.idPiazza2Anon('hr7xjaytsC8'))
예제 #5
0
    def testHighLevelAccessors(self):

        piazzaImporter = PiazzaImporter('unittest',       # MySQL user 
                                        '',               # MySQL pwd
                                        'unittest',       # MySQL db
                                        'piazza_content', # MySQL table
                                        'data/test_PiazzaContent.json', # Test file from Piazza
                                        'data/test_PiazzaUsers.json',
                                        )


        firstPostObj = piazzaImporter[0]
        
        self.assertEqual('hr7xjaytsC8',firstPostObj['piazza_id'])

        #   --- Getting Children --- 
        children = firstPostObj['children']
        self.assertEqual(1, len(children))
        child = children[0]
        self.assertEqual('wABgOWyIDVYn-8kzkgMnNw==', child['oid'])

        childHistArr = child['history']
        self.assertEqual(1, len(childHistArr))
        childHistContentObj = childHistArr[0]
        self.assertEqual('2014-01-26T18:13:50Z', childHistContentObj['created'])

        self.assertEqual('hqq8e4mn3fp115',childHistContentObj['piazza_id'])
        self.assertGreater(childHistContentObj['user_int_id'],-1)

        #   --- Getting Subject --- 
        
        # Getting subject from a PiazzaPost instance:
        subject = firstPostObj['subject']
        self.assertEqual('wireshark shows same packet sent twice', subject)
        
        # Getting subject from a PiazzaPost OID:
        subject = piazzaImporter.getSubject(firstPostObj)
        self.assertEqual('wireshark shows same packet sent twice', subject)
        
        # Getting subject from a PiazzaPost JSON object:
        subject = piazzaImporter.getSubject(firstPostObj.nameValueDict)
        self.assertEqual('wireshark shows same packet sent twice', subject)
        
        # Getting the subject of the first post:
        subject = piazzaImporter[0]['subject']
        self.assertEqual('wireshark shows same packet sent twice', subject)
        
        # Date of second entry in first object's history array:
        theDate = firstPostObj['history'][1]['create_date']
        self.assertEqual('2014-01-26T10:08:18Z', theDate)
        
        # Test history arrays with more than one entry:
        thirdPostObj = piazzaImporter[2]
        
        self.assertEqual('2014-01-22T08:09:32Z', thirdPostObj['updated'])
        self.assertEqual('<p>64-bit Waterfox</p>', thirdPostObj['subject'])
        
        thirdPostKeys = [
                        'folders',
                        'updated',
                        'no_upvotes',
                        'uid',
                        'no_answer',
                        'config',
                        'created',
                        'piazza_id',
                        'user_int_id',
                        'anon_screen_name',
                        'id',
                        'change_log',
                        'anon',
                        'bucket_name',
                        'type',
                        'bucket_order',
                        'data',
                        'children',
                        'subject',
                        'oid'
                        ]
        self.assertItemsEqual(thirdPostKeys, thirdPostObj.keys())

        
        fourthPostObj = piazzaImporter[3]
        
        self.assertEqual(241, fourthPostObj['unique_views'])
        
        #****** Test: nr == 9 when nr is renamed

        fourthObjSubj = fourthPostObj['subject']
        self.assertEqual('pre-course survey is not viewing', fourthObjSubj)

        self.assertEqual('h814wubnNmj',fourthPostObj['history'][4]['piazza_id'])

        
        
        #   --- Getting Content --- 

        # Getting content from a PiazzaPost instance:
        fourthObjContent = ['Content objFour 5th from bottom.',
                            'Content objFour 4th from bottom.',
                            'Content objFour 3rd from bottom.',
                            'Content objFour 2nd from bottom.',
                            'Content objFour 1st from bottom.'
                            ]

        
        
        contentArr = fourthPostObj['content']
        self.assertItemsEqual(fourthObjContent, contentArr)
        
        # Getting content from a PiazzaPost OID:
        contentArr = piazzaImporter.getContent(fourthPostObj['oid'])
        self.assertItemsEqual(fourthObjContent, contentArr)
        
        # Get first post's content (i.e. body):
        content = piazzaImporter[-1]['body']    # For compatibility with OpenEdX forum
        self.assertEqual(fourthObjContent, content)

        
        # Get first post's tags:
        tags = piazzaImporter[0]['tags']
        self.assertEqual([u'lectures',u'student'], tags)
        
        # First post's Piazze UID:
        piazzaUID = piazzaImporter[0]['id']
        self.assertEqual('hr7xjaytsC8', piazzaUID)
        
        # First post's status:
        status = piazzaImporter[0]['status']
        self.assertEqual('active', status)
        
        # First post's 'no answer followup':
        noAnswerFollowup = piazzaImporter[0]['no_answer_followup']
        self.assertEqual(0, noAnswerFollowup)
        
        # First post's creation date:
        cDate = piazzaImporter[0]['created']
        self.assertEqual('2014-01-26T10:08:18Z', cDate)
        
        # First post's creation date using get()
        cDate = piazzaImporter[0].get('created', '0000-00-00T00:00:00')
        self.assertEqual('2014-01-26T10:08:18Z', cDate)
        
        # First post's type:
        theType = piazzaImporter[0]['type']
        self.assertEqual('question', theType)
        
        # First post's good-tags-array of user_int_id's;
        # In original json these are arrays of Piazza Ids.
        # That raw is ['hc19qkoyc9C']; get the user_int_id
        # we expect:
        user_int_id = PiazzaImporter.idPiazza2UserIntId('hc19qkoyc9C')
        tagGoodArr = piazzaImporter[0]['tag_good_arr']
        self.assertEqual([user_int_id], tagGoodArr)
        # Synonym for tag_good_arr: good_tags:
        tagGoodArr = piazzaImporter[0]['good_tags']
        self.assertEqual([user_int_id], tagGoodArr)
        
        # First endorse-tags array:
        tagEndorseArr = piazzaImporter[0]['tag_endorse_arr']
        self.assertEqual([232871], tagEndorseArr)
        # Synonym for tag_endorse_arr: endorse_tags:
        tagEndorseArr = piazzaImporter[0]['endorse_tags']
        self.assertEqual([232871], tagEndorseArr)
        
        
        # Second post's number of up-votes received:
        numVotes = piazzaImporter[1]['no_upvotes'] # second JSON obj!
        self.assertEqual(2, numVotes)
        
        # Second post's number of answers received:
        numAnswers = piazzaImporter[1]['no_answer'] # second JSON obj!
        self.assertEqual(0, numAnswers)
        
        # Second post's number of followup answers received:
        numAnswers = piazzaImporter[1]['no_answer_followup'] # second JSON obj!
        self.assertEqual(0, numAnswers)
        
        # Second post's 'is anonymous post':
        anonLevel = piazzaImporter[1]['anon']
        self.assertEqual('no', anonLevel)
        
        # First post's bucket name property does not exist:
        self.assertIsNone(piazzaImporter[0]['bucket_name'])
        
        self.assertFalse(piazzaImporter[0].has_key('bucket_name'))

        # Second post's bucket name:
        self.assertTrue(piazzaImporter[1].has_key('bucket_name'))
        bucketName = piazzaImporter[1]['bucket_name'] # second JSON obj!
        self.assertEqual('Week 1/19 - 1/25', bucketName)

        # Second post's 'updated' field:
        updated = piazzaImporter[1]['updated'] # second JSON obj!
        self.assertEqual('2014-01-22T06:55:56Z', updated)

        # First post's 'folders' (names) array:
        folders = piazzaImporter[0]['folders']
        self.assertEqual(['lectures'], folders)

        # Test idPiazza2Anon():
        self.assertEqual(260005, piazzaImporter.idPiazza2UserIntId('hr7xjaytsC8'))