| Philipp Le | 4071d91 | 2022-04-26 22:02:43 +0200 | [diff] [blame^] | 1 | # 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 | |
| 7 | import unittest |
| 8 | from typing import List |
| 9 | from pydantic import conint, confloat |
| 10 | from tkinter import ttk |
| 11 | from enum import IntEnum |
| 12 | |
| 13 | from dcs.config.store import Store, ConfigObject, ConfigControlFrame |
| 14 | from dcs.config.ui import ui_create |
| 15 | |
| 16 | |
| 17 | class ExampleSubFrame(ConfigControlFrame): |
| 18 | pass |
| 19 | |
| 20 | |
| 21 | @ui_create |
| 22 | class 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 | |
| 32 | class ExampleIntEnum(IntEnum): |
| 33 | Option1 = 1 |
| 34 | Option2 = 2 |
| 35 | |
| 36 | |
| 37 | @ui_create |
| 38 | class 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 | |
| 50 | class 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)) |