ATG_Config_Test.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Application.ATG_Classic_App;
  2. using Application.ATG_Classic_App.Model;
  3. using AutoMapper;
  4. using Edge.Core.Processor;
  5. using Edge.Core.IndustryStandardInterface.Pump;
  6. using Edge.Core.IndustryStandardInterface.ATG;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Logging;
  10. using Microsoft.Extensions.Logging.Abstractions;
  11. using Microsoft.VisualStudio.TestTools.UnitTesting;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Text.Json;
  18. using System.Threading.Tasks;
  19. using Edge.Core.UniversalApi;
  20. namespace Application.ATG_Classic_App_Test
  21. {
  22. [TestClass]
  23. public class ATG_Config_Test
  24. {
  25. static int TankCount = 6;
  26. //ServiceProvider serviceProvider;
  27. static List<MockProbeHandler> probeHandlers;
  28. static ATG_Classic_App.App atg;
  29. static List<TankProfileData> sharedTankProfileDatas;
  30. static IEnumerable<TankConfig> tankConfigs;
  31. static TankOverallConfig tankOverallConfig;
  32. [ClassInitialize]
  33. public static void ClassInit(TestContext context)
  34. {
  35. var services = new ServiceCollection();
  36. services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
  37. #region AutoMapper, resolve all assemblies under entry folder.
  38. var applicationAssFileInfos = new DirectoryInfo(Directory.GetCurrentDirectory())
  39. .GetFiles().Where(f => //f.Name.ToLower().StartsWith("application") &&
  40. f.Extension != null
  41. && (f.Extension.ToLower() == ".dll" || f.Extension.ToLower() == ".exe"));
  42. var assembliesWithAutoMapperProfileDefined = new List<Assembly>();
  43. foreach (var ai in applicationAssFileInfos)
  44. {
  45. try
  46. {
  47. var ass = Assembly.LoadFrom(ai.FullName);
  48. if (ass.GetTypes().Any(t => typeof(Profile).IsAssignableFrom(t)))
  49. assembliesWithAutoMapperProfileDefined.Add(ass);
  50. }
  51. catch
  52. { }
  53. }
  54. services.AddAutoMapper(assembliesWithAutoMapperProfileDefined);
  55. services.AddSingleton(new UniversalApiHub(null, null));
  56. #endregion
  57. var serviceProvider = services.BuildServiceProvider();
  58. probeHandlers = Enumerable.Range(1, TankCount).Select(i => new MockProbeHandler(i, i * 1000)).ToList();
  59. sharedTankProfileDatas = Enumerable.Range(0, 2001).Select(i =>
  60. new TankProfileData()
  61. {
  62. BatchLabel = "shitday",
  63. Height = i,
  64. Volume = i * 2
  65. }).ToList();
  66. tankConfigs = Enumerable.Range(1, TankCount).Select(i => new TankConfig()
  67. {
  68. TankNumber = (byte)i,
  69. Label = "I'm Tank with Number " + i,
  70. Diameter = i * 1000,
  71. ProbeConfig = new ProbeConfig() { DeviceId = i },
  72. ProductConfig = new ProductConfig() { ProductCode = (i + 90).ToString(), ProductLabel = (i + 90).ToString() + "#" },
  73. TankLimitConfig = new TankLimitConfig() { FullVolume = 1200 * i, MaxVolume = 1100 * i },
  74. //TankProfileDatas = sharedTankProfileDatas
  75. });
  76. tankOverallConfig = new TankOverallConfig() { TcReference = 25 };
  77. var dbContextFactory = new DefaultDbContextFactory();
  78. dbContextFactory.AddProvider(new InMemoryDbContextProvider());
  79. atg = new ATG_Classic_App.App(serviceProvider, 1, true);
  80. atg.DbContextFactory = dbContextFactory;
  81. atg.Init(probeHandlers.Cast<IProcessor>());
  82. var _ = atg.UpsertConfigAsync(tankOverallConfig).Result;
  83. foreach (var tc in tankConfigs)
  84. {
  85. var xx = atg.UpsertConfigAsync(tc).Result;
  86. }
  87. }
  88. class InMemoryDbContextProvider : IDbContextProvider
  89. {
  90. public T CreateDbContext<T>() where T : DbContext
  91. {
  92. //var options = new DbContextOptionsBuilder<InMemoryDbContext>()
  93. // .UseInMemoryDatabase(databaseName: "Test")
  94. // .Options;
  95. return new InMemoryDbContext() as T;
  96. }
  97. public void Dispose()
  98. {
  99. //throw new NotImplementedException();
  100. }
  101. }
  102. [TestInitialize]
  103. public void TestMethodInit()
  104. {
  105. }
  106. [TestMethod]
  107. public async Task Get_Config_TestMethod1()
  108. {
  109. var queriedConfigResult = await atg.GetConfigAsync();
  110. Assert.AreEqual(true, queriedConfigResult != null);
  111. Assert.AreEqual(true, queriedConfigResult.Item1 != null);
  112. Assert.AreEqual(true, queriedConfigResult.Item1.TcReference == tankOverallConfig.TcReference);
  113. Assert.AreEqual(true, queriedConfigResult.Item2 != null);
  114. Assert.AreEqual(true, queriedConfigResult.Item2.Count() == tankConfigs.Count());
  115. var joined = queriedConfigResult.Item2.Join(tankConfigs, (inner) => inner.TankNumber, (outer) => outer.TankNumber, (tc, inner) => tc);
  116. Assert.AreEqual(true, joined.Count() == tankConfigs.Count());
  117. }
  118. [TestMethod]
  119. public async Task OverallConfig_Insert_TestMethod1()
  120. {
  121. BaseConfig insertedConfigResult = await atg.UpsertConfigAsync(null);
  122. Assert.AreEqual(true, insertedConfigResult != null);
  123. var insertedTankOverallConfig = insertedConfigResult as TankOverallConfig;
  124. Assert.AreEqual(true, insertedTankOverallConfig != null);
  125. Assert.AreEqual(true, insertedTankOverallConfig.TcReference == 12.01);
  126. var queriedConfigResult = await atg.GetConfigAsync();
  127. Assert.AreEqual(true, queriedConfigResult != null);
  128. Assert.AreEqual(true, queriedConfigResult.Item1 != null);
  129. Assert.AreEqual(true, queriedConfigResult.Item1.TcReference == 12.01);
  130. Assert.AreEqual(true, queriedConfigResult.Item2.Any());
  131. }
  132. [TestMethod]
  133. public async Task OverallConfig_Insert_TestMethod2()
  134. {
  135. BaseConfig insertedConfigResult = await atg.UpsertConfigAsync(null);
  136. Assert.AreEqual(true, insertedConfigResult != null);
  137. var insertedTankOverallConfig = insertedConfigResult as TankOverallConfig;
  138. Assert.AreEqual(true, insertedTankOverallConfig != null);
  139. Assert.AreEqual(true, insertedTankOverallConfig.TcReference == 12.01);
  140. var queriedConfigResult = await atg.GetConfigAsync();
  141. Assert.AreEqual(true, queriedConfigResult != null);
  142. Assert.AreEqual(true, queriedConfigResult.Item1 != null);
  143. Assert.AreEqual(true, queriedConfigResult.Item1.TcReference == 12.01);
  144. Assert.AreEqual(true, queriedConfigResult.Item2.Any());
  145. }
  146. }
  147. }