|
1 | 1 | import tempfile |
2 | 2 | import unittest as ut |
| 3 | +from _ctypes import COMError |
3 | 4 | from pathlib import Path |
4 | 5 |
|
5 | | -from comtypes import GUID, CoCreateInstance, IPersist, persist |
| 6 | +from comtypes import GUID, CoCreateInstance, IPersist, hresult, persist |
| 7 | +from comtypes.automation import VARIANT |
6 | 8 |
|
7 | 9 | CLSID_ShellLink = GUID("{00021401-0000-0000-C000-000000000046}") |
8 | 10 |
|
@@ -36,3 +38,34 @@ def test_save(self): |
36 | 38 | pf.Save(str(tgt_file), True) |
37 | 39 | self.assertEqual(pf.GetCurFile(), str(tgt_file)) |
38 | 40 | self.assertTrue(tgt_file.exists()) |
| 41 | + |
| 42 | + |
| 43 | +class Test_DictPropertyBag(ut.TestCase): |
| 44 | + def create_itf_ptr(self) -> persist.IPropertyBag: |
| 45 | + # Create a DictPropertyBag instance with some initial values |
| 46 | + impl = persist.DictPropertyBag(Key1="value1", Key2=123, Key3=True) |
| 47 | + # Get the IPropertyBag interface pointer |
| 48 | + itf = impl.QueryInterface(persist.IPropertyBag) |
| 49 | + return itf |
| 50 | + |
| 51 | + def test_read_existing_properties(self): |
| 52 | + itf = self.create_itf_ptr() |
| 53 | + self.assertEqual(itf.Read("Key1", VARIANT(), None), "value1") |
| 54 | + self.assertEqual(itf.Read("Key2", VARIANT(), None), 123) |
| 55 | + self.assertEqual(itf.Read("Key3", VARIANT(), None), True) |
| 56 | + |
| 57 | + def test_write_new_property(self): |
| 58 | + itf = self.create_itf_ptr() |
| 59 | + itf.Write("Key4", "new_value") |
| 60 | + self.assertEqual(itf.Read("Key4", VARIANT(), None), "new_value") |
| 61 | + |
| 62 | + def test_update_existing_property(self): |
| 63 | + itf = self.create_itf_ptr() |
| 64 | + itf.Write("Key1", "updated_value") |
| 65 | + self.assertEqual(itf.Read("Key1", VARIANT(), None), "updated_value") |
| 66 | + |
| 67 | + def test_read_non_existent_property(self): |
| 68 | + itf = self.create_itf_ptr() |
| 69 | + with self.assertRaises(COMError) as cm: |
| 70 | + itf.Read("NonExistentProp", VARIANT(), None) |
| 71 | + self.assertEqual(cm.exception.hresult, hresult.E_INVALIDARG) |
0 commit comments