blob: 688f3caa99b4441ca97b872f173712bc0c060be2 [file] [log] [blame]
Philipp Le4071d912022-04-26 22:02:43 +02001# SPDX-License-Identifier: MPL-2.0
2# Copyright (c) 2022 Philipp Le <philipp@philipple.de>.
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7import unittest
8from typing import List
9from pydantic import conint, confloat
10from tkinter import ttk
11from enum import IntEnum
12
13from dcs.config.store import Store, ConfigObject, ConfigControlFrame
14from dcs.config.ui import ui_create
15
16
17class ExampleSubFrame(ConfigControlFrame):
18 pass
19
20
21@ui_create
22class ExampleSub(ConfigObject):
23 id: conint(ge=0, lt=10000) = 1
24 title: str = 'test'
25
26 def make_config_widget(self, parent: ttk.Widget) -> ConfigControlFrame:
27 frm = ExampleSubFrame(parent)
28 return frm
29
30
31
32class ExampleIntEnum(IntEnum):
33 Option1 = 1
34 Option2 = 2
35
36
37@ui_create
38class ExampleConfig(ConfigObject):
39 _KEY = 'example'
40 id: conint(ge=0, lt=10) = 1
41 name: str = 'Hello'
42 sub: List[ExampleSub] = [
43 ExampleSub(id=1, title='item 1'),
44 ExampleSub(id=2, title='item 2'),
45 ]
46 int_option: ExampleIntEnum = ExampleIntEnum.Option2
47 flt: confloat(ge=0.0, lt=10.0, multiple_of=0.01) = 0.12
48
49
50class UITest(unittest.TestCase):
51 def test_deserialize(self):
52 in_data = {
53 'example': {
54 'id': 0,
55 'name': 'Test1',
56 'sub': [
57 {
58 'id': 0,
59 'title': 'Title1',
60 },
61 {
62 'id': 1,
63 'title': 'Title2',
64 },
65 ]
66 }
67 }
68
69 loaded_store = Store.load_obj(in_data, 'default')
70
71 store = Store.get_instance('default')
72 self.assertEqual(id(loaded_store), id(store))
73
74 cfg: ExampleConfig = store.get_config(ExampleConfig)
75 self.assertIn('ui_create_id', dir(cfg))