private IModelService GetSQLiteDatabaseService(string loadFromPath) { if (!string.IsNullOrEmpty(loadFromPath)) { if (File.Exists(loadFromPath)) { var dbContextFactory = new DesignTimeDbContextFactory(); var args = new string[] { $"ConnectionStrings:DefaultConnection=Data Source={loadFromPath}" }; SQLiteDbContext context = dbContextFactory.CreateDbContext(args); // update context.Database.Migrate(); return(new FindSimilarSQLiteService(context)); } else { var dbContextFactory = new DesignTimeDbContextFactory(); var args = new string[] { $"ConnectionStrings:DefaultConnection=Data Source={loadFromPath}" }; SQLiteDbContext context = dbContextFactory.CreateDbContext(args); // create context.Database.Migrate(); return(new FindSimilarSQLiteService(context)); } } return(null); }
public void GetById_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); IProjectRequestAggregateRepository projectRequestAggregateRepo = new ProjectRequestAggregateRepository(dbContext); ICreateProjectCommand createProjectCommand = new CreateProjectCommand(dbContext, new ProjectValidator()); ISetLogFileUnprocessedCommand setLogFileUnprocessedCommand = Substitute.For <ISetLogFileUnprocessedCommand>(); ICreateProjectRequestAggregateCommand createProjectRequestAggregateCommand = new CreateProjectRequestAggregateCommand(dbContext, new ProjectRequestAggregateValidator(), new LogFileRepository(dbContext), setLogFileUnprocessedCommand); IDeleteProjectRequestAggregateCommand deleteProjectRequestAggregateCommand = new DeleteProjectRequestAggregateCommand(dbContext, projectRequestAggregateRepo, new LogFileRepository(dbContext), setLogFileUnprocessedCommand); // create the project ProjectModel projectA = DataHelper.CreateProjectModel(); createProjectCommand.Execute(projectA); // create the request aggregate record for ProjectA ProjectRequestAggregateModel projectRequestAggregate = DataHelper.CreateProjectRequestAggregateModel(); projectRequestAggregate.ProjectId = projectA.Id; createProjectRequestAggregateCommand.Execute(projectRequestAggregate); int id = projectRequestAggregate.Id; Assert.Greater(id, 0); // fetch the record ProjectRequestAggregateModel result = projectRequestAggregateRepo.GetById(id); Assert.IsNotNull(result); Assert.AreEqual(projectRequestAggregate.Id, id); Assert.AreEqual(projectRequestAggregate.RegularExpression, result.RegularExpression); Assert.AreEqual(projectRequestAggregate.AggregateTarget, result.AggregateTarget); } }
public static void TestSqlite() { string dbPath = "f:\\testdb.sqlite"; string connectString = "Data Source=f:\\testdb.sqlite;Pooling=true;FailIfMissing=false;"; if (File.Exists(dbPath)) { File.Delete(dbPath); } SqlDbContext db = new SQLiteDbContext(connectString); //SmartDb框架提供一个记录日志委托用来记录执行SQL及参数信息,ConsoleWriteInfo在控制台输出,大家可以根据自己需要自己定义方法传给DbHelper.logAction会自动进行调用 db.DbHelper.logAction = db.DbHelper.ConsoleWriteInfo; var sql = "create table UserInfo(UserId int not null,UserName varchar(50),Age int,Email varchar(50))"; db.ExecuteNoneQuery(sql, null); var dbTest = new DbTest(db); dbTest.DeleteAll(); dbTest.Insert(); dbTest.Delete(); dbTest.Update(); dbTest.Query(); dbTest.OrtherQuery(); dbTest.OrtherNoneQuery(); }
public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); // create the user UserModel user = DataHelper.CreateUserModel(); IUserRepository userRepo = new UserRepository(dbContext); IUserValidator userValidator = new UserValidator(userRepo); IPasswordProvider passwordProvider = new PasswordProvider(); ICreateUserCommand createUserCommand = new CreateUserCommand(dbContext, userValidator, passwordProvider); UserModel savedUser = createUserCommand.Execute(user.UserName, user.Password, user.Role); // reset the password string newPassword = Guid.NewGuid().ToString(); IUpdateUserPasswordCommand updateCommand = new UpdateUserPasswordCommand(dbContext, userRepo, passwordProvider); updateCommand.Execute(savedUser.UserName, newPassword); // now fetch it again and check the password is good savedUser = userRepo.GetByUserName(user.UserName); Assert.IsTrue(passwordProvider.CheckPassword(newPassword, savedUser.Password)); } }
public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents = null; using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents = W3CEnumerable.FromStream(logStream).ToList(); } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); // create the project first so we have one ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create the log file LogFileModel logFile = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile); // create the request batch ICreateRequestBatchCommand createRequestBatchCommand = new CreateRequestBatchCommand(dbContext, new RequestValidator()); createRequestBatchCommand.Execute(logFile.Id, logEvents); int rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Requests"); Assert.AreEqual(logEvents.Count, rowCount); } }
private static void BulkInsert(SQLiteDbContext db) { Stopwatch sw = new Stopwatch(); var user = db.Query <User>(x => x.Id > 0).Take(1).FirstOrDefault(); var dataTable = db.SqlQueryDataTable("select Name,Gender,Age,OpTime from Users LIMIT 1"); DataTable dtNew = dataTable.Copy(); dtNew.Clear(); //清楚数据 for (var i = 0; i < 100000; i++) { dtNew.Rows.Add(dataTable.Rows[0].ItemArray); //添加数据行 } sw.Reset(); sw.Start(); db.BulkInsert("Users", dtNew); sw.Stop(); Console.WriteLine("BulkInsert DataTable 耗时:" + sw.ElapsedMilliseconds); //var user = db.Query<User>(x => x.Id > 0).Take(1).FirstOrDefault(); var newList = new List <User>(); for (var i = 0; i < 100000; i++) { newList.Add(user); } sw.Reset(); sw.Start(); db.BulkInsert <User>("Users", newList); sw.Stop(); Console.WriteLine("BulkInsert List 耗时:" + sw.ElapsedMilliseconds); }
public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); IUserRepository userRepo = new UserRepository(dbContext); ICreateUserCommand createUserCommand = new CreateUserCommand(dbContext, new UserValidator(userRepo), new PasswordProvider()); IDeleteUserCommand deleteUserCommand = new DeleteUserCommand(dbContext); // create the user UserModel user = createUserCommand.Execute("test", Guid.NewGuid().ToString(), Roles.User); int rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Users"); Assert.AreEqual(1, rowCount); // run the delete command and check the end tables - should be 0 records deleteUserCommand.Execute(user.Id); rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Users"); Assert.AreEqual(0, rowCount); } }
public void GetAll_Integration_ReturnsSortedList() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); IUserRepository userRepo = new UserRepository(dbContext); ICreateUserCommand createUserCommand = new CreateUserCommand(dbContext, new UserValidator(userRepo), new PasswordProvider()); // create the users UserModel userB = DataHelper.CreateUserModel(); userB.UserName = "******"; createUserCommand.Execute(userB.UserName, userB.Password, userB.Role); UserModel userC = DataHelper.CreateUserModel(); userC.UserName = "******"; createUserCommand.Execute(userC.UserName, userC.Password, userC.Role); UserModel userA = DataHelper.CreateUserModel(); userA.UserName = "******"; createUserCommand.Execute(userA.UserName, userA.Password, userA.Role); List <UserModel> result = userRepo.GetAll().ToList(); Assert.AreEqual(3, result.Count); Assert.AreEqual(userA.UserName, result[0].UserName); Assert.AreEqual(userB.UserName, result[1].UserName); Assert.AreEqual(userC.UserName, result[2].UserName); } }
public void GetAll_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); IProjectRepository projectRepo = new ProjectRepository(dbContext); ProjectModel projectA = DataHelper.CreateProjectModel(); projectA.Name = "AAA"; ProjectModel projectB = DataHelper.CreateProjectModel(); projectB.Name = "BBB"; ProjectModel projectC = DataHelper.CreateProjectModel(); projectC.Name = "CCC"; DataHelper.InsertProjectModel(dbContext, projectA); DataHelper.InsertProjectModel(dbContext, projectC); DataHelper.InsertProjectModel(dbContext, projectB); List <ProjectModel> projects = projectRepo.GetAll().ToList(); Assert.AreEqual(3, projects.Count); Assert.AreEqual("AAA", projects[0].Name); Assert.AreEqual("BBB", projects[1].Name); Assert.AreEqual("CCC", projects[2].Name); } }
public void GetById_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); ILogFileRepository logFileRepo = new LogFileRepository(dbContext); // create the project ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create the log file record LogFileModel logFile = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile); int logFileId = dbContext.ExecuteScalar <int>("select last_insert_rowid()"); LogFileModel result = logFileRepo.GetById(logFileId); Assert.IsNotNull(result); Assert.AreEqual(logFile.FileHash, result.FileHash); } }
public void GetByHash_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); string fileHash = Guid.NewGuid().ToString(); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); ILogFileRepository logFileRepo = new LogFileRepository(dbContext); // create the project ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create the log file record LogFileModel logFile = DataHelper.CreateLogFileModel(); logFile.ProjectId = project.Id; logFile.FileHash = fileHash; DataHelper.InsertLogFileModel(dbContext, logFile); LogFileModel result = logFileRepo.GetByHash(project.Id, fileHash); Assert.IsNotNull(result); Assert.AreEqual(logFile.FileName, result.FileName); result = logFileRepo.GetByHash(0, fileHash); Assert.IsNull(result); } }
public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); IProjectValidator projectValidator = new ProjectValidator(); IProjectRequestAggregateValidator validator = new ProjectRequestAggregateValidator(); ISetLogFileUnprocessedCommand setLogFileUnprocessedCommand = Substitute.For <ISetLogFileUnprocessedCommand>(); ICreateProjectRequestAggregateCommand createProjectRequestAggregateCommand = new CreateProjectRequestAggregateCommand(dbContext, validator, new LogFileRepository(dbContext), setLogFileUnprocessedCommand); // create the project first so we have one ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create the request aggregate ProjectRequestAggregateModel projectRequestAggregate = DataHelper.CreateProjectRequestAggregateModel(); projectRequestAggregate.ProjectId = project.Id; createProjectRequestAggregateCommand.Execute(projectRequestAggregate); Assert.Greater(projectRequestAggregate.Id, 0); int rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM ProjectRequestAggregates"); Assert.Greater(rowCount, 0); ProjectRequestAggregateModel savedModel = dbContext.Query <ProjectRequestAggregateModel>("SELECT * FROM ProjectRequestAggregates WHERE Id = @Id", new { Id = projectRequestAggregate.Id }).Single(); Assert.AreEqual(projectRequestAggregate.RegularExpression, savedModel.RegularExpression); Assert.AreEqual(projectRequestAggregate.AggregateTarget, savedModel.AggregateTarget); } }
public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); // create the user UserModel user = DataHelper.CreateUserModel(); IUserRepository userRepo = new UserRepository(dbContext); IUserValidator userValidator = new UserValidator(userRepo); IPasswordProvider passwordProvider = new PasswordProvider(); ICreateUserCommand createUserCommand = new CreateUserCommand(dbContext, userValidator, passwordProvider); createUserCommand.Execute(user.UserName, user.Password, user.Role); UserModel savedUser = userRepo.GetByUserName(user.UserName); Assert.IsNotNull(savedUser); Assert.AreEqual(user.Role, savedUser.Role); Assert.IsTrue(passwordProvider.CheckPassword(user.Password, savedUser.Password)); } }
public static void Init() { using (var db = new SQLiteDbContext("SqlLiteName")) { //Add(db); //Query(db); BulkInsert(db); } }
public override IDbContext CreateDbContext() { // 直接用无参构造函数时会使用默认配置项 XFrameworkConnString var context = new SQLiteDbContext(connString) { IsDebug = base.IsDebug }; return(context); }
public static Raffle GetRaffle(int RaffleId) { using (var DbContext = new SQLiteDbContext()) { var raffle = DbContext.Raffles.Where(r => r.Id == RaffleId).FirstOrDefault(); // HACK: Lazy Loading and Include does not seem to be working, load the entries manually, research later. var entries = DbContext.RaffleEntries.Where(e => e.RaffleId == raffle.Id).ToList(); raffle.Entries = entries; return(raffle); } }
public static async Task FinishRaffle(int RaffleId, ulong UserId) { using (var DbContext = new SQLiteDbContext()) { var raffle = DbContext.Raffles.Find(RaffleId); raffle.WinnerId = UserId; await SaveTokens(UserId, raffle.TokenPrize); await DbContext.SaveChangesAsync(); } }
public static int GetTokens(ulong UserId) { using (var DbContext = new SQLiteDbContext()) { var token = DbContext.PlayTokens.Find(UserId); if (token == null) { return(0); } return(token.Amount); } }
private static void Add(SQLiteDbContext db) { var user = new User { Age = 1, Gender = 1, Name = "dfaf", OpTime = DateTime.Now }; db.Add <User>(user); }
public static async Task JoinRaffle(int RaffleId, ulong UserId) { RaffleEntries entry = new RaffleEntries { RaffleId = RaffleId, UserId = UserId }; using (var DbContext = new SQLiteDbContext()) { DbContext.RaffleEntries.Add(entry); await DbContext.SaveChangesAsync(); } }
public void GetByUriStemAggregate_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents = null; string uriStemAggregate = Guid.NewGuid().ToString(); int expectedCount = new Random().Next(3, 7); using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents = W3CEnumerable.FromStream(logStream).ToList(); } for (int i = 0; i < expectedCount; i++) { logEvents[i].cs_uri_stem = uriStemAggregate; } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); IRequestRepository requestRepo = new RequestRepository(dbContext); ICreateRequestBatchCommand createRequestBatchCommand = new CreateRequestBatchCommand(dbContext, new RequestValidator()); // create the project ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); ProjectModel project2 = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project2); // create log file and request records for each LogFileModel logFile = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile); createRequestBatchCommand.Execute(logFile.Id, logEvents); LogFileModel logFile2 = DataHelper.CreateLogFileModel(project2.Id); DataHelper.InsertLogFileModel(dbContext, logFile2); createRequestBatchCommand.Execute(logFile2.Id, logEvents); IEnumerable <RequestModel> result = requestRepo.GetByUriStemAggregate(project.Id, uriStemAggregate); Assert.IsNotNull(result); Assert.AreEqual(expectedCount, result.Count()); foreach (RequestModel rm in result) { Assert.AreEqual(logFile.Id, rm.LogFileId); } } }
public void GetPageLoadTimes_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents = new List <W3CEvent>(); logEvents.Add(CreateW3CEvent("PageA", 17)); logEvents.Add(CreateW3CEvent("PageA", 13)); logEvents.Add(CreateW3CEvent("PageA", 21)); logEvents.Add(CreateW3CEvent("PageA", 9)); logEvents.Add(CreateW3CEvent("PageA", 40)); logEvents.Add(CreateW3CEvent("PageB", 1000)); logEvents.Add(CreateW3CEvent("PageB", 3000)); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); ICreateRequestBatchCommand createRequestBatchCommand = new CreateRequestBatchCommand(dbContext, new RequestValidator()); IRequestRepository requestRepo = new RequestRepository(dbContext); // create the project ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create the log file record LogFileModel logFile = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile); // create the requests createRequestBatchCommand.Execute(logFile.Id, logEvents); // update all requests so the aggregate is different to the stem string sql = "UPDATE Requests SET UriStemAggregate = UriStem || '__'"; dbContext.ExecuteNonQuery(sql); IEnumerable <RequestPageLoadTimeModel> result = requestRepo.GetPageLoadTimes(project.Id); Assert.AreEqual(2, result.Count()); RequestPageLoadTimeModel pageAResult = result.Where(x => x.UriStemAggregate == "PageA__").SingleOrDefault(); Assert.IsNotNull(pageAResult); Assert.AreEqual(5, pageAResult.RequestCount); Assert.AreEqual(20, pageAResult.AvgTimeTakenMilliseconds); RequestPageLoadTimeModel pageBResult = result.Where(x => x.UriStemAggregate == "PageB__").SingleOrDefault(); Assert.IsNotNull(pageBResult); Assert.AreEqual(2, pageBResult.RequestCount); Assert.AreEqual(2000, pageBResult.AvgTimeTakenMilliseconds); } }
public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents = null; using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents = W3CEnumerable.FromStream(logStream).ToList(); } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); ICreateProjectCommand createProjectCommand = new CreateProjectCommand(dbContext, new ProjectValidator()); ICreateRequestBatchCommand createRequestBatchCommand = new CreateRequestBatchCommand(dbContext, new RequestValidator()); IDeleteLogFileCommand deleteLogFileCommand = new DeleteLogFileCommand(dbContext); // create the project first so we have one ProjectModel project = DataHelper.CreateProjectModel(); // create 2 the log files LogFileModel logFile1 = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile1); LogFileModel logFile2 = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile2); // create the request batch createRequestBatchCommand.Execute(logFile1.Id, logEvents); createRequestBatchCommand.Execute(logFile2.Id, logEvents); int rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Requests WHERE LogFileId = @LogFileId", new { LogFileId = logFile1.Id }); Assert.AreEqual(logEvents.Count, rowCount); // run the delete command deleteLogFileCommand.Execute(logFile1.Id); // there should be no requests for logFile1, but requests for logFile2 should still exist rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Requests WHERE LogFileId = @LogFileId", new { LogFileId = logFile1.Id }); Assert.AreEqual(0, rowCount); rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM Requests WHERE LogFileId = @LogFileId", new { LogFileId = logFile2.Id }); Assert.AreEqual(logEvents.Count, rowCount); rowCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM LogFiles"); Assert.AreEqual(1, rowCount); } }
public static async Task <int> AddRaffle(string Name, DateTime End, int Prize) { Raffle raffle = new Raffle { CreateDate = DateTime.Now, DoneDate = End, RaffleName = Name, TokenPrize = Prize }; using (var DbContext = new SQLiteDbContext()) { DbContext.Raffles.Add(raffle); await DbContext.SaveChangesAsync(); return(raffle.Id); } }
public static IDataTable <T> GetDatatable <T>(IDbConnection connection) where T : class { IDataTable <T> c = null; var ts = connection.GetType(); if (ts.BaseType.Name == "MySqlDbConnection") { c = new MySqlDbContext <T>(connection); } else { c = new SQLiteDbContext <T>(connection); } return(c); }
public static List <Raffle> GetRaffles(DateTime?EndDate = null) { // we only want the raffles that have not yet expired by default but we may want historical raffles later if (EndDate == null) { EndDate = DateTime.Now; } using (var DbContext = new SQLiteDbContext()) { var raffles = DbContext.Raffles.Where(r => r.DoneDate >= EndDate); return(raffles.ToList()); } }
public void GetByLogFile_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents = null; int logFileId = 0; using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents = W3CEnumerable.FromStream(logStream).ToList().GetRange(0, 10); } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); IRequestRepository requestRepo = new RequestRepository(dbContext); ICreateRequestBatchCommand createRequestBatchCommand = new CreateRequestBatchCommand(dbContext, new RequestValidator()); // create the project ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create multiple log file records for (var i = 0; i < 3; i++) { LogFileModel logFile = DataHelper.CreateLogFileModel(project.Id); DataHelper.InsertLogFileModel(dbContext, logFile); createRequestBatchCommand.Execute(logFile.Id, logEvents); if (logFileId == 0) { logFileId = logFile.Id; } } IEnumerable <RequestModel> result = requestRepo.GetByLogFile(logFileId); Assert.IsNotNull(result); Assert.AreEqual(logEvents.Count, result.Count()); } }
public void GetUnprocessedLogFileCount_Integration_ReturnsValidCount() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); int processedCount = new Random().Next(0, 5); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); IProjectRepository projectRepo = new ProjectRepository(dbContext); ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); ProjectModel project2 = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project2); // create records const string sql = @"INSERT INTO LogFiles (ProjectId, FileName, FileHash, CreateDate, FileLength, RecordCount, Status) VALUES (@ProjectId, @FileName, @FileHash, @CreateDate, @FileLength, @RecordCount, @Status)"; // create two processing record - this should be included LogFileModel logFile = DataHelper.CreateLogFileModel(project.Id); logFile.Status = LogFileStatus.Processing; dbContext.ExecuteNonQuery(sql, logFile); // create a processing record - this should be included logFile = DataHelper.CreateLogFileModel(project.Id); logFile.Status = LogFileStatus.Processing; dbContext.ExecuteNonQuery(sql, logFile); // create an error records - this should not be included logFile = DataHelper.CreateLogFileModel(project.Id); logFile.Status = LogFileStatus.Error; dbContext.ExecuteNonQuery(sql, logFile); // create a pending record for another project - this should also not be included logFile = DataHelper.CreateLogFileModel(project2.Id); logFile.Status = LogFileStatus.Processing; dbContext.ExecuteNonQuery(sql, logFile); int result = projectRepo.GetUnprocessedLogFileCount(project.Id); Assert.AreEqual(2, result); } }
public void Execute_IntegrationTest_SQLite() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); List <W3CEvent> logEvents1 = null; List <W3CEvent> logEvents2 = new List <W3CEvent>(); using (StreamReader logStream = new StreamReader(TestAsset.ReadTextStream(TestAsset.LogFile))) { logEvents1 = W3CEnumerable.FromStream(logStream).ToList(); logEvents2.AddRange(logEvents1.GetRange(0, 10)); } using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); ISetLogFileUnprocessedCommand setLogFileUnprocessedCommand = new SetLogFileUnprocessedCommand(dbContext, _jobRegistrationService); // create the project first so we have one ProjectModel project = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, project); // create the log files LogFileModel logFile1 = DataHelper.CreateLogFileModel(project.Id); logFile1.Status = LogFileStatus.Complete; DataHelper.InsertLogFileModel(dbContext, logFile1); LogFileModel logFile2 = DataHelper.CreateLogFileModel(project.Id); logFile2.Status = LogFileStatus.Complete; DataHelper.InsertLogFileModel(dbContext, logFile2); // check that the log file is processed int processedCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM LogFiles WHERE ProjectId = @ProjectId AND Status = @Status", new { ProjectId = project.Id, Status = LogFileStatus.Complete }); Assert.AreEqual(2, processedCount); // execute for a single log file setLogFileUnprocessedCommand.Execute(logFile1.Id); processedCount = dbContext.ExecuteScalar <int>("SELECT COUNT(*) FROM LogFiles WHERE ProjectId = @ProjectId AND Status = @Status", new { ProjectId = project.Id, Status = LogFileStatus.Processing }); Assert.AreEqual(1, processedCount); } }
public void GetByProject_Integration_ReturnsData() { string filePath = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".dbtest"); using (SQLiteDbContext dbContext = new SQLiteDbContext(filePath)) { dbContext.Initialise(); dbContext.BeginTransaction(); IProjectRequestAggregateRepository projectRequestAggregateRepo = new ProjectRequestAggregateRepository(dbContext); ISetLogFileUnprocessedCommand setLogFileUnprocessedCommand = Substitute.For <ISetLogFileUnprocessedCommand>(); ICreateProjectRequestAggregateCommand projectRequestAggregateCommand = new CreateProjectRequestAggregateCommand(dbContext, new ProjectRequestAggregateValidator(), new LogFileRepository(dbContext), setLogFileUnprocessedCommand); // create the projects ProjectModel projectA = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, projectA); ProjectModel projectZ = DataHelper.CreateProjectModel(); DataHelper.InsertProjectModel(dbContext, projectZ); // create the request aggregate records for ProjectA int numRecords = new Random().Next(5, 10); for (var i = 0; i < numRecords; i++) { ProjectRequestAggregateModel projectRequestAggregate = DataHelper.CreateProjectRequestAggregateModel(); projectRequestAggregate.ProjectId = projectA.Id; projectRequestAggregateCommand.Execute(projectRequestAggregate); } // create the log file records for ProjectB that should be excluded by the query for (var i = 0; i < 5; i++) { ProjectRequestAggregateModel projectRequestAggregate = DataHelper.CreateProjectRequestAggregateModel(); projectRequestAggregate.ProjectId = projectZ.Id; projectRequestAggregateCommand.Execute(projectRequestAggregate); } IEnumerable <ProjectRequestAggregateModel> result = projectRequestAggregateRepo.GetByProject(projectA.Id); Assert.IsNotNull(result); Assert.AreEqual(numRecords, result.Count()); } }