// Clean test data protected override void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects) using var tempServiceScope = GeneralContext.CreateServiceScope(); var dbContext = tempServiceScope.ServiceProvider.GetService <CRPMContext>(); var formResults = dbContext.Form.Where(x => x.FormGuid.Contains("only_for_test")); if (formResults != null && formResults.Any()) { dbContext.Form.RemoveRange(formResults); } var formTemplates = dbContext.FormTemplate.Where(x => x.Description.Contains("only_for_test")); if (formTemplates != null && formTemplates.Any()) { dbContext.FormTemplate.RemoveRange(formTemplates); } dbContext.SaveChanges(); base.Dispose(disposing); } disposedValue = true; } }
protected override void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects) using var tempServiceScope = GeneralContext.CreateServiceScope(); var userService = GeneralContext.GetService <UserService>(); userService.DbContext = tempServiceScope.ServiceProvider.GetService <CRPMContext>(); // remove testusers var results = userService.DbContext.User.AsEnumerable() .Where(x => int.Parse(x.UserId) < 0 && x.UserName.Contains("only_for_test")); if (results != null && results.Any()) { userService.DbContext.User.RemoveRange(results); } userService.OkResult().Wait(); base.Dispose(disposing); } disposedValue = true; } }
public TService GetChildScopeService <TService>() where TService : BaseService { var _serviceScope = GeneralContext.CreateServiceScope(); var service = _serviceScope.ServiceProvider.GetService <TService>(); service.DbContext = _serviceScope.ServiceProvider.GetService <CRPMContext>(); return(service as TService); }
/// <summary> On setup migrations </summary> private void OnSetUp(MigrationBuilderEnsured migrationBuilder) { using var initServiceScope = GeneralContext.CreateServiceScope(); var initServiceProvider = initServiceScope.ServiceProvider; this.Init(migrationBuilder, initServiceProvider); this.Up(migrationBuilder); this.Down(migrationBuilder); }
/// <summary> temp Exists method </summary> private bool Exists_(string tableName) { using var completeServiceScope = GeneralContext.CreateServiceScope(); var services = completeServiceScope.ServiceProvider; using var dbContext = services.GetService <CRPMContext>() as CRPMContext; var count = dbContext.Database.ExecuteSqlRaw("SELECT FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'public' AND c.relname = @p0)", tableName); return(count > 0); }
/// <summary> method "Exists", testing existing data by the straight direction to db</summary> public bool Exists(string tableSchema, string tableName, string columnName = null, object columnValue = null) { using var completeServiceScope = GeneralContext.CreateServiceScope(); var services = completeServiceScope.ServiceProvider; using var dbContext = services.GetService <CRPMContext>(); using var conn = dbContext.Database.GetDbConnection(); if (conn.State.Equals(ConnectionState.Closed)) { conn.Open(); } bool exists = _existedTables.Exists(x => x == tableName); using var command = conn.CreateCommand(); if (!exists) { // exist table command.CommandText = @$ "SELECT EXISTS(SELECT FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = '{tableSchema}' AND c.relname = '{tableName}')"; var result = command.ExecuteScalar(); exists = Convert.ToBoolean(result); if (exists) { _existedTables.Add(tableName); } } // exist column if (exists && columnName != null) { command.CommandText = @$ "SELECT EXISTS(SELECT FROM information_schema.columns WHERE table_schema = '{tableSchema}' AND table_name = '{tableName}' AND column_name = '{columnName}')"; exists = Convert.ToBoolean(command.ExecuteScalar()); // exist value if (exists && columnValue != null) { if (columnValue.GetType() == typeof(string)) { columnValue = $"'{columnValue}'"; } command.CommandText = @$ "SELECT exists (SELECT 1 FROM " "{tableName}" " WHERE " "{columnName}" " = {columnValue} LIMIT 1)"; exists = Convert.ToBoolean(command.ExecuteScalar()); } } return(exists); }
public void MigrationsTest() { _testBaseWebHost = new TestWebHost(); using var tempServiceScope = GeneralContext.CreateServiceScope(); var dbContext = tempServiceScope.ServiceProvider.GetService <CRPMContext>(); var pendingMigrations = dbContext.Database.GetPendingMigrations(); var appliedMigrations = dbContext.Database.GetAppliedMigrations(); if (GeneralContext.LastErrors.Count == 0 && !pendingMigrations.Any() && appliedMigrations.Any(x => x == MigrationName)) { Assert.True(true); } else { Assert.True(false); } }
protected override void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects) using var tempServiceScope = GeneralContext.CreateServiceScope(); //var userService = GeneralContext.GetService<UserService>(); var dbContext = tempServiceScope.ServiceProvider.GetService <CRPMContext>(); var results = dbContext.OrganizationObject.Where(x => x.Name.Contains("only_for_test")); if (results != null && results.Any()) { dbContext.OrganizationObject.RemoveRange(results); } dbContext.SaveChanges(); base.Dispose(disposing); } disposedValue = true; } }