예제 #1
0
def test_locales_order():
    with patch_conf(LOADER_CONFIG_1):
        wd = WordDictionary()
        assert wd.list_locales() == ['fr', 'en']

    with patch_conf(LOADER_CONFIG_2):
        wd = WordDictionary()
        assert wd.list_locales() == ['en', 'fr']
예제 #2
0
def test_health_check():
    with patch_conf({'MIDDLEWARES': None}):
        assert len(list(MiddlewareManager.health_check())) == 1

    with patch_conf({'MIDDLEWARES': ['does.not.Exist']}):
        assert len(list(MiddlewareManager.health_check())) == 1

    conf = {'MIDDLEWARES': ['tests.issue_0029.test_manager.IsNotMiddleware']}
    with patch_conf(conf):
        assert len(list(MiddlewareManager.health_check())) == 1

    conf = {'MIDDLEWARES': ['tests.issue_0029.test_manager.AddOne']}
    with patch_conf(conf):
        assert len(list(MiddlewareManager.health_check())) == 0
예제 #3
0
def test_init():
    conf = {'MIDDLEWARES': ['tests.issue_0029.test_manager.AddOne']}
    with patch_conf(conf):
        m = MiddlewareManager()
        m.init()

        assert m.middlewares == [AddOne]
예제 #4
0
def test_intents_maker_singleton():
    with patch_conf(LOADER_CONFIG_3):
        from sys import modules
        if 'bernard.i18n' in modules:
            del modules['bernard.i18n']

        from bernard.i18n import intents as i
        assert i.FOO.strings() == ['bar', 'baz']
예제 #5
0
def test_wrong_import():
    conf = dict(BASE_CONF)
    conf['DEFAULT_STATE'] = 'does.not.Exist'

    with patch_conf(conf):
        problems = run(get_fsm_problems())

    assert any(x.code == '00005' for x in problems)
예제 #6
0
def test_fsm_init():
    with patch_conf(settings_file=ENGINE_SETTINGS_FILE):
        fsm = FSM()
        assert isinstance(fsm.register, RedisRegisterStore)
        assert isinstance(fsm.transitions, list)

        for t in fsm.transitions:
            assert isinstance(t, Transition)
예제 #7
0
def test_text_trigger(text_request):
    with patch_conf(LOADER_CONFIG):
        tt_factory = trig.Text.builder(intents.BAZ)
        tt = tt_factory(text_request)
        assert run(tt.rank()) == 1.0

        tt_factory = trig.Text.builder(intents.FOO)
        tt = tt_factory(text_request)
        assert run(tt.rank()) == 0.0
예제 #8
0
def test_translator_call():
    with patch_conf(LOADER_CONFIG):
        wd = WordDictionary()
        t = Translator(wd)
        s = t('FOO', 42, bar='baz')

        assert s.key == 'FOO'
        assert s.count == 42
        assert s.params == {'bar': 'baz'}
예제 #9
0
def test_translator_attr():
    with patch_conf(LOADER_CONFIG):
        wd = WordDictionary()
        t = Translator(wd)
        s = t.FOO

        assert s.key == 'FOO'
        assert s.count is None
        assert s.params == {}
예제 #10
0
def test_inline_keyboard():
    with patch_conf(LOADER_CONFIG):
        t = Translator()
        k = InlineKeyboard([[
            InlineKeyboardButton(text=t.HELLO),
        ]])

        assert run(k.serialize(None)) == {
            'inline_keyboard': [[{
                'text': 'Hello',
            }]],
        }
예제 #11
0
def test_fsm_confused_state():
    with patch_conf(settings_file=ENGINE_SETTINGS_FILE):
        fsm = FSM()

        reg = Register({})
        req = Request(MockEmptyMessage(), reg)
        run(req.transform())
        assert fsm._confused_state(req) == BaseTestState

        reg = Register({Register.STATE: 'tests.issue_0001.states.Hello'})
        req = Request(MockEmptyMessage(), reg)
        run(req.transform())
        assert fsm._confused_state(req) == Hello
예제 #12
0
def test_transform_layers(reg):
    with patch_conf(LOADER_CONFIG):
        req = Request(
            MockTextMessage(),
            reg,
        )
        run(req.transform())
        stack = req.stack

        assert layers.RawText in stack._transformed
        assert stack.get_layer(layers.RawText).text == 'foo'
        assert len(stack.get_layers(layers.Text)) == 1
        assert len(stack.get_layers(layers.RawText)) == 1
예제 #13
0
def test_render():
    with patch_conf(LOADER_CONFIG):
        wd = WordDictionary()
        t = Translator(wd)
        req = MockRequest()

        req.flags = {'gender': 'unknown'}
        assert run(t.HELLO.render_list(req)) == ['hello', 'wassup?']

        req.flags = {'gender': 'male'}
        assert run(t.HELLO.render_list(req)) == ['hello boy', 'wassup?']

        req.flags = {'gender': 'female'}
        assert run(t.HELLO.render_list(req)) == ['hello girl', 'wassup?']
예제 #14
0
def test_translate():
    with patch_conf(LOADER_CONFIG_1):
        wd = WordDictionary()
        t = Translator(wd)
        req = MockRequest()

        req.locale = 'fr'
        assert run(t.HELLO.render(req)) == 'Bonjour'

        req.locale = 'en'
        assert run(t.HELLO.render(req)) == 'Hello'

        req.locale = 'de'
        assert run(t.HELLO.render(req)) == 'Bonjour'
예제 #15
0
def test_choice_trigger(reg):
    with patch_conf(LOADER_CONFIG):
        req = MockRequest(MockTextMessage('foo', True), reg)
        run(req.transform())
        ct_factory = trig.Choice.builder()
        ct = ct_factory(req)  # type: trig.Choice
        assert run(ct.rank()) == 1.0
        assert ct.slug == 'foo'

        req = MockRequest(MockTextMessage('some other stuff'), reg)
        run(req.transform())
        ct_factory = trig.Choice.builder()
        ct = ct_factory(req)  # type: trig.Choice
        assert run(ct.rank()) == 1.0
        assert ct.slug == 'bar'
예제 #16
0
def test_get_strings():
    with patch_conf(LOADER_CONFIG):
        db = IntentsDb()
        maker = IntentsMaker(db)
        req = MockRequest()

        assert run(maker.HELLO.strings()) == [('Bonjour',)]

        req.locale = 'fr'
        assert run(maker.HELLO.strings(req)) == [('Bonjour',)]

        req.locale = 'en'
        assert run(maker.HELLO.strings(req)) == [('Hello',)]

        req.locale = 'de'
        assert run(maker.HELLO.strings(req)) == [('Bonjour',)]
예제 #17
0
def test_serialize():
    assert serialize('foo') == {
        'type': 'string',
        'value': 'foo',
    }

    with patch_conf(LOADER_CONFIG):
        wd = WordDictionary()
        t = Translator(wd)
        s = t.FOO

        assert serialize(s) == {
            'type': 'trans',
            'key': 'FOO',
            'count': None,
            'params': {},
        }
예제 #18
0
def test_acq_serialize():
    with patch_conf(LOADER_CONFIG):
        t = Translator()
        acq = AnswerCallbackQuery(
            text=t.HELLO,
            show_alert=False,
            url='http://google.fr',
            cache_time=42,
        )

        assert run(acq.serialize('foo', None)) == {
            'callback_query_id': 'foo',
            'text': 'Hello',
            'show_alert': False,
            'url': 'http://google.fr',
            'cache_time': 42,
        }
예제 #19
0
def test_redis_register_lock(redis_store):
    key = 'my-key'

    async def test_one():
        async with redis_store.work_on_register(key):
            await asyncio.sleep(0.002)

    async def test_two():
        async with redis_store.work_on_register(key):
            pass

    with patch_conf({'REDIS_POLL_INTERVAL': 0.001}):
        loop = asyncio.get_event_loop()
        loop.run_until_complete(asyncio.gather(
            test_one(),
            test_two(),
        ))
예제 #20
0
def test_unserialize():
    with patch_conf(LOADER_CONFIG):
        wd = WordDictionary()

        v = 42
        with pytest.raises(ValueError):
            # noinspection PyTypeChecker
            unserialize(wd, v)

        v = {}
        with pytest.raises(ValueError):
            unserialize(wd, v)

        v = {'type': 'string'}
        with pytest.raises(ValueError):
            unserialize(wd, v)

        v = {'type': 'trans'}
        with pytest.raises(ValueError):
            unserialize(wd, v)

        v = {'type': 'trans', 'params': 42}
        with pytest.raises(ValueError):
            unserialize(wd, v)

        v = {'type': 'trans', 'params': {42: True}}
        with pytest.raises(ValueError):
            unserialize(wd, v)

        v = {'type': 'trans', 'params': {'42': True}}
        with pytest.raises(ValueError):
            unserialize(wd, v)

        v = {
            'type': 'trans',
            'params': {
                '42': True
            },
            'key': 'FOO',
            'count': None
        }
        assert isinstance(unserialize(wd, v), StringToTranslate)

        v = {'type': 'string', 'value': 'foo'}
        assert unserialize(wd, v) == 'foo'
예제 #21
0
def test_story_hello():
    with patch_conf(settings_file=ENGINE_SETTINGS_FILE):
        _, platform = make_test_fsm()

        platform.handle(l.Text('Hello!'), )
        platform.assert_state(HowAreYou)
        platform.assert_sent(
            stack(l.Text(t.HELLO)),
            stack(
                l.Text(t.HOW_ARE_YOU),
                fbl.QuickRepliesList([
                    fbl.QuickRepliesList.TextOption('yes', t.YES, intents.YES),
                    fbl.QuickRepliesList.TextOption('no', t.NO, intents.NO),
                ])),
        )

        platform.handle(
            l.Text('Yes'),
            fbl.QuickReply('yes'),
        )
        platform.assert_sent(stack(l.Text(t.GREAT)))
예제 #22
0
def test_fsm_find_trigger(reg):
    with patch_conf(settings_file=ENGINE_SETTINGS_FILE):
        fsm = FSM()
        run(fsm.async_init())
        req = MockRequest(MockTextMessage('hello'), reg)
        run(req.transform())

        trigger, state, dnr = run(fsm._find_trigger(req))
        assert isinstance(trigger, trig.Text)
        assert state == Hello

        req = MockRequest(MockChoiceMessage(), reg)
        run(req.transform())
        trigger, state, dnr = run(fsm._find_trigger(req))
        assert trigger is None
        assert state is None

        reg = Register({
            Register.STATE: HowAreYou.name(),
            Register.TRANSITION: {
                'choices': {
                    'yes': {
                        'text': 'Yes',
                        'intent': 'YES',
                    },
                    'no': {
                        'text': 'No',
                        'intent': 'NO'
                    },
                },
            },
        })

        req = MockRequest(MockChoiceMessage(), reg)
        run(req.transform())
        trigger, state, dnr = run(fsm._find_trigger(req))
        assert isinstance(trigger, trig.Choice)
        assert state == Great
예제 #23
0
def test_multi_sentence_reverse():
    with patch_conf({'I18N_TRANSLATION_LOADERS': []}):
        wd = WordDictionary()

        wd.update_lang(None, [
            ('HELLO+1', 'hello'),
            ('HELLO+2', 'wassup?'),
        ], {'gender': 'unknown'})

        wd.update_lang(None, [
            ('HELLO+2', 'wassup girl?'),
        ], {'gender': 'female'})

        wd.update_lang(None, [
            ('HELLO+2', 'wassup boy?'),
        ], {'gender': 'male'})

        assert wd.get('HELLO', flags={
            'gender': 'male'
        }) == ['hello', 'wassup boy?']

        assert wd.get('HELLO', flags={
            'gender': 'female'
        }) == ['hello', 'wassup girl?']
예제 #24
0
def test_intent():
    with patch_conf(LOADER_CONFIG_3):
        db = IntentsDb()
        intent = Intent(db, 'FOO')
        assert intent.strings() == ['bar', 'baz']
예제 #25
0
def test_word_dict_param():
    with patch_conf(LOADER_CONFIG_2):
        wd = WordDictionary()
        assert wd.get('WITH_PARAM', name='Mike') == 'Hello Mike'
예제 #26
0
def test_word_dict_missing_param():
    with patch_conf(LOADER_CONFIG_2):
        wd = WordDictionary()

        with pytest.raises(MissingParamError):
            wd.get('WITH_PARAM')
예제 #27
0
def test_translate_render():
    with patch_conf(LOADER_CONFIG):
        wd = WordDictionary()
        t = Translator(wd)

        assert run(t.FOO.render()) == 'éléphant'
예제 #28
0
def test_anything_trigger(text_request):
    with patch_conf(LOADER_CONFIG):
        anything = trig.Anything.builder()
        t = anything(text_request)
        assert t.rank() == 1.0
예제 #29
0
def test_intents_db():
    with patch_conf(LOADER_CONFIG_3):
        db = IntentsDb()
        assert db.get('FOO') == ['bar', 'baz']
예제 #30
0
def test_intents_maker():
    with patch_conf(LOADER_CONFIG_3):
        db = IntentsDb()
        maker = IntentsMaker(db)
        assert maker.FOO.strings() == ['bar', 'baz']