Ejemplo n.º 1
0
def test_field_cause():
    obj = Obj()
    obj.disruption = models.Disruption()
    cause = models.Cause()
    obj.disruption.cause = cause
    class_field_cause = fields.FieldCause(Obj())
    eq_(class_field_cause.output(None, obj), cause.wording)
Ejemplo n.º 2
0
def create_cause_from_json(cause_json):
    cause = models.Cause()
    cause.id = cause_json['id']
    cause.created_at = get_datetime_from_json_attr(cause_json, 'created_at')
    cause.updated_at = get_datetime_from_json_attr(cause_json, 'updated_at')
    cause.wordings = create_cause_wordings_from_json(cause_json['wordings'])
    cause.category = create_cause_category_from_json(cause_json)

    return cause
Ejemplo n.º 3
0
def test_field_cause_none():
    obj = Obj()
    obj.disruption = models.Disruption()
    cause = models.Cause()
    wording = models.Wording()
    wording.key = 'external_long'
    wording.value = 'external medium'
    cause.wordings.append(wording)
    obj.disruption.cause = cause
    class_field_cause = fields.FieldCause(Obj())
    eq_(class_field_cause.output(None, obj), None)
Ejemplo n.º 4
0
class Cause(flask_restful.Resource):
    def __init__(self):
        self.parsers = {}
        self.parsers["get"] = reqparse.RequestParser()
        parser_get = self.parsers["get"]
        parser_get.add_argument("category", type=utils.get_uuid)

    @validate_client()
    @validate_id()
    def get(self, client, id=None):
        args = self.parsers['get'].parse_args()
        category_id = args['category']
        if id:
            response = {'cause': models.Cause.get(id, client.id, category_id)}
            return marshal(response, one_cause_fields)
        else:
            response = {
                'causes': models.Cause.all(client.id, category_id),
                'meta': {}
            }
            return marshal(response, causes_fields)

    @validate_client(True)
    def post(self, client):
        json = request.get_json(silent=True)
        logging.getLogger(__name__).debug('Post cause: %s', json)
        try:
            validate(json, cause_input_format)
        except ValidationError, e:
            logging.debug(str(e))
            # TODO: generate good error messages
            return marshal({'error': {
                'message': utils.parse_error(e)
            }}, error_fields), 400

        cause = models.Cause()
        mapper.fill_from_json(cause, json, mapper.cause_mapping)
        cause.client = client
        try:
            db_helper.manage_wordings(cause, json["wordings"])
        except exceptions.InvalidJson, e:
            return marshal({'error': {
                'message': utils.parse_error(e)
            }}, error_fields), 400
Ejemplo n.º 5
0
def test_disruption_with_rabbitmq_exception():
    '''
    Tests when a disruption fail to publish to rabbitmq
    :return:
    '''
    disruption = models.Disruption()

    #contributor
    disruption.contributor = models.Contributor()
    disruption.contributor_id = disruption.contributor.id

    #cause
    disruption.cause = models.Cause()
    disruption.cause.wording = "CauseTest"
    disruption.cause.category = models.Category()
    disruption.cause.category.name = "CategoryTest"
    disruption.reference = "DisruptionTest"

    #localization
    localization = models.PTobject()
    localization.uri = "stop_area:123"
    localization.type = "stop_area"
    disruption.localizations.append(localization)

    # Wording
    wording = models.Wording()
    wording.key = "key_1"
    wording.value = "value_1"
    disruption.cause.wordings.append(wording)
    wording = models.Wording()
    wording.key = "key_2"
    wording.value = "value_2"
    disruption.cause.wordings.append(wording)

    # Tag
    tag = models.Tag()
    tag.name = "rer"
    disruption.tags.append(tag)

    disruption.status = 'published'

    chaos.publisher.publish = MagicMock(return_value=False)
    to_rabbitmq_not_sent = send_disruption_to_navitia(disruption)
    eq_(to_rabbitmq_not_sent, False)
Ejemplo n.º 6
0
def test_disruption_with_published_status_is_sent():
    '''
    Tests that a disruption with status published is sent to navitia
    :return:
    '''
    disruption = models.Disruption()

    #contributor
    disruption.contributor = models.Contributor()
    disruption.contributor_id = disruption.contributor.id

    #cause
    disruption.cause = models.Cause()
    disruption.cause.wording = "CauseTest"
    disruption.cause.category = models.Category()
    disruption.cause.category.name = "CategoryTest"
    disruption.reference = "DisruptionTest"

    #localization
    localization = models.PTobject()
    localization.uri = "stop_area:123"
    localization.type = "stop_area"
    disruption.localizations.append(localization)

    # Wording
    wording = models.Wording()
    wording.key = "key_1"
    wording.value = "value_1"
    disruption.cause.wordings.append(wording)
    wording = models.Wording()
    wording.key = "key_2"
    wording.value = "value_2"
    disruption.cause.wordings.append(wording)

    # Tag
    tag = models.Tag()
    tag.name = "rer"
    disruption.tags.append(tag)

    disruption.status = 'published'
    has_been_sent = send_disruption_to_navitia(disruption)

    eq_(has_been_sent, None)