Exemplo n.º 1
0
    def test_import(self):
        """Tests the importing of a batch of FB pages as Orgs"""
        mgr = PageImportManager()
        pid_notice_pairs = [
            ("84714961156", None),  # Square Cafe
            ("139288502700", None),  # Pgh Marathon
            ("220439", TypeError),  # user page
            ("291107654260858", TypeError),  # event page
            ("9423481220941280", FacebookAPIError),  # bogus id
            ("53379078585", PageImportReport.ModelInstanceExists),
        ]
        random.shuffle(pid_notice_pairs)

        # grab original FB records from any pages that already exist
        original_fb_records = {}
        for pid, notice in pid_notice_pairs:
            if notice is PageImportReport.ModelInstanceExists:
                original_fb_records[pid] = FacebookOrgRecord.objects.get(fb_id=pid)
        pids = [pair[0] for pair in pid_notice_pairs]

        # run insertion code
        mgr.pull_page_info(pids)  # cache pages
        results = [mgr.import_org(pid) for pid in pids]
        self.assertEquals(
            [result.page_id for result in results],
            [pid for pid, _ in pid_notice_pairs],
            "non-parallel list of PageImportReports returned",
        )

        for pair, result in zip(pid_notice_pairs, results):
            pid, 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:
                    org = FacebookOrgRecord.objects.get(fb_id=pid).organization
                except FacebookOrgRecord.DoesNotExist:
                    self.fail("No organization record for fbid %s" % pid)
                if org != result.model_instance:
                    self.fail("No organization created for fbid %s" % pid)
            else:
                # assert no model instance is returned
                self.assertIsNone(result.model_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), pid),
                )

                # if notice was a ModelInstanceExists, be sure the original record wasn't touched
                if expected_notice is PageImportReport.ModelInstanceExists:
                    self.assertEquals(original_fb_records[pid], FacebookOrgRecord.objects.get(fb_id=pid))
                # otherwise, make sure no record was created at all
                else:
                    with self.assertRaises(FacebookOrgRecord.DoesNotExist):
                        FacebookOrgRecord.objects.get(fb_id=pid)
Exemplo n.º 2
0
    def test_pulling(self):
        """
        Tests internal FB page info gathering code -- not model importing.
        """
        page_ids = ["84714961156", "9423481220941280"]  # Square Cafe  # invalid fbid
        # add 100 random ids to the list to ensure batch code is working well
        page_ids.extend([str(random.randint(1, 1e12)) for i in range(100)])

        mgr = PageImportManager()
        page_infos = mgr.pull_page_info(page_ids)

        self.assertEquals(len(page_infos), len(page_ids))

        # can't really assert anything about some third party pages. be content
        # with just testing that there's a few of them and the first one has some
        # page-specific fields
        valid_info = page_infos[0]
        self.assertIn("name", valid_info.keys())
        self.assertIn("location", valid_info.keys())  # this is a Place page

        # the bogus id shouldn't be cached
        invalid_info = page_infos[1]
        self.assertTrue(isinstance(invalid_info, FacebookAPIError))