Esempio n. 1
0
    def seed(cls, data, session=None, **attributes):
        session = session or cls.session
        trend_index = len(data) + 1
        seeded = []
        for item in data:
            serialized = item.as_dict
            petition = Petition(id=item.data.id, initial_data=serialized)
            petition.trend_index = trend_index
            petition.sync(serialized, dt.now())
            seeded.append(petition)

        session.add_all(seeded)
        session.commit()
        return seeded
Esempio n. 2
0
 def test_unpopulated_petitions_are_discovered(self, app):
     self.query = QueryFactory(imports=self.to_be_discovered,
                               items_per_page=self.counts["page"])
     results = Petition.discover(state="open")
     assert Petition.remote.async_query.call_count == 1
     assert len(results) == self.counts["discovered"]
     assert len(results - {p.data.id for p in self.to_be_discovered}) == 0
Esempio n. 3
0
    def test_with_missing_and_raise(self, handler=False):
        expected_message = "Petition(s) not found, for trend index update."
        expected_message += f" Missing ids: {self.expected_missing_ids}."
        expected_message += f" Found ids: {self.expected_found_ids}."

        error = None
        with pytest.raises(PetitionsNotFound) as error:
            result = Petition.update_trend_indexes(handle_missing=handler)
        assert str(error.value) == expected_message
Esempio n. 4
0
    def test_without_missing(self, handler):
        self.delete_missing_petitions()
        self.manage_expectations(handler, missing=True)
        result = Petition.update_trend_indexes(handle_missing=handler)
        result["found"].sort(key=lambda x: x.id)

        assert not any(result["missing"])
        assert self.expected_found_ids == [p.id for p in result["found"]]
        assert self.validate_updates(result["found"], self.managers["found"])
Esempio n. 5
0
    def test_raises_without_found(self, handler=False):
        self.delete_disinct_records()
        expected_message = "Record(s) not found, for growth rate update."
        expected_message += f" Found ids: []."

        error = None
        with pytest.raises(RecordsNotFound) as error:
            result = Petition.update_trend_indexes(handle_missing=handler)
        assert str(error.value) == expected_message
Esempio n. 6
0
    def test_populate(self):
        self.populated = Petition.populate(ids=self.expected_ids)
        assert sorted([p.id for p in self.populated]) == self.expected_ids

        expected = {"polled_at": FROZEN_DATETIME, "trend_index": 6}
        for petition in self.populated:
            expected.update(petition=petition,
                            expected=self.remote_data[petition.id])
            assert self.validate_petition(**expected)
Esempio n. 7
0
def get_petitions_where(state="all"):
    values = {"state": state}
    ViewUtils.validate_state_or_400(state)

    params = ViewUtils.get_params(request, whitelist="petition")
    query = Petition.where(state=state, **params)
    context = {"meta": {"query": params, "values": values}}

    petitions = ViewUtils.handle(context, query, request, get_petitions_where)
    serialized_petitions = PetitionSchema(many=True).dump(petitions)
    context.update(petitions=serialized_petitions)
    return context
    def test_combined_expressions(self, min_sigs=500_000):
        min_index = self.operands["trend_index"]
        trending_ids = self.expected_ids["trend_index"]["le"]
        expected_ids = self.expected_ids["trend_index"]["le"][0:3]
        trending_pts = self.query_ids(trending_ids).all()
        expected_pts = self.query_ids(expected_ids).all()
        trending_pts = [
            p.update(signatures=randrange(1, 100)) for p in trending_pts
        ]
        expected_pts = [
            p.update(signatures=randrange(min_sigs, (min_sigs * 2)))
            for p in expected_pts
        ]
        self.session.commit()

        filters = Petition.filter_expression(signatures={"gt": min_sigs},
                                             trend_index={"le": min_index})
        query = Petition.query.filter(*filters)
        assert self.sorted_ids(query.all()) == expected_ids
    def test_base_poll(self, session):
        self.configure_poll(session)
        for poll_index in range(3):
            for manager in self.petition_managers.values():
                manager.current_index = poll_index

        result = Petition.poll(geographic=False)

        for record in result:
            manager = self.petition_managers[record.petition_id]
            expected_petition = manager.current.petition
            initial_data = manager.timeline[0].petition.as_dict
            assert self.validate_petition(
                petition=record.petition,
                expected=expected_petition,
                polled_at=FROZEN_DATETIME,
                initial_data=initial_data,
                trend_index=self.trend_index
            )
            assert self.validate_base_record(
                record=record,
                signature_count=expected_petition.signature_count
            )
Esempio n. 10
0
 def test_existing_petitions_are_not_discovered(self, app):
     self.query = QueryFactory(imports=self.petition_data,
                               items_per_page=self.counts["page"])
     results = Petition.discover(state="open")
     assert Petition.remote.async_query.call_count == 1
     assert len(results) == 0
Esempio n. 11
0
def onboard_self_task(self, *args, **kwargs):
    with TaskHandler.execute(bind=self) as handler:
        kwargs = handler.getcallkwargs(Petition.onboard, kwargs)
        petition = Petition.onboard(kwargs["id"])
        logger.info(f"onboarded petition id: {petition.id}")
        return handler.commit(result=petition, schema=PetitionSchema())
Esempio n. 12
0
def geo_poll_task(self, *args, **kwargs):
    with TaskHandler.execute(bind=self) as handler:
        kwargs = handler.getcallkwargs(Petition.poll, kwargs)
        records = Petition.poll(geographic=True, **kwargs)
        logger.info(f"Petitions base polled: {records}")
        return handler.commit(result=[r.petition_id for r in records])
Esempio n. 13
0
def populate_task(self, *args, **kwargs):
    with TaskHandler.execute(bind=self) as handler:
        kwargs = handler.getcallkwargs(Petition.populate, kwargs)
        petitions = Petition.populate(**kwargs)
        logger.info(f"Petitions onboarded: {petitions}")
        return handler.commit(result=[p.id for p in petitions])
Esempio n. 14
0
def update_trend_indexes_task(self, *args, **kwargs):
    with TaskHandler.execute(bind=self) as handler:
        kwargs = handler.getcallkwargs(Petition.update_trend_indexes, kwargs)
        petitions = Petition.update_trend_indexes(**kwargs)
        logger.info(f"Petitions trend indexes updated: {petitions}")
        return handler.commit(result=[p.id for p in petitions])
 def test_trend_index_expressions(self, opr):
     filters = Petition.filter_expression(
         trend_index={opr: self.operands["trend_index"]})
     query = Petition.query.filter(*filters)
     assert self.sorted_ids(
         query.all()) == self.expected_ids["trend_index"][opr]
 def test_growth_expressions(self, opr):
     filters = Petition.filter_expression(
         growth_rate={opr: self.operands["growth_rate"]})
     query = Petition.query.filter(*filters)
     assert self.sorted_ids(
         query.all()) == self.expected_ids["growth_rate"][opr]
 def test_signatures_expressions(self, opr):
     filters = Petition.filter_expression(
         signatures={opr: self.operands["signatures"]})
     query = Petition.query.filter(*filters)
     assert self.sorted_ids(
         query.all()) == self.expected_ids["signatures"][opr]