Skip to content

Commit 7a8db13

Browse files
committed
Migrate tests to pytest
1 parent 921e87d commit 7a8db13

File tree

4 files changed

+79
-72
lines changed

4 files changed

+79
-72
lines changed

bitrix24/bitrix24.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import warnings
88
from time import sleep
9+
from typing import Any, Dict
910
from urllib.parse import urlparse
1011

1112
import requests
@@ -36,13 +37,13 @@ def __init__(self, domain: str, timeout: int = 60, safe: bool = True):
3637
def _prepare_domain(self, domain: str):
3738
"""Normalize user passed domain to a valid one."""
3839
if not domain:
39-
raise Exception("Empty domain")
40+
raise BitrixError("Empty domain")
4041

4142
o = urlparse(domain)
4243
user_id, code = o.path.split("/")[2:4]
4344
return "{0}://{1}/rest/{2}/{3}".format(o.scheme, o.netloc, user_id, code)
4445

45-
def _prepare_params(self, params, prev=""):
46+
def _prepare_params(self, params: Dict[str, Any], prev=""):
4647
"""
4748
Transform list of parameters to a valid bitrix array.
4849
@@ -80,7 +81,8 @@ def _prepare_params(self, params, prev=""):
8081
ret += "{0}={1}&".format(key, value)
8182
return ret
8283

83-
def request(self, url, method, p):
84+
def request(self, method, p):
85+
url = "{0}/{1}.json".format(self.domain, method)
8486
if method.rsplit(".", 1)[0] in ["add", "update", "delete", "set"]:
8587
r = requests.post(url, data=p, timeout=self.timeout, verify=self.safe).json()
8688
else:
@@ -111,12 +113,11 @@ def callMethod(self, method: str, **params):
111113
Returning the REST method response as an array, an object or a scalar
112114
"""
113115
if not method or len(method.split(".")) < 3:
114-
raise BitrixError("Empty method")
116+
raise BitrixError("Wrong method name", 400)
115117

116118
try:
117-
url = "{0}/{1}.json".format(self.domain, method)
118119
p = self._prepare_params(params)
119-
r = self.request(url, method, p)
120+
r = self.request(method, p)
120121
if not r:
121122
return None
122123
except ValueError:

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
3+
from bitrix24 import Bitrix24
4+
5+
6+
@pytest.fixture
7+
def b24():
8+
return Bitrix24("https://example.bitrix24.com/rest/1/123456789")

tests/test_bitrix24.py

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,23 @@
1-
import unittest
1+
import pytest
22

33
from bitrix24 import Bitrix24, BitrixError
44

55

6-
class Bitrix24Test(unittest.TestCase):
7-
def setUp(self):
8-
self.b24 = Bitrix24("https://example.bitrix24.com/rest/1/123456789")
6+
def test_init_with_empty_domain():
7+
with pytest.raises(BitrixError):
8+
Bitrix24("")
99

10-
def test_init_with_empty_domain(self):
11-
with self.assertRaises(Exception):
12-
Bitrix24("")
1310

14-
def test_call_with_empty_method(self):
15-
with self.assertRaises(BitrixError):
16-
self.b24.callMethod("")
11+
def test_call_with_empty_method(b24):
12+
with pytest.raises(BitrixError):
13+
b24.callMethod("")
1714

18-
def test_call_non_exists_method(self):
19-
with self.assertRaises(BitrixError):
20-
self.b24.callMethod("hello.world")
2115

22-
def test_call_wrong_method(self):
23-
with self.assertRaises(BitrixError):
24-
self.b24.callMethod("helloworld")
16+
def test_call_non_exists_method(b24):
17+
with pytest.raises(BitrixError):
18+
b24.callMethod("hello.world")
2519

2620

27-
class ParamsPreparationTest(unittest.TestCase):
28-
def setUp(self):
29-
self.b24 = Bitrix24("https://example.bitrix24.com/rest/1/123456789")
30-
31-
def test_one_level(self):
32-
params = {"fruit": "apple"}
33-
param_string = self.b24._prepare_params(params)
34-
self.assertEqual(param_string, "fruit=apple&")
35-
36-
def test_one_level_several_items(self):
37-
params = {"fruit": "apple", "vegetable": "broccoli"}
38-
param_string = self.b24._prepare_params(params)
39-
self.assertEqual(param_string, "fruit=apple&vegetable=broccoli&")
40-
41-
def test_multi_level(self):
42-
params = {"fruit": {"citrus": "lemon"}}
43-
param_string = self.b24._prepare_params(params)
44-
self.assertEqual(param_string, "fruit[citrus]=lemon&")
45-
46-
def test_multi_level_deep(self):
47-
params = {"root": {"level 1": {"level 2": {"level 3": "value"}}}}
48-
param_string = self.b24._prepare_params(params)
49-
self.assertEqual(param_string, "root[level 1][level 2][level 3]=value&")
50-
51-
def test_list_dict_mixed(self):
52-
params = {"root": {"level 1": [{"list_dict 1": "value 1"}, {"list_dict 2": "value 2"}]}}
53-
param_string = self.b24._prepare_params(params)
54-
self.assertEqual(
55-
param_string,
56-
"root[level 1][0][list_dict 1]=value 1&root[level 1][1][list_dict 2]=value 2&",
57-
)
58-
59-
def test_multi_level_several_items(self):
60-
params = {"fruit": {"citrus": "lemon", "sweet": "apple"}}
61-
param_string = self.b24._prepare_params(params)
62-
self.assertEqual(param_string, "fruit[citrus]=lemon&fruit[sweet]=apple&")
63-
64-
def test_list(self):
65-
params = {"fruit": ["lemon", "apple"]}
66-
param_string = self.b24._prepare_params(params)
67-
self.assertEqual(param_string, "fruit[0]=lemon&fruit[1]=apple&")
68-
69-
def test_tuple(self):
70-
params = {"fruit": ("lemon", "apple")}
71-
param_string = self.b24._prepare_params(params)
72-
self.assertEqual(param_string, "fruit[0]=lemon&fruit[1]=apple&")
73-
74-
def test_string(self):
75-
param_string = self.b24._prepare_params("")
76-
self.assertEqual(param_string, "")
21+
def test_call_wrong_method(b24):
22+
with pytest.raises(BitrixError):
23+
b24.callMethod("helloworld")

tests/test_params_preparation.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
def test_one_level(b24):
2+
params = {"fruit": "apple"}
3+
param_string = b24._prepare_params(params)
4+
assert param_string == "fruit=apple&"
5+
6+
7+
def test_one_level_several_items(b24):
8+
params = {"fruit": "apple", "vegetable": "broccoli"}
9+
param_string = b24._prepare_params(params)
10+
assert param_string == "fruit=apple&vegetable=broccoli&"
11+
12+
13+
def test_multi_level(b24):
14+
params = {"fruit": {"citrus": "lemon"}}
15+
param_string = b24._prepare_params(params)
16+
assert param_string == "fruit[citrus]=lemon&"
17+
18+
19+
def test_multi_level_deep(b24):
20+
params = {"root": {"level 1": {"level 2": {"level 3": "value"}}}}
21+
param_string = b24._prepare_params(params)
22+
assert param_string == "root[level 1][level 2][level 3]=value&"
23+
24+
25+
def test_list_dict_mixed(b24):
26+
params = {"root": {"level 1": [{"list_d 1": "value 1"}, {"list_d 2": "value 2"}]}}
27+
param_string = b24._prepare_params(params)
28+
assert param_string == "root[level 1][0][list_d 1]=value 1&root[level 1][1][list_d 2]=value 2&"
29+
30+
31+
def test_multi_level_several_items(b24):
32+
params = {"fruit": {"citrus": "lemon", "sweet": "apple"}}
33+
param_string = b24._prepare_params(params)
34+
assert param_string == "fruit[citrus]=lemon&fruit[sweet]=apple&"
35+
36+
37+
def test_list(b24):
38+
params = {"fruit": ["lemon", "apple"]}
39+
param_string = b24._prepare_params(params)
40+
assert param_string == "fruit[0]=lemon&fruit[1]=apple&"
41+
42+
43+
def test_tuple(b24):
44+
params = {"fruit": ("lemon", "apple")}
45+
param_string = b24._prepare_params(params)
46+
assert param_string == "fruit[0]=lemon&fruit[1]=apple&"
47+
48+
49+
def test_string(b24):
50+
param_string = b24._prepare_params("")
51+
assert param_string == ""

0 commit comments

Comments
 (0)