Beispiel #1
0
        private async Task CreatePerson(Func <EmailManager, string, string, Task <Person> > createPerson)
        {
            const int    id    = 17;
            const string name  = "First Last";
            const string email = "*****@*****.**";
            var          mocks = CommandBuilderMocks.SetupFor(new CommandSetup {
                { EmailManager.CreatePersonCommand, (mockCmd, record) => mockCmd.Setup(cmd => cmd.ExecuteScalarAsync(AnyCancellationToken)).ReturnsWithDelay(id).Callback(record) },
            });

            var person = await createPerson(CreateEmailManager(mocks.ProviderFactory.Object), name, email);

            mocks.Connection.VerifySet(conn => conn.ConnectionString = connectionString);
            mocks.Commands[EmailManager.CreatePersonCommand].Verify(command => command.ExecuteScalarAsync(AnyCancellationToken), Times.Once());

            Assert.Collection(
                mocks.Executions[EmailManager.CreatePersonCommand],
                execution =>
            {
                Assert.Equal(execution["@FullName"], name);
                Assert.Equal(execution["@Email"], email);
            }
                );
            Assert.Equal(id, person.Id);
            Assert.Equal(name, person.FullName);
            Assert.Equal(email, person.Email);
            Assert.False(person.OptOut);
        }
        private async Task GetPersonById(Func <EmailManager, int, Task <Person> > getPerson)
        {
            const int    id     = 17;
            const string name   = "First Last";
            const string email  = "*****@*****.**";
            const bool   optOut = false;
            var          mocks  = CommandBuilderMocks.SetupFor(new CommandSetup
            {
                {
                    EmailManager.FindPersonByIdCommand,
                    (mockCmd, record) => mockCmd.Setup(cmd => cmd.PublicExecuteDbDataReaderAsync(System.Data.CommandBehavior.Default, AnyCancellationToken)).ReturnsWithDelay(new FakeDataReader(new[] {
                        new Dictionary <string, object>
                        {
                            { "FullName", name },
                            { "Email", email },
                            { "OptOut", optOut },
                        }
                    })).Callback(record)
                },
            });

            var person = await getPerson(CreateEmailManager(mocks.ProviderFactory.Object), id);

            mocks.Connection.VerifySet(conn => conn.ConnectionString = connectionString);
            mocks.Commands[EmailManager.FindPersonByIdCommand].Verify(command => command.PublicExecuteDbDataReaderAsync(System.Data.CommandBehavior.Default, AnyCancellationToken), Times.Once());

            Assert.Equal(1, mocks.Executions[EmailManager.FindPersonByIdCommand].Count);
            Assert.Equal(mocks.Executions[EmailManager.FindPersonByIdCommand][0]["@Id"], id);
            Assert.Equal(id, person.Id);
            Assert.Equal(name, person.FullName);
            Assert.Equal(email, person.Email);
            Assert.Equal(false, person.OptOut);
        }
Beispiel #3
0
        private async Task GetOptedIn(Func <EmailManager, Task <IEnumerable <Person> > > optOut)
        {
            const string email1  = "*****@*****.**";
            const int    id1     = 17;
            const string name1   = "First Last";
            const bool   optOut1 = false;
            const string email2  = "*****@*****.**";
            const int    id2     = 212;
            const string name2   = "Other Person";
            const bool   optOut2 = true;
            var          mocks   = CommandBuilderMocks.SetupFor(new CommandSetup
            {
                {
                    EmailManager.GetOptedInCommand,
                    (mockCmd, record) => mockCmd.Setup(cmd => cmd.PublicExecuteDbDataReaderAsync(System.Data.CommandBehavior.Default, AnyCancellationToken)).ReturnsWithDelay(new FakeDataReader(new[] {
                        new Dictionary <string, object>
                        {
                            { "Id", id1 },
                            { "FullName", name1 },
                            { "Email", email1 },
                            { "OptOut", optOut1 },
                        },
                        new Dictionary <string, object>
                        {
                            { "Id", id2 },
                            { "FullName", name2 },
                            { "Email", email2 },
                            { "OptOut", optOut2 },
                        },
                    })).Callback(record)
                },
            });

            var people = await optOut(CreateEmailManager(mocks.ProviderFactory.Object));

            mocks.Connection.VerifySet(conn => conn.ConnectionString = connectionString);
            mocks.Commands[EmailManager.GetOptedInCommand].Verify(command => command.PublicExecuteDbDataReaderAsync(System.Data.CommandBehavior.Default, AnyCancellationToken), Times.Once());

            Assert.Collection(
                mocks.Executions[EmailManager.GetOptedInCommand],
                execution => {
                Assert.Empty(execution);
            });

            var results = people.ToArray();

            Assert.Equal(id1, results[0].Id);
            Assert.Equal(name1, results[0].FullName);
            Assert.Equal(email1, results[0].Email);
            Assert.Equal(optOut1, results[0].OptOut);
            Assert.Equal(id2, results[1].Id);
            Assert.Equal(name2, results[1].FullName);
            Assert.Equal(email2, results[1].Email);
            Assert.Equal(optOut2, results[1].OptOut);
        }
Beispiel #4
0
        public void ApplyOrderedParametersOnlyIfExactLengthMatch()
        {
            var builder =
                new CommandBuilderFactory(commandText: @"SELECT 1")
            {
                { "@param", DbType.String },
                { "@param2", DbType.String, p => p.Size = 128 }
            }.Build();
            var mocks = CommandBuilderMocks.SetupFor(new CommandSetup {
                { builder, (mockCmd, record) => { } },
            });

            Assert.Throws <InvalidOperationException>(() => builder.BuildFrom(mocks.Connection.Object, new[] { "param" }));
            Assert.Throws <InvalidOperationException>(() => builder.BuildFrom(mocks.Connection.Object, new[] { "param", "param2", "param3" }));
        }
        private async Task OptOutByEmail(Func <EmailManager, string, Task <int> > optOut)
        {
            const string email           = "*****@*****.**";
            const int    expectedResults = 2;
            var          mocks           = CommandBuilderMocks.SetupFor(new CommandSetup {
                { EmailManager.OptOutCommand, (mockCmd, record) => mockCmd.Setup(cmd => cmd.ExecuteNonQueryAsync(AnyCancellationToken)).ReturnsWithDelay(expectedResults).Callback(record) },
            });

            var actualResults = await optOut(CreateEmailManager(mocks.ProviderFactory.Object), email);

            mocks.Connection.VerifySet(conn => conn.ConnectionString = connectionString);
            mocks.Commands[EmailManager.OptOutCommand].Verify(command => command.ExecuteNonQueryAsync(AnyCancellationToken), Times.Once());

            Assert.Equal(1, mocks.Executions[EmailManager.OptOutCommand].Count);
            Assert.Equal(mocks.Executions[EmailManager.OptOutCommand][0]["@Email"], email);

            Assert.Equal(expectedResults, actualResults);
        }
Beispiel #6
0
        public void ApplyOrderedParameters()
        {
            var builder =
                new CommandBuilderFactory(commandText: @"SELECT 1")
            {
                { "@param", DbType.String },
                { "@param2", DbType.String, p => p.Size = 128 }
            }.Build();
            var mocks = CommandBuilderMocks.SetupFor(new CommandSetup {
                { builder, (mockCmd, record) => { } },
            });

            var command = builder.BuildFrom(mocks.Connection.Object, new[] { "param", "param2" });

            Assert.Collection(command.Parameters.OfType <DbParameter>(),
                              param => { Assert.Equal("@param", param.ParameterName); Assert.Equal("param", param.Value); },
                              param2 => { Assert.Equal("@param2", param2.ParameterName); Assert.Equal("param2", param2.Value); }
                              );
        }
Beispiel #7
0
        public void AllowCustomizedUnnamedNamedParameters()
        {
            var builder =
                new CommandBuilderFactory(commandText: @"SELECT 1")
            {
                { DbType.String },
                { DbType.String, p => p.Size = 128 }
            }.Build();
            var mocks = CommandBuilderMocks.SetupFor(new CommandSetup {
                { builder, (mockCmd, record) => { } },
            });

            var command = builder.BuildFrom(mocks.Connection.Object);

            Assert.Collection(command.Parameters.OfType <DbParameter>(),
                              param => { Assert.Equal(DbType.String, param.DbType); Assert.NotEqual(128, param.Size); },
                              param2 => { Assert.Equal(DbType.String, param2.DbType); Assert.Equal(128, param2.Size); }
                              );
        }
        public void SaveExcludedTest()
        {
            const int entryId = 18;
            var       mocks   = CommandBuilderMocks.SetupFor(new CommandSetup
            {
                { SqlLogRecorder.cmdCreateEntry, (mockCmd, record) => mockCmd.Setup(cmd => cmd.ExecuteScalarAsync(AnyCancellationToken)).ReturnsWithDelay(entryId).Callback(record) },
                { SqlLogRecorder.cmdCreateIndex, (mockCmd, record) => mockCmd.Setup(cmd => cmd.ExecuteNonQueryAsync(AnyCancellationToken)).ReturnsWithDelay(1).Callback(record) },
            });

            var target = (ILogRecorder) new SqlLogRecorder(connectionString, Severity.Debug, new[] { "Test" }, mocks.ProviderFactory.Object);
            var log    = new LogEntry
            {
                Timestamp = DateTime.Parse("2016-05-21"),
                Severity  = Severity.Notice,
                Message   = "Basic Test",
                Data      = { { "Test", "Item" } },
                Indexes   = { { "Index1", "Value" }, { "Index2", "AnotherValue" } }
            };

            target.Save(log).Wait();


            mocks.Connection.VerifySet(conn => conn.ConnectionString = connectionString);
            mocks.Commands[SqlLogRecorder.cmdCreateEntry].Verify(command => command.ExecuteScalarAsync(AnyCancellationToken), Times.Once());
            mocks.Commands[SqlLogRecorder.cmdCreateIndex].Verify(command => command.ExecuteNonQueryAsync(AnyCancellationToken), Times.Exactly(2));

            Assert.AreEqual(1, mocks.Executions[SqlLogRecorder.cmdCreateEntry].Count);
            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateEntry][0]["@timestamp"], log.Timestamp);
            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateEntry][0]["@severity"], log.Severity.ToString());
            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateEntry][0]["@message"], log.Message);
            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateEntry][0]["@data"], "{}");
            Assert.IsInstanceOfType(mocks.Executions[SqlLogRecorder.cmdCreateEntry][0]["@exception"], typeof(DBNull));

            Assert.AreEqual(2, mocks.Executions[SqlLogRecorder.cmdCreateIndex].Count);
            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateIndex][0]["@entryid"], entryId);
            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateIndex][0]["@key"], log.Indexes.AllKeys.First());
            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateIndex][0]["@value"], log.Indexes[log.Indexes.AllKeys.First()]);

            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateIndex][1]["@entryid"], entryId);
            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateIndex][1]["@key"], log.Indexes.AllKeys.Skip(1).First());
            Assert.AreEqual(mocks.Executions[SqlLogRecorder.cmdCreateIndex][1]["@value"], log.Indexes[log.Indexes.AllKeys.Skip(1).First()]);
        }