Explorar el Código

Add tests qdr.app.qlbrc.common.Tests (full scoped), fix ObjectExt, add PropertyInfoExt

Dalibor Votruba hace 3 años
padre
commit
e9ccdaa88e

+ 0 - 1
Common/qdr.app.qlbrc.common/Configuration/OlbrcConfiguration.cs

@@ -1,5 +1,4 @@
 using System.IO.Abstractions;
-using System.Reflection;
 using System.Text.Json;
 
 namespace Quadarax.Application.QLiberace.Common.Configuration

+ 23 - 24
Common/qdr.app.qlbrc.common/Configuration/Settings.cs

@@ -1,26 +1,25 @@
-using System.Reflection;
-using System.Text.Json.Serialization;
+using System.Text.Json.Serialization;
 
-namespace Quadarax.Application.QLiberace.Common.Configuration
-{
-    public class Settings
-    {
-
-        [JsonPropertyName("modules")]
-        public IList<ModuleConfiguration> Modules { get; set; }
-
-        public Settings()
-        {
-            Modules = new List<ModuleConfiguration>();
-        }
-    }
-
-
-    public class ModuleConfiguration
-    {
-        [JsonPropertyName("code")]
-        public string Code { get; set; }
-        [JsonPropertyName("connection-string")]
-        public string ConnectionString { get; set; }
-    }
+namespace Quadarax.Application.QLiberace.Common.Configuration
+{
+    public class Settings
+    {
+
+        [JsonPropertyName("modules")]
+        public IList<ModuleConfiguration> Modules { get; set; }
+
+        public Settings()
+        {
+            Modules = new List<ModuleConfiguration>();
+        }
+    }
+
+
+    public class ModuleConfiguration
+    {
+        [JsonPropertyName("code")]
+        public string Code { get; set; }
+        [JsonPropertyName("connection-string")]
+        public string ConnectionString { get; set; }
+    }
 }

+ 2 - 1
Common/qdr.app.qlbrc.common/Settings/ModuleSetting.cs

@@ -29,9 +29,10 @@ namespace Quadarax.Application.QLiberace.Common.Settings
         {
             var properties = this.GetType().GetAllProperties();
             var result = this.GetAllPropertyValues().
-                Select(x=> new Tuple<string, string, object?>(x.Key, properties.First(y=>y.Name == x.Key).PropertyType.Name, properties.First(y=>y.Name == x.Key).GetValue(this)));
+                Select(x=> new Tuple<string, string, object?>(x.Key, properties.First(y=>y.Name == x.Key).GetFinalPropertyType().FullName ?? string.Empty, properties.First(y=>y.Name == x.Key).GetValue(this))).ToArray();
 
             return result;
         }
+
     }
 }

+ 1 - 1
Common/qdr.fnd.core/Object/Extensions/ObjectExt.cs

@@ -22,7 +22,7 @@ namespace Quadarax.Foundation.Core.Object.Extensions
             foreach (var prop in propList)
             {
                 var val = prop.GetValue(obj, new object[] { });
-                if (val is IEnumerable enumerable)
+                if (val is IEnumerable enumerable && !(val is string))
                 {
                     var cnt = 0;
                     foreach (var item in enumerable)

+ 21 - 0
Common/qdr.fnd.core/Reflection/Extensions/PropertyInfoExt.cs

@@ -0,0 +1,21 @@
+using System;
+using System.Reflection;
+
+namespace Quadarax.Foundation.Core.Reflection.Extensions
+{
+    public static class PropertyInfoExt
+    {
+        public static Type GetFinalPropertyType(this PropertyInfo owner)
+        {
+            if (owner == null) throw new ArgumentNullException(nameof(owner));
+            var propertyType = owner.PropertyType;
+            if (propertyType.IsGenericType &&
+                propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
+            {
+                propertyType = propertyType.GetGenericArguments()[0];
+            }
+
+            return propertyType;
+        }
+    }
+}

+ 27 - 0
Tests/qdr.app.qlbrc.common.Tests/Configuration/Fakes/SettingsFake.cs

@@ -0,0 +1,27 @@
+using Quadarax.Application.QLiberace.Common.Configuration;
+
+namespace Quadarax.Application.QLiberace.Common.Tests.Configuration.Fakes
+{
+    internal class SettingsFake : Common.Configuration.Settings
+    {
+        public const string DefModule1 = "mod1";
+        public const string DefModule1Cs = "csMod1";
+        public const string DefModule2 = "mod2";
+        public const string DefModule2Cs = "csMod2";
+
+        public void Reset()
+        {
+            this.Modules = new List<ModuleConfiguration>();
+            this.Modules.Add(new ModuleConfiguration(){Code = DefModule1, ConnectionString = DefModule1Cs});
+            this.Modules.Add(new ModuleConfiguration(){Code = DefModule2, ConnectionString = DefModule2Cs});
+
+        }
+
+        public void Clear()
+        {
+            this.Modules = new List<ModuleConfiguration>();
+        }
+
+
+    }
+}

+ 86 - 0
Tests/qdr.app.qlbrc.common.Tests/Configuration/QlbrcConfigurationTest.cs

@@ -0,0 +1,86 @@
+using Quadarax.Application.QLiberace.Common.Configuration;
+using Quadarax.Application.QLiberace.Common.Tests.Configuration.Fakes;
+
+namespace Quadarax.Application.QLiberace.Common.Tests.Configuration
+{
+    [TestFixture(Category = "Configuration")]
+    public class QlbrcConfigurationTest
+    {
+        private const string CfgFileName = "qlbrc.test.json";
+
+        [SetUp]
+        public void Setup()
+        {
+            OlbrcConfiguration.ConfigurationFileName = CfgFileName;
+        }
+
+        [Test(TestOf = typeof(OlbrcConfiguration))]
+        public void GetNotNullOk()
+        {
+            OlbrcConfiguration.ConfigurationFileName = CfgFileName;
+            var settings = OlbrcConfiguration.Get();
+            Assert.NotNull(settings);
+
+        }
+
+        [Test(TestOf = typeof(OlbrcConfiguration))]
+        public void GetNullOk()
+        {
+            OlbrcConfiguration.ConfigurationFileName = "xxx";
+            var settings = OlbrcConfiguration.Get();
+            Assert.IsNull(settings);
+
+        }
+
+        [Test(TestOf = typeof(OlbrcConfiguration))]
+        public void GetDeserializationOk()
+        {
+            OlbrcConfiguration.ConfigurationFileName = CfgFileName;
+            var newSetting = new SettingsFake();
+            newSetting.Reset();
+            var settings = OlbrcConfiguration.Get();
+            Assert.IsNotNull(settings);
+
+            var modulesLeft = settings.Modules.Select(x => x.Code + x.ConnectionString).ToArray();
+            var modulesRight = newSetting.Modules.Select(x => x.Code + x.ConnectionString).ToArray();
+
+            Assert.That(modulesRight, Is.EquivalentTo(modulesLeft));
+
+        }
+
+        [Test(TestOf = typeof(OlbrcConfiguration))]
+        public void GetModuleOk()
+        {
+            OlbrcConfiguration.ConfigurationFileName = CfgFileName;
+            var mod = OlbrcConfiguration.Get(SettingsFake.DefModule2.ToUpper());
+            Assert.IsNotNull(mod);
+            Assert.That(mod.ConnectionString, Is.EqualTo(SettingsFake.DefModule2Cs));
+
+        }
+
+        [Test(TestOf = typeof(OlbrcConfiguration))]
+        public void GetModuleConfigurationMissingFail()
+        {
+            OlbrcConfiguration.ConfigurationFileName = "xxx";
+            var ex = Assert.Throws<InvalidOperationException>(delegate
+            {
+                OlbrcConfiguration.Get(SettingsFake.DefModule2.ToUpper());
+            });
+            Assert.IsNotNull(ex);
+            Assert.IsTrue(ex.Message.StartsWith("Cannot obtain"));
+        }
+
+        [Test(TestOf = typeof(OlbrcConfiguration))]
+        public void GetModuleMissingFail()
+        {
+            OlbrcConfiguration.ConfigurationFileName = CfgFileName;
+            var ex = Assert.Throws<InvalidOperationException>(delegate
+            {
+                OlbrcConfiguration.Get("fofo");
+            });
+            Assert.IsNotNull(ex);
+            Assert.IsTrue(ex.Message.StartsWith("Module '"));
+        }
+
+    }
+}

+ 35 - 3
Tests/qdr.app.qlbrc.common.Tests/Settings/Fakes/ModuleSettingFake.cs

@@ -2,7 +2,7 @@
 
 namespace Quadarax.Application.QLiberace.Common.Tests.Settings.Fakes
 {
-    internal class ModuleSettingFake : ModuleSetting
+    internal class ModuleSettingFake : ModuleSetting, IEquatable<ModuleSettingFake>
     {
 
         public const string DefPropertyString1 = "test1";
@@ -24,7 +24,7 @@ namespace Quadarax.Application.QLiberace.Common.Tests.Settings.Fakes
         public int? PropertyInt { get; set; }
         public bool PropertyBool { get; set; }
 
-
+        private bool InvisiblePropertyBool { get; set; }
 
         public ModuleSettingFake()
         {
@@ -32,7 +32,18 @@ namespace Quadarax.Application.QLiberace.Common.Tests.Settings.Fakes
             Reset();
         }
 
-        private void Reset()
+        public void Clear()
+        {
+            PropertyString1 = string.Empty;
+            PropertyString2 = null;
+            PropertyDateTime1 = DateTime.MinValue;
+            PropertyDateTime2 = null;
+            PropertyTimeSpan = null;
+            PropertyInt = null;
+            PropertyBool = false;
+            InvisiblePropertyBool = true;
+        }
+        public void Reset()
         {
             PropertyString1 = DefPropertyString1;
             PropertyString2 = DefPropertyString2;
@@ -45,6 +56,27 @@ namespace Quadarax.Application.QLiberace.Common.Tests.Settings.Fakes
                 : TimeSpan.Parse(DefPropertyTimeSpanString);
             PropertyInt = DefPropertyInt;
             PropertyBool = DefPropertyBool;
+            InvisiblePropertyBool = true;
+        }
+
+        public bool Equals(ModuleSettingFake? other)
+        {
+            if (ReferenceEquals(null, other)) return false;
+            if (ReferenceEquals(this, other)) return true;
+            return PropertyString1 == other.PropertyString1 && PropertyString2 == other.PropertyString2 && PropertyDateTime1.Equals(other.PropertyDateTime1) && Nullable.Equals(PropertyDateTime2, other.PropertyDateTime2) && Nullable.Equals(PropertyTimeSpan, other.PropertyTimeSpan) && PropertyInt == other.PropertyInt && PropertyBool == other.PropertyBool && InvisiblePropertyBool == other.InvisiblePropertyBool;
+        }
+
+        public override bool Equals(object? obj)
+        {
+            if (ReferenceEquals(null, obj)) return false;
+            if (ReferenceEquals(this, obj)) return true;
+            if (obj.GetType() != this.GetType()) return false;
+            return Equals((ModuleSettingFake)obj);
+        }
+
+        public override int GetHashCode()
+        {
+            return HashCode.Combine(PropertyString1, PropertyString2, PropertyDateTime1, PropertyDateTime2, PropertyTimeSpan, PropertyInt, PropertyBool, InvisiblePropertyBool);
         }
     }
 }

+ 42 - 10
Tests/qdr.app.qlbrc.common.Tests/Settings/ModuleSettingsTest.cs

@@ -1,7 +1,9 @@
+using Quadarax.Application.QLiberace.Common.Settings;
 using Quadarax.Application.QLiberace.Common.Tests.Settings.Fakes;
 
 namespace Quadarax.Application.QLiberace.Common.Tests.Settings
 {
+    [TestFixture(Category = "Dto")]
     public class ModuleSettingsTest
     {
         private ModuleSettingFake _setting;
@@ -14,26 +16,56 @@ namespace Quadarax.Application.QLiberace.Common.Tests.Settings
             _setting = new ModuleSettingFake();
         }
 
-        [Test]
-        public void GetData()
+        [Test(TestOf = typeof(ModuleSetting))]
+        public void GetDataOk()
         {
-            //TODO: fix test
             var data = _setting.GetData().ToArray();
 
             Assert.That(data.Length, Is.EqualTo(7));
-            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyString1", string.Empty, ModuleSettingFake.DefPropertyString1));
-            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyString2", string.Empty, ModuleSettingFake.DefPropertyString2));
-            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyDateTime1", string.Empty, string.IsNullOrEmpty(ModuleSettingFake.DefPropertyDateTime1String)
+            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyString1", "System.String", ModuleSettingFake.DefPropertyString1));
+            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyString2", "System.String", ModuleSettingFake.DefPropertyString2));
+            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyDateTime1", "System.DateTime", string.IsNullOrEmpty(ModuleSettingFake.DefPropertyDateTime1String)
                 ? (DateTime?)null
                 : DateTime.Parse(ModuleSettingFake.DefPropertyDateTime1String!)));
-            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyDateTime2", string.Empty, string.IsNullOrEmpty(ModuleSettingFake.DefPropertyDateTime2String)
+            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyDateTime2", "System.DateTime", string.IsNullOrEmpty(ModuleSettingFake.DefPropertyDateTime2String)
                 ? (DateTime?)null
                 : DateTime.Parse(ModuleSettingFake.DefPropertyDateTime2String!)));
-            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyTimeSpan", string.Empty, string.IsNullOrEmpty(ModuleSettingFake.DefPropertyTimeSpanString)
+            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyTimeSpan", "System.TimeSpan", string.IsNullOrEmpty(ModuleSettingFake.DefPropertyTimeSpanString)
                 ? (TimeSpan?)null
                 : TimeSpan.Parse(ModuleSettingFake.DefPropertyTimeSpanString!)));
-            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyInt", string.Empty, ModuleSettingFake.DefPropertyInt));
-            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyBool", string.Empty, ModuleSettingFake.DefPropertyBool));
+            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyInt", "System.Int32", ModuleSettingFake.DefPropertyInt));
+            CollectionAssert.Contains(data,new Tuple<string, string, object?>("PropertyBool", "System.Boolean", ModuleSettingFake.DefPropertyBool));
+        }
+
+        [Test(TestOf = typeof(ModuleSetting))]
+        public void SetDataOk()
+        {
+            var data = new[]
+            {
+                new Tuple<string, string, object?>("PropertyString1", "System.String",
+                    ModuleSettingFake.DefPropertyString1),
+                new Tuple<string, string, object?>("PropertyString2", "System.String",
+                    ModuleSettingFake.DefPropertyString2),
+                new Tuple<string, string, object?>("PropertyDateTime1", "System.DateTime",
+                    string.IsNullOrEmpty(ModuleSettingFake.DefPropertyDateTime1String)
+                        ? (DateTime?)null
+                        : DateTime.Parse(ModuleSettingFake.DefPropertyDateTime1String!)),
+                new Tuple<string, string, object?>("PropertyDateTime2", "System.DateTime",
+                    string.IsNullOrEmpty(ModuleSettingFake.DefPropertyDateTime2String)
+                        ? (DateTime?)null
+                        : DateTime.Parse(ModuleSettingFake.DefPropertyDateTime2String!)),
+                new Tuple<string, string, object?>("PropertyTimeSpan", "System.TimeSpan",
+                    string.IsNullOrEmpty(ModuleSettingFake.DefPropertyTimeSpanString)
+                        ? (TimeSpan?)null
+                        : TimeSpan.Parse(ModuleSettingFake.DefPropertyTimeSpanString!)),
+                new Tuple<string, string, object?>("PropertyInt", "System.Int32", ModuleSettingFake.DefPropertyInt),
+                new Tuple<string, string, object?>("PropertyBool", "System.Boolean", ModuleSettingFake.DefPropertyBool)
+            };
+
+            _setting.Clear();
+            _setting.SetData(data);
+            var newSettings = new ModuleSettingFake();
+            Assert.That(_setting,Is.EqualTo(newSettings));
         }
     }
 }

+ 6 - 0
Tests/qdr.app.qlbrc.common.Tests/qdr.app.qlbrc.common.Tests.csproj

@@ -22,4 +22,10 @@
     <ProjectReference Include="..\..\common\qdr.app.qlbrc.common\qdr.app.qlbrc.common.csproj" />
   </ItemGroup>
 
+  <ItemGroup>
+    <None Update="qlbrc.test.json">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </None>
+  </ItemGroup>
+
 </Project>

+ 12 - 0
Tests/qdr.app.qlbrc.common.Tests/qlbrc.test.json

@@ -0,0 +1,12 @@
+{
+  "modules": [
+    {
+      "code": "mod1",
+      "connection-string": "csMod1"
+    },
+    {
+      "code": "mod2",
+      "connection-string": "csMod2"
+    }
+  ]
+}