Exemple #1
0
def test_save_context():
    bot = Minette(prepare_table=True)
    with bot.connection_provider.get_connection() as connection:
        # register context for test
        ctx = bot.context_store.get(
            channel="save_context_test", channel_user_id=user_id,
            connection=connection)
        ctx.data["unixtime"] = date_to_unixtime(now)

        # save
        ctx.topic.keep_on = True
        bot._save_context(ctx, connection)

        # check
        request = Message(
            text="hello", channel="save_context_test", channel_user_id=user_id)
        context = bot._get_context(request, connection)
        assert context.channel == "save_context_test"
        assert context.channel_user_id == user_id
        assert context.data["unixtime"] == date_to_unixtime(now)
Exemple #2
0
def test_get_context():
    bot = Minette(prepare_table=True)
    with bot.connection_provider.get_connection() as connection:
        # register context for test
        ctx = bot.context_store.get(
            channel="get_context_test", channel_user_id=user_id,
            connection=connection)
        ctx.data["unixtime"] = date_to_unixtime(now)
        bot.context_store.save(ctx, connection)
        ctx_group = bot.context_store.get(
            channel="get_context_test", channel_user_id="group_" + user_id,
            connection=connection)
        ctx_group.data["unixtime"] = date_to_unixtime(now)
        bot.context_store.save(ctx_group, connection)
        ctx_detail = bot.context_store.get(
            channel="get_context_test_detail", channel_user_id=user_id,
            connection=connection)
        ctx_detail.data["unixtime"] = date_to_unixtime(now)
        bot.context_store.save(ctx_detail, connection)

        # without detail
        request = Message(
            text="hello", channel="get_context_test", channel_user_id=user_id)
        context = bot._get_context(request, connection)
        assert context.channel == "get_context_test"
        assert context.channel_user_id == user_id
        assert context.data["unixtime"] == date_to_unixtime(now)

        # group without group
        request = Message(
            text="hello", channel="get_context_test", channel_user_id=user_id)
        request.group = Group(id="group_" + user_id)
        context = bot._get_context(request, connection)
        assert context.channel == "get_context_test"
        assert context.channel_user_id == "group_" + user_id
        assert context.data["unixtime"] == date_to_unixtime(now)

        # with detail
        bot.config.confg_parser.set(
            "minette", "context_scope", "channel_detail")
        request = Message(
            text="hello", channel="get_context_test", channel_detail="detail",
            channel_user_id=user_id)
        context = bot._get_context(request, connection)
        assert context.channel == "get_context_test_detail"
        assert context.channel_user_id == user_id
        assert context.data["unixtime"] == date_to_unixtime(now)
Exemple #3
0
from pytz import timezone
from logging import Logger, FileHandler, getLogger
from datetime import datetime
from types import GeneratorType

from minette import (
    Minette, DialogService, SQLiteConnectionProvider,
    SQLiteContextStore, SQLiteUserStore, SQLiteMessageLogStore,
    Tagger, Config, DialogRouter, StoreSet, Message, User, Group,
    DependencyContainer, Payload
)
from minette.utils import date_to_unixtime
from minette.tagger.janometagger import JanomeTagger

now = datetime.now()
user_id = "user_id" + str(date_to_unixtime(now))
print("user_id: {}".format(user_id))


class CustomTagger(Tagger):
    pass


class CustomConnectionProvider(SQLiteConnectionProvider):
    pass


class CustomContextStore(SQLiteContextStore):
    pass

Exemple #4
0
def test_save(datastore_class, connection_str):
    if not datastore_class:
        pytest.skip("Unable to import DataStoreSet")
    if not connection_str:
        pytest.skip("Connection string for {} is not provided".format(
            datastore_class.connection_provider.__name__))

    ms = datastore_class.messagelog_store(table_name=table_name,
                                          timezone=timezone("Asia/Tokyo"))
    with datastore_class.connection_provider(
            connection_str).get_connection() as connection:
        # request
        request = Message(id=str(date_to_unixtime(now)),
                          channel="TEST",
                          channel_user_id=user_id,
                          text="request message {}".format(
                              str(date_to_unixtime(now))))
        # response
        response = Response(messages=[
            Message(channel="TEST",
                    channel_user_id=user_id,
                    text="response message {}".format(
                        str(date_to_unixtime(now))))
        ])
        # context
        context = Context("TEST", user_id)
        context.data = {
            "strvalue": "value1",
            "intvalue": 2,
            "dtvalue": date_to_str(now),
            "dictvalue": {
                "k1": "v1",
                "k2": 2,
            }
        }

        # save
        save_res = ms.save(request, response, context, connection)

        # check
        if AzureTableConnection and isinstance(connection,
                                               AzureTableConnection):
            record = connection.get_entity(table_name, user_id, save_res)
        elif SQLAlchemyConnection and isinstance(connection,
                                                 SQLAlchemyConnection):
            record = connection.query(SQLAlchemyMessageLog).filter(
                SQLAlchemyMessageLog.request_id == str(date_to_unixtime(
                    now))).first()
            record = dumpd(record)
        else:
            cursor = connection.cursor()
            if MySQLConnection and isinstance(connection, MySQLConnection):
                sql = "select * from {} where request_id = %s"
            else:
                sql = "select * from {} where request_id = ?"
            cursor.execute(sql.format(table_name),
                           (str(date_to_unixtime(now)), ))
            row = cursor.fetchone()
            if isinstance(row, dict):
                record = row
            else:
                record = dict(
                    zip([column[0] for column in cursor.description], row))

        assert record["request_text"] == "request message {}".format(
            str(date_to_unixtime(now)))
        assert record["response_text"] == "response message {}".format(
            str(date_to_unixtime(now)))
Exemple #5
0
SQLAlchemyStores = None
SQLAlchemyConnection = None
SQLAlchemyMessageLog = None
SQLAlchemyMessageLogStore = None
try:
    from minette.datastore.sqlalchemystores import (SQLAlchemyStores,
                                                    SQLAlchemyConnection,
                                                    SQLAlchemyMessageLog,
                                                    SQLAlchemyMessageLogStore)
except Exception:
    pass

from minette.utils import date_to_unixtime, date_to_str

now = datetime.now()
table_name = "messagelog" + str(date_to_unixtime(now))
user_id = "user_id" + str(date_to_unixtime(now))
print("messagelog_table: {}".format(table_name))
print("user_id: {}".format(user_id))

dbconfig = Config("config/test_config_datastores.ini")

datastore_params = [
    (
        SQLiteStores,
        "test.db",
    ),
    (
        SQLDBStores,
        dbconfig.get("sqldb_connection_str"),
    ),
Exemple #6
0
# SQLAlchemy
SQLAlchemyStores = None
SQLAlchemyConnectionProvider = None
try:
    from minette.datastore.sqlalchemystores import (
        SQLAlchemyStores,
        SQLAlchemyConnectionProvider
    )
except Exception:
    pass

from minette.utils import date_to_unixtime

now = datetime.now(tz=timezone("Asia/Tokyo"))
table_name = "context" + str(date_to_unixtime(now))
user_id = "user_id" + str(date_to_unixtime(now))
print("context_table: {}".format(table_name))
print("user_id: {}".format(user_id))

dbconfig = Config("config/test_config_datastores.ini")

datastore_params = [
    (
        SQLiteStores,
        "test.db",
    ),
    (
        SQLDBStores,
        dbconfig.get("sqldb_connection_str"),
    ),
Exemple #7
0
def test_date_to_unixtime():
    assert u.date_to_unixtime(aware_dt) == 1546365845