public PartBuilder()
 {
     _part             = BuildValid();
     _part.Name        = RandomValueGen.GetRandomString();
     _part.Description = RandomValueGen.GetRandomString();
     _part.Price       = RandomValueGen.GetRandomInt();
 }
Exemple #2
0
        public void Spool_PurgesUnsendableEmailOlderThanConfiguredKeepSentForValue()
        {
            // test setup
            var email = CreateSubstituteEmail();
            var func  = EmailGeneratorWith(email);
            var dto   = EmailBuilder.BuildRandomWithRecipient();
            var ctx   = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build();
            var deps  = FakeEmailSpoolerDependenciesBuilder.Create()
                        .WithDbContext(ctx)
                        .WithEmailGenerator(func)
                        .Build();
            var dayVal = RandomValueGen.GetRandomInt(30, 40);

            deps.EmailSpoolerConfig.PurgeMessageWithAgeInDays.ReturnsForAnyArgs(ci => dayVal);
            dto.Sent         = false;
            dto.SendAttempts = deps.EmailSpoolerConfig.MaxSendAttempts;
            dto.LastModified = DateTime.Now.AddDays(-dayVal).AddSeconds(-10);

            // pre-conditions
            Assert.AreEqual(1, ctx.Emails.Count());


            // execute test
            using (var spooler = new EmailSpooler(deps))
            {
                spooler.Spool();
            }

            // test result
            email.DidNotReceive().Send();
            Assert.AreEqual(0, ctx.Emails.Count());
            ctx.Received().SaveChanges();
        }
Exemple #3
0
        public void Spool_WhenOneEmailNotSentButSendAttemptsExceeded_DoesNotSendMail()
        {
            // test setup
            var email       = CreateSubstituteEmail();
            var func        = EmailGeneratorWith(email);
            var dto         = EmailBuilder.Create().WithRandomProps().WithRandomRecipient().WithRandomAttachment(2).Build();
            var maxAttempts = RandomValueGen.GetRandomInt(3, 6);

            dto.EmailRecipients.First().IsBCC = true;
            dto.EmailRecipients.First().IsCC  = true;
            dto.SendAt       = DateTime.Now.AddYears(-1);
            dto.SendAttempts = maxAttempts + 1;

            var ctx  = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build();
            var deps = FakeEmailSpoolerDependenciesBuilder.Create()
                       .WithDbContext(ctx)
                       .WithEmailGenerator(func)
                       .Build();

            deps.EmailSpoolerConfig.MaxSendAttempts.ReturnsForAnyArgs(ci => maxAttempts);
            // pre-conditions
            Assert.AreEqual(2, dto.EmailAttachments.Count(a => a.Data.Length > 0 && a.Name.Length > 0));

            // execute test
            using (var spooler = new EmailSpooler(deps))
            {
                spooler.Spool();
            }

            // test result
            email.DidNotReceive().Send();
        }
        public void Continue_With_ShouldContinueNextTaskWithResultFromPriorOneWhereApplicable_Level2()
        {
            //---------------Set up test pack-------------------
            var sut      = Create() as TaskRunner;
            var barrier1 = new Barrier(2);
            var barrier2 = new Barrier(2);
            var expected = RandomValueGen.GetRandomInt(1, 10);
            var task1    = sut.Run(() =>
            {
                barrier1.SignalAndWait();
                return(expected);
            });

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var resultTask = sut.Continue(task1).With(t =>
            {
                barrier2.SignalAndWait();
                return(t.Result);
            });

            //---------------Test Result -----------------------
            barrier1.SignalAndWait();
            barrier2.SignalAndWait();

            Assert.AreEqual(expected, resultTask.Result);
        }
        public void Continue_With_Fluently()
        {
            //---------------Set up test pack-------------------
            var sut      = Create() as TaskRunner;
            var expected = RandomValueGen.GetRandomInt(1, 10);

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var resultTask = sut.Run(() => { Thread.Sleep(150); })
                             .Using(sut).ContinueWith(t =>
            {
                Thread.Sleep(100);
                return(expected);
            })
                             .Using(sut).ContinueWith(t => t.Result)
                             .Using(sut).ContinueWith(t =>
            {
                Thread.Sleep(250);
                Console.WriteLine("last task");
                return(t.Result);
            });

            //---------------Test Result -----------------------
            Console.WriteLine("About to wait on result");

            Assert.AreEqual(expected, resultTask.Result);
        }
        public void ImmediateTaskRunner_ContinuationWithResultTest()
        {
            //---------------Set up test pack-------------------
            var sut = ImmediateTaskRunnerBuilder
                      .Create()
                      .WithSupportForTaskOfType <int>()
                      .WithSupportForContinuationOfType <int, int>()
                      .Build();
            var firstCalled  = false;
            var secondCalled = false;
            var expected     = RandomValueGen.GetRandomInt(1, 10);
            var result       = -1;

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            sut.Run(() =>
            {
                firstCalled = true;
                return(expected);
            })
            .Using(sut).ContinueWith(t =>
            {
                secondCalled = true;
                result       = expected;
                return(result);
            });

            //---------------Test Result -----------------------
            Assert.IsTrue(firstCalled);
            Assert.IsTrue(secondCalled);
            Assert.AreEqual(expected, result);
        }
 public CarPartBuilder()
 {
     _carPart          = BuildValid();
     _carPart.Quantity = RandomValueGen.GetRandomInt();
     _carPart.CarId    = RandomValueGen.GetRandomGuid();
     _carPart.PartId   = RandomValueGen.GetRandomGuid();
 }
        public void SetPropertyValue_WhenHaveSubInterface_ShouldSetOnShimThroughToOriginal()
        {
            //--------------- Arrange -------------------
            var expected = RandomValueGen.GetRandomInt();
            var original = RandomValueGen.GetAnother(expected);
            var data     = new Dictionary <string, object>()
            {
                { "HaveId", new Dictionary <string, object>()
                  {
                      { "Id", original }
                  } }
            };
            var sut = Create(data, typeof(INested));
            var sub = sut.GetPropertyValue("HaveId") as IHaveId;

            //--------------- Assume ----------------
            Expect(sub).Not.To.Be.Null();

            //--------------- Act ----------------------
            sub.Id = expected;

            //--------------- Assert -----------------------
            var cast = data["HaveId"] as Dictionary <string, object>;

            Expect(cast).Not.To.Be.Null();
            Expect(cast)
            .To.Contain.Key("Id")
            .With.Value(expected);
        }
Exemple #9
0
        private EmailConfiguration CreateRandomEmailConfig()
        {
            var host = RandomValueGen.GetRandomString();
            var port = RandomValueGen.GetRandomInt(20, 128);
            var user = RandomValueGen.GetRandomString();
            var pass = RandomValueGen.GetRandomString();
            var ssl  = RandomValueGen.GetRandomBoolean();

            return(new EmailConfiguration(host, port, user, pass, ssl));
        }
        private NameValueCollection CreateRandomSettings()
        {
            var           result  = new NameValueCollection();
            Func <string> randInt = () => RandomValueGen.GetRandomInt().ToString();

            result["MaxSendAttempts"]           = randInt();
            result["BackoffIntervalInMinutes"]  = randInt();
            result["BackoffMultiplier"]         = randInt();
            result["PurgeMessageWithAgeInDays"] = randInt();
            return(result);
        }
Exemple #11
0
        private static IEmailConfiguration CreateRandomFallbackEmailConfiguration()
        {
            var randomConfig = Substitute.For <IEmailConfiguration>();
            var host         = RandomValueGen.GetRandomString();

            randomConfig.Host.Returns(host);
            randomConfig.Port.Returns(RandomValueGen.GetRandomInt(25, 1024));
            randomConfig.UserName.Returns(RandomValueGen.GetRandomString());
            randomConfig.Password.Returns(RandomValueGen.GetRandomString());
            return(randomConfig);
        }
        public void Test_GetRandomIntTwice_ShouldReturnDiffValue()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var randomInt1 = RandomValueGen.GetRandomInt();
            var randomInt2 = RandomValueGen.GetRandomInt();

            //---------------Test Result -----------------------
            Assert.AreNotEqual(randomInt1, randomInt2);
        }
        public void Test_GetRandomInt_WithNoMaxAndMin_ShouldReturnRandomInt()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var randomInt = RandomValueGen.GetRandomInt();

            //---------------Test Result -----------------------
            Assert.GreaterOrEqual(randomInt, GetMin <int>(), "Should return value greater than min");
            Assert.LessOrEqual(randomInt, GetMax <int>(), "Should return value less than min");
        }
Exemple #14
0
        public void Level_WhenSet_ShouldReturnSameValue()
        {
            //---------------Set up test pack-------------------
            var edgeCaseImplementationResult = CreateEdgeCaseImplementationResult();
            var expected = RandomValueGen.GetRandomInt(0, 50);

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            edgeCaseImplementationResult.Level = expected;
            //---------------Test Result -----------------------
            Assert.That(edgeCaseImplementationResult.Level, Is.EqualTo(expected));
        }
        public void Test_GetRandomInt_WithMaxAndMinAsMaxAndMin_ShouldReturnRandomInt()
        {
            //---------------Set up test pack-------------------
            var min = GetMin <int>();
            var max = GetMax <int>();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var randomInt = RandomValueGen.GetRandomInt(min, max);

            //---------------Test Result -----------------------
            Assert.GreaterOrEqual(randomInt, min, "Should be greater than min");
            Assert.LessOrEqual(randomInt, max, "should be less than max");
        }
        public override SystemStatsConfigBuilder WithRandomProps()
        {
            var allDrives = DriveInfo.GetDrives();
            var primary   = allDrives.First();
            var secondary = allDrives.Skip(1).FirstOrDefault() ?? primary;

            // probably normally 5 / 30 / 300 in prod
            return(WithProp(o => o.ShortTermCpuWindowSeconds.Returns(RandomValueGen.GetRandomInt(1, 5)))
                   .WithProp(o => o.MediumTermCpuWindowSeconds.Returns(RandomValueGen.GetRandomInt(10, 20)))
                   .WithProp(o => o.LongTermCpuWindowSeconds.Returns(RandomValueGen.GetRandomInt(3, 60)))
                   .WithProp(o => o.OSDrive.Returns(primary.Name.Substring(0, 1)))
                   .WithProp(o => o.DataDrive.Returns(secondary.Name.Substring(0, 1))));
        }
        public void Test_GetRandomInt_WithMaxLTMin_ShouldReturnMin()
        {
            //---------------Set up test pack-------------------
            var min = GetMax <int>() - 100;
            var max = GetMax <int>() - 500;

            //---------------Assert Precondition----------------
            Assert.Less(max, min);
            //---------------Execute Test ----------------------
            var randomInt = RandomValueGen.GetRandomInt(min, max);

            //---------------Test Result -----------------------
            Assert.AreEqual(randomInt, min);
        }
Exemple #18
0
        public void ShouldHaveMaxLengthOf_WhenPropertyHasMaxLengthAttributeWithDifferentValue_ShouldThrow()
        {
            //---------------Set up test pack-------------------
            var sut      = Create();
            var expected = RandomValueGen.GetRandomInt(10, 20);

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var ex = Assert.Throws <AssertionException>(() => sut.ShouldHaveMaxLengthOf(expected, o => o.PropertyWithMaxLength));

            //---------------Test Result -----------------------
            StringAssert.Contains("incorrect maxlength", ex.Message.ToLower());
        }
        public void SetPropertyValue_GivenUnkownPropertyName_ShouldThrow_PropertyNotFoundException()
        {
            //--------------- Arrange -------------------
            var sut = Create(null, typeof(IHaveId));

            //--------------- Assume ----------------

            //--------------- Act ----------------------
            Expect(() =>
                   sut.SetPropertyValue(RandomValueGen.GetRandomString(10, 20), RandomValueGen.GetRandomInt())
                   )
            .To.Throw <PropertyNotFoundException>();

            //--------------- Assert -----------------------
        }
Exemple #20
0
        public void Construct_GivenPort_ShouldSetMessage()
        {
            //---------------Set up test pack-------------------
            var port     = RandomValueGen.GetRandomInt();
            var expected = "Can't listen on specified port '" + port + "': probably already in use?";

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var sut    = new PortUnavailableException(port);
            var result = sut.Message;

            //---------------Test Result -----------------------
            Assert.AreEqual(expected, result);
        }
Exemple #21
0
        public void Test_ToString_WhenHasIntPKProp_AndValueSet_ShouldReturnTheSingleValueAsAToString()
        {
            //---------------Set up test pack-------------------
            BOWithIntPKProp.LoadClassDefs();
            var myBO          = new BOWithIntPKProp();
            int pk1Prop1Value = RandomValueGen.GetRandomInt();

            myBO.PK1Prop1 = pk1Prop1Value;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(myBO.PK1Prop1);
            //---------------Execute Test ----------------------
            string actualToString = myBO.ToString();

            //---------------Test Result -----------------------
            Assert.AreEqual(pk1Prop1Value.ToString(), actualToString);
        }
        public void Construct_GivenFuncWhichReturnsT_ShouldProvideTValueToSecondFuncDuringDisposal()
        {
            //---------------Set up test pack-------------------
            var initial = RandomValueGen.GetRandomInt();
            var output  = -1;

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            using (new AutoResetter <int>(() => initial, v => output = v))
            {
            }

            //---------------Test Result -----------------------
            Expect(initial).To.Equal(output);
        }
        public void Construct_RegularConstructor_ShouldUseAppSettings(string propertyName)
        {
            //---------------Set up test pack-------------------
            var expected = RandomValueGen.GetRandomInt();

            ConfigurationManager.AppSettings[propertyName] = expected.ToString();
            //var expected = Convert.ToInt32(ConfigurationManager.AppSettings[propertyName]);

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var sut    = Create(Substitute.For <ISimpleLogger>());
            var result = sut.GetPropertyValue <int>(propertyName);

            //---------------Test Result -----------------------
            Assert.AreEqual(expected, result);
        }
        public void ConnectionTimeout_ShouldReturnValueOfUnderlyingProperty()
        {
            //---------------Set up test pack-------------------
            var connection = Substitute.For <IDbConnection>();
            var expected   = RandomValueGen.GetRandomInt(1, 100);

            connection.ConnectionTimeout.Returns(expected);
            var sut = Create(connection);

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = sut.ConnectionTimeout;

            //---------------Test Result -----------------------
            Assert.AreEqual(expected, result);
        }
        public void SetPropertyValue_GivenKnownPropertyNameAndConvertableValue_ShouldSetPropertyWithCorrectType()
        {
            //--------------- Arrange -------------------
            var data     = new Dictionary <string, object>();
            var expected = RandomValueGen.GetRandomInt();
            var sut      = Create(data, typeof(IHaveId));

            //--------------- Assume ----------------

            //--------------- Act ----------------------
            sut.SetPropertyValue("Id", expected.ToString());

            //--------------- Assert -----------------------
            Expect(data)
            .To.Contain.Key("Id")
            .With.Value(expected);
        }
        public void CallThrough_WhenConstructedWith_FuzzyTrue_AndMethodNameCaseMismatch_ShoulCallThrough()
        {
            //--------------- Arrange -------------------
            var toWrap = new Cow();
            var pitch  = RandomValueGen.GetRandomString();
            var count  = RandomValueGen.GetRandomInt();
            var sut    = Create(toWrap, true);

            //--------------- Assume ----------------

            //--------------- Act ----------------------
            sut.CallThrough("mOo", new object[] { count, pitch });

            //--------------- Assert -----------------------
            Expect(toWrap.LastCount).To.Equal(count);
            Expect(toWrap.LastPitch).To.Equal(pitch);
        }
        public void Test_DockInForm_WithWidth_ShouldSetCollapsibleMenuWidth()
        {
            //---------------Set up test pack-------------------
            IFormHabanero formHabanero = GetControlFactory().CreateForm();

            formHabanero.MinimumSize = new Size(RandomValueGen.GetRandomInt(250, 300), 100);
            IMainMenuHabanero mainMenu = CreateControl();
            int menuWidth = RandomValueGen.GetRandomInt(100, 200);

            //---------------Assert Precondition----------------
            Assert.AreEqual(0, formHabanero.Controls.Count);
            //---------------Execute Test ----------------------
            mainMenu.DockInForm(formHabanero, menuWidth);
            //---------------Test Result -----------------------
            var collapsiblePanelGroupControl = TestUtil.AssertIsInstanceOf <ICollapsiblePanelGroupControl>(mainMenu);

            Assert.AreEqual(menuWidth, collapsiblePanelGroupControl.Width);
        }
        public void CreateNotStartedFor_GivenAction_ShouldReturnUnstartedTaskWhichCanBeStartedAtTheWhimsyOfOne()
        {
            //---------------Set up test pack-------------------
            var sut      = Create();
            var expected = RandomValueGen.GetRandomInt(10, 100);
            var result   = 0;

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var task = sut.CreateNotStartedFor(() => { result = expected; });

            //---------------Test Result -----------------------
            Assert.IsInstanceOf <Task>(task);
            task.Start();
            task.Wait();
            Assert.AreEqual(expected, result);
        }
        public void Construct_ShouldSetMessage()
        {
            //--------------- Arrange -------------------
            var methodInfo = GetType().GetMethod(nameof(SomeMethod));
            var count      = RandomValueGen.GetRandomInt();

            //--------------- Assume ----------------

            //--------------- Act ----------------------
            var result = new ParameterCountMismatchException(
                count, methodInfo
                );

            //--------------- Assert -----------------------
            Expect(result.Message)
            .To.Contain($"{count} parameters were provided for method {methodInfo?.DeclaringType?.Name ?? "(unknown type)"}.{methodInfo?.Name ?? "(unknown method)"}")
            .Then("but it requires 2 parameters");
        }
        public void GetPropertyValue_WhenPropertyDataExistsAndMatchesType_ShouldReturnValue()
        {
            //--------------- Arrange -------------------
            var expected = RandomValueGen.GetRandomInt();
            var data     = new Dictionary <string, object>()
            {
                { "Id", expected }
            };
            var sut = Create(data, typeof(IHaveId));

            //--------------- Assume ----------------

            //--------------- Act ----------------------
            var result = sut.GetPropertyValue("Id");

            //--------------- Assert -----------------------
            Expect(result).To.Equal(expected);
        }