Skip to content

Commit

Permalink
TST: add preliminary tests for QDataclassBridge, DataWidget, RootTree
Browse files Browse the repository at this point in the history
  • Loading branch information
tangkong committed Jun 26, 2024
1 parent f594a78 commit 747b643
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
12 changes: 12 additions & 0 deletions superscore/tests/test_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Largely smoke tests for various pages"""

from pytestqt.qtbot import QtBot

from superscore.model import Collection
from superscore.widgets.page.entry import CollectionPage


def test_collection_page(qtbot: QtBot):
data = Collection()
page = CollectionPage(data=data)
qtbot.addWidget(page)
84 changes: 84 additions & 0 deletions superscore/tests/test_qt_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Union

import pytest
from pytestqt.qtbot import QtBot

from superscore.qt_helpers import (QDataclassBridge, QDataclassList,
QDataclassValue)


@pytest.fixture(scope='function')
def bridge() -> QDataclassBridge:

@dataclass
class MyDataclass:
int_field: int = 2
bool_field: bool = True
str_field: str = 'stringy'

list_field: List[str] = field(default_factory=list)
dict_field: Dict[str, Any] = field(default_factory=dict)
optional_field: Optional[int] = None

union_field: Union[str, int] = 'two'

return QDataclassBridge(MyDataclass())


def test_bridge_creation(bridge: QDataclassBridge):
# check correct bridge types were generated
assert isinstance(bridge.int_field, QDataclassValue)
assert 'int' in str(type(bridge.int_field))

assert isinstance(bridge.bool_field, QDataclassValue)
assert 'bool' in str(type(bridge.bool_field))

assert isinstance(bridge.str_field, QDataclassValue)
assert 'str' in str(type(bridge.str_field))

assert isinstance(bridge.list_field, QDataclassList)
assert 'str' in str(type(bridge.list_field))

assert isinstance(bridge.dict_field, QDataclassValue)
assert 'object' in str(type(bridge.dict_field))

assert isinstance(bridge.optional_field, QDataclassValue)
assert 'object' in str(type(bridge.optional_field))

assert isinstance(bridge.union_field, QDataclassValue)
assert 'object' in str(type(bridge.union_field))


def test_value_signals(qtbot: QtBot, bridge: QDataclassBridge):
val = bridge.int_field

assert val.get() == 2
with qtbot.waitSignals([val.changed_value, val.updated]):
val.put(3)

assert val.get() == 3


def test_list_signals(qtbot: QtBot, bridge: QDataclassBridge):
val = bridge.list_field

assert val.get() == []

with qtbot.waitSignals([val.added_value, val.added_index, val.updated]):
val.append('one')
assert val.get() == ['one']
val.append('end')
assert val.get() == ['one', 'end']

with qtbot.waitSignals([val.changed_value, val.changed_index, val.updated]):
val.put_to_index(0, 'zero')
assert val.get() == ['zero', 'end']

with qtbot.waitSignals([val.removed_value, val.removed_index, val.updated]):
val.remove_index(0)
assert val.get() == ['end']

with qtbot.waitSignals([val.removed_value, val.removed_index, val.updated]):
val.remove_value('end')
assert val.get() == []
49 changes: 49 additions & 0 deletions superscore/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from operator import attrgetter
from typing import Any
from uuid import uuid4

import pytest
from pytestqt.qtbot import QtBot

from superscore.model import Collection, Root
from superscore.widgets.core import DataWidget
from superscore.widgets.tree import RootTree


@pytest.mark.parametrize(
'attr, signal, value',
[
('title', 'changed_value', 'new_title'),
('title', 'updated', 'new_title'),
('children', 'updated', [Collection(), Collection()]),
('uuid', 'changed_value', uuid4()),
]
)
def test_collection_datawidget_bridge(
qtbot: QtBot,
attr: str,
signal: str,
value: Any
):
data = Collection()
widget1 = DataWidget(data=data)
widget2 = DataWidget(data=data)

assert getattr(data, attr) != value

signal = attrgetter('.'.join((attr, signal)))(widget2.bridge)
with qtbot.waitSignal(signal):
getattr(widget1.bridge, attr).put(value)

assert getattr(data, attr) == value

qtbot.addWidget(widget1)
qtbot.addWidget(widget2)


def test_roottree_setup(sample_database: Root):
tree_model = RootTree(base_entry=sample_database)
root_index = tree_model.index_from_item(tree_model.root_item)
# Check that the entire tree was created
assert tree_model.rowCount(root_index) == 4
assert tree_model.root_item.child(3).childCount() == 3

0 comments on commit 747b643

Please sign in to comment.