Ejemplo n.º 1
0
def test_load_save():
    store = InMemoryStore()
    assert store.load(17) is None

    data = ChatData(show_text=False, charset="ita2")
    store.save(17, data)
    assert store.load(17) == data
Ejemplo n.º 2
0
def test_save(context, kwargs, expected):
    _, _, store, message_context = context
    store.save(17, ChatData(charset="ita2", output_format=Format.TEXT))
    message_context.save(**kwargs)

    result = store.load(17)
    assert result == expected
Ejemplo n.º 3
0
def test_cancel_nothing():
    with patch("keypunch_bot.bot.Updater"):
        bot = KeyPunchBot("", MagicMock())

    context = MagicMock()
    context.data = ChatData()
    bot.cancel_format(context)
    context.answer.assert_called_with(context.lang.__getitem__.return_value)
    context.lang.__getitem__.assert_called_with(("cancel", "fail"))
Ejemplo n.º 4
0
def test_clearing_format_on_text():
    with patch("keypunch_bot.bot.Updater"):
        bot = KeyPunchBot("", MagicMock())

    context = MagicMock()
    context.data = ChatData()
    context.message = "hello, world"
    bot.text(context)
    context.save.assert_called_with(format=Format.DEFAULT)
Ejemplo n.º 5
0
def test_cancel_done():
    with patch("keypunch_bot.bot.Updater"):
        bot = KeyPunchBot("", MagicMock())

    context = MagicMock()
    context.data = ChatData(output_format=Format.PNG)
    bot.cancel_format(context)
    context.answer.assert_called_with(context.lang.__getitem__.return_value)
    context.lang.__getitem__.assert_called_with(("cancel", "done"))
    context.save.assert_called_with(format=Format.DEFAULT)
Ejemplo n.º 6
0
def test_setting_format_argument():
    with patch("keypunch_bot.bot.Updater"):
        bot = KeyPunchBot("", MagicMock())

    context = MagicMock()
    context.data = ChatData()
    context.message = "hello, world"
    bot.set_format(context, Format.PNG)

    context.send_file.assert_called()
    context.save.assert_not_called()
Ejemplo n.º 7
0
    lang = message_context.lang
    assert lang is translation_manager.get.return_value
    translation_manager.get.assert_called_with("ru")


def test_no_language(context):
    update, translation_manager, _, message_context = context

    update.message.from_user.language_code = None
    lang = message_context.lang
    assert lang is translation_manager.default_lang


@pytest.mark.parametrize("kwargs, expected", [
    ({"charset": "mtk2"},
     ChatData(charset="mtk2", output_format=Format.TEXT, show_text=True)),
    ({"show_text": False},
     ChatData(charset="ita2", output_format=Format.TEXT, show_text=False)),
    ({"format": Format.PNG},
     ChatData(charset="ita2", output_format=Format.PNG, show_text=True)),
    ({"charset": "mtk2", "format": Format.PNG, "show_text": False},
     ChatData(charset="mtk2", output_format=Format.PNG, show_text=False))
])
def test_save(context, kwargs, expected):
    _, _, store, message_context = context
    store.save(17, ChatData(charset="ita2", output_format=Format.TEXT))
    message_context.save(**kwargs)

    result = store.load(17)
    assert result == expected
Ejemplo n.º 8
0
# (at your option) any later version.

# KeypunchBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with KeypunchBot. If not, see <http://www.gnu.org/licenses/>.

from unittest.mock import patch
import pytest
import mongomock
from keypunch_bot.persistance import MongoStore, ChatData, Format

DATA = ChatData(show_text=False, output_format=Format.PNG, charset="ita2")
RECORD = {"_id": 74, "show_text": False, "format": "png", "charset": "ita2"}
RECORD_2 = {"_id": 74, "show_text": True, "format": "text", "charset": "ita2"}


@patch("keypunch_bot.persistance.mongo.MongoClient", mongomock.MongoClient)
def test_inserting():
    store = MongoStore("mongodb://localhost/database")
    store.save(74, DATA)
    collection = store.mongo.database.chat_info
    assert collection.find_one({"_id": 74}) == RECORD


@patch("keypunch_bot.persistance.mongo.MongoClient", mongomock.MongoClient)
def test_updating():
    store = MongoStore("mongodb://localhost/database")
Ejemplo n.º 9
0
def test_default():
    store = InMemoryStore()
    assert store.load_or_default(17) == ChatData()