Ejemplo n.º 1
0
    async def test_success(self):
        all_impacts = await self.prepere_data()

        data = impacts_pb2.GetTargetsImpactsRequest(targets=[
            impacts_pb2.Object(type=1, id=10),
            impacts_pb2.Object(type=1, id=20),
            impacts_pb2.Object(type=2, id=30)
        ]).SerializeToString()

        request = await self.client.post('/get-targets-impacts', data=data)
        answer = await self.check_success(
            request, impacts_pb2.GetTargetsImpactsResponse)

        self.assertCountEqual(
            [protobuf.to_target_impact(impact) for impact in answer.impacts], [
                objects.TargetImpact(
                    target=objects.Object(1, 10),
                    amount=all_impacts[-1].amount + all_impacts[-5].amount,
                    turn=max(all_impacts[-1].turn, all_impacts[-5].turn),
                    time=max(all_impacts[-1].time, all_impacts[-5].time)),
                objects.TargetImpact(
                    target=objects.Object(1, 20),
                    amount=all_impacts[-2].amount + all_impacts[-6].amount,
                    turn=max(all_impacts[-2].turn, all_impacts[-6].turn),
                    time=max(all_impacts[-2].time, all_impacts[-6].time)),
                objects.TargetImpact(target=objects.Object(2, 30),
                                     amount=all_impacts[-4].amount,
                                     turn=all_impacts[-4].turn,
                                     time=all_impacts[-4].time)
            ])
Ejemplo n.º 2
0
 def tt_object(self):
     return impacts_pb2.Impact(
         actor=impacts_pb2.Object(type=self.actor_type.value,
                                  id=self.actor_id),
         target=impacts_pb2.Object(type=self.target_type.value,
                                   id=self.target_id),
         amount=int(math.ceil(self.amount)),
         transaction=self.transaction.hex,
         turn=self.turn,
         time=time.mktime(self.time.timetuple()) +
         self.time.microsecond / 1000000 if self.time else None)
Ejemplo n.º 3
0
    async def test_success(self):
        await self.prepere_data()

        data = impacts_pb2.GetImpactersRatingsRequest(
            targets=[
                impacts_pb2.Object(type=1, id=10),
                impacts_pb2.Object(type=1, id=20),
                impacts_pb2.Object(type=2, id=10),
                impacts_pb2.Object(type=3, id=30)
            ],
            actor_types=[0],
            limit=2).SerializeToString()

        request = await self.client.post('/get-impacters-ratings', data=data)
        answer = await self.check_success(
            request, impacts_pb2.GetImpactersRatingsResponse)

        self.assertEqual(len(answer.ratings), 4)

        ratings = sorted(answer.ratings,
                         key=lambda rating:
                         (rating.target.type, rating.target.id))

        self.assertEqual(ratings[0].target, impacts_pb2.Object(type=1, id=10))
        self.assertEqual(len(ratings[0].records), 1)

        self.assertEqual(ratings[1].target, impacts_pb2.Object(type=1, id=20))
        self.assertEqual(len(ratings[1].records), 2)

        self.assertEqual(ratings[2].target, impacts_pb2.Object(type=2, id=10))
        self.assertEqual(len(ratings[2].records), 2)  # limit

        self.assertEqual(ratings[3].target, impacts_pb2.Object(type=3, id=30))
        self.assertEqual(len(ratings[3].records), 0)
Ejemplo n.º 4
0
    def cmd_get_impacters_ratings(self, targets, actor_types, limit):
        data = impacts_pb2.GetImpactersRatingsRequest(
            targets=[
                impacts_pb2.Object(type=target_type.value, id=target_id)
                for target_type, target_id in targets
            ],
            actor_types=[actor_type.value for actor_type in actor_types],
            limit=limit)

        answer = tt_api.sync_request(
            url=self.entry_point + 'get-impacters-ratings',
            data=data,
            AnswerType=impacts_pb2.GetImpactersRatingsResponse)

        ratings = {}

        for answer_rating in answer.ratings:
            target = (OBJECT_TYPE(answer_rating.target.type),
                      answer_rating.target.id)
            rating = [
                PowerImpact(type=self.impact_type,
                            actor_type=OBJECT_TYPE(record.actor.type),
                            actor_id=record.actor.id,
                            target_type=target[0],
                            target_id=target[1],
                            amount=record.amount)
                for record in answer_rating.records
            ]

            ratings[target] = rating

        return ratings
Ejemplo n.º 5
0
    async def test_last_actor_targets_impacts__limit(self):
        all_impacts = await self.prepere_data()

        data = impacts_pb2.GetImpactsHistoryRequest(
            filter=impacts_pb2.GetImpactsHistoryRequest.FilterType.Value(
                'BOTH'),
            actor=impacts_pb2.Object(type=1, id=10),
            target=impacts_pb2.Object(type=100, id=1000),
            limit=1).SerializeToString()

        request = await self.client.post('/get-impacts-history', data=data)
        answer = await self.check_success(
            request, impacts_pb2.GetImpactsHistoryResponse)

        self.assertEqual([
            protobuf.to_impact(impact, use_time=True)
            for impact in answer.impacts
        ], [all_impacts[4]])
Ejemplo n.º 6
0
    def cmd_get_last_power_impacts(self,
                                   limit,
                                   actor_type=None,
                                   actor_id=None,
                                   target_type=None,
                                   target_id=None):

        filter_type = impacts_pb2.GetImpactsHistoryRequest.FilterType.Value(
            'NONE')

        if actor_type is not None:
            if target_type is not None:
                filter_type = impacts_pb2.GetImpactsHistoryRequest.FilterType.Value(
                    'BOTH')
            else:
                filter_type = impacts_pb2.GetImpactsHistoryRequest.FilterType.Value(
                    'ONLY_ACTOR')
        elif target_type is not None:
            filter_type = impacts_pb2.GetImpactsHistoryRequest.FilterType.Value(
                'ONLY_TARGET')

        data = impacts_pb2.GetImpactsHistoryRequest(
            filter=filter_type,
            actor=impacts_pb2.Object(
                type=actor_type.value if actor_type else None, id=actor_id),
            target=impacts_pb2.Object(
                type=target_type.value if target_type else None, id=target_id),
            limit=limit)

        answer = tt_api.sync_request(
            url=self.entry_point + 'get-impacts-history',
            data=data,
            AnswerType=impacts_pb2.GetImpactsHistoryResponse)

        return [
            PowerImpact.from_tt_object(type=self.impact_type, tt_impact=impact)
            for impact in answer.impacts
        ]
Ejemplo n.º 7
0
    async def test_no_impacts(self):
        await self.prepair_data()

        data = impacts_pb2.GetActorImpactsRequest(
            actor=impacts_pb2.Object(type=666, id=1000),
            target_types=[1, 2]).SerializeToString()

        request = await self.client.post('/get-actor-impacts', data=data)
        answer = await self.check_success(request,
                                          impacts_pb2.GetActorImpactsResponse)

        self.assertCountEqual(
            [protobuf.to_target_impact(impact) for impact in answer.impacts],
            [])
Ejemplo n.º 8
0
    async def test_has_impacts__filter_target_types(self):
        all_impacts = await self.prepair_data()

        data = impacts_pb2.GetActorImpactsRequest(
            actor=impacts_pb2.Object(type=100, id=1000),
            target_types=[2]).SerializeToString()

        request = await self.client.post('/get-actor-impacts', data=data)
        answer = await self.check_success(request,
                                          impacts_pb2.GetActorImpactsResponse)

        self.assertCountEqual(
            [protobuf.to_target_impact(impact) for impact in answer.impacts], [
                objects.TargetImpact(target=objects.Object(2, 30),
                                     amount=all_impacts[-4].amount,
                                     turn=all_impacts[-4].turn,
                                     time=all_impacts[-4].time)
            ])
Ejemplo n.º 9
0
    def cmd_get_actor_impacts(self, actor_type, actor_id, target_types):
        data = impacts_pb2.GetActorImpactsRequest(
            actor=impacts_pb2.Object(type=actor_type.value, id=actor_id),
            target_types=[target_type.value for target_type in target_types])

        answer = tt_api.sync_request(
            url=self.entry_point + 'get-actor-impacts',
            data=data,
            AnswerType=impacts_pb2.GetActorImpactsResponse)

        return [
            PowerImpact(type=self.impact_type,
                        actor_type=actor_type,
                        actor_id=actor_id,
                        target_type=OBJECT_TYPE(impact.target.type),
                        target_id=impact.target.id,
                        amount=impact.amount) for impact in answer.impacts
        ]
Ejemplo n.º 10
0
    def cmd_get_targets_impacts(self, targets):
        data = impacts_pb2.GetTargetsImpactsRequest(targets=[
            impacts_pb2.Object(type=target_type.value, id=target_id)
            for target_type, target_id in targets
        ])

        answer = tt_api.sync_request(
            url=self.entry_point + 'get-targets-impacts',
            data=data,
            AnswerType=impacts_pb2.GetTargetsImpactsResponse)

        return [
            PowerImpact(type=self.impact_type,
                        actor_type=None,
                        actor_id=None,
                        target_type=OBJECT_TYPE(impact.target.type),
                        target_id=impact.target.id,
                        amount=impact.amount) for impact in answer.impacts
        ]
Ejemplo n.º 11
0
def from_object(object):
    return impacts_pb2.Object(type=object.type, id=object.id)