Skip to content

Commit

Permalink
🎉 Release 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
torrua committed Dec 12, 2020
1 parent eaefe6d commit fa96f20
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
branch = True
source=callbaker
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
sudo: false
language: python
python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9-dev"
- "3.9"
- "3.10-dev"
- "nightly"

# command to install dependencies
install:
- pip install -r requirements.txt
- pip install -r requirements-tests.txt

# command to run tests
script:
- pytest tests/
- pytest --cov=callbaker tests/

after_success:
- bash <(curl -s https://codecov.io/bash)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# 👨‍🍳 callbaker
[![Download Callbaker](https://img.shields.io/pypi/v/callbaker.svg)](https://pypi.python.org/pypi/callbaker)
![CodeQL](https://github.com/torrua/callbaker/workflows/CodeQL/badge.svg?branch=master)
[![Build Status](https://travis-ci.com/torrua/callbaker.svg?branch=main)](https://travis-ci.com/torrua/callbaker)
[![codecov](https://codecov.io/gh/torrua/callbaker/branch/master/graph/badge.svg?token=CHCS5JEGZI)](https://codecov.io/gh/torrua/callbaker)

Telegram callback queries converter
23 changes: 20 additions & 3 deletions callbaker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
EXTERNAL_MARK_SEPARATOR = "&"
INTERNAL_MARK_SEPARATOR = "="
DEFAULT_MARK_VALUE = "*"

EMS = EXTERNAL_MARK_SEPARATOR
IMS = INTERNAL_MARK_SEPARATOR
DMV = DEFAULT_MARK_VALUE
Expand All @@ -20,6 +21,13 @@ def info_from_callback(call_data: str, separators: tuple = (EMS, IMS, DMV)) -> d
:return:
"""

if not isinstance(call_data, str):
raise TypeError(f"call_data should be a str type. You input {type(call_data)}.")

for sep in separators:
if not isinstance(sep, str):
raise TypeError(f"Separator should be a str type. You input {type(sep)}.")

_ems, _ims, _ = separators
separated_items = call_data.split(_ems)
parsed_items = [element.split(_ims)
Expand All @@ -40,9 +48,18 @@ def callback_from_info(info: dict, separators: tuple = (EMS, IMS, DMV)) -> str:
:param separators:
:return:
"""
if not isinstance(info, dict):
raise TypeError(f"info should be a dict type. You input {type(info)}.")

for sep in separators:
if not isinstance(sep, str):
raise TypeError(f"Separator should be a str type. You input {type(sep)}.")

_ems, _ims, _ = separators
return "".join(["%s%s%s%s" % (_ems, mark, _ims, value) for mark, value in info.items()])

callback = "".join(["%s%s%s%s" % (_ems, mark, _ims, value) for mark, value in info.items()])

if __name__ == "__main__":
pass
if len(callback) > 64:
raise ValueError("The length of callback_data should not be more that 64 symbols."
f"Your callback's length is {len(callback)} symbols.")
return callback
2 changes: 2 additions & 0 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest
pytest-cov
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

15 changes: 12 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
"""
Setup package module
"""

from distutils.core import setup


def read(filename):
with open(filename, encoding='utf-8') as file:
return file.read()


setup(
name='callbaker',
packages=['callbaker'],
version='1.0.0',
version='1.1.0',
license='MIT',
description="Telegram Button Callback Data Converter",
long_description=read("README.md"),
long_description_content_type="text/markdown",
author='torrua',
author_email='torrua@gmail.com',
url='https://github.com/torrua/callbaker',
download_url='https://github.com/torrua/callbaker/archive/v1.0.0.tar.gz',
download_url='https://github.com/torrua/callbaker/archive/v1.1.0.tar.gz',
keywords=['Convert', 'Callback', 'Keyboard', 'Telegram', "Inline"],
classifiers=[
'Development Status :: 4 - Beta', # "3 - Alpha", "4 - Beta" or "5 - Production/Stable"
Expand All @@ -25,5 +32,7 @@
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
python_requires='>=3.5',
)
Empty file added tests/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions tests/test_callback_from_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from callbaker import callback_from_info


class TestCallbackFromInfo:
@pytest.mark.parametrize("data", [None, False, 234, "str"])
def test_info_not_a_str(self, data):
with pytest.raises(TypeError) as _:
callback_from_info(info=data)

def test_separators_not_a_str(self):
with pytest.raises(TypeError) as _:
callback_from_info(info={"k": "v", }, separators=("1", "2", 3))

def test_info(self):
result = callback_from_info(
info={"k1": "str", "k2": 2, "k3": None, "k4": False, "k5": (1, 2)})
assert isinstance(result, str)
assert result == "&k1=str&k2=2&k3=None&k4=False&k5=(1, 2)"

def test_info_wrong(self):
with pytest.raises(ValueError) as _:
callback_from_info(
info={"k1": "s" * 60, "k2": 2, "k3": None,
"k4": False, "k5": (1, 2)})
51 changes: 51 additions & 0 deletions tests/test_info_from_callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest

from callbaker import info_from_callback


class TestInfoFromCallback:
"""
"""

bool_tuple = (None, False, True)

@pytest.mark.parametrize("data", ["key_1=value_1", "&key_1=value_1"])
def test_call_data_1(self, data):
result = info_from_callback(call_data=data)
assert isinstance(result, dict)
assert len(result) == 1
assert result == {'key_1': 'value_1'}

def test_call_data_2(self):
result = info_from_callback(
call_data="&key_1=value_1&key_2=value_2")
assert isinstance(result, dict)
assert len(result) == 2
assert result == {'key_1': 'value_1', 'key_2': 'value_2'}

@pytest.mark.parametrize("data", [None, False, 234])
def test_call_data_not_a_str(self, data):
with pytest.raises(TypeError) as _:
info_from_callback(call_data=data)

def test_separators_not_a_str(self):
with pytest.raises(TypeError) as _:
info_from_callback(call_data="test", separators=("1", "2", 3))

def test_call_data_without_separators(self):
with pytest.raises(ValueError) as _:
info_from_callback(call_data="test")

@pytest.mark.parametrize("value", bool_tuple)
def test_call_data_bool(self, value):
result = info_from_callback(
call_data=f"&key_1={value}")
assert isinstance(result, dict)
assert result['key_1'] == value

def test_call_data_digit(self):
result = info_from_callback(
call_data=f"&key_1=20")
assert isinstance(result, dict)
assert result['key_1'] == 20

0 comments on commit fa96f20

Please sign in to comment.