Example #1
0
def test_serialization_with_custom_object():
    ejson.cleanup_registry()
    value = Person('Lincoln', 25)

    # Before registering
    ejson.dumps.when.called_with(value).should.throw(
        TypeError, 'Person("Lincoln", 25) is not JSON serializable')

    # After registering
    @ejson.register_serializer(Person)
    def serialize_person(instance):
        return {"name": instance.name, "age": instance.age}
    ejson.dumps(value).should.be.equals(
        '{"__class__": "tests.test_ejson.Person", "__value__": {"age": 25, "name": "Lincoln"}}')
Example #2
0
def test_serialization_with_datetime():
    ejson.cleanup_registry()
    value = {'dt': datetime(2012, 8, 22, 3, 44, 05)}

    # Before registering
    ejson.dumps.when.called_with(value).should.throw(
        TypeError,
        'datetime.datetime(2012, 8, 22, 3, 44, 5) is not JSON serializable')

    # After registering
    @ejson.register_serializer(datetime)
    def serialize_datetime(instance):
        return instance.isoformat()
    ejson.dumps(value).should.be.equals(
        '{"dt": {"__class__": "datetime.datetime", "__value__": "2012-08-22T03:44:05"}}')
Example #3
0
def test_log_when_debug_is_false(datetime, find_event, tasks):
    conf.DEBUG = False
    core.cleanup_handlers()
    datetime.now.return_value = 'tea time'

    eventlib.log('app.Event')
    tasks.process_task.delay.assert_called_once_with('app.Event', ejson.dumps({
        '__ip_address__': '0.0.0.0', '__datetime__': 'tea time',
    }))
Example #4
0
def test_process_raises_the_exception_when_debugging(find_event):
    core.cleanup_handlers()
    conf.DEBUG = True

    handler_fail = Mock()
    handler_fail.side_effect = ValueError('P0wned!!!')
    eventlib.handler('myapp.CoolEvent')(handler_fail)
    name, data = 'myapp.CoolEvent', ejson.dumps({'a': 1})
    core.process.when.called_with(name,
                                  data).should.throw(ValueError, 'P0wned!!!')
Example #5
0
def test_process_raises_the_exception_when_debugging(find_event):
    core.cleanup_handlers()
    conf.DEBUG = True

    handler_fail = Mock()
    handler_fail.side_effect = ValueError('P0wned!!!')
    eventlib.handler('myapp.CoolEvent')(handler_fail)
    name, data = 'myapp.CoolEvent', ejson.dumps({'a': 1})
    core.process.when.called_with(name, data).should.throw(
        ValueError, 'P0wned!!!')
Example #6
0
def test_log_when_debug_is_false(datetime, find_event, tasks):
    conf.DEBUG = False
    core.cleanup_handlers()
    datetime.now.return_value = 'tea time'

    eventlib.log('app.Event')
    tasks.process_task.delay.assert_called_once_with(
        'app.Event',
        ejson.dumps({
            '__ip_address__': '0.0.0.0',
            '__datetime__': 'tea time',
        }))
Example #7
0
def test_process(find_event):
    core.cleanup_handlers()

    handler = Mock()
    eventlib.handler('app.Event')(handler)

    handler2 = Mock()
    eventlib.handler('app.Event')(handler2)

    data = {'file': '/etc/passwd', 'server': 'yipster'}
    core.process('app.Event', ejson.dumps(data))

    handler.assert_called_once_with(data)
    handler2.assert_called_once_with(data)
Example #8
0
def test_log(find_event, process, datetime):
    conf.DEBUG = True
    core.cleanup_handlers()
    datetime.now.return_value = 'tea time'

    event_cls = Mock()
    find_event.return_value = event_cls

    data = {'name': 'Event System', 'code': 42}
    eventlib.log('app.Event', data)
    find_event.assert_called_once_with('app.Event')
    event_cls.assert_called_once_with('app.Event', data)
    event_cls.return_value.validate.assert_called_once()
    process.assert_called_once_with('app.Event', ejson.dumps(data))
Example #9
0
def test_process(find_event):
    core.cleanup_handlers()

    handler = Mock()
    eventlib.handler('app.Event')(handler)

    handler2 = Mock()
    eventlib.handler('app.Event')(handler2)

    data = {'file': '/etc/passwd', 'server': 'yipster'}
    core.process('app.Event', ejson.dumps(data))

    handler.assert_called_once_with(data)
    handler2.assert_called_once_with(data)
Example #10
0
def test_log(find_event, process, datetime):
    conf.DEBUG = True
    core.cleanup_handlers()
    datetime.now.return_value = 'tea time'

    event_cls = Mock()
    find_event.return_value = event_cls

    data = {'name': 'Event System', 'code': 42}
    eventlib.log('app.Event', data)
    find_event.assert_called_once_with('app.Event')
    event_cls.assert_called_once_with('app.Event', data)
    event_cls.return_value.validate.assert_called_once()
    process.assert_called_once_with('app.Event', ejson.dumps(data))
Example #11
0
def test_process_data_clean(logger, find_event):
    core.cleanup_handlers()

    class MyEvent(eventlib.BaseEvent):
        def clean(self):
            raise exceptions.ValidationError('Owned!!11')

    data = {'name': 'Lincoln', 'answer': 42}
    find_event.return_value = MyEvent
    core.process('stuff', ejson.dumps(data))
    logger.warning.assert_called_once_with(
        'The event system just got an exception while cleaning data '
        "for the event 'stuff'\n"
        "data: {\"answer\": 42, \"name\": \"Lincoln\"}\n"
        "exc: Owned!!11")
Example #12
0
def test_process_data_clean(logger, find_event):
    core.cleanup_handlers()

    class MyEvent(eventlib.BaseEvent):
        def clean(self):
            raise exceptions.ValidationError('Owned!!11')

    data = {'name': 'Lincoln', 'answer': 42}
    find_event.return_value = MyEvent
    core.process('stuff', ejson.dumps(data))
    logger.warning.assert_called_once_with(
        'The event system just got an exception while cleaning data '
        "for the event 'stuff'\n"
        "data: {\"answer\": 42, \"name\": \"Lincoln\"}\n"
        "exc: Owned!!11")
Example #13
0
def test_process_fails_gracefully(logger, find_event):
    core.cleanup_handlers()
    conf.DEBUG = False

    handler_fail = Mock()
    handler_fail.side_effect = ValueError('P0wned!!!')
    eventlib.handler('myapp.CoolEvent')(handler_fail)

    handler = Mock()
    eventlib.handler('myapp.CoolEvent')(handler)

    data = {'a': 1}
    event = 'myapp.CoolEvent'
    core.process(event, ejson.dumps(data))

    logger.warning.assert_called_once_with(
        'One of the handlers for the event "myapp.CoolEvent" has '
        'failed with the following exception: P0wned!!!')
    handler.assert_called_once_with(data)
Example #14
0
def test_process_fails_gracefully(logger, find_event):
    core.cleanup_handlers()
    conf.DEBUG = False

    handler_fail = Mock()
    handler_fail.side_effect = ValueError('P0wned!!!')
    eventlib.handler('myapp.CoolEvent')(handler_fail)

    handler = Mock()
    eventlib.handler('myapp.CoolEvent')(handler)

    data = {'a': 1}
    event = 'myapp.CoolEvent'
    core.process(event, ejson.dumps(data))

    logger.warning.assert_called_once_with(
        'One of the handlers for the event "myapp.CoolEvent" has '
        'failed with the following exception: P0wned!!!')
    handler.assert_called_once_with(data)