1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
import pytest
import pyqtgraph as pg
pg.mkQApp()
def test_SpinBox_defaults():
sb = pg.SpinBox()
assert sb.opts['decimals'] == 6
assert sb.opts['int'] is False
englishLocale = pg.QtCore.QLocale(pg.QtCore.QLocale.Language.English)
germanLocale = pg.QtCore.QLocale(pg.QtCore.QLocale.Language.German, pg.QtCore.QLocale.Country.Germany)
@pytest.mark.parametrize("value,expected_text,opts", [
(0, '0', dict(suffix='', siPrefix=False, dec=False, int=False)),
(100, '100', dict()),
(1000000, '1e+06', dict()),
(1000, '1e+03', dict(decimals=2)),
(1000000, '1000000 V', dict(int=True, suffix='V')),
(12345678955, '12345678955', dict(int=True, decimals=100)),
(1.45e-9, '1.45e-09 A', dict(int=False, decimals=6, suffix='A', siPrefix=False)),
(1.45e-9, '1.45 nA', dict(int=False, decimals=6, suffix='A', siPrefix=True)),
(1.45, '1.45 PSI', dict(int=False, decimals=6, suffix='PSI', siPrefix=True)),
(1.45e-3, '1.45 mPSI', dict(int=False, decimals=6, suffix='PSI', siPrefix=True)),
(-2500.3427, '$-2500.34', dict(int=False, format='${value:0.02f}')),
(1000, '1 k', dict(siPrefix=True, suffix="")),
(1.45e-9, 'i = 1.45e-09 A', dict(int=False, decimals=6, suffix='A', siPrefix=False, prefix='i =')),
(0, '0 mV', dict(suffix='V', siPrefix=True, scaleAtZero=1e-3)),
(0, '0 mV', dict(suffix='V', siPrefix=True, minStep=5e-6, scaleAtZero=1e-3)),
(0, '0 mV', dict(suffix='V', siPrefix=True, step=1e-3)),
(0, '0 mV', dict(suffix='V', dec=True, siPrefix=True, minStep=15e-3)),
(123456.789, '123457', dict(int=False)),#No group separator expected
])
def test_SpinBox_formatting(value, expected_text, opts):
if 'e' in expected_text:
expect_failure_on_buggy_qt()
sb = pg.SpinBox(**opts)
sb.setLocale(englishLocale)
sb.setValue(value)
assert sb.value() == value
assert sb.text() == expected_text
@pytest.mark.parametrize("value,expected_text,opts", [
(0, '0', dict(suffix='', siPrefix=False, dec=False, int=False)),
(100, '100', dict()),
(1000000, '1e+06', dict()),
(1000, '1e+03', dict(decimals=2)),
(1000000, '1000000 V', dict(int=True, suffix='V')),
(12345678955, '12345678955', dict(int=True, decimals=100)),
(1.45e-9, '1,45e-09 A', dict(int=False, decimals=6, suffix='A', siPrefix=False)),
(1.45e-9, '1,45 nA', dict(int=False, decimals=6, suffix='A', siPrefix=True)),
(1.45, '1,45 PSI', dict(int=False, decimals=6, suffix='PSI', siPrefix=True)),
(1.45e-3, '1,45 mPSI', dict(int=False, decimals=6, suffix='PSI', siPrefix=True)),
(-2500.3427, '$-2500.34', dict(int=False, format='${value:0.02f}')),#format specifier provided, so decimal separator unaffected by locale
(1000, '1 k', dict(siPrefix=True, suffix="")),
(1.45e-9, 'i = 1,45e-09 A', dict(int=False, decimals=6, suffix='A', siPrefix=False, prefix='i =')),
(0, '0 mV', dict(suffix='V', siPrefix=True, scaleAtZero=1e-3)),
(0, '0 mV', dict(suffix='V', siPrefix=True, minStep=5e-6, scaleAtZero=1e-3)),
(0, '0 mV', dict(suffix='V', siPrefix=True, step=1e-3)),
(0, '0 mV', dict(suffix='V', dec=True, siPrefix=True, minStep=15e-3)),
(123456.789, '123457', dict(int=False)),#No group separator expected
])
def test_SpinBox_formatting_with_comma_decimal_separator(value, expected_text, opts):
if 'e' in expected_text:
expect_failure_on_buggy_qt()
sb = pg.SpinBox(**opts)
sb.setLocale(germanLocale)
sb.setValue(value)
assert sb.value() == value
assert sb.text() == expected_text
def test_evalFunc():
sb = pg.SpinBox(evalFunc=lambda s: 100)
sb.lineEdit().setText('3')
sb.editingFinishedEvent()
assert sb.value() == 100
sb.lineEdit().setText('0')
sb.editingFinishedEvent()
assert sb.value() == 100
def spinBox_gui_set_value_test(expected, valueText, suffix, locale):
sb = pg.SpinBox(suffix=suffix, locale=locale)
sb.lineEdit().setText(f'{valueText}{suffix}')
sb.editingFinishedEvent()
assert sb.value() == expected
@pytest.mark.parametrize("expected,valueText,suffix", [(0.1, "0.1", ""), (0.1e-3, "0.1 m", "V"), (0, "0,325", "A")])
def test_SpinBox_gui_set_value_english(expected, valueText, suffix):
spinBox_gui_set_value_test(expected, valueText, suffix, locale=englishLocale)
@pytest.mark.parametrize("expected,valueText,suffix", [(0.1, "0,1", ""), (0.1e-3, "0,1 m", "V"), (0, "0.325", "A")])
def test_SpinBox_gui_set_value_german(expected, valueText, suffix):
spinBox_gui_set_value_test(expected, valueText, suffix, locale=germanLocale)
def compare_semantic_versions(v1, v2):
try:
parts1 = [int(p) for p in v1.split('.')]
parts2 = [int(p) for p in v2.split('.')]
for p1, p2 in zip(parts1, parts2):
if p1 < p2:
return -1
elif p1 > p2:
return 1
return 0
except ValueError:
return 0
def expect_failure_on_buggy_qt():
if compare_semantic_versions(pg.Qt.QtVersion, '6.9.0') < 0 \
and compare_semantic_versions(pg.Qt.QtVersion, '6.0.0') >= 0:
pytest.xfail("A known bug in Qt 6.0.0 - 6.8.x causes scientific notation with 'g' format to use capital 'E' for the exponent.")
|