Skip to content

Commit

Permalink
Add initial ghost point tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leorudczenko committed Aug 22, 2023
1 parent 3fb0fba commit 3a11b65
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
"""
Definitions for test fixtures used within other tests.
"""
from typing import (
Callable,
Any,
)

import pytest
from PyQt5.QtCore import (
Qt,
QEvent,
QPoint,
)
from PyQt5.QtGui import QMouseEvent

from tactool.table_model import TableModel
from tactool.main import TACtool
Expand Down Expand Up @@ -36,3 +47,29 @@ def public_index(model: TableModel):
The index in the list of headers where the public headers end.
"""
return len(model.public_headers)


def create_mock_event(
x: int = 0,
y: int = 0,
) -> QMouseEvent:
"""
Create a QEvent object for tests to simulate mouse/keyboard input from user.
"""
event = QMouseEvent(
QEvent.MouseMove,
QPoint(x, y),
Qt.NoButton,
Qt.MouseButtons(Qt.NoButton),
Qt.KeyboardModifiers(Qt.NoModifier),
)
return event


def create_function_return(value: Any) -> Callable[[Any], Any]:
"""
Return a function which returns the given value.
"""
def new_function(*args, **kwargs) -> Any:
return value
return new_function
27 changes: 27 additions & 0 deletions test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
from tactool.main import TACtool
from tactool.analysis_point import AnalysisPoint
from conftest import create_mock_event, create_function_return


def test_add_and_remove_points(tactool: TACtool, public_index: int):
Expand All @@ -24,6 +25,12 @@ def test_add_and_remove_points(tactool: TACtool, public_index: int):
# The 1st Analysis Point has default settings
tactool.graphics_view.left_click.emit(101, 101)

# Override the function with a new function which always returns True, to avoid genuine mouse detection during test
tactool.graphics_view._image.isUnderMouse = create_function_return(True)
# Ensure the ghost point is not created as it is disabled by default
tactool.graphics_view.mouseMoveEvent(create_mock_event(x=203, y=305))
assert tactool.graphics_view.ghost_point is None

# Adjust the settings for the 2nd Analysis Point
tactool.window.update_point_settings(
sample_name="sample_x83",
Expand All @@ -36,6 +43,14 @@ def test_add_and_remove_points(tactool: TACtool, public_index: int):
)
tactool.graphics_view.left_click.emit(202, 202)

# Enable the ghost point and create it with a mouse movement event
tactool.window.menu_bar_tools_ghost_point.setChecked(True)
tactool.graphics_view.mouseMoveEvent(create_mock_event(x=203, y=305))
# Check that the ghost point inherits the correct settings
assert tactool.graphics_view.ghost_point.aslist()[:public_index] == AnalysisPoint(
3, "Spot", 203, 305, 50, 2.0, "#222222", "sample_x83", "mount_x81", "rock", "", None, None, None
).aslist()[:public_index]

# Adjust the settings for the 3rd Analysis Point
# Purposefully making it overlap the 2nd Analysis Point
tactool.window.update_point_settings(
Expand Down Expand Up @@ -70,6 +85,12 @@ def test_add_and_remove_points(tactool: TACtool, public_index: int):
# Nothing should change
tactool.graphics_view.right_click.emit(0, 0)

# Check that the ghost point uses the newly deleted ID value of 3
tactool.graphics_view.mouseMoveEvent(create_mock_event(x=176, y=301))
assert tactool.graphics_view.ghost_point.aslist()[:public_index] == AnalysisPoint(
3, "RefMark", 176, 301, 50, 2.0, "#333333", "sample_x67", "mount_x15", "duck", "", None, None, None
).aslist()[:public_index]

# Adjust the settings for the 4th Analysis Point to match those of the 2nd Analysis Point
# This is done by emitting a signal from the PyQt Table View of the selected Analysis Point
tactool.table_view.selected_analysis_point.emit(expected_data[1], 0)
Expand All @@ -89,6 +110,12 @@ def test_add_and_remove_points(tactool: TACtool, public_index: int):
expected_ellipse = (expected_data[index].diameter * expected_data[index].scale) + offset
assert analysis_point._outer_ellipse.boundingRect().width() == expected_ellipse

# Check that the ghost point inherits the correct settings
tactool.graphics_view.mouseMoveEvent(create_mock_event(x=82, y=288))
assert tactool.graphics_view.ghost_point.aslist()[:public_index] == AnalysisPoint(
4, "Spot", 82, 288, 50, 2.0, "#222222", "sample_x83", "mount_x81", "rock", "", None, None, None
).aslist()[:public_index]


def test_clear_points(tactool: TACtool):
"""
Expand Down

0 comments on commit 3a11b65

Please sign in to comment.