Esempio n. 1
0
    def test_import(self):
        '''Tests the pulling and insertion of a batch of FB events'''
        eid_notice_pairs = [('110580932351209',None),   # 
                            ('185404598198638',None),   # 
                            ('35942576698',TypeError),  # page id 
                            ('9423481220941280',FacebookAPIError),      # bogus id
                            ('291107654260858',EventImportReport.EventInstanceExists),
            ]
        random.shuffle(eid_notice_pairs)

        # grab original FB records from any pages that already exist
        original_fb_records = {}
        for eid,notice in eid_notice_pairs:
            if notice is EventImportReport.EventInstanceExists:
                original_fb_records[eid] = FacebookEventRecord.objects.get(fb_id=eid)

        # run insertion code
        mgr = EventImportManager()
        eids = [pair[0] for pair in eid_notice_pairs]
        mgr.pull_event_info(eids)
        results = [mgr.import_event(eid) for eid in eids]
        self.assertEquals([result.fbevent_id for result in results],
                          [eid for eid,_ in eid_notice_pairs],
                          'non-parallel list of EventImportReports returned')

        for pair,result in zip(eid_notice_pairs,results):
            eid,expected_notice = pair
            if not expected_notice:
                self.assertEquals([],result.notices)
                # assert a new model instance was created and it's FB record matches what was returned
                try:
                    event = FacebookEventRecord.objects.get(fb_id=eid).event
                except FacebookEventRecord.DoesNotExist:
                    self.fail('No event record for fbid %s' % eid)
                if event != result.event_instance:
                    self.fail('No event created for fbid %s' % eid)
            else:
                # assert no model instance is returned
                self.assertIsNone(result.event_instance)
                # assert expected notice was generated
                self.assertEquals(len(result.notices),1)
                self.assertTrue(isinstance(result.notices[0],expected_notice),
                                'Expecting notice %s from importing fb page %s' % (str(expected_notice),eid))
                
                # if notice was a EventInstanceExists, be sure the original record wasn't touched
                if expected_notice is EventImportReport.EventInstanceExists:
                    self.assertEquals(original_fb_records[eid],
                                        FacebookEventRecord.objects.get(fb_id=eid))
                # otherwise, make sure no record was created at all
                else:
                    with self.assertRaises(FacebookEventRecord.DoesNotExist):
                        FacebookEventRecord.objects.get(fb_id=eid)
Esempio n. 2
0
    def test_import_no_related(self):
        '''Tests the importing of a batch of FB events without permission to import related object'''
        owner_not_stored = '184248831671921'    # (org not in fixture)
        owner_stored = '143902725663363'        # (org in fixture already)

        before_orgs = list(Organization.objects.all())
        before_org_records = list(FacebookOrgRecord.objects.all())

        mgr = EventImportManager()
        
        # ensure no event role is set since nothing existed without an import
        result = mgr.import_event(owner_not_stored,import_owners=False)
        self.assertEquals(0,result.event_instance.role_set.count())

        # get the related host id Facebook id for the event that has a host already stored
        event_info = mgr.pull_event_info([owner_stored])[0]
        host_fbid = event_info['owner']['id']

        # ensure the existing org was found used to connect to the second event
        result = mgr.import_event(owner_stored,import_owners=False)
        self.assertEquals(result.event_instance.role_set.get(role_type='host').organization,
                            FacebookOrgRecord.objects.get(fb_id=host_fbid).organization)

        # double check that the Place, Organization, and related link tables weren't touched
        self.assertEquals(before_orgs,list(Organization.objects.all()))
        self.assertEquals(before_org_records,list(FacebookOrgRecord.objects.all()))
Esempio n. 3
0
    def test_pulling(self):
        '''
        Tests pulling of a batch of FB events -- not Event model saving
        '''
        event_ids = ['159484480801269',  # valid event page
                     '828371892334123']  # invalid fbid
        # add 100 random ids to the list to ensure batch code is working well
        event_ids.extend([str(random.randint(1,1e13)) for i in range(100)])

        mgr = EventImportManager()
        fbevents = mgr.pull_event_info(event_ids)
        self.assertEquals(len(fbevents),len(event_ids))

        valid_event = fbevents[0]
        self.assertIn('start_time',valid_event.keys())
        self.assertIn('owner',valid_event.keys())

        invalid_event = fbevents[1]
        self.assertTrue(isinstance(invalid_event,FacebookAPIError))