-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_collectioneditor.py
1048 lines (885 loc) · 38 KB
/
test_collectioneditor.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright © Spyder Project Contributors
#
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
# ----------------------------------------------------------------------------
"""
Tests for the Variable Explorer Collections Editor.
"""
# Standard library imports
import os # Example module for testing display inside CollecitonsEditor
from os import path
import copy
import datetime
from xml.dom.minidom import parseString
from unittest.mock import Mock, patch
# Third party imports
import numpy
import pandas
import pytest
from flaky import flaky
from qtpy.QtCore import Qt, QPoint
from qtpy.QtWidgets import QDateEdit, QMessageBox, QWidget
# Local imports
from spyder.config.manager import CONF
from spyder.widgets.collectionseditor import (
CollectionsEditor, CollectionsEditorTableView, CollectionsEditorWidget,
CollectionsModel, LARGE_NROWS, natsort, RemoteCollectionsEditorTableView,
ROWS_TO_LOAD)
from spyder.plugins.variableexplorer.widgets.tests.test_dataframeeditor import (
generate_pandas_indexes)
from spyder_kernels.utils.nsview import get_size
# =============================================================================
# Constants
# =============================================================================
# Full path to this file's parent directory for loading data
LOCATION = path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
# =============================================================================
# Utility functions and classes
# =============================================================================
def data(cm, i, j):
return cm.data(cm.index(i, j))
def data_table(cm, n_rows, n_cols):
return [[data(cm, i, j) for i in range(n_rows)] for j in range(n_cols)]
class MockParent(QWidget):
def __init__(self):
QWidget.__init__(self)
self.proxy_model = None
# =============================================================================
# Pytest Fixtures
# =============================================================================
@pytest.fixture
def nonsettable_objects_data():
"""Rturn Python objects with immutable attribs to test CollectionEditor."""
test_objs = [pandas.Period("2018-03"), pandas.Categorical([1, 2, 42])]
expected_objs = [pandas.Period("2018-03"), pandas.Categorical([1, 2, 42])]
keys_test = [["_typ", "day", "dayofyear", "hour"],
["_typ", "nbytes", "ndim"]]
return zip(test_objs, expected_objs, keys_test)
# =============================================================================
# Tests
# ============================================================================
def test_rename_variable(qtbot):
"""Test renaming of the correct variable."""
variables = {'a': 1,
'b': 2,
'c': 3,
'd': '4',
'e': 5}
editor = CollectionsEditorTableView(None, variables.copy())
qtbot.addWidget(editor)
editor.setCurrentIndex(editor.model.index(1, 0))
editor.rename_item(new_name='b2')
assert editor.model.rowCount() == 5
assert data(editor.model, 0, 0) == 'a'
assert data(editor.model, 1, 0) == 'b2'
assert data(editor.model, 2, 0) == 'c'
assert data(editor.model, 3, 0) == 'd'
assert data(editor.model, 4, 0) == 'e'
# Reset variables and try renaming one again
new_variables = {'a': 1,
'b': 2,
'b2': 2,
'c': 3,
'd': '4',
'e': 5}
editor.set_data(new_variables.copy())
editor.adjust_columns()
editor.setCurrentIndex(editor.model.index(1, 0))
editor.rename_item(new_name='b3')
assert editor.model.rowCount() == 6
assert data(editor.model, 0, 0) == 'a'
assert data(editor.model, 1, 0) == 'b2'
assert data(editor.model, 2, 0) == 'b3'
assert data(editor.model, 3, 0) == 'c'
assert data(editor.model, 4, 0) == 'd'
assert data(editor.model, 5, 0) == 'e'
def test_remove_variable(qtbot):
"""Test removing of the correct variable."""
variables = {'a': 1,
'b': 2,
'c': 3,
'd': '4',
'e': 5}
editor = CollectionsEditorTableView(None, variables.copy())
qtbot.addWidget(editor)
editor.setCurrentIndex(editor.model.index(1, 0))
editor.remove_item(force=True)
assert editor.model.rowCount() == 4
assert data(editor.model, 0, 0) == 'a'
assert data(editor.model, 1, 0) == 'c'
assert data(editor.model, 2, 0) == 'd'
assert data(editor.model, 3, 0) == 'e'
# Reset variables and try removing one again
editor.set_data(variables.copy())
editor.adjust_columns()
editor.setCurrentIndex(editor.model.index(1, 0))
editor.remove_item(force=True)
assert editor.model.rowCount() == 4
assert data(editor.model, 0, 0) == 'a'
assert data(editor.model, 1, 0) == 'c'
assert data(editor.model, 2, 0) == 'd'
assert data(editor.model, 3, 0) == 'e'
def test_remove_remote_variable(qtbot, monkeypatch):
"""Test the removing of the correct remote variable."""
variables = {'a': {'type': 'int',
'size': 1,
'view': '1',
'python_type': 'int',
'numpy_type': 'Unknown'},
'b': {'type': 'int',
'size': 1,
'view': '2',
'python_type': 'int',
'numpy_type': 'Unknown'},
'c': {'type': 'int',
'size': 1,
'view': '3',
'python_type': 'int',
'numpy_type': 'Unknown'},
'd': {'type': 'str',
'size': 1,
'view': '4',
'python_type': 'int',
'numpy_type': 'Unknown'},
'e': {'type': 'int',
'size': 1,
'view': '5',
'python_type': 'int',
'numpy_type': 'Unknown'}}
editor = RemoteCollectionsEditorTableView(None, variables.copy())
qtbot.addWidget(editor)
editor.setCurrentIndex(editor.model.index(1, 0))
# Monkey patch remove variables
def remove_values(ins, names):
assert names == ['b']
data = {'a': {'type': 'int',
'size': 1,
'view': '1',
'python_type': 'int',
'numpy_type': 'Unknown'},
'c': {'type': 'int',
'size': 1,
'view': '3',
'python_type': 'int',
'numpy_type': 'Unknown'},
'd': {'type': 'str',
'size': 1,
'view': '4',
'python_type': 'int',
'numpy_type': 'Unknown'},
'e': {'type': 'int',
'size': 1,
'view': '5',
'python_type': 'int',
'numpy_type': 'Unknown'}}
editor.set_data(data)
monkeypatch.setattr(
'spyder.widgets'
'.collectionseditor.RemoteCollectionsEditorTableView.remove_values',
remove_values)
editor.remove_item(force=True)
assert editor.model.rowCount() == 4
assert data(editor.model, 0, 0) == 'a'
assert data(editor.model, 1, 0) == 'c'
assert data(editor.model, 2, 0) == 'd'
assert data(editor.model, 3, 0) == 'e'
# Reset variables and try removing one again
editor.set_data(variables.copy())
editor.adjust_columns()
editor.setCurrentIndex(editor.model.index(1, 0))
editor.remove_item(force=True)
assert editor.model.rowCount() == 4
assert data(editor.model, 0, 0) == 'a'
assert data(editor.model, 1, 0) == 'c'
assert data(editor.model, 2, 0) == 'd'
assert data(editor.model, 3, 0) == 'e'
def test_filter_rows(qtbot):
"""Test rows filtering."""
data = (
{'dfa':
{'type': 'DataFrame',
'size': (2, 1),
'view': 'Column names: 0',
'python_type': 'DataFrame',
'numpy_type': 'Unknown'},
'dfb':
{'type': 'DataFrame',
'size': (2, 1),
'view': 'Column names: 0',
'python_type': 'DataFrame',
'numpy_type': 'Unknown'}}
)
editor = RemoteCollectionsEditorTableView(None, data)
qtbot.addWidget(editor)
# Initially two rows
assert editor.model.rowCount() == 2
# Match two rows by name
editor.do_find("df")
assert editor.model.rowCount() == 2
# Match two rows by type
editor.do_find("DataFrame")
assert editor.model.rowCount() == 2
# Only one match
editor.do_find("dfb")
assert editor.model.rowCount() == 1
# No match
editor.do_find("dfbc")
assert editor.model.rowCount() == 0
def test_remote_make_data_function():
"""
Test that the function returned by make_data_function() is the expected
one.
"""
variables = {'a': {'type': 'int',
'size': 1,
'view': '1',
'python_type': 'int',
'numpy_type': 'Unknown'}}
mock_shellwidget = Mock()
editor = RemoteCollectionsEditorTableView(
None, variables, mock_shellwidget
)
index = editor.model.index(0, 0)
data_function = editor.delegate.make_data_function(index)
value = data_function()
mock_shellwidget.get_value.assert_called_once_with('a')
assert value == mock_shellwidget.get_value.return_value
def test_create_dataframeeditor_with_correct_format(qtbot):
df = pandas.DataFrame(['foo', 'bar'])
editor = CollectionsEditorTableView(None, {'df': df})
qtbot.addWidget(editor)
CONF.set('variable_explorer', 'dataframe_format', '10d')
editor.delegate.createEditor(None, None, editor.model.index(0, 3))
dataframe_editor = next(iter(editor.delegate._editors.values()))['editor']
qtbot.addWidget(dataframe_editor)
dataframe_editor.dataModel._format_spec == '10d'
def test_collectionsmodel_with_two_ints():
coll = {'x': 1, 'y': 2}
cm = CollectionsModel(MockParent(), coll)
assert cm.rowCount() == 2
assert cm.columnCount() == 4
# dict is unordered, so first row might be x or y
assert data(cm, 0, 0) in {'x',
'y'}
if data(cm, 0, 0) == 'x':
row_with_x = 0
row_with_y = 1
else:
row_with_x = 1
row_with_y = 0
assert data(cm, row_with_x, 1) == 'int'
assert data(cm, row_with_x, 2) == 1
assert data(cm, row_with_x, 3) == '1'
assert data(cm, row_with_y, 0) == 'y'
assert data(cm, row_with_y, 1) == 'int'
assert data(cm, row_with_y, 2) == 1
assert data(cm, row_with_y, 3) == '2'
def test_collectionsmodel_with_index():
# Regression test for spyder-ide/spyder#3380,
# modified for spyder-ide/spyder#3758.
for rng_name, rng in generate_pandas_indexes().items():
coll = {'rng': rng}
cm = CollectionsModel(MockParent(), coll)
assert data(cm, 0, 0) == 'rng'
assert data(cm, 0, 1) == rng_name
assert data(cm, 0, 2) == '(20,)' or data(cm, 0, 2) == '(20L,)'
try:
assert data(cm, 0, 3) == rng._summary()
except AttributeError:
assert data(cm, 0, 3) == rng.summary()
def test_shows_dataframeeditor_when_editing_index(monkeypatch):
for __, rng in generate_pandas_indexes().items():
MockDataFrameEditor = Mock()
mockDataFrameEditor_instance = MockDataFrameEditor()
attr_to_patch_dfedit = ('spyder.plugins.variableexplorer.widgets.' +
'dataframeeditor.DataFrameEditor')
monkeypatch.setattr(attr_to_patch_dfedit, MockDataFrameEditor)
coll = {'rng': rng}
editor = CollectionsEditorTableView(None, coll)
editor.delegate.createEditor(None, None,
editor.model.index(0, 3))
mockDataFrameEditor_instance.show.assert_called_once_with()
def test_sort_numpy_numeric_collectionsmodel():
var_list = [
numpy.float64(1e16), numpy.float64(10), numpy.float64(1),
numpy.float64(0.1), numpy.float64(1e-6),
numpy.float64(0), numpy.float64(-1e-6), numpy.float64(-1),
numpy.float64(-10), numpy.float64(-1e16)
]
cm = CollectionsModel(MockParent(), var_list)
assert cm.rowCount() == 10
assert cm.columnCount() == 4
cm.sort(0) # sort by index
assert data_table(cm, 10, 4) == [list(range(0, 10)),
[u'float64']*10,
[1]*10,
['1e+16', '10.0', '1.0', '0.1',
'1e-06', '0.0', '-1e-06',
'-1.0', '-10.0', '-1e+16']]
cm.sort(3) # sort by value
assert data_table(cm, 10, 4) == [list(range(9, -1, -1)),
[u'float64']*10,
[1]*10,
['-1e+16', '-10.0', '-1.0',
'-1e-06', '0.0', '1e-06',
'0.1', '1.0', '10.0', '1e+16']]
def test_sort_float_collectionsmodel():
var_list = [
float(1e16), float(10), float(1), float(0.1), float(1e-6),
float(0), float(-1e-6), float(-1), float(-10), float(-1e16)
]
cm = CollectionsModel(MockParent(), var_list)
assert cm.rowCount() == 10
assert cm.columnCount() == 4
cm.sort(0) # sort by index
assert data_table(cm, 10, 4) == [list(range(0, 10)),
[u'float']*10,
[1]*10,
['1e+16', '10.0', '1.0', '0.1',
'1e-06', '0.0', '-1e-06',
'-1.0', '-10.0', '-1e+16']]
cm.sort(3) # sort by value
assert data_table(cm, 10, 4) == [list(range(9, -1, -1)),
[u'float']*10,
[1]*10,
['-1e+16', '-10.0', '-1.0',
'-1e-06', '0.0', '1e-06',
'0.1', '1.0', '10.0', '1e+16']]
def test_sort_collectionsmodel():
var_list1 = [0, 1, 2]
var_list2 = [3, 4, 5, 6]
var_dataframe1 = pandas.DataFrame([[1, 2, 3], [20, 30, 40], [2, 2, 2]])
var_dataframe2 = pandas.DataFrame([[1, 2, 3], [20, 30, 40]])
var_series1 = pandas.Series(var_list1)
var_series2 = pandas.Series(var_list2)
coll = [1, 3, 2]
cm = CollectionsModel(MockParent(), coll)
assert cm.rowCount() == 3
assert cm.columnCount() == 4
cm.sort(0) # sort by index
assert data_table(cm, 3, 4) == [[0, 1, 2],
['int', 'int', 'int'],
[1, 1, 1],
['1', '3', '2']]
cm.sort(3) # sort by value
assert data_table(cm, 3, 4) == [[0, 2, 1],
['int', 'int', 'int'],
[1, 1, 1],
['1', '2', '3']]
coll = [1, var_list1, var_list2, var_dataframe1, var_dataframe2,
var_series1, var_series2]
cm = CollectionsModel(MockParent(), coll)
assert cm.rowCount() == 7
assert cm.columnCount() == 4
cm.sort(1) # sort by type
assert data_table(cm, 7, 4) == [
[3, 4, 5, 6, 0, 1, 2],
['DataFrame', 'DataFrame', 'Series', 'Series', 'int', 'list', 'list'],
['(3, 3)', '(2, 3)', '(3,)', '(4,)', 1, 3, 4],
['Column names: 0, 1, 2',
'Column names: 0, 1, 2',
'Series object of pandas.core.series module',
'Series object of pandas.core.series module',
'1',
'[0, 1, 2]',
'[3, 4, 5, 6]']]
cm.sort(2) # sort by size
assert data_table(cm, 7, 4) == [
[3, 4, 5, 6, 0, 1, 2],
['DataFrame', 'DataFrame', 'Series', 'Series', 'int', 'list', 'list'],
['(2, 3)', '(3,)', '(3, 3)', '(4,)', 1, 3, 4],
['Column names: 0, 1, 2',
'Column names: 0, 1, 2',
'Series object of pandas.core.series module',
'Series object of pandas.core.series module',
'1',
'[0, 1, 2]',
'[3, 4, 5, 6]']] or data_table(cm, 7, 4) == [
[0, 1, 2, 4, 5, 3, 6],
[u'int', u'list', u'list', u'DataFrame', u'Series', u'DataFrame',
u'Series'],
[1, 3, 4, u'(2, 3)', u'(3,)', u'(3, 3)', u'(4,)'],
['1',
'[0, 1, 2]',
'[3, 4, 5, 6]',
'Column names: 0, 1, 2',
'Series object of pandas.core.series module',
'Column names: 0, 1, 2',
'Series object of pandas.core.series module',
]]
def test_sort_and_fetch_collectionsmodel_with_many_rows():
coll = list(range(2*LARGE_NROWS))
cm = CollectionsModel(MockParent(), coll)
assert cm.rowCount() == cm.rows_loaded == ROWS_TO_LOAD
assert cm.columnCount() == 4
cm.sort(1) # This was causing an issue (#5232)
cm.fetchMore()
assert cm.rowCount() == 2 * ROWS_TO_LOAD
for _ in range(3):
cm.fetchMore()
assert cm.rowCount() == len(coll)
def test_rename_and_duplicate_item_in_collection_editor():
collections = {'list': ([1, 2, 3], False, True),
'tuple': ((1, 2, 3), False, False),
'dict': ({'a': 1, 'b': 2}, True, True)}
for coll, rename_enabled, duplicate_enabled in collections.values():
coll_copy = copy.copy(coll)
editor = CollectionsEditorTableView(None, coll)
assert editor.rename_action.isEnabled()
assert editor.duplicate_action.isEnabled()
editor.setCurrentIndex(editor.model.index(0, 0))
editor.refresh_menu()
assert editor.rename_action.isEnabled() == rename_enabled
assert editor.duplicate_action.isEnabled() == duplicate_enabled
if isinstance(coll, list):
editor.duplicate_item()
assert editor.source_model.get_data() == coll_copy + [coll_copy[0]]
def test_collectioneditorwidget_refresh_action_disabled():
"""
Test that the Refresh button is disabled by default.
"""
lst = [1, 2, 3, 4]
widget = CollectionsEditorWidget(None, lst.copy())
assert not widget.refresh_action.isEnabled()
def test_collectioneditor_refresh():
"""
Test that after pressing the refresh button, the value of the editor is
replaced by the return value of the data_function.
"""
old_list = [1, 2, 3, 4]
new_list = [3, 1, 4, 1, 5]
editor = CollectionsEditor(None, data_function=lambda: new_list)
editor.setup(old_list)
assert editor.get_value() == old_list
assert editor.widget.refresh_action.isEnabled()
editor.widget.refresh_action.trigger()
assert editor.get_value() == new_list
@pytest.mark.parametrize('result', [QMessageBox.Yes, QMessageBox.No])
def test_collectioneditor_refresh_after_edit(result):
"""
Test that after changing a value in the collections editor, refreshing the
editor opens a dialog box (which asks for confirmation), and that the
editor is only refreshed if the user clicks Yes.
"""
old_list = [1, 2, 3, 4]
edited_list = [1, 2, 3, 5]
new_list = [3, 1, 4, 1, 5]
editor = CollectionsEditor(None, data_function=lambda: new_list)
editor.setup(old_list)
editor.show()
model = editor.widget.editor.source_model
model.setData(model.index(3, 3), '5')
with patch('spyder.widgets.collectionseditor.QMessageBox.question',
return_value=result) as mock_question:
editor.widget.refresh_action.trigger()
mock_question.assert_called_once()
editor.accept()
if result == QMessageBox.Yes:
assert editor.get_value() == new_list
else:
assert editor.get_value() == edited_list
def test_collectioneditor_refresh_when_variable_deleted(qtbot):
"""
Test that if the variable is deleted and then the editor is refreshed
(resulting in data_function raising a KeyError), a critical dialog box
is displayed and that the editor is closed.
"""
def datafunc():
raise KeyError
lst = [1, 2, 3, 4]
editor = CollectionsEditor(None, data_function=datafunc)
editor.setup(lst)
with patch('spyder.widgets.collectionseditor.QMessageBox'
'.critical') as mock_critical, \
qtbot.waitSignal(editor.rejected, timeout=0):
editor.widget.refresh_action.trigger()
mock_critical.assert_called_once()
def test_collectioneditor_refresh_nested():
"""
Open an editor for a list with a tuple nested inside, and then open another
editor for the nested tuple. Test that refreshing the second editor works.
"""
old_list = [1, 2, 3, (4, 5)]
new_list = [1, 2, 3, (4,)]
editor = CollectionsEditor(None, data_function=lambda: new_list)
editor.setup(old_list)
view = editor.widget.editor
view.edit(view.model.index(3, 3))
nested_editor = list(view.delegate._editors.values())[0]['editor']
assert nested_editor.get_value() == (4, 5)
nested_editor.widget.refresh_action.trigger()
assert nested_editor.get_value() == (4,)
def test_edit_datetime(monkeypatch):
"""
Test datetimes are editable and NaT values are correctly handled.
Regression test for spyder-ide/spyder#13557 and spyder-ide/spyder#8329
"""
variables = [pandas.NaT, datetime.date.today()]
editor_list = CollectionsEditorTableView(None, variables)
# Test that the NaT value cannot be edited on the variable explorer
editor_list_value = editor_list.delegate.createEditor(
None, None, editor_list.model.index(0, 3))
assert editor_list_value is None
# Test that a date can be edited on the variable explorer
editor_list_value = editor_list.delegate.createEditor(
None, None, editor_list.model.index(1, 3))
assert isinstance(editor_list_value, QDateEdit)
def test_edit_mutable_and_immutable_types(monkeypatch):
"""
Test that mutable objs/vals are editable in VarExp; immutable ones aren't.
Regression test for spyder-ide/spyder#5991.
"""
MockQLineEdit = Mock()
attr_to_patch_qlineedit = ('spyder.plugins.variableexplorer.widgets.' +
'collectionsdelegate.QLineEdit')
monkeypatch.setattr(attr_to_patch_qlineedit, MockQLineEdit)
MockTextEditor = Mock()
attr_to_patch_textedit = ('spyder.plugins.variableexplorer.widgets.' +
'collectionsdelegate.TextEditor')
monkeypatch.setattr(attr_to_patch_textedit, MockTextEditor)
MockQDateTimeEdit = Mock()
attr_to_patch_qdatetimeedit = ('spyder.plugins.variableexplorer.widgets.' +
'collectionsdelegate.QDateTimeEdit')
monkeypatch.setattr(attr_to_patch_qdatetimeedit, MockQDateTimeEdit)
MockCollectionsEditor = Mock()
mockCollectionsEditor_instance = MockCollectionsEditor()
attr_to_patch_coledit = ('spyder.widgets.' +
'collectionseditor.CollectionsEditor')
monkeypatch.setattr(attr_to_patch_coledit, MockCollectionsEditor)
list_test = [1, "012345678901234567901234567890123456789012",
datetime.datetime(2017, 12, 24, 7, 9), [1, 2, 3], (2, "eggs")]
tup_test = tuple(list_test)
# Tests for mutable type (list) #
editor_list = CollectionsEditorTableView(None, list_test)
# Directly editable values inside list
editor_list_value = editor_list.delegate.createEditor(
None, None, editor_list.model.index(0, 3))
assert editor_list_value is not None
assert MockQLineEdit.call_count == 1
# Text Editor for long text inside list
editor_list.delegate.createEditor(None, None,
editor_list.model.index(1, 3))
assert MockTextEditor.call_count == 2
assert not MockTextEditor.call_args[1]["readonly"]
# Datetime inside list
editor_list_datetime = editor_list.delegate.createEditor(
None, None, editor_list.model.index(2, 3))
assert editor_list_datetime is not None
assert MockQDateTimeEdit.call_count == 1
# List inside list
editor_list.delegate.createEditor(None, None,
editor_list.model.index(3, 3))
assert mockCollectionsEditor_instance.show.call_count == 1
assert not mockCollectionsEditor_instance.setup.call_args[1]["readonly"]
# Tuple inside list
editor_list.delegate.createEditor(None, None,
editor_list.model.index(4, 3))
assert mockCollectionsEditor_instance.show.call_count == 2
assert mockCollectionsEditor_instance.setup.call_args[1]["readonly"]
# Tests for immutable type (tuple) #
editor_tup = CollectionsEditorTableView(None, tup_test)
# Directly editable values inside tuple
editor_tup_value = editor_tup.delegate.createEditor(
None, None, editor_tup.model.index(0, 3))
assert editor_tup_value is None
assert MockQLineEdit.call_count == 1
# Text Editor for long text inside tuple
editor_tup.delegate.createEditor(None, None,
editor_tup.model.index(1, 3))
assert MockTextEditor.call_count == 4
assert MockTextEditor.call_args[1]["readonly"]
# Datetime inside tuple
editor_tup_datetime = editor_tup.delegate.createEditor(
None, None, editor_tup.model.index(2, 3))
assert editor_tup_datetime is None
assert MockQDateTimeEdit.call_count == 1
# List inside tuple
editor_tup.delegate.createEditor(None, None,
editor_tup.model.index(3, 3))
assert mockCollectionsEditor_instance.show.call_count == 3
assert mockCollectionsEditor_instance.setup.call_args[1]["readonly"]
# Tuple inside tuple
editor_tup.delegate.createEditor(None, None,
editor_tup.model.index(4, 3))
assert mockCollectionsEditor_instance.show.call_count == 4
assert mockCollectionsEditor_instance.setup.call_args[1]["readonly"]
@flaky(max_runs=3)
def test_view_module_in_coledit():
"""
Test that modules don't produce an error when opening in Variable Explorer.
Also check that they are set as readonly. Regression test for
spyder-ide/spyder#6080.
"""
editor = CollectionsEditor()
editor.setup(os, "module_test", readonly=False)
assert editor.widget.editor.readonly
def test_notimplementederror_multiindex():
"""
Test that the NotImplementedError when scrolling a MultiIndex is handled.
Regression test for spyder-ide/spyder#6284.
"""
time_deltas = [pandas.Timedelta(minutes=minute)
for minute in range(5, 35, 5)]
time_delta_multiindex = pandas.MultiIndex.from_product([[0, 1, 2, 3, 4],
time_deltas])
col_model = CollectionsModel(MockParent(), time_delta_multiindex)
assert col_model.rowCount() == col_model.rows_loaded == ROWS_TO_LOAD
assert col_model.columnCount() == 4
col_model.fetchMore()
assert col_model.rowCount() == 2 * ROWS_TO_LOAD
for _ in range(3):
col_model.fetchMore()
assert col_model.rowCount() == 5 * ROWS_TO_LOAD
def test_editor_parent_set(monkeypatch):
"""
Test that editors have their parent set so they close with Spyder.
Regression test for spyder-ide/spyder#5696.
"""
# Mocking and setup
test_parent = QWidget()
MockCollectionsEditor = Mock()
attr_to_patch_coledit = ('spyder.widgets.' +
'collectionseditor.CollectionsEditor')
monkeypatch.setattr(attr_to_patch_coledit, MockCollectionsEditor)
MockArrayEditor = Mock()
attr_to_patch_arredit = ('spyder.plugins.variableexplorer.widgets.' +
'arrayeditor.ArrayEditor')
monkeypatch.setattr(attr_to_patch_arredit, MockArrayEditor)
MockDataFrameEditor = Mock()
attr_to_patch_dfedit = ('spyder.plugins.variableexplorer.widgets.' +
'dataframeeditor.DataFrameEditor')
monkeypatch.setattr(attr_to_patch_dfedit, MockDataFrameEditor)
MockTextEditor = Mock()
attr_to_patch_textedit = ('spyder.plugins.variableexplorer.widgets.' +
'collectionsdelegate.TextEditor')
monkeypatch.setattr(attr_to_patch_textedit, MockTextEditor)
MockObjectExplorer = Mock()
attr_to_patch_objectexplorer = ('spyder.plugins.variableexplorer.widgets.'
+ 'objectexplorer.ObjectExplorer')
monkeypatch.setattr(attr_to_patch_objectexplorer, MockObjectExplorer)
editor_data = [[0, 1, 2, 3, 4],
numpy.array([1.0, 42.0, 1337.0]),
pandas.DataFrame([[1, 2, 3], [20, 30, 40]]),
os,
"012345678901234567890123456789012345678901234567890123456"]
col_editor = CollectionsEditorTableView(test_parent, editor_data)
assert col_editor.parent() is test_parent
for idx, mock_class in enumerate([MockCollectionsEditor,
MockArrayEditor,
MockDataFrameEditor,
MockObjectExplorer,
MockTextEditor]):
col_editor.delegate.createEditor(col_editor.parent(), None,
col_editor.model.index(idx, 3))
assert mock_class.call_count == 1 + (idx // 4)
assert mock_class.call_args[1]["parent"] is test_parent
def test_xml_dom_element_view():
"""
Test that XML DOM ``Element``s are able to be viewied in CollectionsEditor.
Regression test for spyder-ide/spyder#5642.
"""
xml_path = path.join(LOCATION, 'dom_element_test.xml')
with open(xml_path) as xml_file:
xml_data = xml_file.read()
xml_content = parseString(xml_data)
xml_element = xml_content.getElementsByTagName("note")[0]
col_editor = CollectionsEditor(None)
col_editor.setup(xml_element)
col_editor.show()
assert col_editor.get_value()
col_editor.accept()
def test_pandas_dateoffset_view():
"""
Test that pandas ``DateOffset`` objs can be viewied in CollectionsEditor.
Regression test for spyder-ide/spyder#6729.
"""
test_dateoffset = pandas.DateOffset()
col_editor = CollectionsEditor(None)
col_editor.setup(test_dateoffset)
col_editor.show()
assert col_editor.get_value()
col_editor.accept()
def test_set_nonsettable_objects(nonsettable_objects_data):
"""
Test that errors trying to set attributes in ColEdit are handled properly.
Unit regression test for issues spyder-ide/spyder#6727 and
spyder-ide/spyder#6728.
"""
for test_obj, expected_obj, keys in nonsettable_objects_data:
col_model = CollectionsModel(None, test_obj)
col_model.load_all()
indicies = [col_model.get_index_from_key(key) for key in keys]
for idx in indicies:
assert not col_model.set_value(idx, "2")
# Due to numpy's deliberate breakage of __eq__ comparison
assert all([key == "_typ" or
(getattr(col_model.get_data().__obj__, key)
== getattr(expected_obj, key)) for key in keys])
@flaky(max_runs=3)
@pytest.mark.no_xvfb
def test_edit_nonsettable_objects(qtbot, nonsettable_objects_data):
"""
Test that errors trying to edit attributes in ColEdit are handled properly.
Integration regression test for issues spyder-ide/spyder#6727 and
spyder-ide/spyder#6728.
"""
for test_obj, expected_obj, keys in nonsettable_objects_data:
col_editor = CollectionsEditor(None)
col_editor.setup(test_obj)
with qtbot.waitExposed(col_editor):
col_editor.show()
view = col_editor.widget.editor
indicies = [view.source_model.get_index_from_key(key) for key in keys]
for _ in range(3):
qtbot.keyClick(view, Qt.Key_Right)
last_row = -1
rows_to_test = [index.row() for index in indicies]
for row in rows_to_test:
for _ in range(row - last_row - 1):
qtbot.keyClick(view, Qt.Key_Down)
qtbot.keyClick(view, Qt.Key_Space)
qtbot.keyClick(view.focusWidget(), Qt.Key_Backspace)
qtbot.keyClicks(view.focusWidget(), "2")
qtbot.keyClick(view.focusWidget(), Qt.Key_Down)
last_row = row
qtbot.wait(100)
# Due to numpy's deliberate breakage of __eq__ comparison
assert all([key == "_typ" or (getattr(col_editor.get_value(), key)
== getattr(expected_obj, key)) for key in keys])
col_editor.accept()
qtbot.wait(200)
# Same reason as above
assert all([key == "_typ" or (getattr(col_editor.get_value(), key)
== getattr(expected_obj, key)) for key in keys])
if getattr(test_obj, "_typ", None) is None:
keys.remove("_typ")
assert all([getattr(test_obj, key)
== getattr(expected_obj, key) for key in keys])
def test_collectionseditor_with_class_having_buggy_copy(qtbot):
"""
Test that editor for object whose .copy() returns a different type is
readonly; cf. spyder-ide/spyder#6936.
"""
class MyDictWithBuggyCopy(dict):
pass
md = MyDictWithBuggyCopy({1: 2})
editor = CollectionsEditor()
editor.setup(md)
assert editor.widget.editor.readonly
def test_collectionseditor_with_class_having_correct_copy(qtbot):
"""
Test that editor for object whose .copy() returns the same type is not
readonly; cf. spyder-ide/spyder#6936.
"""
class MyDictWithCorrectCopy(dict):
def copy(self):
return MyDictWithCorrectCopy(self)
md = MyDictWithCorrectCopy({1: 2})
editor = CollectionsEditor()
editor.setup(md)
assert not editor.widget.editor.readonly
def test_collectionseditor_when_clicking_on_header_and_large_rows(qtbot):
"""
Test that sorting works when clicking in its header and there's a
large number of rows.
"""
li = [1] * 10000
editor = CollectionsEditor()
editor.setup(li)
editor.show()
# Perform the sorting. It should be done quite quickly because
# there's a very small number of rows in display.
view = editor.widget.editor
header = view.horizontalHeader()
with qtbot.waitSignal(header.sectionClicked, timeout=200):
qtbot.mouseClick(header.viewport(), Qt.LeftButton, pos=QPoint(1, 1))
# Assert data was sorted correctly.
assert data(view.model, 0, 0) == 9999
editor.accept()
def test_dicts_with_mixed_types_as_key(qtbot):
"""
Test that we can show dictionaries with mixed data types as keys.
This is a regression for spyder-ide/spyder#13481.
"""
colors = {1: 'red', 'Y': 'yellow'}
editor = CollectionsEditor()
editor.setup(colors)
assert editor.widget.editor.source_model.keys == [1, 'Y']
def test_dicts_natural_sorting(qtbot):
"""
Test that natural sorting actually does what it should do
"""
import random
numbers = list(range(100))
random.shuffle(numbers)
dictionary = {'test{}'.format(i): None for i in numbers}
data_sorted = sorted(list(dictionary.keys()), key=natsort)
# numbers should be as a human would sort, e.g. test3 before test100
# regular sort would sort test1, test10, test11,..., test2, test20,...
expected = ['test{}'.format(i) for i in list(range(100))]
editor = CollectionsEditor()
editor.setup(dictionary)
editor.widget.editor.source_model.sort(0)
assert data_sorted == expected, 'Function failed'
assert editor.widget.editor.source_model.keys == expected, \
'GUI sorting fail'
def test_dicts_natural_sorting_mixed_types():
"""
Test that natural sorting actually does what it should do.
testing for issue 13733, as mixed types were sorted incorrectly.
Sorting for other columns will be tested as well.
"""
import pandas as pd
dictionary = {'DSeries': pd.Series(dtype=int), 'aStr': 'algName',
'kDict': {2: 'asd', 3: 2}}
# put this here variable, as it might change later to reflect string length
str_size = get_size(dictionary['aStr'])
editor = CollectionsEditor()
editor.setup(dictionary)
cm = editor.widget.editor.source_model
cm.sort(0)
keys = cm.keys
types = cm.types
sizes = cm.sizes