File: test_list_selection.py

package info (click to toggle)
python-apptools 5.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,552 kB
  • sloc: python: 9,868; makefile: 80
file content (65 lines) | stat: -rw-r--r-- 2,249 bytes parent folder | download
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
# (C) Copyright 2005-2025 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import unittest

from apptools._testing.optional_dependencies import (
    numpy,
    requires_numpy,
)

if numpy is not None:
    from apptools.selection.api import ListSelection


@requires_numpy
class TestListSelection(unittest.TestCase):
    def test_list_selection(self):
        all_items = ["a", "b", "c", "d"]
        selected = ["d", "b"]
        list_selection = ListSelection.from_available_items(
            provider_id="foo", selected=selected, all_items=all_items
        )

        self.assertEqual(list_selection.items, selected)
        self.assertEqual(list_selection.indices, [3, 1])

    def test_list_selection_of_sequence_items(self):
        all_items = [["a", "b"], ["c", "d"], ["e", "f"]]
        selected = [all_items[2], all_items[1]]
        list_selection = ListSelection.from_available_items(
            provider_id="foo", selected=selected, all_items=all_items
        )

        self.assertEqual(list_selection.items, selected)
        self.assertEqual(list_selection.indices, [2, 1])

    def test_list_selection_of_numpy_array_items(self):
        data = numpy.arange(10)
        all_items = [data, data + 10, data + 30]
        selected = [all_items[0], all_items[2]]

        list_selection = ListSelection.from_available_items(
            provider_id="foo", selected=selected, all_items=all_items
        )

        self.assertEqual(list_selection.items, selected)
        self.assertEqual(list_selection.indices, [0, 2])

    def test_list_selection_with_invalid_selected_items(self):
        data = numpy.arange(10)
        all_items = [data, data + 10, data + 30]
        selected = [
            data - 10,
        ]

        with self.assertRaises(ValueError):
            ListSelection.from_available_items(
                provider_id="foo", selected=selected, all_items=all_items
            )