Skip to content

Commit 3a2d9e0

Browse files
committed
feat: Implementing Unit Test
1 parent 38ba6ec commit 3a2d9e0

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/__pycache__
2+
.pytest_cache
23
.venv

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [Python-barcode](https://pypi.org/project/python-barcode/)
2323
- [Pillow](https://pypi.org/project/pillow/)
2424
- [Cerberus](https://docs.python-cerberus.org/)
25+
- [Pytest](https://docs.pytest.org/en/8.0.x/getting-started.html)
2526

2627
## 🔗Useful links:
2728
- [Notion](https://efficient-sloth-d85.notion.site/NLW-14-Expert-9e11ff472de64b08a5f9e277a20c3ecc)
@@ -101,10 +102,29 @@ Our business rules are located in this place. <br>
101102
<summary><b>⏰Day-3</b></summary>
102103
- Implementing Error Handler
103104
- Implementing Validator
105+
- Implementing Unit Test
104106

105107
----
106108
**Cerberus e Validator_raw.py** <br>
107109

108110
Para não criarmos cada validação de entrada manualmente, podemos utilizar o 'Validator' da biblioteca do Cerberus. <br>
109111

112+
----
113+
**Mock_value** <br>
114+
115+
Para evitar a criação desnecessária de barcodes a cada teste, foi instanciada uma cópia fantasma ou espelho, que atenderá as demandas do teste unitário sem necessariamente criar algo, retornando o valor desejado através do 'mock_value', no trecho de código abaixo. <br>
116+
```py
117+
from unittest.mock import patch
118+
from src.drivers.barcode_handler import BarcodeHandler
119+
from .tag_creator_controller import TagCreatorController
120+
121+
@patch.object(BarcodeHandler, 'create_barcode')
122+
def test_create(mock_create_barcode):
123+
mock_value = "image_path"
124+
mock_create_barcode.value = mock_value
125+
tag_creator_controller = TagCreatorController()
126+
127+
result = tag_creator_controller.create(mock_value)
128+
```
129+
110130
</details>

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
astroid==3.0.3
22
blinker==1.7.0
3+
Cerberus==1.3.5
34
cfgv==3.4.0
45
click==8.1.7
56
colorama==0.4.6
@@ -8,16 +9,20 @@ distlib==0.3.8
89
filelock==3.13.1
910
Flask==3.0.2
1011
identify==2.5.33
12+
iniconfig==2.0.0
1113
isort==5.13.2
1214
itsdangerous==2.1.2
1315
Jinja2==3.1.3
1416
MarkupSafe==2.1.5
1517
mccabe==0.7.0
1618
nodeenv==1.8.0
19+
packaging==23.2
1720
pillow==10.2.0
1821
platformdirs==4.2.0
22+
pluggy==1.4.0
1923
pre-commit==3.6.0
2024
pylint==3.0.3
25+
pytest==8.0.0
2126
python-barcode==0.15.1
2227
PyYAML==6.0.1
2328
setuptools==69.0.3
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from unittest.mock import patch
2+
from src.drivers.barcode_handler import BarcodeHandler
3+
from .tag_creator_controller import TagCreatorController
4+
5+
@patch.object(BarcodeHandler, 'create_barcode')
6+
def test_create(mock_create_barcode):
7+
mock_value = "image_path"
8+
mock_create_barcode.value = mock_value
9+
tag_creator_controller = TagCreatorController()
10+
11+
result = tag_creator_controller.create(mock_value)
12+
13+
assert isinstance(result, dict)
14+
assert "data" in result
15+
assert "type" in result["data"]
16+
assert "count" in result["data"]
17+
assert "path" in result["data"]
18+
19+
assert result["data"]["type"] == "Tag Image"
20+
assert result["data"]["count"] == 1
21+
assert result["data"]["type"] == f'{mock_value}.png'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from src.errors.error_types.http_unprocessable_entity import HttpUnprocessableEntityError
2+
from .tag_creator_validator import tag_creator_validator
3+
4+
class MockRequest:
5+
def __init__(self, json) -> None:
6+
self.json = json
7+
8+
def test_tag_creator_validator():
9+
req = MockRequest(json={ "product_code": "12345" })
10+
tag_creator_validator(req)
11+
12+
def test_tag_creator_validator_error():
13+
req = MockRequest(json={ "product_code": 12345 })
14+
15+
try:
16+
tag_creator_validator(req)
17+
assert False
18+
except Exception as exception:
19+
assert isinstance(exception, HttpUnprocessableEntityError)

0 commit comments

Comments
 (0)