blob: 3d76bc465ef0ecfc54b24d8eff557c11595f6df0 [file] [log] [blame]
Philipp Le6203e732022-04-26 21:50:26 +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 BaseModel
10
11from dcs.config.store import Store, ConfigObject
12
13
14class ExampleSub(BaseModel):
15 id: int
16 title: str
17
18
19class ExampleConfig(ConfigObject):
20 _KEY = 'example'
21 id: int
22 name: str
23 sub: List[ExampleSub]
24
25
26class 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())