public static void AreEqual(IDataItem expected, IDataItem actual)
        {
            var expectedNames = expected.GetFieldNames();

            Assert.IsNotNull(expectedNames, Resources.NullExpectedFieldNames);

            var actualNames = actual.GetFieldNames();

            Assert.IsNotNull(actualNames, Resources.NullActualFieldNames);

            CollectionAssert.AreEquivalent(expectedNames.ToArray(), actualNames.ToArray(), Resources.InvalidFieldNames);

            foreach (var fieldName in expectedNames)
            {
                var expectedValue = expected.GetValue(fieldName);
                var actualValue   = actual.GetValue(fieldName);

                if (expectedValue is IDataItem)
                {
                    Assert.IsTrue(actualValue is IDataItem, Resources.InvalidDataItemFieldValue);
                    AreEqual((IDataItem)expectedValue, (IDataItem)actualValue);
                }
                else
                {
                    if (expectedValue != null && expectedValue.GetType().IsValueType)
                    {
                        // Try change type to expected for value types to workaround DocumentDB deserialization issue (Int32 are becoming Int64)
                        try { actualValue = Convert.ChangeType(actualValue, expectedValue.GetType(), CultureInfo.InvariantCulture); } catch { }
                    }
                    Assert.AreEqual(expectedValue, actualValue, Resources.InvalidFieldValue);
                }
            }
        }
Пример #2
0
        public void KeepsOnlyIntersectionIncludingRightNumberOfDuplicatesAndExcludesBadItems()
        {
            DataBinding[] bindings = new DataBinding[] {
                new DataBinding(0, null)
            };
            IDataProvider[] providers = new IDataProvider[] {
                Mocks.StrictMock <IDataProvider>(),
                Mocks.StrictMock <IDataProvider>(),
                Mocks.StrictMock <IDataProvider>()
            };

            using (Mocks.Record())
            {
                IDataItem badItem = Mocks.StrictMock <IDataItem>();
                Expect.Call(badItem.GetValue(bindings[0])).Throw(new InvalidOperationException("Test exception"));

                Expect.Call(providers[0].GetItems(bindings, true)).Return(new IDataItem[] {
                    new ScalarDataItem <int>(1, null, false),
                    new ScalarDataItem <int>(2, null, true),
                    new ScalarDataItem <int>(1, null, false),
                    new ScalarDataItem <int>(3, null, false),
                    new ScalarDataItem <int>(6, null, false),
                });

                Expect.Call(providers[1].GetItems(bindings, true)).Return(new IDataItem[] {
                    new ScalarDataItem <int>(1, null, false),
                    new ScalarDataItem <int>(1, null, false),
                    badItem,
                    new ScalarDataItem <int>(2, null, true),
                    new ScalarDataItem <int>(6, null, false),
                    new ScalarDataItem <int>(1, null, false),
                    new ScalarDataItem <int>(4, null, false),
                });

                Expect.Call(providers[2].GetItems(bindings, true)).Return(new IDataItem[] {
                    new ScalarDataItem <int>(1, null, false),
                    new ScalarDataItem <int>(1, null, false),
                    new ScalarDataItem <int>(5, null, false),
                    new ScalarDataItem <int>(3, null, false),
                    new ScalarDataItem <int>(2, null, true)
                });
            }

            using (Mocks.Playback())
            {
                List <IDataItem> items = new List <IDataItem>(IntersectionMergeStrategy.Instance.Merge(providers, bindings, true));
                Assert.Count(3, items);

                Assert.AreEqual(1, items[0].GetValue(bindings[0]));
                Assert.IsFalse(items[0].IsDynamic);

                Assert.AreEqual(1, items[1].GetValue(bindings[0]));
                Assert.IsFalse(items[1].IsDynamic);

                Assert.AreEqual(2, items[2].GetValue(bindings[0]));
                Assert.IsTrue(items[2].IsDynamic);
            }
        }
Пример #3
0
            protected override object GetValueImpl(DataBinding binding)
            {
                if (binding == null)
                {
                    throw new ArgumentNullException("binding");
                }

                return(item.GetValue(source.TranslateBinding(binding)));
            }
        protected static void VerifyDataItem(IReadOnlyDictionary<string, object> expected, IDataItem actual)
        {
            var actualColumns = actual.GetFieldNames().ToList();

            foreach (var key in expected.Keys)
            {
                Assert.IsTrue(actualColumns.Contains(key), TestResources.PropertyMissingFormat, key);
                Assert.AreEqual(expected[key], actual.GetValue(key), TestResources.InvalidPropertyValueFormat, key);
            }
        }
Пример #5
0
        protected static void VerifyDataItem(IReadOnlyDictionary <string, object> expected, IDataItem actual)
        {
            var actualColumns = actual.GetFieldNames().ToList();

            foreach (var key in expected.Keys)
            {
                Assert.IsTrue(actualColumns.Contains(key), TestResources.PropertyMissingFormat, key);
                Assert.AreEqual(expected[key], actual.GetValue(key), TestResources.InvalidPropertyValueFormat, key);
            }
        }
 private static object[] GetValues(IDataItem item, ICollection <DataBinding> bindings)
 {
     try
     {
         return(GenericCollectionUtils.ConvertAllToArray <DataBinding, object>(bindings, delegate(DataBinding binding)
         {
             return item.GetValue(binding);
         }));
     }
     catch
     {
         return(null);
     }
 }
Пример #7
0
        public void GetValueCallsRowsGetValueWithTheBinding()
        {
            IDataItem item = Mocks.StrictMock <IDataItem>();

            DataBinding binding = new DataBinding(0, null);

            using (Mocks.Record())
            {
                Expect.Call(item.GetValue(binding)).Return(42);
            }

            using (Mocks.Playback())
            {
                BoundDataAccessor accessor = new BoundDataAccessor(binding);
                Assert.AreEqual(42, accessor.GetValue(item));
            }
        }
Пример #8
0
        public void EliminatesDuplicatesAndIncludesBadItems()
        {
            DataBinding[] bindings = new DataBinding[] {
                new DataBinding(0, null)
            };
            IDataProvider[] providers = new IDataProvider[] {
                Mocks.StrictMock <IDataProvider>(),
                Mocks.StrictMock <IDataProvider>(),
                Mocks.StrictMock <IDataProvider>()
            };

            using (Mocks.Record())
            {
                IDataItem badItem = Mocks.StrictMock <IDataItem>();
                Expect.Call(badItem.GetValue(bindings[0])).Repeat.Twice().Throw(new InvalidOperationException("Test exception"));

                Expect.Call(providers[0].GetItems(bindings, true)).Return(new IDataItem[] {
                    new ScalarDataItem <int>(1, null, true),
                    new ScalarDataItem <int>(1, null, true),
                    new ScalarDataItem <int>(2, null, false),
                });

                Expect.Call(providers[1].GetItems(bindings, true)).Return(EmptyArray <IDataItem> .Instance);

                Expect.Call(providers[2].GetItems(bindings, true)).Return(new IDataItem[] {
                    badItem,
                    new ScalarDataItem <int>(3, null, true),
                    new ScalarDataItem <int>(2, null, true)
                });
            }

            using (Mocks.Playback())
            {
                List <IDataItem> items = new List <IDataItem>(UnionMergeStrategy.Instance.Merge(providers, bindings, true));
                Assert.Count(4, items);

                Assert.AreEqual(1, items[0].GetValue(bindings[0]));
                Assert.IsTrue(items[0].IsDynamic);
                Assert.AreEqual(2, items[1].GetValue(bindings[0]));
                Assert.IsFalse(items[1].IsDynamic);
                Assert.Throws <InvalidOperationException>(delegate { items[2].GetValue(bindings[0]); });
                Assert.AreEqual(3, items[3].GetValue(bindings[0]));
                Assert.IsTrue(items[3].IsDynamic);
            }
        }
        private static int GetDataItemHash(IDataItem dataItem)
        {
            var result = 0;

            foreach (var fieldName in dataItem.GetFieldNames())
            {
                var value = dataItem.GetValue(fieldName);
                if (value == null)
                    continue;

                result ^= fieldName.GetHashCode() ^ (
                    value is IDataItem
                        ? GetDataItemHash((IDataItem)value)
                        : GetValueHashCode(value));
            }

            return result;
        }
Пример #10
0
        private static object Bind(IDataAccessor accessor, IDataItem bindingItem, IFormatter formatter)
        {
            try
            {
                return(accessor.GetValue(bindingItem));
            }
            catch (DataBindingException ex)
            {
                using (TestLog.Failures.BeginSection("Data binding failure"))
                {
                    if (!string.IsNullOrEmpty(ex.Message))
                    {
                        TestLog.Failures.WriteLine(ex.Message);
                    }

                    bool first = true;
                    foreach (DataBinding binding in bindingItem.GetBindingsForInformalDescription())
                    {
                        if (first)
                        {
                            TestLog.Failures.Write("\nAvailable data bindings for this item:\n\n");
                            first = false;
                        }

                        using (TestLog.Failures.BeginMarker(Marker.Label))
                        {
                            TestLog.Failures.Write(binding);
                            TestLog.Failures.Write(": ");
                        }

                        TestLog.Failures.WriteLine(bindingItem.GetValue(binding));
                    }

                    if (first)
                    {
                        TestLog.Failures.Write("\nThis item does not appear to provide any data bindings.\n");
                    }
                }

                throw new SilentTestException(TestOutcome.Error);
            }
        }
Пример #11
0
        public static void AreEqual(IDataItem expected, IDataItem actual)
        {
            var expectedNames = expected.GetFieldNames();

            Assert.IsNotNull(expectedNames, Resources.NullExpectedFieldNames);

            var actualNames = actual.GetFieldNames();

            Assert.IsNotNull(actualNames, Resources.NullActualFieldNames);

            CollectionAssert.AreEquivalent(expectedNames.ToArray(), actualNames.ToArray(), Resources.InvalidFieldNames);

            foreach (var fieldName in expectedNames)
            {
                var expectedValue = expected.GetValue(fieldName);
                var actualValue   = actual.GetValue(fieldName);

                CheckItem(expectedValue, actualValue);
            }
        }
Пример #12
0
        private static int GetDataItemHash(IDataItem dataItem)
        {
            var result = 0;

            foreach (var fieldName in dataItem.GetFieldNames())
            {
                var value = dataItem.GetValue(fieldName);
                if (value == null)
                {
                    continue;
                }

                result ^= fieldName.GetHashCode() ^ (
                    value is IDataItem
                        ? GetDataItemHash((IDataItem)value)
                        : GetValueHashCode(value));
            }

            return(result);
        }
Пример #13
0
 /// <inheritdoc />
 protected override object GetValueImpl(IDataItem item)
 {
     object value = item.GetValue(binding);
     return value;
 }
Пример #14
0
        /// <inheritdoc />
        protected override object GetValueImpl(IDataItem item)
        {
            object value = item.GetValue(binding);

            return(value);
        }
Пример #15
0
 public object GetValue(string fieldName)
 {
     return(dataItem.GetValue(fieldName));
 }
Пример #16
0
 public virtual object GetValue(string fieldName)
 {
     Guard.NotNull("fieldName", fieldName);
     return(dataItem.GetValue(fieldName));
 }
Пример #17
0
        private static object Bind(IDataAccessor accessor, IDataItem bindingItem, IFormatter formatter)
        {
            try
            {
                return accessor.GetValue(bindingItem);
            }
            catch (DataBindingException ex)
            {
                using (TestLog.Failures.BeginSection("Data binding failure"))
                {
                    if (!string.IsNullOrEmpty(ex.Message))
                        TestLog.Failures.WriteLine(ex.Message);

                    bool first = true;
                    foreach (DataBinding binding in bindingItem.GetBindingsForInformalDescription())
                    {
                        if (first)
                        {
                            TestLog.Failures.Write("\nAvailable data bindings for this item:\n\n");
                            first = false;
                        }

                        using (TestLog.Failures.BeginMarker(Marker.Label))
                        {
                            TestLog.Failures.Write(binding);
                            TestLog.Failures.Write(": ");
                        }

                        TestLog.Failures.WriteLine(bindingItem.GetValue(binding));
                    }

                    if (first)
                        TestLog.Failures.Write("\nThis item does not appear to provide any data bindings.\n");
                }

                throw new SilentTestException(TestOutcome.Error);
            }
        }
 private static object[] GetValues(IDataItem item, ICollection<DataBinding> bindings)
 {
     try
     {
         return GenericCollectionUtils.ConvertAllToArray<DataBinding, object>(bindings, delegate(DataBinding binding)
         {
             return item.GetValue(binding);
         });
     }
     catch
     {
         return null;
     }
 }