File: test_list_selection.py

package info (click to toggle)
python-apptools 4.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,652 kB
  • sloc: python: 16,657; makefile: 77
file content (50 lines) | stat: -rw-r--r-- 1,760 bytes parent folder | download | duplicates (2)
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
import numpy

from traits.testing.unittest_tools import unittest

from apptools.selection.api import ListSelection


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.array(range(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.array(range(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)


if __name__ == '__main__':
    unittest.main()