示例#1
0
        public void CreateSubstituteDataReader_GivenDataOnly_ShouldReturnDataReaderWithExpectedData()
        {
            //---------------Set up test pack-------------------
            var dataReaderData = new List <dynamic>
            {
                new { Id = Guid.NewGuid(), Name = RandomValueGenerator.CreateRandomString(10, 50) },
                new { Id = Guid.NewGuid(), Name = RandomValueGenerator.CreateRandomString(10, 50) },
                new { Id = Guid.NewGuid(), Name = RandomValueGenerator.CreateRandomString(10, 50) }
            };
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var dataReader = TestHelper.CreateSubstituteDataReader(dataReaderData);
            //---------------Test Result -----------------------
            var allFakeData = new List <FakeDataClass>();

            while (dataReader.Read())
            {
                var dataClass = new FakeDataClass
                {
                    Id   = dataReader.GetValue <Guid>(nameof(FakeDataClass.Id)),
                    Name = dataReader.GetValue <string>(nameof(FakeDataClass.Name)),
                };
                allFakeData.Add(dataClass);
            }

            allFakeData.Count.Should().Be(3);

            foreach (var currentData in dataReaderData)
            {
                var foundDataClass = allFakeData.FirstOrDefault(dataClass => dataClass.Id.Equals(currentData.Id));
                foundDataClass.Should().NotBeNull();
                foundDataClass?.Name.Should().Be(currentData.Name);
            }
        }
示例#2
0
 private FakeDataClass CreateRandomFakeDataClass()
 {
     return(new FakeDataClass
     {
         Id = Guid.NewGuid(),
         Name = RandomValueGenerator.CreateRandomString(10, 20),
         DbAliasName = RandomValueGenerator.CreateRandomString(30, 60)
     });
 }
 private FakeTestDataModel CreateRandomTestDataModel(Guid dataModelId = default)
 {
     return(new FakeTestDataModel
     {
         Id = dataModelId == Guid.Empty ? Guid.NewGuid() : dataModelId,
         Name = RandomValueGenerator.CreateRandomString(20, 40),
         Date = RandomValueGenerator.CreateRandomDate(),
         SomeField = RandomValueGenerator.CreateRandomString(30, 60)
     });
 }
示例#4
0
        public void CreateRandomString_GivenMinimumAndMaximumValues_ShouldGenerateStringWithLenBetweenGivenValues(int minimumLength, int maximumLength)
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var randomString = RandomValueGenerator.CreateRandomString(minimumLength, maximumLength);

            //---------------Test Result -----------------------
            randomString.Length.Should().BeGreaterOrEqualTo(minimumLength);
            randomString.Length.Should().BeLessOrEqualTo(maximumLength);
        }
示例#5
0
        public async Task GetAsync_GivenDataDoesNotExist_ShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            var cacheKey    = RandomValueGenerator.CreateRandomString(5, 10);
            var thuriaCache = new ThuriaCache <string>();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var cacheDataReturned = await thuriaCache.GetAsync(cacheKey);

            //---------------Test Result -----------------------
            cacheDataReturned.Should().BeNull();
        }
示例#6
0
        public async Task ExistsAsync_GivenDataDoesNotExist_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            var cacheKey    = RandomValueGenerator.CreateRandomString(5, 10);
            var thuriaCache = new ThuriaCache <string>();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var dataExist = await thuriaCache.ExistsAsync(cacheKey);

            //---------------Test Result -----------------------
            dataExist.Should().BeFalse();
        }
示例#7
0
        public void SetErrorResult_ShouldSetExpectedProperties()
        {
            //---------------Set up test pack-------------------
            var errorMessage = RandomValueGenerator.CreateRandomString(20, 40);
            var actionResult = new DbContextActionResult <object>();

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            actionResult.SetErrorResult(errorMessage);
            //---------------Test Result -----------------------
            actionResult.ActionResult.Should().Be(DbContextActionResult.Error);
            actionResult.Results.Should().BeNull();
            actionResult.ActionMessage.Trim().Should().Be($"Error: {errorMessage}");
            actionResult.Exception.Should().BeNull();
        }
示例#8
0
        public async Task ExistsAsync_GivenDataExists_And_NotExpired_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            var cacheKey    = RandomValueGenerator.CreateRandomString(5, 10);
            var cacheData   = RandomValueGenerator.CreateRandomString(20, 50);
            var thuriaCache = new ThuriaCache <string>();

            await thuriaCache.UpsertAsync(cacheKey, new ThuriaCacheData <string>(cacheData));

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var dataExist = await thuriaCache.ExistsAsync(cacheKey);

            //---------------Test Result -----------------------
            dataExist.Should().BeTrue();
        }
示例#9
0
        public async Task GetAsync_GivenDataExists_ShouldReturnData()
        {
            //---------------Set up test pack-------------------
            var cacheKey    = RandomValueGenerator.CreateRandomString(5, 10);
            var cacheData   = RandomValueGenerator.CreateRandomString(20, 50);
            var thuriaCache = new ThuriaCache <string>();

            await thuriaCache.UpsertAsync(cacheKey, new ThuriaCacheData <string>(cacheData));

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var cacheDataReturned = await thuriaCache.GetAsync(cacheKey);

            //---------------Test Result -----------------------
            cacheDataReturned.Should().NotBeNullOrWhiteSpace();
            cacheDataReturned.Should().Be(cacheData);
        }
示例#10
0
        public async Task PopulateAsync_GivenSourceColumnWithNoProperty_ShouldNotThrowException()
        {
            //---------------Set up test pack-------------------
            var sourceData = new Dictionary <string, object>
            {
                { "Id", Guid.NewGuid() },
                { "UnknownColumn", RandomValueGenerator.CreateRandomString(20, 30) },
            };
            var dataModelPopulateProvider = new SqlDataModelPopulateProvider();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var dataModel = await dataModelPopulateProvider.PopulateAsync <FakeTestDataModel>(sourceData, DbContextAction.Retrieve);

            //---------------Test Result -----------------------
            dataModel.Should().NotBeNull();
            dataModel.Id.Should().Be((Guid)sourceData["Id"]);
        }
示例#11
0
        public async Task Upsert_GivenValidData_And_SetCacheExpiryIsFalse_ShouldNotUpdateCacheExpiry()
        {
            //---------------Set up test pack-------------------
            DateTime?dataExpiry      = null;
            var      cacheKey        = RandomValueGenerator.CreateRandomString(5, 10);
            var      thuriaCacheData = Substitute.For <IThuriaCacheData <string> >();

            thuriaCacheData.Expiry = Arg.Do <DateTime>(time => dataExpiry = time);
            var thuriaCache = new ThuriaCache <string>();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var upsertResult = await thuriaCache.UpsertAsync(cacheKey, thuriaCacheData, false);

            //---------------Test Result -----------------------
            upsertResult.Should().BeTrue();
            dataExpiry.Should().BeNull();
        }
示例#12
0
        public async Task ExistsAsync_GivenDataExists_And_Expired_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            var cacheKey        = RandomValueGenerator.CreateRandomString(5, 10);
            var cacheData       = RandomValueGenerator.CreateRandomString(20, 50);
            var thuriaCache     = new ThuriaCache <string>();
            var thuriaCacheData = new ThuriaCacheData <string>(cacheData, DateTime.UtcNow.AddMilliseconds(10));

            await thuriaCache.UpsertAsync(cacheKey, thuriaCacheData, false);

            Thread.Sleep(200);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var dataExist = await thuriaCache.ExistsAsync(cacheKey);

            //---------------Test Result -----------------------
            dataExist.Should().BeFalse();
        }
示例#13
0
        public void CreateRandomString_GivenMultipleCalls_ShouldGenerateUniqueStrings(int numberOfStrings)
        {
            //---------------Set up test pack-------------------
            var generatedStrings = new List <string>();

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            for (int loopCount = 0; loopCount < numberOfStrings; loopCount++)
            {
                var randomString = RandomValueGenerator.CreateRandomString(1, 10);
                //---------------Test Result -----------------------
                if (generatedStrings.Contains(randomString))
                {
                    Console.WriteLine($"Duplicate string found: {randomString}");
                }
                generatedStrings.Add(randomString);
            }
        }
        public void WithDatabaseContextName_GivenName_ShouldBuildContextWithDatabaseContextName()
        {
            //---------------Set up test pack-------------------
            var dbContextName = RandomValueGenerator.CreateRandomString(10, 30);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var databaseContext = SqlDatabaseContextBuilder.Create
                                  .WithConnectionStringProvider(Substitute.For <IConnectionStringProvider>())
                                  .WithDatabaseConnectionProvider(Substitute.For <IDatabaseConnectionProvider>())
                                  .WithStatementBuildProvider(Substitute.For <IStatementBuildProvider>())
                                  .WithDataModelPopulateProvider(Substitute.For <IDataModelPopulateProvider>())
                                  .WithDatabaseContextName(dbContextName)
                                  .Build();

            //---------------Test Result -----------------------
            databaseContext.Should().NotBeNull();
            databaseContext.DbContextName.Should().Be(dbContextName);
        }
示例#15
0
        public async Task Upsert_GivenValidData_And_CacheDataDoesNotExist_ShouldAddCacheDataToCache_And_ReturnTrue()
        {
            //---------------Set up test pack-------------------
            var cacheKey        = RandomValueGenerator.CreateRandomString(5, 10);
            var cacheData       = RandomValueGenerator.CreateRandomString(20, 50);
            var thuriaCacheData = new ThuriaCacheData <string>(cacheData);
            var thuriaCache     = new ThuriaCache <string>();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var upsertResult = await thuriaCache.UpsertAsync(cacheKey, thuriaCacheData);

            //---------------Test Result -----------------------
            upsertResult.Should().BeTrue();

            var returnedData = await thuriaCache.GetAsync(cacheKey);

            returnedData.Should().Be(cacheData);
        }
示例#16
0
        public void CreateSubstituteDataReader_GivenData_And_AdditionalColumnValues_ShouldReturnDataReaderWithExpectedData()
        {
            //---------------Set up test pack-------------------
            var dataReaderData = new List <dynamic>
            {
                new { Id = Guid.NewGuid(), Name = RandomValueGenerator.CreateRandomString(10, 50) },
                new { Id = Guid.NewGuid(), Name = RandomValueGenerator.CreateRandomString(10, 50) },
                new { Id = Guid.NewGuid(), Name = RandomValueGenerator.CreateRandomString(10, 50) }
            };
            var additionalColumnValues = new Dictionary <string, object>
            {
                { "Sequence", RandomValueGenerator.CreateRandomInt(100, 500) },
                { "Amount", RandomValueGenerator.CreateRandomDecimal(1000, 5000) },
            };
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var dataReader = TestHelper.CreateSubstituteDataReader(dataReaderData, additionalColumnValues);
            //---------------Test Result -----------------------
            var allFakeData = new List <FakeDataClass>();

            while (dataReader.Read())
            {
                var dataClass = new FakeDataClass
                {
                    Id       = dataReader.GetValue <Guid>(nameof(FakeDataClass.Id)),
                    Name     = dataReader.GetValue <string>(nameof(FakeDataClass.Name)),
                    Sequence = dataReader.GetValue <int>(nameof(FakeDataClass.Sequence)),
                    Amount   = dataReader.GetValue <decimal>(nameof(FakeDataClass.Amount)),
                };
                allFakeData.Add(dataClass);
            }

            allFakeData.Count.Should().Be(3);

            foreach (var currentData in dataReaderData)
            {
                var foundDataClass = allFakeData.FirstOrDefault(dataClass => dataClass.Id.Equals(currentData.Id));
                foundDataClass.Should().NotBeNull();
                foundDataClass?.Name.Should().Be(currentData.Name);
                foundDataClass?.Sequence.Should().Be((int)additionalColumnValues["Sequence"]);
                foundDataClass?.Amount.Should().Be((decimal)additionalColumnValues["Amount"]);
            }
        }
示例#17
0
        public void HeliumConstructSqlQueryMessage_GivenValidMessage_ShouldHandleMessage()
        {
            //---------------Set up test pack-------------------
            var actorRef            = CreateActor();
            var heliumFakeDataModel = new HeliumFakeDataModel
            {
                Id          = Guid.NewGuid(),
                Description = RandomValueGenerator.CreateRandomString(1, 10),
                IsActive    = RandomValueGenerator.CreateRandomBoolean()
            };
            var sqlQueryMessage = new HeliumConstructSqlQueryMessage(HeliumAction.Insert, heliumFakeDataModel);

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            actorRef.Tell(sqlQueryMessage);
            //---------------Test Result -----------------------
            var resultMessage = ExpectMsg <HeliumConstructSqlQueryResultMessage>();

            resultMessage.Should().NotBeNull();
        }
        public void GetThuriaDataModelConditions_GivenDataModelWithNoConditions_ShouldReturnEmptyConditionList(DbContextAction DbContextAction)
        {
            //---------------Set up test pack-------------------
            var dataModel = new ThuriaPocoDataModel
            {
                Id           = Guid.NewGuid(),
                Name         = RandomValueGenerator.CreateRandomString(),
                Description  = RandomValueGenerator.CreateRandomString(),
                ModifiedDate = RandomValueGenerator.CreateRandomDate(),
                IsActive     = true
            };
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var allConditions = dataModel.GetThuriaDataModelConditions(DbContextAction);
            //---------------Test Result -----------------------
            var dataModelConditions = allConditions.ToList();

            dataModelConditions.Should().NotBeNull();
            dataModelConditions.Count.Should().Be(0);
        }
        public void BuildInsertStatement_GivenDataModel_ShouldBuildExpectedStatement()
        {
            //---------------Set up test pack-------------------
            FakeTestDataModel dataModel = new FakeTestDataModel
            {
                Id        = Guid.NewGuid(),
                Name      = RandomValueGenerator.CreateRandomString(10, 20),
                Date      = RandomValueGenerator.CreateRandomDate(),
                SomeField = RandomValueGenerator.CreateRandomString(15, 30)
            };
            var expectedStatement = "INSERT INTO [FakeTest] ([Id],[Name],[Date],[SomeFieldAlias]) " +
                                    $"VALUES ('{dataModel.Id}','{dataModel.Name}','{dataModel.Date}','{dataModel.SomeField}')";
            var buildProvider = CreateProvider();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            // ReSharper disable once ExpressionIsAlwaysNull
            var insertStatement = buildProvider.BuildInsertStatement(dataModel);

            //---------------Test Result -----------------------
            insertStatement.Trim().Should().Be(expectedStatement);
        }
示例#20
0
        public void HeliumConstructSqlQueryMessage_GivenValidMessage_ShouldHandleMessageAndReturnExpectedSqlQuery()
        {
            //---------------Set up test pack-------------------
            var heliumFakeDataModel = new HeliumFakeDataModel
            {
                Id          = Guid.NewGuid(),
                Description = RandomValueGenerator.CreateRandomString(1, 10)
            };
            var expectedSqlQuery = $"INSERT INTO [HeliumFake] ([Id],[Description]) VALUES ('{heliumFakeDataModel.Id}','{heliumFakeDataModel.Description}')";
            var actorRef         = CreateActor(InsertStatementBuilder.Create, ConditionBuilder.Create);
            var sqlQueryMessage  = new HeliumConstructSqlQueryMessage(HeliumAction.Insert, heliumFakeDataModel);

            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            actorRef.Tell(sqlQueryMessage);
            //---------------Test Result -----------------------
            var resultMessage = ExpectMsg <HeliumConstructSqlQueryResultMessage>();

            resultMessage.Should().NotBeNull();
            resultMessage.HeliumAction.Should().Be(sqlQueryMessage.HeliumAction);
            resultMessage.SqlQuery.Should().Be(expectedSqlQuery);
        }
示例#21
0
        public async Task PopulateAsync_GivenGenericType_ShouldCreateAndPopulateDataModelAsExpected()
        {
            //---------------Set up test pack-------------------
            var sourceData = new Dictionary <string, object>
            {
                { "Id", Guid.NewGuid() },
                { "Name", RandomValueGenerator.CreateRandomString(10, 30) },
                { "Date", RandomValueGenerator.CreateRandomDate() },
                { "SomeFieldAlias", RandomValueGenerator.CreateRandomString(30, 50) }
            };
            var dataModelPopulateProvider = new SqlDataModelPopulateProvider();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var dataModel = await dataModelPopulateProvider.PopulateAsync <FakeTestDataModel>(sourceData, DbContextAction.Retrieve);

            //---------------Test Result -----------------------
            dataModel.Should().NotBeNull();
            dataModel.Id.Should().Be((Guid)sourceData["Id"]);
            dataModel.Name.Should().Be((string)sourceData["Name"]);
            dataModel.Date.Should().Be((DateTime)sourceData["Date"]);
            dataModel.SomeField.Should().Be((string)sourceData["SomeFieldAlias"]);
        }
        public void Build_WithConnectionStringProvider_ShouldBuildConnectionWithConnectionString()
        {
            //---------------Set up test pack-------------------
            var connectionString         = RandomValueGenerator.CreateRandomString(20, 30);
            var connectionStringProvider = Substitute.For <IConnectionStringProvider>();

            connectionStringProvider.GetConnectionString(Arg.Any <string>())
            .Returns(connectionString);

            var dbConnectionProvider = Substitute.For <IDatabaseConnectionProvider>();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var databaseContext = SqlDatabaseContextBuilder.Create
                                  .WithConnectionStringProvider(connectionStringProvider)
                                  .WithDatabaseConnectionProvider(dbConnectionProvider)
                                  .WithStatementBuildProvider(Substitute.For <IStatementBuildProvider>())
                                  .WithDataModelPopulateProvider(Substitute.For <IDataModelPopulateProvider>())
                                  .Build();

            //---------------Test Result -----------------------
            databaseContext.ExecuteActionAsync(DbContextAction.Retrieve, new object());
            dbConnectionProvider.Received(1).GetConnection(Arg.Any <string>());
        }
示例#23
0
        public async Task Upsert_GivenValidData_And_CacheDataExists_ShouldUpdateCacheDataInCache_And_ReturnTrue()
        {
            //---------------Set up test pack-------------------
            var cacheKey         = RandomValueGenerator.CreateRandomString(5, 10);
            var cacheData1       = RandomValueGenerator.CreateRandomString(20, 50);
            var cacheData2       = RandomValueGenerator.CreateRandomString(20, 50);
            var thuriaCacheData1 = new ThuriaCacheData <string>(cacheData1);
            var thuriaCacheData2 = new ThuriaCacheData <string>(cacheData2);
            var thuriaCache      = new ThuriaCache <string>();
            var upsertResult1    = await thuriaCache.UpsertAsync(cacheKey, thuriaCacheData1);

            //---------------Assert Precondition----------------
            upsertResult1.Should().BeTrue();
            //---------------Execute Test ----------------------
            var upsertResult2 = await thuriaCache.UpsertAsync(cacheKey, thuriaCacheData2);

            //---------------Test Result -----------------------
            upsertResult2.Should().BeTrue();

            var returnedData = await thuriaCache.GetAsync(cacheKey);

            returnedData.Should().Be(cacheData2);
        }
        public void GetThuriaPopulatedRelationshipMetadata_GivenDataModelWithRelationships_ShouldReturnListOfRelationships()
        {
            //---------------Set up test pack-------------------
            var dataModel = new ThuriaTestDataModel
            {
                Id                   = Guid.NewGuid(),
                Name                 = RandomValueGenerator.CreateRandomString(),
                Description          = RandomValueGenerator.CreateRandomString(),
                ModifiedDate         = RandomValueGenerator.CreateRandomDate(),
                IsActive             = true,
                ForeignTestDataModel = new ThuriaForeignTestDataModel(),
                AllForeignTests      = new List <ThuriaForeignTestDataModel> {
                    new ThuriaForeignTestDataModel()
                }
            };
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var modelRelationships = dataModel.GetThuriaPopulatedRelationshipMetadata();
            //---------------Test Result -----------------------
            var thuriaRelationshipAttributes = modelRelationships.ToList();

            thuriaRelationshipAttributes.Should().NotBeNull();
            thuriaRelationshipAttributes.Count.Should().Be(2);
        }