SharedConfigStoreTests.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.IO;
  2. using ivf_tl_Operate.Helpers;
  3. using Xunit;
  4. namespace ivf_tl_Operate.Tests
  5. {
  6. public class SharedConfigStoreTests
  7. {
  8. private static string TempFile() =>
  9. Path.Combine(Path.GetTempPath(), "tl-shared-test-" + Path.GetRandomFileName() + ".config");
  10. [Fact]
  11. public void Read_missing_file_returns_null()
  12. {
  13. var path = TempFile();
  14. Assert.Null(SharedConfigStore.Read(path, "urlIp"));
  15. }
  16. [Fact]
  17. public void Write_then_Read_roundtrips()
  18. {
  19. var path = TempFile();
  20. try
  21. {
  22. SharedConfigStore.Write(path, "urlIp", "http://10.0.0.5");
  23. SharedConfigStore.Write(path, "urlPort", "10010");
  24. Assert.Equal("http://10.0.0.5", SharedConfigStore.Read(path, "urlIp"));
  25. Assert.Equal("10010", SharedConfigStore.Read(path, "urlPort"));
  26. }
  27. finally { File.Delete(path); }
  28. }
  29. [Fact]
  30. public void Write_existing_key_updates_value()
  31. {
  32. var path = TempFile();
  33. try
  34. {
  35. SharedConfigStore.Write(path, "mqttIp", "192.168.0.108");
  36. SharedConfigStore.Write(path, "mqttIp", "192.168.0.200");
  37. Assert.Equal("192.168.0.200", SharedConfigStore.Read(path, "mqttIp"));
  38. }
  39. finally { File.Delete(path); }
  40. }
  41. [Fact]
  42. public void Written_file_is_appSettings_fragment()
  43. {
  44. var path = TempFile();
  45. try
  46. {
  47. SharedConfigStore.Write(path, "kfkaIP", "192.168.0.108");
  48. var text = File.ReadAllText(path);
  49. Assert.Contains("<appSettings>", text);
  50. Assert.Contains("key=\"kfkaIP\"", text);
  51. }
  52. finally { File.Delete(path); }
  53. }
  54. }
  55. }