CfgSectionTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. namespace Apq.Cfg.Tests;
  2. /// <summary>
  3. /// ICfgSection 配置节测试
  4. /// </summary>
  5. public class CfgSectionTests : IDisposable
  6. {
  7. private readonly string _testDir;
  8. public CfgSectionTests()
  9. {
  10. _testDir = Path.Combine(Path.GetTempPath(), $"ApqCfgTests_{Guid.NewGuid():N}");
  11. Directory.CreateDirectory(_testDir);
  12. }
  13. public void Dispose()
  14. {
  15. if (Directory.Exists(_testDir))
  16. {
  17. Directory.Delete(_testDir, true);
  18. }
  19. }
  20. #region GetSection 基本功能
  21. [Fact]
  22. public void GetSection_SimpleSection_ReturnsCorrectValues()
  23. {
  24. // Arrange
  25. var jsonPath = Path.Combine(_testDir, "config.json");
  26. File.WriteAllText(jsonPath, """
  27. {
  28. "Database": {
  29. "Host": "localhost",
  30. "Port": 5432,
  31. "Name": "testdb"
  32. }
  33. }
  34. """);
  35. using var cfg = new CfgBuilder()
  36. .AddJson(jsonPath, level: 0, writeable: false)
  37. .Build();
  38. // Act
  39. var dbSection = cfg.GetSection("Database");
  40. // Assert
  41. Assert.Equal("Database", dbSection.Path);
  42. Assert.Equal("localhost", dbSection.Get("Host"));
  43. Assert.Equal("5432", dbSection.Get("Port"));
  44. Assert.Equal("testdb", dbSection.Get("Name"));
  45. }
  46. [Fact]
  47. public void GetSection_TypedGet_ReturnsCorrectType()
  48. {
  49. // Arrange
  50. var jsonPath = Path.Combine(_testDir, "config.json");
  51. File.WriteAllText(jsonPath, """
  52. {
  53. "Settings": {
  54. "MaxRetries": 3,
  55. "Enabled": true,
  56. "Timeout": 30.5
  57. }
  58. }
  59. """);
  60. using var cfg = new CfgBuilder()
  61. .AddJson(jsonPath, level: 0, writeable: false)
  62. .Build();
  63. // Act
  64. var section = cfg.GetSection("Settings");
  65. // Assert
  66. Assert.Equal(3, section.Get<int>("MaxRetries"));
  67. Assert.True(section.Get<bool>("Enabled"));
  68. Assert.Equal(30.5, section.Get<double>("Timeout"));
  69. }
  70. [Fact]
  71. public void GetSection_NestedSection_ReturnsCorrectValues()
  72. {
  73. // Arrange
  74. var jsonPath = Path.Combine(_testDir, "config.json");
  75. File.WriteAllText(jsonPath, """
  76. {
  77. "Database": {
  78. "Connection": {
  79. "Host": "localhost",
  80. "Port": 5432
  81. },
  82. "Pool": {
  83. "MinSize": 5,
  84. "MaxSize": 100
  85. }
  86. }
  87. }
  88. """);
  89. using var cfg = new CfgBuilder()
  90. .AddJson(jsonPath, level: 0, writeable: false)
  91. .Build();
  92. // Act
  93. var dbSection = cfg.GetSection("Database");
  94. var connSection = dbSection.GetSection("Connection");
  95. var poolSection = dbSection.GetSection("Pool");
  96. // Assert
  97. Assert.Equal("Database:Connection", connSection.Path);
  98. Assert.Equal("localhost", connSection.Get("Host"));
  99. Assert.Equal(5432, connSection.Get<int>("Port"));
  100. Assert.Equal("Database:Pool", poolSection.Path);
  101. Assert.Equal(5, poolSection.Get<int>("MinSize"));
  102. Assert.Equal(100, poolSection.Get<int>("MaxSize"));
  103. }
  104. [Fact]
  105. public void GetSection_Exists_ReturnsCorrectResult()
  106. {
  107. // Arrange
  108. var jsonPath = Path.Combine(_testDir, "config.json");
  109. File.WriteAllText(jsonPath, """
  110. {
  111. "App": {
  112. "Name": "TestApp",
  113. "Version": "1.0.0"
  114. }
  115. }
  116. """);
  117. using var cfg = new CfgBuilder()
  118. .AddJson(jsonPath, level: 0, writeable: false)
  119. .Build();
  120. // Act
  121. var section = cfg.GetSection("App");
  122. // Assert
  123. Assert.True(section.Exists("Name"));
  124. Assert.True(section.Exists("Version"));
  125. Assert.False(section.Exists("NonExistent"));
  126. }
  127. #endregion
  128. #region GetSection 写入功能
  129. [Fact]
  130. public async Task GetSection_Set_UpdatesValue()
  131. {
  132. // Arrange
  133. var jsonPath = Path.Combine(_testDir, "config.json");
  134. File.WriteAllText(jsonPath, """
  135. {
  136. "App": {
  137. "Name": "OldName"
  138. }
  139. }
  140. """);
  141. using var cfg = new CfgBuilder()
  142. .AddJson(jsonPath, level: 0, writeable: true, isPrimaryWriter: true)
  143. .Build();
  144. // Act
  145. var section = cfg.GetSection("App");
  146. section.Set("Name", "NewName");
  147. await cfg.SaveAsync();
  148. // Assert
  149. Assert.Equal("NewName", section.Get("Name"));
  150. Assert.Equal("NewName", cfg.Get("App:Name"));
  151. }
  152. [Fact]
  153. public async Task GetSection_Remove_RemovesKey()
  154. {
  155. // Arrange
  156. var jsonPath = Path.Combine(_testDir, "config.json");
  157. File.WriteAllText(jsonPath, """
  158. {
  159. "App": {
  160. "Name": "TestApp",
  161. "ToRemove": "RemoveMe"
  162. }
  163. }
  164. """);
  165. using var cfg = new CfgBuilder()
  166. .AddJson(jsonPath, level: 0, writeable: true, isPrimaryWriter: true)
  167. .Build();
  168. // Act
  169. var section = cfg.GetSection("App");
  170. section.Remove("ToRemove");
  171. await cfg.SaveAsync();
  172. // Assert
  173. Assert.True(section.Exists("Name"));
  174. Assert.False(section.Exists("ToRemove"));
  175. }
  176. #endregion
  177. #region GetChildKeys 功能
  178. [Fact]
  179. public void GetChildKeys_ReturnsAllTopLevelKeys()
  180. {
  181. // Arrange
  182. var jsonPath = Path.Combine(_testDir, "config.json");
  183. File.WriteAllText(jsonPath, """
  184. {
  185. "App": { "Name": "Test" },
  186. "Database": { "Host": "localhost" },
  187. "Logging": { "Level": "Info" }
  188. }
  189. """);
  190. using var cfg = new CfgBuilder()
  191. .AddJson(jsonPath, level: 0, writeable: false)
  192. .Build();
  193. // Act
  194. var keys = cfg.GetChildKeys().ToList();
  195. // Assert
  196. Assert.Contains("App", keys);
  197. Assert.Contains("Database", keys);
  198. Assert.Contains("Logging", keys);
  199. Assert.Equal(3, keys.Count);
  200. }
  201. [Fact]
  202. public void GetSection_GetChildKeys_ReturnsSectionKeys()
  203. {
  204. // Arrange
  205. var jsonPath = Path.Combine(_testDir, "config.json");
  206. File.WriteAllText(jsonPath, """
  207. {
  208. "Database": {
  209. "Host": "localhost",
  210. "Port": 5432,
  211. "Name": "testdb"
  212. }
  213. }
  214. """);
  215. using var cfg = new CfgBuilder()
  216. .AddJson(jsonPath, level: 0, writeable: false)
  217. .Build();
  218. // Act
  219. var section = cfg.GetSection("Database");
  220. var keys = section.GetChildKeys().ToList();
  221. // Assert
  222. Assert.Contains("Host", keys);
  223. Assert.Contains("Port", keys);
  224. Assert.Contains("Name", keys);
  225. Assert.Equal(3, keys.Count);
  226. }
  227. #endregion
  228. #region GetSection 边界情况
  229. [Fact]
  230. public void GetSection_NonExistentSection_ReturnsEmptySection()
  231. {
  232. // Arrange
  233. var jsonPath = Path.Combine(_testDir, "config.json");
  234. File.WriteAllText(jsonPath, """{"Key": "Value"}""");
  235. using var cfg = new CfgBuilder()
  236. .AddJson(jsonPath, level: 0, writeable: false)
  237. .Build();
  238. // Act
  239. var section = cfg.GetSection("NonExistent");
  240. // Assert
  241. Assert.Equal("NonExistent", section.Path);
  242. Assert.Null(section.Get("AnyKey"));
  243. Assert.False(section.Exists("AnyKey"));
  244. }
  245. [Fact]
  246. public void GetSection_EmptyPath_WorksCorrectly()
  247. {
  248. // Arrange
  249. var jsonPath = Path.Combine(_testDir, "config.json");
  250. File.WriteAllText(jsonPath, """{"TopLevel": "Value"}""");
  251. using var cfg = new CfgBuilder()
  252. .AddJson(jsonPath, level: 0, writeable: false)
  253. .Build();
  254. // Act - 获取空路径的 section,然后访问顶级键
  255. var section = cfg.GetSection("");
  256. // Assert
  257. Assert.Equal("", section.Path);
  258. Assert.Equal("Value", section.Get("TopLevel"));
  259. }
  260. [Fact]
  261. public void GetSection_DeepNesting_WorksCorrectly()
  262. {
  263. // Arrange
  264. var jsonPath = Path.Combine(_testDir, "config.json");
  265. File.WriteAllText(jsonPath, """
  266. {
  267. "Level1": {
  268. "Level2": {
  269. "Level3": {
  270. "Level4": {
  271. "DeepValue": "Found"
  272. }
  273. }
  274. }
  275. }
  276. }
  277. """);
  278. using var cfg = new CfgBuilder()
  279. .AddJson(jsonPath, level: 0, writeable: false)
  280. .Build();
  281. // Act
  282. var section = cfg.GetSection("Level1")
  283. .GetSection("Level2")
  284. .GetSection("Level3")
  285. .GetSection("Level4");
  286. // Assert
  287. Assert.Equal("Level1:Level2:Level3:Level4", section.Path);
  288. Assert.Equal("Found", section.Get("DeepValue"));
  289. }
  290. #endregion
  291. #region GetOrDefault 扩展方法
  292. [Fact]
  293. public void GetOrDefault_ExistingKey_ReturnsValue()
  294. {
  295. // Arrange
  296. var jsonPath = Path.Combine(_testDir, "config.json");
  297. File.WriteAllText(jsonPath, """{"IntValue": 42}""");
  298. using var cfg = new CfgBuilder()
  299. .AddJson(jsonPath, level: 0, writeable: false)
  300. .Build();
  301. // Act & Assert
  302. Assert.Equal(42, cfg.GetOrDefault("IntValue", 0));
  303. }
  304. [Fact]
  305. public void GetOrDefault_NonExistingKey_ReturnsDefault()
  306. {
  307. // Arrange
  308. var jsonPath = Path.Combine(_testDir, "config.json");
  309. File.WriteAllText(jsonPath, """{"Key": "Value"}""");
  310. using var cfg = new CfgBuilder()
  311. .AddJson(jsonPath, level: 0, writeable: false)
  312. .Build();
  313. // Act & Assert
  314. Assert.Equal(100, cfg.GetOrDefault("NonExistent", 100));
  315. Assert.Equal("DefaultString", cfg.GetOrDefault("NonExistent", "DefaultString"));
  316. }
  317. #endregion
  318. }