Example #1
0
    def test_can_summarize_model_mutations(self):


        # summarize the service
        summarized = self.service.summarize()

        # the target summary
        target = {
            'mutations': [
                {
                    'name': api_conventions.crud_mutation_name(action='create', model=self.model),
                    'event': conventions.get_crud_action(method='create', model=self.model),
                    'isAsync': False,
                    'inputs': api_conventions.create_mutation_inputs(self),
                    'outputs': api_conventions.create_mutation_outputs(self),
                },
                {
                    'name': api_conventions.crud_mutation_name(action='update', model=self.model),
                    'event': conventions.get_crud_action(method='update', model=self.model),
                    'isAsync': False,
                    'inputs': api_conventions.update_mutation_inputs(self),
                    'outputs': api_conventions.update_mutation_outputs(self),
                },
                {
                    'name': api_conventions.crud_mutation_name(action='delete', model=self.model),
                    'event': conventions.get_crud_action(method='delete', model=self.model),
                    'isAsync': False,
                    'inputs': api_conventions.delete_mutation_inputs(self),
                    'outputs': api_conventions.delete_mutation_outputs(self),
                },
            ]
        }

        # make sure the mutations match
        assert len(target['mutations']) == len(summarized['mutations']), (
            "Incorrect number of mutations in summary."
        )

        # go over every mutation in the
        for summarized_mutation in summarized['mutations']:

            # grab the corresponding entry in the target
            equiv = [mut for mut in target['mutations'] \
                                if mut['name'] == summarized_mutation['name']][0]
            # make sure the fields match up
            assert equiv['event'] == summarized_mutation['event'], (
                "Summarized mutation has the wrong event value"
            )
            assert equiv['isAsync'] == summarized_mutation['isAsync'], (
                "Summarized mutation has the wrong isAsync value"
            )
Example #2
0
    async def test_listens_for_related_deletes(self):
        # the model connecting the two
        connection_model = self.model()
        setattr(connection_model, model_service_name(self.service1), 1)
        setattr(connection_model, model_service_name(self.service2), 1)
        connection_model.save()

        # make sure the connection model can be found
        assert self.model.get(self.service1_value == 1), (
            "Test record could not be created before deleting."
        )

        # the action data for a related delete
        action_type = conventions.get_crud_action(
            'delete',
            self.service1,
            status='success'
        )
        payload = dict(id=1)

        # instantiate an action handler to test
        handler = self.service.action_handler()
        # fire the action
        await handler.handle_action(
            action_type=action_type,
            payload=payload,
            props={},
            notify=False
        )

        # make sure the model can't be found
        self.assertRaises(Exception, self.model.get, self.service1_value == 1)
Example #3
0
 async def _verify_delete_action_handler(self, model_id):
     # fire a delete action
     await self.action_handler.handle_action(
         action_type=conventions.get_crud_action('delete', self.model),
         payload=dict(id=model_id),
         props={},
         notify=False
     )
     # expect an error
     self.assertRaises(Exception, self.model.get, model_id)
Example #4
0
 async def _verify_update_action_handler(self, model_id):
     # fire an update action
     await self.action_handler.handle_action(
         action_type=conventions.get_crud_action('update', self.model),
         payload=dict(id=model_id, name='barz'),
         props={},
         notify=False
     )
     # check that a model matches
     self.model.get(self.model.name == 'barz')
Example #5
0
    async def _verify_create_action_handler(self):
        # fire a create action
        await self.action_handler.handle_action(
            action_type=conventions.get_crud_action('create', self.model),
            payload=dict(name='foo'),
            props={},
            notify=False
        )

        # make sure the created record was found and save the id
        return self.model.get(self.model.name == 'foo').id
Example #6
0
 async def _verify_action_handler_delete(self):
     # create an instance of the action handler
     handler = self.service.action_handler()
     # call the service action handler
     await handler.handle_action(
         action_type=conventions.get_crud_action('delete', self.model),
         payload=dict(id=self.model_id),
         props={},
         notify=False
     )
     # expect an error
     self.assertRaises(Exception, self.model.get, self.model_id)
Example #7
0
 async def _verify_action_handler_update(self):
     payload = {'id':self.model_id, model_service_name(self.service1): 'bars'}
     # create an instance of the action handler
     handler = self.service.action_handler()
     # call the service action handler
     await handler.handle_action(
         action_type=conventions.get_crud_action('update', self.model),
         props={},
         payload=payload,
         notify=False
     )
     # check that a model matches
     self.model.get(self.service1_value == 'bars')
Example #8
0
 async def _verify_action_handler_create(self):
     action_type = conventions.get_crud_action('create', self.service.model)
     payload = {
         model_service_name(self.service1): 'foo',
         model_service_name(self.service2): 'bar'
     }
     # create an instance of the action handler
     handler = self.service.action_handler()
     # fire a create action
     await handler.handle_action(
         action_type=action_type,
         payload=payload,
         props={},
         notify=False
     )
     # the query to find a matching model
     matching_model = self.service1_value == 'foo'
     # make sure the created record was found and save the id
     self.model_id = self.model_id = self.model.get(matching_model).id