-
Notifications
You must be signed in to change notification settings - Fork 66
Ajustes teste unitarios #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,5 @@ var/ | |
.idea/ | ||
.vscode/ | ||
venv | ||
env39 | ||
tests/validation_images/*.png | ||
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,13 +1,19 @@ | ||||||
from typing import Tuple, Union | ||||||
from typing import Tuple, Union, Dict | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eu sugeria eu ordenar os imports para ficar mais organizado:
Suggested change
|
||||||
from src.shared.typings import BBox, Coordinate, GrayImage, Slot | ||||||
from src.utils.core import hashit, locate | ||||||
from .config import arrowsImagesHashes, gameWindowCache, images | ||||||
|
||||||
def get_global_game_cache_window_cache() -> Dict: | ||||||
""" | ||||||
function to ease mocking the cache | ||||||
""" | ||||||
global gameWindowCache | ||||||
return gameWindowCache | ||||||
Comment on lines
+6
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pode explicar o que aconteceu aqui? Evita o snake, estou usando o lowerUpperCase There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. flake8 usa o snake case, criei o metodo pra facilitar o mock, o mock anterior não funcionava |
||||||
|
||||||
# TODO: add unit tests | ||||||
# TODO: add perf | ||||||
def getLeftArrowPosition(screenshot: GrayImage) -> Union[BBox, None]: | ||||||
global gameWindowCache | ||||||
gameWindowCache = get_global_game_cache_window_cache() | ||||||
if gameWindowCache['left']['position'] is not None: | ||||||
leftArrowImage = images['arrows'][gameWindowCache['left']['arrow']] | ||||||
leftArrowImageHash = hashit(leftArrowImage) | ||||||
|
@@ -38,7 +44,7 @@ def getLeftArrowPosition(screenshot: GrayImage) -> Union[BBox, None]: | |||||
# TODO: add unit tests | ||||||
# TODO: add perf | ||||||
def getRightArrowPosition(screenshot: GrayImage) -> Union[BBox, None]: | ||||||
global gameWindowCache | ||||||
gameWindowCache = get_global_game_cache_window_cache() | ||||||
if gameWindowCache['right']['position'] is not None: | ||||||
images['arrows'][gameWindowCache['right']['arrow']] | ||||||
rightArrowImage = images['arrows'][gameWindowCache['right']['arrow']] | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
import pyautogui | ||
import cv2 | ||
import numpy as np | ||
|
||
class PyCamera(): | ||
is_capturing = True | ||
|
||
def get_latest_frame(self, path='screen.png'): | ||
img = pyautogui.screenshot() | ||
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY) | ||
if path: | ||
# import cv2; cv2.imwrite('screen_test.png', screenshot) | ||
cv2.imwrite(path, img) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deveria evitar qualquer escrita em disco uma vez que atrasa muito o processamento. |
||
return img | ||
|
||
Comment on lines
+1
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seria legal criar uma classe com Polimorfismo para decidir qual SO usar e qual lib usar. o DxCam serve para o windows, dxlib serve para linux, tb deve ser para mac, que tal tentar? Performance interessa muito nesse projeto. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pode remover essa linha em branco? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pode, desculpa |
||
import numpy as np | ||
import pathlib | ||
from src.repositories.actionBar.extractors import getCooldownsImage | ||
|
@@ -10,10 +11,8 @@ | |
|
||
|
||
def test_should_return_None_when_cannot_get_left_arrows_position(mocker): | ||
getLeftArrowsPositionSpy = mocker.patch( | ||
'actionBar.locators.getLeftArrowsPosition', return_value=None) | ||
getRightArrowsPositionSpy = mocker.patch( | ||
'actionBar.locators.getRightArrowsPosition', return_value=None) | ||
getLeftArrowsPositionSpy = mocker.patch('src.repositories.actionBar.extractors.getLeftArrowsPosition', return_value=None) | ||
getRightArrowsPositionSpy = mocker.patch('src.repositories.actionBar.extractors.getRightArrowsPosition', return_value=None) | ||
cooldownsImage = getCooldownsImage(screenshotImage) | ||
expectedCooldownsImage = None | ||
assert cooldownsImage is expectedCooldownsImage | ||
|
@@ -23,9 +22,9 @@ def test_should_return_None_when_cannot_get_left_arrows_position(mocker): | |
|
||
def test_should_return_None_when_cannot_get_right_arrows_position(mocker): | ||
getLeftArrowsPositionSpy = mocker.patch( | ||
'actionBar.locators.getLeftArrowsPosition', return_value=(0, 392, 17, 34)) | ||
'src.repositories.actionBar.extractors.getLeftArrowsPosition', return_value=(0, 392, 17, 34)) | ||
getRightArrowsPositionSpy = mocker.patch( | ||
'actionBar.locators.getRightArrowsPosition', return_value=None) | ||
'src.repositories.actionBar.extractors.getRightArrowsPosition', return_value=None) | ||
result = getCooldownsImage(screenshotImage) | ||
assert result == None | ||
getLeftArrowsPositionSpy.assert_called_once_with(screenshotImage) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,8 @@ | |||||||||||||||
from src.repositories.battleList.locators import getContainerBottomBarPosition | ||||||||||||||||
from src.utils.image import loadFromRGBToGray | ||||||||||||||||
|
||||||||||||||||
from tests.utils import result_pos_draw | ||||||||||||||||
|
||||||||||||||||
Comment on lines
2
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
currentPath = pathlib.Path(__file__).parent.resolve() | ||||||||||||||||
|
||||||||||||||||
|
@@ -10,5 +12,8 @@ | |||||||||||||||
def test_should_get_container_bottom_bar_pos(): | ||||||||||||||||
screenshotImage = loadFromRGBToGray(f'{currentPath}/screenshot.png') | ||||||||||||||||
containerBottomBarPos = getContainerBottomBarPosition(screenshotImage) | ||||||||||||||||
expectedContainerBottomBarPos = (1748, 621, 156, 4) | ||||||||||||||||
# save for conference | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove o comentário |
||||||||||||||||
result_pos_draw(screenshotImage, containerBottomBarPos, 'test_should_get_container_bottom_bar_pos.png') | ||||||||||||||||
|
||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove essa linh |
||||||||||||||||
expectedContainerBottomBarPos = (1572, 1036, 156, 4) | ||||||||||||||||
assert containerBottomBarPos == expectedContainerBottomBarPos |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Essa screenshot serve para qual caso? Renomeia para o caso em especifico. |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seria interessante se as imagens fossem sempre em RGB pois o load automaticamente converte para gray. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Essa screenshot serve para qual caso? Renomeia para o caso em especifico. |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Essa screenshot serve para qual caso? Renomeia para o caso em especifico. |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Essa screenshot serve para qual caso? Renomeia para o caso em especifico. |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||||||||||||||||||||||||||||
import pathlib | ||||||||||||||||||||||||||||||
import cv2 | ||||||||||||||||||||||||||||||
import numpy as np | ||||||||||||||||||||||||||||||
import pytest | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
from src.repositories.gameWindow.core import getLeftArrowPosition, getRightArrowPosition | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
from tests.utils import result_pos_draw | ||||||||||||||||||||||||||||||
Comment on lines
+1
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
currentPath = pathlib.Path(__file__).parent.resolve() | ||||||||||||||||||||||||||||||
img01 = f'{currentPath}/screenshot01.png' | ||||||||||||||||||||||||||||||
img02_gray = f'{currentPath}/screenshot02_gray.png' | ||||||||||||||||||||||||||||||
img03 = f'{currentPath}/screenshot03.png' | ||||||||||||||||||||||||||||||
img04 = f'{currentPath}/screenshot04.png' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def rgb_to_gray(img_path: str, is_gray=False): | ||||||||||||||||||||||||||||||
img = cv2.imread(img_path) | ||||||||||||||||||||||||||||||
if is_gray: | ||||||||||||||||||||||||||||||
return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY) | ||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||
return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@pytest.mark.parametrize("img, is_gray", [ | ||||||||||||||||||||||||||||||
(img01, False), | ||||||||||||||||||||||||||||||
(img02_gray, True), | ||||||||||||||||||||||||||||||
(img03, False), | ||||||||||||||||||||||||||||||
(img04, False), | ||||||||||||||||||||||||||||||
]) | ||||||||||||||||||||||||||||||
Comment on lines
+26
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Qual a idéia aqui? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usar diversas imagens como input do teste unitario, sendo alguns 1920X1080 e outras resolução mac os(16:10) |
||||||||||||||||||||||||||||||
def test_getLeftArrowPosition(mocker, img, is_gray): | ||||||||||||||||||||||||||||||
# clean preview test | ||||||||||||||||||||||||||||||
this_gameWindowCache = { | ||||||||||||||||||||||||||||||
'left': {'arrow': None, 'position': None}, | ||||||||||||||||||||||||||||||
'right': {'arrow': None, 'position': None}, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
mocker.patch('src.repositories.gameWindow.core.get_global_game_cache_window_cache', return_value=this_gameWindowCache) | ||||||||||||||||||||||||||||||
# ---------------------------------- | ||||||||||||||||||||||||||||||
img_gray = rgb_to_gray(img, is_gray) | ||||||||||||||||||||||||||||||
result = getLeftArrowPosition(img_gray) | ||||||||||||||||||||||||||||||
assert result is not None | ||||||||||||||||||||||||||||||
assert result == (2, 23, 7, 54) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@pytest.mark.parametrize("img, is_gray", [ | ||||||||||||||||||||||||||||||
(img01, False), | ||||||||||||||||||||||||||||||
(img02_gray, True), | ||||||||||||||||||||||||||||||
(img03, False), | ||||||||||||||||||||||||||||||
]) | ||||||||||||||||||||||||||||||
Comment on lines
+46
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Qual a idéia aqui? |
||||||||||||||||||||||||||||||
def test_getRightArrowPosition(mocker, img, is_gray): | ||||||||||||||||||||||||||||||
# clean preview test | ||||||||||||||||||||||||||||||
this_gameWindowCache = { | ||||||||||||||||||||||||||||||
'left': {'arrow': None, 'position': None}, | ||||||||||||||||||||||||||||||
'right': {'arrow': None, 'position': None}, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
mocker.patch('src.repositories.gameWindow.core.get_global_game_cache_window_cache', return_value=this_gameWindowCache) | ||||||||||||||||||||||||||||||
# ---------------------------------- | ||||||||||||||||||||||||||||||
img_gray = rgb_to_gray(img, is_gray) | ||||||||||||||||||||||||||||||
result = getRightArrowPosition(img_gray) | ||||||||||||||||||||||||||||||
# result == x, y, width, height | ||||||||||||||||||||||||||||||
assert result is not None | ||||||||||||||||||||||||||||||
# img04 should be differrent | ||||||||||||||||||||||||||||||
result_pos_draw(img_gray,result,f"test_getRightArrowPosition__{img[-15:]}") | ||||||||||||||||||||||||||||||
assert result == (1561, 23, 7, 54) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def test_getRightArrowPosition_04(mocker, img=img04, is_gray=False): | ||||||||||||||||||||||||||||||
# clean preview test | ||||||||||||||||||||||||||||||
this_gameWindowCache = { | ||||||||||||||||||||||||||||||
'left': {'arrow': None, 'position': None}, | ||||||||||||||||||||||||||||||
'right': {'arrow': None, 'position': None}, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
mocker.patch('src.repositories.gameWindow.core.get_global_game_cache_window_cache', return_value=this_gameWindowCache) | ||||||||||||||||||||||||||||||
# ---------------------------------- | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
from src.repositories.gameWindow.config import gameWindowCache | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
img_gray = rgb_to_gray(img, is_gray) | ||||||||||||||||||||||||||||||
result = getRightArrowPosition(img_gray) | ||||||||||||||||||||||||||||||
# result == x, y, width, height | ||||||||||||||||||||||||||||||
assert result is not None | ||||||||||||||||||||||||||||||
# img04 should be differrent | ||||||||||||||||||||||||||||||
result_pos_draw(img_gray,result,"test_getRightArrowPosition__04.png") | ||||||||||||||||||||||||||||||
assert result == (1385, 23, 7, 54) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
import cv2 | ||
import pathlib | ||
|
||
currentPath = pathlib.Path(__file__).parent.resolve() | ||
import cv2 | ||
|
||
def result_pos_draw(image, result_position, name, color_bgr = (255, 255, 0), thickness=2): | ||
path = f"{currentPath}/validation_images/{name}" | ||
x, y, width, height = result_position | ||
cv2.rectangle(image, (x, y), (x+width, y+height), color_bgr, thickness) | ||
cv2.imwrite(path, image) | ||
Comment on lines
+1
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pode formatar o file, organizar os imports e usar lowerUpperCase na função? Pode também explicar para o que serve a função? |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Não versiona isso |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Por curiosidade, o que gerou esses folders?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
env39/
pasta do venv que criei dentro da pasta.
Fiz um metodo pra criar imagens desenhando um quadrado na parte selecinada baseado na posição recebida pelos metodos, pra validação visual dos metodos
tests/validation_images/*.png