From aec104fe072b5443fbaff0c2b8d6d12f05096590 Mon Sep 17 00:00:00 2001 From: Alexander Chocron Date: Tue, 7 Nov 2017 13:11:51 -0800 Subject: [PATCH] Fix unit tests --- .../Ingestion/Models/PageLogTest.cs | 4 +- .../Ingestion/Models/StartSessionLogTest.cs | 2 +- .../AppCenterTest.cs | 2 +- .../CustomPropertiesTest.cs | 38 +++-- .../Models/CustomPropertiesLogTest.cs | 22 +-- .../Ingestion/Models/DeviceTest.cs | 151 ++++++++++++------ .../Ingestion/Models/LogWithPropertiesTest.cs | 2 +- .../Ingestion/Models/StartServiceLogTest.cs | 20 +-- .../Ingestion/Models/TestLog.cs | 2 +- 9 files changed, 139 insertions(+), 104 deletions(-) diff --git a/Tests/Microsoft.AppCenter.Analytics.Test.Windows/Ingestion/Models/PageLogTest.cs b/Tests/Microsoft.AppCenter.Analytics.Test.Windows/Ingestion/Models/PageLogTest.cs index 2b1df1870..cdf59c6f1 100644 --- a/Tests/Microsoft.AppCenter.Analytics.Test.Windows/Ingestion/Models/PageLogTest.cs +++ b/Tests/Microsoft.AppCenter.Analytics.Test.Windows/Ingestion/Models/PageLogTest.cs @@ -23,7 +23,7 @@ namespace Microsoft.AppCenter.Test.Windows.Ingestion.Models var mockDevice = new Mock(); PageLog emptyLog = new PageLog(); - PageLog log = new PageLog(Timestamp, mockDevice.Object, Name); + PageLog log = new PageLog(mockDevice.Object, Name, Timestamp); Assert.IsNotNull(emptyLog); Assert.IsNotNull(log); @@ -40,7 +40,7 @@ namespace Microsoft.AppCenter.Test.Windows.Ingestion.Models const string NullName = null; var mockDevice = new Mock(); - PageLog log = new PageLog(Timestamp, mockDevice.Object, NullName); + PageLog log = new PageLog(mockDevice.Object, NullName, Timestamp); Assert.ThrowsException(() => log.Validate()); } } diff --git a/Tests/Microsoft.AppCenter.Analytics.Test.Windows/Ingestion/Models/StartSessionLogTest.cs b/Tests/Microsoft.AppCenter.Analytics.Test.Windows/Ingestion/Models/StartSessionLogTest.cs index bf8672b77..cab239583 100644 --- a/Tests/Microsoft.AppCenter.Analytics.Test.Windows/Ingestion/Models/StartSessionLogTest.cs +++ b/Tests/Microsoft.AppCenter.Analytics.Test.Windows/Ingestion/Models/StartSessionLogTest.cs @@ -21,7 +21,7 @@ namespace Microsoft.AppCenter.Test.Windows.Ingestion.Models var mockDevice = new Mock(); StartSessionLog emptyLog = new StartSessionLog(); - StartSessionLog log = new StartSessionLog(Timestamp, mockDevice.Object); + StartSessionLog log = new StartSessionLog(mockDevice.Object, Timestamp); Assert.IsNotNull(emptyLog); Assert.IsNotNull(log); diff --git a/Tests/Microsoft.AppCenter.Test.Windows/AppCenterTest.cs b/Tests/Microsoft.AppCenter.Test.Windows/AppCenterTest.cs index b23b0ad3c..acf25d404 100644 --- a/Tests/Microsoft.AppCenter.Test.Windows/AppCenterTest.cs +++ b/Tests/Microsoft.AppCenter.Test.Windows/AppCenterTest.cs @@ -515,7 +515,7 @@ namespace Microsoft.AppCenter.Test var properties = new CustomProperties(); properties.Set("test", "test"); AppCenter.SetCustomProperties(properties); - channelUnitMock.Verify(channel => channel.EnqueueAsync(It.Is(log => + channelUnitMock.Verify(channel => channel.EnqueueAsync(It.Is(log => log.Properties == properties.Properties)), Times.Once()); } } diff --git a/Tests/Microsoft.AppCenter.Test.Windows/CustomPropertiesTest.cs b/Tests/Microsoft.AppCenter.Test.Windows/CustomPropertiesTest.cs index 76e705bd9..e2ab62b3a 100644 --- a/Tests/Microsoft.AppCenter.Test.Windows/CustomPropertiesTest.cs +++ b/Tests/Microsoft.AppCenter.Test.Windows/CustomPropertiesTest.cs @@ -1,5 +1,8 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using Microsoft.AppCenter.Ingestion.Models; +using System.Collections.Generic; namespace Microsoft.AppCenter.Test.Windows { @@ -144,7 +147,7 @@ namespace Microsoft.AppCenter.Test.Windows var normalValue = "test"; properties.Set(key, normalValue); Assert.AreEqual(1, properties.Properties.Count); - Assert.AreEqual(normalValue, properties.Properties[key]); + FindProperty(properties.Properties, key, normalValue); } /// @@ -161,7 +164,7 @@ namespace Microsoft.AppCenter.Test.Windows var normalValue = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); properties.Set(key, normalValue); Assert.AreEqual(1, properties.Properties.Count); - Assert.AreEqual(normalValue, properties.Properties[key]); + FindProperty(properties.Properties, key, normalValue); } /// @@ -185,15 +188,15 @@ namespace Microsoft.AppCenter.Test.Windows properties.Set("t4", value4); properties.Set("t5", value5); Assert.AreEqual(5, properties.Properties.Count); - Assert.AreEqual(value1, properties.Properties["t1"]); - Assert.AreEqual(value2, properties.Properties["t2"]); - Assert.AreEqual(value3, properties.Properties["t3"]); - Assert.AreEqual(value4, properties.Properties["t4"]); - Assert.AreEqual(value5, properties.Properties["t5"]); + FindProperty(properties.Properties, "t1", value1); + FindProperty(properties.Properties, "t2", value2); + FindProperty(properties.Properties, "t3", value3); + FindProperty(properties.Properties, "t4", value4); + FindProperty(properties.Properties, "t5", value5); } /// - /// Verify that bool setting correct. + /// Verify that bool setting correc /// [TestMethod] public void TestSetBool() @@ -206,7 +209,7 @@ namespace Microsoft.AppCenter.Test.Windows var normalValue = false; properties.Set(key, normalValue); Assert.AreEqual(1, properties.Properties.Count); - Assert.AreEqual(normalValue, properties.Properties[key]); + FindProperty(properties.Properties, key, normalValue); } /// @@ -220,7 +223,22 @@ namespace Microsoft.AppCenter.Test.Windows Assert.AreEqual(0, properties.Properties.Count); properties.Clear(key); Assert.AreEqual(1, properties.Properties.Count); - Assert.IsNull(properties.Properties[key]); + FindProperty(properties.Properties, key, null); + } + + private static void FindProperty(IList properties, string key, object value) + { + CustomProperty compareProperty = null; + foreach (var elt in properties) + { + if (elt.Name == key) + { + compareProperty = elt; + break; + } + } + Assert.IsNotNull(compareProperty); + Assert.AreEqual(value, compareProperty.GetValue()); } } } diff --git a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/CustomPropertiesLogTest.cs b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/CustomPropertiesLogTest.cs index 744112607..eaa7a2c5a 100644 --- a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/CustomPropertiesLogTest.cs +++ b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/CustomPropertiesLogTest.cs @@ -45,8 +45,8 @@ namespace Microsoft.AppCenter.Test.Windows.Ingestion.Models { new StringProperty("t1", "test"), new DateTimeProperty("t2", new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)), - new NumberProperty("t3", 1), - new NumberProperty("t4", 0.1f), + new NumberProperty("t3", (long)1), + new NumberProperty("t4", 0.1), new BooleanProperty("t5", false), new ClearProperty("t6") }, @@ -64,26 +64,10 @@ namespace Microsoft.AppCenter.Test.Windows.Ingestion.Models { var retrievedProperty = GetPropertyWithName(retrievedLog.Properties, addedProperty.Name); Assert.IsNotNull(retrievedProperty); - Assert.IsTrue(EqualityComparer.Default.Equals(addedProperty.GetValue(), retrievedProperty.GetValue())); + Assert.AreEqual(addedProperty.GetValue(), retrievedProperty.GetValue()); } } - /// - /// Validate that log is not valid with nullable 'Properties' - /// - [TestMethod] - public void ValidateStartServiceLog() - { - var log = new CustomPropertyLog - { - Properties = null, - Device = new DeviceInformationHelper().GetDeviceInformationAsync().RunNotAsync(), - Timestamp = DateTime.Now - }; - - Assert.ThrowsException((Action)log.Validate); - } - private static CustomProperty GetPropertyWithName(IList properties, string name) { foreach (var property in properties) diff --git a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/DeviceTest.cs b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/DeviceTest.cs index 4852a869f..1c86459a7 100644 --- a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/DeviceTest.cs +++ b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/DeviceTest.cs @@ -28,8 +28,20 @@ namespace Microsoft.AppCenter.Test.Ingestion.Models { const string deafultString = default(string); var emptyDevice = new Device(); - var device = new Device(SdkName, SdkVersion, Model, OemName, OsName, OsVersion, Locale, TimeZoneOffset, ScreenSize, AppVersion, AppBuild); - + var device = new Device + { + SdkName = SdkName, + SdkVersion = SdkVersion, + OsName = OsName, + OsVersion = OsVersion, + Locale = Locale, + TimeZoneOffset = TimeZoneOffset, + AppVersion = AppVersion, + AppBuild = AppBuild, + Model = Model, + ScreenSize = ScreenSize, + OemName = OemName + }; Assert.IsNotNull(emptyDevice); Assert.IsNotNull(device); @@ -63,8 +75,20 @@ namespace Microsoft.AppCenter.Test.Ingestion.Models [TestMethod] public void TestValidateSuccessfullWhenAllFieldsArePresent() { - var device = new Device(SdkName, SdkVersion, Model, OemName, OsName, OsVersion, Locale, TimeZoneOffset, ScreenSize, AppVersion, AppBuild); - + var device = new Device + { + SdkName = SdkName, + SdkVersion = SdkVersion, + OsName = OsName, + OsVersion = OsVersion, + Locale = Locale, + TimeZoneOffset = TimeZoneOffset, + AppVersion = AppVersion, + AppBuild = AppBuild, + Model = Model, + ScreenSize = ScreenSize, + OemName = OemName + }; device.Validate(); } @@ -74,8 +98,7 @@ namespace Microsoft.AppCenter.Test.Ingestion.Models [TestMethod] public void TestValidateThrowsExceptionWhenSdkNameIsNull() { - const string nullSdkName = null; - var device = new Device(nullSdkName, SdkVersion, Model, OemName, OsName, OsVersion, Locale, TimeZoneOffset, ScreenSize, AppVersion, AppBuild); + var device = new Device(null, SdkVersion, OsName, OsVersion, Locale, TimeZoneOffset, AppVersion, AppBuild, null, null, Model); Assert.ThrowsException(() => device.Validate()); } @@ -85,30 +108,20 @@ namespace Microsoft.AppCenter.Test.Ingestion.Models [TestMethod] public void TestValidateThrowsExceptionWhenSdkVersionIsNull() { - const string nullSdkVersion = null; - var device = new Device(SdkName, nullSdkVersion, Model, OemName, OsName, OsVersion, Locale, TimeZoneOffset, ScreenSize, AppVersion, AppBuild); - Assert.ThrowsException(() => device.Validate()); - } - - /// - /// Verify that Validate method throws ValidationException when model == null. - /// - [TestMethod] - public void TestValidateThrowsExceptionWhenModelIsNull() - { - const string nullModel = null; - var device = new Device(SdkName, SdkVersion, nullModel, OemName, OsName, OsVersion, Locale, TimeZoneOffset, ScreenSize, AppVersion, AppBuild); - Assert.ThrowsException(() => device.Validate()); - } - - /// - /// Verify that Validate method throws ValidationException when oemName == null. - /// - [TestMethod] - public void TestValidateThrowsExceptionWhenOemNameIsNull() - { - const string nullOemName = null; - var device = new Device(SdkName, SdkVersion, Model, nullOemName, OsName, OsVersion, Locale, TimeZoneOffset, ScreenSize, AppVersion, AppBuild); + var device = new Device + { + SdkName = SdkName, + SdkVersion = null, + OsName = OsName, + OsVersion = OsVersion, + Locale = Locale, + TimeZoneOffset = TimeZoneOffset, + AppVersion = AppVersion, + AppBuild = AppBuild, + Model = Model, + ScreenSize = ScreenSize, + OemName = OemName + }; Assert.ThrowsException(() => device.Validate()); } @@ -118,8 +131,7 @@ namespace Microsoft.AppCenter.Test.Ingestion.Models [TestMethod] public void TestValidateThrowsExceptionWhenOsNameIsNull() { - const string nullOsName = null; - var device = new Device(SdkName, SdkVersion, Model, OemName, nullOsName, OsVersion, Locale, TimeZoneOffset, ScreenSize, AppVersion, AppBuild); + var device = new Device(SdkName, SdkVersion, null, OsVersion, Locale, TimeZoneOffset, AppVersion, AppBuild, null, null, Model, OemName); Assert.ThrowsException(() => device.Validate()); } @@ -129,8 +141,20 @@ namespace Microsoft.AppCenter.Test.Ingestion.Models [TestMethod] public void TestValidateThrowsExceptionWhenOsVersionIsNull() { - const string nullOsVersion = null; - var device = new Device(SdkName, SdkVersion, Model, OemName, OsName, nullOsVersion, Locale, TimeZoneOffset, ScreenSize, AppVersion, AppBuild); + var device = new Device + { + SdkName = SdkName, + SdkVersion = SdkVersion, + OsName = OsName, + OsVersion = null, + Locale = Locale, + TimeZoneOffset = TimeZoneOffset, + AppVersion = AppVersion, + AppBuild = AppBuild, + Model = Model, + ScreenSize = ScreenSize, + OemName = OemName + }; Assert.ThrowsException(() => device.Validate()); } @@ -140,19 +164,20 @@ namespace Microsoft.AppCenter.Test.Ingestion.Models [TestMethod] public void TestValidateThrowsExceptionWhenLocaleIsNull() { - const string nullLocale = null; - var device = new Device(SdkName, SdkVersion, Model, OemName, OsName, OsVersion, nullLocale, TimeZoneOffset, ScreenSize, AppVersion, AppBuild); - Assert.ThrowsException(() => device.Validate()); - } - - /// - /// Verify that Validate method throws ValidationException when screenSize == null. - /// - [TestMethod] - public void TestValidateThrowsExceptionWhenScreenSizeIsNull() - { - const string nullScreenSize = null; - var device = new Device(SdkName, SdkVersion, Model, OemName, OsName, OsVersion, Locale, TimeZoneOffset, nullScreenSize, AppVersion, AppBuild); + var device = new Device + { + SdkName = SdkName, + SdkVersion = SdkVersion, + OsName = OsName, + OsVersion = OsVersion, + Locale = null, + TimeZoneOffset = TimeZoneOffset, + AppVersion = AppVersion, + AppBuild = AppBuild, + Model = Model, + ScreenSize = ScreenSize, + OemName = OemName + }; Assert.ThrowsException(() => device.Validate()); } @@ -162,8 +187,20 @@ namespace Microsoft.AppCenter.Test.Ingestion.Models [TestMethod] public void TestValidateThrowsExceptionWhenAppVersionIsNull() { - const string nullAppVersion = null; - var device = new Device(SdkName, SdkVersion, Model, OemName, OsName, OsVersion, Locale, TimeZoneOffset, ScreenSize, nullAppVersion, AppBuild); + var device = new Device + { + SdkName = SdkName, + SdkVersion = SdkVersion, + OsName = OsName, + OsVersion = OsVersion, + Locale = Locale, + TimeZoneOffset = TimeZoneOffset, + AppVersion = null, + AppBuild = AppBuild, + Model = Model, + ScreenSize = ScreenSize, + OemName = OemName + }; Assert.ThrowsException(() => device.Validate()); } @@ -173,8 +210,20 @@ namespace Microsoft.AppCenter.Test.Ingestion.Models [TestMethod] public void TestValidateThrowsExceptionWhenAppBuildnIsNull() { - const string nullAppBuild = null; - var device = new Device(SdkName, SdkVersion, Model, OemName, OsName, OsVersion, Locale, TimeZoneOffset, ScreenSize, AppVersion, nullAppBuild); + var device = new Device + { + SdkName = SdkName, + SdkVersion = SdkVersion, + OsName = OsName, + OsVersion = OsVersion, + Locale = Locale, + TimeZoneOffset = TimeZoneOffset, + AppVersion = AppVersion, + AppBuild = null, + Model = Model, + ScreenSize = ScreenSize, + OemName = OemName + }; Assert.ThrowsException(() => device.Validate()); } } diff --git a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/LogWithPropertiesTest.cs b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/LogWithPropertiesTest.cs index 822a7790f..f615de38c 100644 --- a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/LogWithPropertiesTest.cs +++ b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/LogWithPropertiesTest.cs @@ -35,6 +35,6 @@ namespace Microsoft.AppCenter.Test.Windows.Ingestion.Models { public TestLogWithProperties() { } public TestLogWithProperties(DateTime? timestamp, Device device, Guid? sid = default(Guid?), IDictionary properties = default(IDictionary)) - : base(timestamp, device, sid, properties) { } + : base(device, timestamp, sid, properties) { } } } diff --git a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/StartServiceLogTest.cs b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/StartServiceLogTest.cs index 156cfe18f..a15041643 100644 --- a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/StartServiceLogTest.cs +++ b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/StartServiceLogTest.cs @@ -38,8 +38,8 @@ namespace Microsoft.AppCenter.Test.Windows.Ingestion.Models public void CheckInitialValuesWithServices() { var servicesNames = new List { "Service0", "Service1", "Service2" }; - var log = new StartServiceLog(null, null, servicesNames); - + var log = new StartServiceLog(); + log.Services = servicesNames; Assert.IsNotNull(log.Services); foreach (var serviceName in log.Services) { @@ -73,21 +73,5 @@ namespace Microsoft.AppCenter.Test.Windows.Ingestion.Models Assert.IsTrue(retrievedLog.Services.Contains(serviceName)); } } - - /// - /// Validate that log is not valid with nullable 'Services' - /// - [TestMethod] - public void ValidateStartServiceLog() - { - var log = new StartServiceLog - { - Services = null, - Device = new DeviceInformationHelper().GetDeviceInformationAsync().RunNotAsync(), - Timestamp = DateTime.Now - }; - - Assert.ThrowsException((Action)log.Validate); - } } } diff --git a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/TestLog.cs b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/TestLog.cs index 4020bf379..c5553f01b 100644 --- a/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/TestLog.cs +++ b/Tests/Microsoft.AppCenter.Test.Windows/Ingestion/Models/TestLog.cs @@ -9,6 +9,6 @@ namespace Microsoft.AppCenter.Test.Windows.Ingestion.Models { public TestLog() {} public TestLog(DateTime? timestamp, Device device, System.Guid? sid = default(System.Guid?)) - : base(timestamp, device, sid){ } + : base(device, timestamp, sid) { } } }