Пример #1
0
        public void op_Verify_IRepository_whenExists()
        {
            var obj = new RepositoryDeleteUrn <RandomObject>
            {
                Record1 =
                {
                    Key = AlphaDecimal.Random()
                }
            };

            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.Insert(obj.Record1))
            .Returns(obj.Record1)
            .Verifiable();
            repository
            .Setup(x => x.Delete(obj.Record1.Urn))
            .Returns(true)
            .Verifiable();
            repository
            .Setup(x => x.Exists(obj.Record1.Urn))
            .Returns(true)
            .Verifiable();

            Assert.Throws <RepositoryTestException>(() => obj.Verify(repository.Object));

            repository.VerifyAll();
        }
        public void op_Verify_IRepository_whenKeyIsDifferent()
        {
            var obj = new RepositorySelectUrn <RandomObject>
            {
                Record1 =
                {
                    Key = AlphaDecimal.Random()
                }
            };

            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.Insert(obj.Record1))
            .Returns(obj.Record1)
            .Verifiable();

            var record = new Mock <IRecord <RandomObject> >();

            record
            .SetupGet(x => x.Key)
            .Returns(AlphaDecimal.Random());
            record
            .SetupGet(x => x.Urn)
            .Returns(obj.Record1.Urn);
            repository
            .Setup(x => x.Select(obj.Record1.Urn))
            .Returns(record.Object)
            .Verifiable();

            Assert.Throws <RepositoryTestException>(() => obj.Verify(repository.Object));

            repository.VerifyAll();
        }
        public void op_Verify_IRepository()
        {
            var key = AlphaDecimal.Random();

            var obj = new RepositoryModifiedSinceKey <RandomObject>
            {
                Record1 =
                {
                    Key = key
                }
            };

            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.Insert(obj.Record1))
            .Returns(obj.Record1)
            .Verifiable();
            repository
            .Setup(x => x.ModifiedSince(key, DateTime.MaxValue))
            .Returns(false)
            .Verifiable();
            repository
            .Setup(x => x.ModifiedSince(key, DateTime.MinValue))
            .Returns(true)
            .Verifiable();

            obj.Verify(repository.Object);

            repository.VerifyAll();
        }
        public void op_ToString_whenRecordNullXml()
        {
            var key    = AlphaDecimal.Random();
            var record = new Record <string>
            {
                Cacheability = "public",
                Created      = new DateTime(1999, 12, 31, 01, 00, 00, 00),
                Etag         = "\"xyz\"",
                Expiration   = "P1D",
                Key          = key,
                Modified     = new DateTime(2001, 12, 31, 01, 00, 00, 00),
                Status       = 200,
                Urn          = "urn://example.com/abc",
                Value        = null
            };

            var obj = new RecordFile(record);

            var expected = new StringBuilder();

            expected.AppendLine("urn: urn://example.com/abc");
            expected.AppendLine("key: " + key);
            expected.AppendLine("etag: \"xyz\"");
            expected.AppendLine("created: 1999-12-31T01:00:00Z");
            expected.AppendLine("modified: 2001-12-31T01:00:00Z");
            expected.AppendLine("cacheability: public");
            expected.AppendLine("expiration: P1D");
            expected.AppendLine("status: 200");
            expected.AppendLine(string.Empty);

            var actual = obj.ToString();

            Assert.Equal(expected.ToString(), actual);
        }
        protected override void OnVerify(IRepository <T> repository)
        {
            if (null == repository)
            {
                throw new ArgumentNullException("repository");
            }

            repository.Insert(Record1);

            RepositoryException expected = null;

            try
            {
                Record2.Key = AlphaDecimal.Random();
                repository.Update(Record2);
            }
            catch (RepositoryException exception)
            {
                expected = exception;
            }
            catch (Exception exception)
            {
                throw new RepositoryTestException(Resources.Repository_UnexpectedException_ExceptionMessage, exception);
            }

            if (null == expected)
            {
                throw new RepositoryTestException(Resources.Repository_ExpectExceptionWhenRecordKeyNotFound_UnitTestExceptionMessage.FormatWith("Update"));
            }
        }
Пример #6
0
        public static TsvDataSheet Download(AbsoluteUri location)
        {
            if (null == location)
            {
                throw new ArgumentNullException("location");
            }

            TsvDataSheet tsv = null;

            var request = WebRequest.Create((Uri)location);

            using (var response = request.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    if (null != stream)
                    {
                        using (var reader = new StreamReader(stream))
                        {
#if NET20
                            var file = new FileInfo(StringExtensionMethods.FormatWith("{0}.tsv", AlphaDecimal.Random()));
                            FileInfoExtensionMethods.Create(file, reader.ReadToEnd());
#else
                            var file = new FileInfo("{0}.tsv".FormatWith(AlphaDecimal.Random()));
                            file.Create(reader.ReadToEnd());
#endif

                            tsv = new TsvDataSheet(file);
                        }
                    }
                }
            }

            return(tsv);
        }
        public void op_Verify_IRepository()
        {
            var key = AlphaDecimal.Random();

            var obj = new RepositorySelectKey <RandomObject>
            {
                Record1 =
                {
                    Key = key
                }
            };

            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.Insert(obj.Record1))
            .Returns(obj.Record1)
            .Verifiable();
            repository
            .Setup(x => x.Select(key))
            .Returns(obj.Record1)
            .Verifiable();

            obj.Verify(repository.Object);

            repository.VerifyAll();
        }
        public void op_Verify_IRepository_whenNullIsReturned()
        {
            var key = AlphaDecimal.Random();

            var obj = new RepositorySelectKey <RandomObject>
            {
                Record1 =
                {
                    Key = key
                }
            };

            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.Insert(obj.Record1))
            .Returns(obj.Record1)
            .Verifiable();
            repository
            .Setup(x => x.Select(key))
            .Returns(null as IRecord <RandomObject>)
            .Verifiable();

            Assert.Throws <RepositoryTestException>(() => obj.Verify(repository.Object));

            repository.VerifyAll();
        }
        public void op_Verify_IRepository_whenUrnIsDifferent()
        {
            var key = AlphaDecimal.Random();

            var obj = new RepositoryToUrnKey <RandomObject>
            {
                Record1 =
                {
                    Key = key
                }
            };

            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.Insert(obj.Record1))
            .Returns(obj.Record1)
            .Verifiable();
            repository
            .Setup(x => x.ToUrn(key))
            .Returns("urn://example.com/" + Guid.NewGuid())
            .Verifiable();

            Assert.Throws <RepositoryTestException>(() => obj.Verify(repository.Object));

            repository.VerifyAll();
        }
        public void op_Verify_IRepository_whenFalse()
        {
            var key = AlphaDecimal.Random();

            var obj = new RepositoryExistsKey <RandomObject>
            {
                Record1 =
                {
                    Key = key
                }
            };

            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.Insert(obj.Record1))
            .Returns(obj.Record1)
            .Verifiable();
            repository
            .Setup(x => x.Exists(key))
            .Returns(false)
            .Verifiable();

            Assert.Throws <RepositoryTestException>(() => obj.Verify(repository.Object));

            repository.VerifyAll();
        }
        public void op_Verify_IRepository_whenModifiedSinceMaxDate()
        {
            var key = AlphaDecimal.Random();

            var obj = new RepositoryModifiedSinceKey <RandomObject>
            {
                Record1 =
                {
                    Key = key
                }
            };

            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.Insert(obj.Record1))
            .Returns(obj.Record1)
            .Verifiable();
            repository
            .Setup(x => x.ModifiedSince(key, DateTime.MaxValue))
            .Returns(true)
            .Verifiable();

            Assert.Throws <RepositoryTestException>(() => obj.Verify(repository.Object));

            repository.VerifyAll();
        }
        public void op_Verify_IRepository()
        {
            var obj = new RepositoryToKeyUrn <RandomObject>
            {
                Record1 =
                {
                    Key = AlphaDecimal.Random()
                }
            };

            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.Insert(obj.Record1))
            .Returns(obj.Record1)
            .Verifiable();
            repository
            .Setup(x => x.ToKey(obj.Record1.Urn))
            .Returns(obj.Record1.Key)
            .Verifiable();

            obj.Verify(repository.Object);

            repository.VerifyAll();
        }
        private static T AddToCacheOfT <T>(ICacheCollection cache)
        {
            var obj = default(T);

            cache.Add(AlphaDecimal.Random(), obj);

            return(obj);
        }
        private static object AddToCache(ICacheCollection cache)
        {
            var obj = new object();

            cache.Add(AlphaDecimal.Random(), obj);

            return(obj);
        }
Пример #15
0
        public void op_Extract_FileInfo_DirectoryInfoMissing()
        {
            using (var temp = new TempDirectory())
            {
                var source      = new FileInfo("example.txt.gz");
                var destination = temp.Info.ToDirectory(AlphaDecimal.Random());

                Assert.Throws <DirectoryNotFoundException>(() => GZip.Extract(source, destination));
            }
        }
        public void op_Clear()
        {
            var obj = new MemoryCacheCollection
            {
                { AlphaDecimal.Random(), new object() }
            };

            Assert.Equal(1, obj.Count);
            obj.Clear();
            Assert.Equal(0, obj.Count);
        }
Пример #17
0
        protected override void OnVerify(IRepository <T> repository)
        {
            if (null == repository)
            {
                throw new ArgumentNullException("repository");
            }

            if (repository.Match(AlphaDecimal.Random(), "\"{0}\"".FormatWith(Guid.NewGuid())))
            {
                throw new RepositoryTestException(Resources.Repository_Match_ReturnsTrue_ExceptionMessage);
            }
        }
        protected override void OnVerify(IRepository <T> repository)
        {
            if (null == repository)
            {
                throw new ArgumentNullException("repository");
            }

            if (repository.Delete(AlphaDecimal.Random()))
            {
                throw new RepositoryTestException(Resources.Repository_ExpectWhenDoesNotExist_ExceptionMessage.FormatWith("Delete", "false"));
            }
        }
        protected override void OnVerify(IRepository <T> repository)
        {
            if (null == repository)
            {
                throw new ArgumentNullException("repository");
            }

            if (repository.ModifiedSince(AlphaDecimal.Random(), DateTime.MinValue))
            {
                throw new RepositoryTestException(Resources.Repository_ModifiedSince_ReturnsTrue_ExceptionMessage);
            }
        }
Пример #20
0
        public void op_Extract_FileInfoMissing_DirectoryInfo()
        {
            using (var temp = new TempDirectory())
            {
                var source = new FileInfo(AlphaDecimal.Random());

                // ReSharper disable AccessToDisposedClosure
                Assert.Throws <FileNotFoundException>(() => GZip.Extract(source, temp.Info));

                // ReSharper restore AccessToDisposedClosure
            }
        }
Пример #21
0
        public void op_RenderAsHtml()
        {
            string expected = AlphaDecimal.Random();

            var view = new HtmlRepresentation();

            view.ViewBag.Message = expected;
            var html = view.RenderAsHtml();

            var actual = html.DocumentNode.SelectSingleNode("//p[@id='message']").InnerText;

            Assert.Equal(expected, actual);
        }
        public void op_Verify_IRepository_whenKeyIsReturned()
        {
            var repository = new Mock <IRepository <RandomObject> >();

            repository
            .Setup(x => x.ToKey(It.IsAny <AbsoluteUri>()))
            .Returns(AlphaDecimal.Random())
            .Verifiable();

            Assert.Throws <RepositoryTestException>(() => new RepositoryToKeyUrnNotFound <RandomObject>().Verify(repository.Object));

            repository.VerifyAll();
        }
        protected override void OnVerify(IRepository <T> repository)
        {
            if (null == repository)
            {
                throw new ArgumentNullException("repository");
            }

            var urn = repository.ToUrn(AlphaDecimal.Random());

            if (null != urn)
            {
                throw new RepositoryTestException(Resources.Repository_ExpectNullWhenRecordNotFound_ExceptionMessage.FormatWith("ToUrn", "key"));
            }
        }
Пример #24
0
        public ActionResult Post()
        {
            var key = AlphaDecimal.Random();

            HttpContext.Cache.Add(key,
                                  Request.Form,
                                  null,
                                  DateTime.UtcNow.AddMinutes(30),
                                  Cache.NoSlidingExpiration,
                                  CacheItemPriority.Default,
                                  null);

            return(new SeeOtherResult("/echo/{0}".FormatWith(key)));
        }
Пример #25
0
        public void prop_Key_set()
        {
            AlphaDecimal?value = AlphaDecimal.Random();

            var mock = new Mock <IRecord>();

            mock
            .SetupSet(x => x.Key = value)
            .Verifiable();

            mock.Object.Key = value;

            mock.VerifyAll();
        }
        public void op_ContainsKey_string_whenFalse()
        {
            var obj = new MemoryCacheCollection();

            try
            {
                obj[AlphaDecimal.Random()] = new object();

                Assert.False(obj.ContainsKey(AlphaDecimal.Random()));
            }
            finally
            {
                obj.Clear();
            }
        }
        public void prop_Count_get()
        {
            var obj = new MemoryCacheCollection();

            try
            {
                Assert.Equal(0, obj.Count);
                obj.Add(AlphaDecimal.Random(), new object());
                Assert.Equal(1, obj.Count);
            }
            finally
            {
                obj.Clear();
            }
        }
Пример #28
0
        public void op_Exists_AlphaDecimal()
        {
            var key = AlphaDecimal.Random();

            var mock = new Mock <IRepository <int> >();

            mock
            .Setup(x => x.Exists(key))
            .Returns(true)
            .Verifiable();

            Assert.True(mock.Object.Exists(key));

            mock.VerifyAll();
        }
Пример #29
0
        public void op_Match_AlphaDecimal_string()
        {
            var          key  = AlphaDecimal.Random();
            const string etag = "\"xyz\"";

            var mock = new Mock <IRepository <int> >();

            mock
            .Setup(x => x.Match(key, etag))
            .Returns(true)
            .Verifiable();

            Assert.True(mock.Object.Match(key, etag));

            mock.VerifyAll();
        }
Пример #30
0
        public void op_ModifiedSince_AlphaDecimal_DateTime()
        {
            var key   = AlphaDecimal.Random();
            var value = DateTime.UtcNow;

            var mock = new Mock <IRepository <int> >();

            mock
            .Setup(x => x.ModifiedSince(key, value))
            .Returns(true)
            .Verifiable();

            Assert.True(mock.Object.ModifiedSince(key, value));

            mock.VerifyAll();
        }