Skip to content

Commit

Permalink
Add tests for datetime connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Jan 12, 2024
1 parent 0688ab1 commit c6c7caa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
2 changes: 0 additions & 2 deletions echo/qt/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ def __init__(self, instance, prop, widget):

def update_prop(self):
qdatetime = self._widget.dateTime().toUTC()
qdatetime = qdatetime.toUTC()
value = np.datetime64(qdatetime.toPython())
setattr(self._instance, self._prop, value)

Expand Down Expand Up @@ -630,4 +629,3 @@ def disconnect(self):
self._widget.dateTimeChanged.disconnect(self.update_prop)
self._widget.dateChanged.disconnect(self.update_prop)
self._widget.timeChanged.disconnect(self.update_prop)

Check warning on line 631 in echo/qt/connect.py

View check run for this annotation

Codecov / codecov/patch

echo/qt/connect.py#L628-L631

Added lines #L628 - L631 were not covered by tests

19 changes: 19 additions & 0 deletions echo/qt/tests/test_autoconnect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from datetime import datetime

from numpy import datetime64
from qtpy import QtWidgets, QtGui
from qtpy.QtCore import QDateTime, Qt

from echo.qt.autoconnect import autoconnect_callbacks_to_qt
from echo import CallbackProperty
Expand Down Expand Up @@ -47,13 +51,18 @@ def __init__(self, parent=None):
self.bool_log.setCheckable(True)
self.layout.addWidget(self.bool_log)

self.datetime_dob = QtWidgets.QDateTimeEdit(objectName="datetime_dob")
self.datetime_dob.setTimeSpec(Qt.TimeSpec.UTC)
self.layout.addWidget(self.datetime_dob)

class Person(object):
planet = CallbackProperty()
dataset = CallbackProperty()
name = CallbackProperty()
age = CallbackProperty()
height = CallbackProperty()
log = CallbackProperty()
dob = CallbackProperty()

widget = CustomWidget()

Expand Down Expand Up @@ -86,6 +95,11 @@ class Person(object):
widget.bool_log.setChecked(True)
assert person.log

dob = datetime(2000, 1, 1, 11, 52, 6)
qdob = QDateTime(dob.date(), dob.time(), Qt.TimeSpec.UTC)
widget.datetime_dob.setDateTime(qdob)
assert person.dob.item() == dob

# Check that modifying the callback properties updates the Qt widget

person.planet = 'mars'
Expand All @@ -106,6 +120,11 @@ class Person(object):
person.log = False
assert not widget.bool_log.isChecked()

dob = datetime(2010, 2, 3, 16, 22, 11)
person.dob = datetime64(dob)
qdatetime = widget.datetime_dob.dateTime().toUTC()
assert qdatetime.toPython() == person.dob.item()


def test_autoconnect_with_empty_qt_item():

Expand Down
29 changes: 28 additions & 1 deletion echo/qt/tests/test_connect.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from datetime import datetime

import pytest
from numpy import datetime64
from unittest.mock import MagicMock

from qtpy import QtWidgets
from qtpy.QtCore import QDateTime, Qt

from echo import CallbackProperty
from echo.qt.connect import (connect_checkable_button, connect_text,
from echo.qt.connect import (connect_checkable_button, connect_datetime, connect_text,
connect_combo_data, connect_combo_text,
connect_float_text, connect_value, connect_button,
UserDataWrapper)
Expand Down Expand Up @@ -211,3 +215,26 @@ class Example(object):
assert e.a.call_count == 0
button.clicked.emit()
assert e.a.call_count == 1


def test_connect_datetime():

class Example(object):
t = CallbackProperty()

e = Example()

widget = QtWidgets.QDateTimeEdit()
widget.setTimeSpec(Qt.TimeSpec.UTC)

conn = connect_datetime(e, 't', widget) # noqa

dt = datetime(2010, 5, 7, 13, 15, 3)
qdt = QDateTime(dt.date(), dt.time(), Qt.TimeSpec.UTC)
widget.setDateTime(qdt)
widget.dateTimeChanged.emit(qdt)
assert dt == e.t.item()

dt = datetime(2020, 3, 4, 7, 48, 16)
e.t = datetime64(dt)
assert widget.dateTime().toUTC().toPython() == dt

0 comments on commit c6c7caa

Please sign in to comment.