| Philipp Le | 6203e73 | 2022-04-26 21:50:26 +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 BaseModel |
| 10 | |
| 11 | from dcs.config.store import Store, ConfigObject |
| 12 | |
| 13 | |
| 14 | class ExampleSub(BaseModel): |
| 15 | id: int |
| 16 | title: str |
| 17 | |
| 18 | |
| 19 | class ExampleConfig(ConfigObject): |
| 20 | _KEY = 'example' |
| 21 | id: int |
| 22 | name: str |
| 23 | sub: List[ExampleSub] |
| 24 | |
| 25 | |
| 26 | class StoreTest(unittest.TestCase): |
| 27 | def test_deserialize(self): |
| 28 | in_data = { |
| 29 | 'example': { |
| 30 | 'id': 1, |
| 31 | 'name': 'Test1', |
| 32 | 'sub': [ |
| 33 | { |
| 34 | 'id': 0, |
| 35 | 'title': 'Title1', |
| 36 | }, |
| 37 | { |
| 38 | 'id': 1, |
| 39 | 'title': 'Title2', |
| 40 | }, |
| 41 | ] |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | loaded_store = Store.load_obj(in_data, 'default') |
| 46 | |
| 47 | store = Store.get_instance('default') |
| 48 | self.assertEqual(id(loaded_store), id(store)) |
| 49 | |
| 50 | cfg = store.get_config(ExampleConfig) |
| 51 | self.assertEqual(ExampleConfig, type(cfg)) |
| 52 | self.assertEqual(2, len(cfg.sub)) |
| 53 | self.assertEqual(ExampleSub, type(cfg.sub[0])) |
| 54 | self.assertEqual(ExampleSub, type(cfg.sub[1])) |
| 55 | |
| 56 | sub0 = cfg.sub[0] |
| 57 | self.assertEqual(id(cfg.sub[0]), id(sub0)) |
| 58 | |
| 59 | self.assertEqual(id(cfg), id(store.get_config(ExampleConfig))) |
| 60 | |
| 61 | self.assertEqual(in_data, store.dump_obj()) |