public void Teardown() { using (var db = new TestModelContext()) { db.Database.EnsureDeleted(); } }
public void AddCommandTest(bool isAdmin) { using (var modelContext = new TestModelContext()) { var userId = 100; var now = DateTime.Now; var entity = new DivisionInfo { DivisionID = 1 }; var securityContext = new TestSecurityContext(userId, isAdmin); var command = new AddCommand <DivisionInfo> (modelContext, securityContext); command.Add(entity, now); var entityLoaded = modelContext .QueryWhere <DivisionInfo> (d => d.DivisionID == entity.DivisionID) .SingleOrDefault(); Assert.Equal(isAdmin, null != entityLoaded); if (entityLoaded != null) { Assert.Equal(now, entityLoaded.CreatedOnDate); Assert.Equal(now, entityLoaded.LastModifiedOnDate); Assert.Equal(userId, entityLoaded.CreatedByUserId); Assert.Equal(userId, entityLoaded.LastModifiedByUserId); } } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var mappingConfig = new MapperConfiguration(mc => { mc.AddProfile(new ParentMapperProfile()); }); IMapper mapper = mappingConfig.CreateMapper(); using (var connection = new SqliteConnection("DataSource=:memory:")) { connection.Open(); var options = new DbContextOptionsBuilder <TestModelContext>() .UseSqlite(connection) // Set the connection explicitly, so it won't be closed automatically by EF .Options; // Create the dabase schema // You can use MigrateAsync if you use Migrations using (var context = new TestModelContext(options)) { context.Database.EnsureCreated(); LoadMockData(context); } // The connection is not closed, so the database still exists using (var context = new TestModelContext(options)) { var repository = new ParentRespository(context, mapper); var users = repository.GetAllParents(); } } }
[HttpGet] // Can use custom method name then. // public HttpResponseMessage Get() public HttpResponseMessage Get(string search = "") { // return new string[] { "value1", "value2" }; //var db = new TestModelContext(); //var models = db.TestModels.ToList(); //return models; // return data; HttpResponseMessage result; var db = new TestModelContext(); var models = search == "" || search == null? db.TestModels.ToList() : db.TestModels.Where(x => x.Name.StartsWith(search)).ToList(); if (models != null) { result = Request.CreateResponse(HttpStatusCode.OK, models); } else { result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No TestModels were found."); } return(result); }
[HttpGet] // Can use custom method name then. public HttpResponseMessage Get(int id) { // return "value"; //var db = new TestModelContext(); //var model = db.TestModels.Where(x => x.Id == id).FirstOrDefault(); //return model; // return data[id]; HttpResponseMessage result; var db = new TestModelContext(); var model = db.TestModels.Where(x => x.Id == id).FirstOrDefault(); if (model != null) { result = Request.CreateResponse(HttpStatusCode.OK, model); } else { result = Request.CreateErrorResponse(HttpStatusCode.NotFound, "TestModel id = [" + id.ToString() + "] was not found."); } return(result); }
public ActionResult CreateNewTestModelEntity(FormCollection formCollection) //public ActionResult CreateNewTestModelEntity(string Name, string Location) { //foreach(string key in formCollection.AllKeys) //{ // Response.Write("Key [" + key + "]"); // Response.Write(formCollection[key]); // Response.Write("<br />"); //} //return View(); var newTestModel = new TestModel(); // newTestModel.Name = formCollection["Name"]; // newTestModel.Location = formCollection["Location"]; // newTestModel.Name = Name; // newTestModel.Location = Location; TryUpdateModel(newTestModel, formCollection); var db = new TestModelContext(); db.TestModels.Add(newTestModel); db.SaveChanges(); return(RedirectToAction("TestModelEntityGetAll2")); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestModelContext>() .UseSqlite(connection) // Set the connection explicitly, so it won't be closed automatically by EF .Options; // Create the dabase schema // You can use MigrateAsync if you use Migrations var context = new TestModelContext(options); context.Database.EnsureCreated(); LoadMockData(context); services.AddSingleton <TestModelContext>(context); services.AddControllers(); services.AddOData(); services.AddODataQueryFilter(new EnableQueryAttribute() { PageSize = 100 }); services.AddMvc(m => m.EnableEndpointRouting = false); }
public void DatabaseSingleItem() { Guid projectId = ProjectTestUtilities.CreateFullDbHierarchy(); Project sourceObject = null; using (var db = new TestModelContext()) { sourceObject = db.Projects.Find(projectId); } object result = _expander.Expand(sourceObject, includes: PropertyReference.Parse($"[{nameof(ProjectProjection.Name)}]")); result.ShouldNotBeNull(); ProjectProjection projection = result as ProjectProjection; projection.ShouldNotBeNull(); // Verify the properties in this object were projected correctly projection.Name.ShouldBe(sourceObject.Name); projection.Description.ShouldBeNull(); // And verify the navigation property was retrieved correctly projection.Environments.ShouldBeNull(); projection.CredentialDefinitions.ShouldBeNull(); projection.Sections.ShouldBeNull(); }
public ActionResult TestModelEntityGetAll2() { var db = new TestModelContext(); var model = db.TestModels.ToList(); return(View(model)); }
public ActionResult TestModelEntity(int Id) { var db = new TestModelContext(); var model = db.TestModels.FirstOrDefault(x => x.Id == Id); return(View(model)); }
static void Main(string[] args) { var options = SqliteInMemory.CreateOptions <TestModelContext>(); var dbContext = new TestModelContext(options); dbContext.Database.EnsureCreated(); //create book with one page and one image ( image has navigation props to book and bookpage) var book = CreateBasicBook(); var page = CreateBasicBookPage(book); var image = CreateBasicImage(book, page); dbContext.Add(book); dbContext.Add(page); dbContext.Add(image); dbContext.SaveChanges(); //take existing book var bookToCopy = dbContext.Books.Include(b => b.BookPages).Include(b => b.Images).First() as Book; //reset Connection to Database dbContext.Dispose(); dbContext = new TestModelContext(options); // set keyproperties to zero bookToCopy.Id = 0; //set keyproperties of children to zero foreach (var i in bookToCopy.Images) { i.Id = 0; } foreach (var p in bookToCopy.BookPages) { p.Id = 0; } //add to context dbContext.Add(bookToCopy); //save context dbContext.SaveChanges(); //reset Connection to Database changes everything //dbContext.Dispose(); //dbContext = new TestModelContext(options); //output var allBooks = dbContext.Books.Include(b => b.BookPages).Include(b => b.Images).ToList(); Console.WriteLine("Initial Book: " + JsonConvert.SerializeObject(allBooks[0], new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); Console.WriteLine("Copied Book: " + JsonConvert.SerializeObject(allBooks[1], new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); }
public ActionResult EditTestModelEntity(int Id, FormCollection formCollection) { var db = new TestModelContext(); var model = db.TestModels.FirstOrDefault(x => x.Id == Id); TryUpdateModel(model, formCollection); db.SaveChanges(); return(RedirectToAction("TestModelEntityGetAll2")); }
public ActionResult DeleteTestModelEntity(int Id, FormCollection formCollection) { var db = new TestModelContext(); var model = db.TestModels.FirstOrDefault(x => x.Id == Id); db.TestModels.Remove(model); db.SaveChanges(); return(RedirectToAction("TestModelEntityGetAll2")); }
public void DatabaseNestedList() { Guid projectId = ProjectTestUtilities.CreateFullDbHierarchy(); Project sourceObject = null; using (var db = new TestModelContext()) { sourceObject = db.Projects.Find(projectId); } var includeValues = $"{nameof(CredentialKeyValueProjection.Key)}"; var includeCredentials = $"{nameof(CredentialProjection.Values)}[{includeValues}]"; var includeEnvironments = $"{nameof(EnvironmentProjection.Name)},{nameof(EnvironmentProjection.Credentials)}[{includeCredentials}]"; var includeCredentialDefinitions = $"{nameof(CredentialDefinitionProjection.Name)},{nameof(CredentialDefinitionProjection.Type)}"; var includeProject = $"{nameof(ProjectProjection.Name)},{nameof(ProjectProjection.CredentialDefinitions)}[{includeCredentialDefinitions}],{nameof(ProjectProjection.Environments)}[{includeEnvironments}]"; object result = _expander.Expand(sourceObject, includes: PropertyReference.Parse($"[{includeProject}]")); result.ShouldNotBeNull(); ProjectProjection projection = result as ProjectProjection; projection.ShouldNotBeNull(); projection.ShouldNotBeNull(); // Verify the properties in this object were projected correctly projection.Name.ShouldBe(sourceObject.Name); projection.Description.ShouldBeNull(); // And verify each navigation property was retrieved correctly projection.CredentialDefinitions.ShouldNotBeNull(); projection.CredentialDefinitions.Count.ShouldBe(1); projection.CredentialDefinitions.First().Name.ShouldBe("Creds"); projection.CredentialDefinitions.First().DisplayName.ShouldBeNull(); projection.CredentialDefinitions.First().Type.ShouldNotBeNull(); projection.CredentialDefinitions.First().Type.Name.ShouldBe("CredType"); projection.Environments.ShouldNotBeNull(); projection.Environments.Count.ShouldBe(1); projection.Environments.First().Name.ShouldBe("EnvironmentName"); projection.Environments.First().BaseUrl.ShouldBeNull(); projection.Environments.First().Credentials.ShouldNotBeNull(); projection.Environments.First().Credentials.Count.ShouldBe(1); projection.Environments.First().Credentials.First().Id.ShouldBe(Guid.Empty); projection.Environments.First().Credentials.First().Definition.ShouldBeNull(); projection.Environments.First().Credentials.First().Values.ShouldNotBeNull(); projection.Environments.First().Credentials.First().Values.Count.ShouldBe(1); projection.Environments.First().Credentials.First().Values.First().Key.ShouldBe("Key"); projection.Environments.First().Credentials.First().Values.First().Value.ShouldBeNull(); }
public void DeleteCommandTest(bool isAdmin) { using (var modelContext = new TestModelContext()) { var entity = new DivisionInfo { DivisionID = 1 }; modelContext.Add(entity); var securityContext = new TestSecurityContext(1, isAdmin); var command = new DeleteCommand <DivisionInfo> (modelContext, securityContext); command.Delete(entity); Assert.Equal(isAdmin, null == modelContext .QueryOne <DivisionInfo> (d => d.DivisionID == entity.DivisionID) .SingleOrDefault()); } }
private static void LoadMockData(TestModelContext context) { context.Parent.Add(new ParentModel() { Key = 1, Name = "1" }); context.Parent.Add(new ParentModel() { Key = 2, Name = "2" }); context.Parent.Add(new ParentModel() { Key = 3, Name = "3" }); context.Parent.Add(new ParentModel() { Key = 4, Name = "4" }); context.Children.Add(new ChildModel() { Key = 1, ParentKey = 1, Display = "1.1" }); context.Children.Add(new ChildModel() { Key = 2, ParentKey = 1, Display = "2.1" }); context.Children.Add(new ChildModel() { Key = 3, ParentKey = 2, Display = "3.2" }); context.Children.Add(new ChildModel() { Key = 4, ParentKey = 4, Display = "4.4" }); context.Children.Add(new ChildModel() { Key = 5, ParentKey = 3, Display = "5.3" }); context.SaveChanges(); }
public void EntityFrameworkMappingConfig() { Guid projectId = ProjectTestUtilities.CreateFullDbHierarchy(); Project sourceObject = null; using (var db = new TestModelContext()) { sourceObject = db.Projects.Find(projectId); } var includeProject = $"{nameof(ProjectProjection.Name)},{nameof(ProjectProjection.Id)}"; object result = _expander.Expand(sourceObject, includes: PropertyReference.Parse($"[{includeProject}]")); result.ShouldNotBeNull(); ProjectProjection projection = result as ProjectProjection; projection.ShouldNotBeNull(); projection.Name.ShouldBe(sourceObject.Name); projection.Id.ShouldNotBe(sourceObject.Id); }
[HttpDelete] // Can use custom method name then. public HttpResponseMessage Delete(int id) { // data.RemoveAt(id); //var db = new TestModelContext(); //var oldTestModel = db.TestModels.Where(x => x.Id == id).FirstOrDefault(); //db.TestModels.Remove(oldTestModel); //db.SaveChanges(); HttpResponseMessage result; try { var db = new TestModelContext(); var oldTestModel = db.TestModels.Where(x => x.Id == id).FirstOrDefault(); if (oldTestModel != null) { db.TestModels.Remove(oldTestModel); db.SaveChanges(); result = Request.CreateResponse(HttpStatusCode.OK); } else { result = Request.CreateErrorResponse(HttpStatusCode.NotFound, "TestModel id = [" + id.ToString() + "] was not found."); } } catch (Exception exception) { result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception); } return(result); }
public void Setup() { _expander = new Expander(); var config = new PopcornConfiguration(_expander); config.MapEntityFramework <Project, ProjectProjection, TestModelContext>(TestModelContext.ConfigureOptions()); config.MapEntityFramework <PopcornCoreTest.Models.Environment, EnvironmentProjection, TestModelContext>(TestModelContext.ConfigureOptions()); config.MapEntityFramework <Credential, CredentialProjection, TestModelContext>(TestModelContext.ConfigureOptions()); config.MapEntityFramework <CredentialDefinition, CredentialDefinitionProjection, TestModelContext>(TestModelContext.ConfigureOptions()); config.MapEntityFramework <CredentialType, CredentialTypeProjection, TestModelContext>(TestModelContext.ConfigureOptions()); config.MapEntityFramework <CredentialKeyValue, CredentialKeyValueProjection, TestModelContext>(TestModelContext.ConfigureOptions()); using (var db = new TestModelContext()) { db.Database.EnsureDeleted(); } using (var db = new TestModelContext()) { db.Database.EnsureCreated(); } }
public ModelIntegrationTests() { fakedContext = new XrmFakedContext(); fakedContext.SetEntityMetadata(BusinessUnitMetadata.GetMetadata()); //fakedContext.ProxyTypesAssembly = Assembly.GetExecutingAssembly(); organizationService = fakedContext.GetFakedOrganizationService(); testContext = new TestModelContext(organizationService); bu = new BusinessUnit() { _name = "GG" }; bu.Id = testContext.Create(bu); t = new Team() { _name = "GUGI" }; t.Id = testContext.Create(t); Agust = new User() { _name = "Agust", _bu = new EntityReference("", bu.Id), _teamid = new EntityReference("", t.Id), }; Agust.Id = testContext.Create(Agust); Andrea = new User() { _name = "Andrea", _bu = new EntityReference("", bu.Id), _teamid = new EntityReference("", t.Id), }; Andrea.Id = testContext.Create(Andrea); }
public void Setup() { _expander = new Expander(); var config = new PopcornConfiguration(_expander); config.MapEntityFramework <Project, ProjectProjection, TestModelContext>(TestModelContext.ConfigureOptions(), null, (definition) => { definition.Translate(o => o.Id, () => Guid.NewGuid()); }); config.MapEntityFramework <PopcornNetStandardTest.Models.Environment, EnvironmentProjection, TestModelContext>(TestModelContext.ConfigureOptions()); config.MapEntityFramework <Credential, CredentialProjection, TestModelContext>(TestModelContext.ConfigureOptions()); config.MapEntityFramework <CredentialDefinition, CredentialDefinitionProjection, TestModelContext>(TestModelContext.ConfigureOptions()); config.MapEntityFramework <CredentialType, CredentialTypeProjection, TestModelContext>(TestModelContext.ConfigureOptions()); config.MapEntityFramework <CredentialKeyValue, CredentialKeyValueProjection, TestModelContext>(TestModelContext.ConfigureOptions()); using (var db = new TestModelContext()) { db.Database.EnsureDeleted(); } using (var db = new TestModelContext()) { db.Database.EnsureCreated(); } }
[HttpPut] // Can use custom method name then. // public HttpResponseMessage Put(int id, [FromBody] TestModel testModel) // public HttpResponseMessage Put([FromBody] int id, [FromUri] TestModel testModel) public HttpResponseMessage Put([FromUri] int id, [FromUri] TestModel testModel) { // data[id] = value; //var db = new TestModelContext(); //var oldTestModel = db.TestModels.Where(x => x.Id == id).FirstOrDefault(); //oldTestModel.Name = testModel.Name; //oldTestModel.Location = testModel.Location; //db.SaveChanges(); HttpResponseMessage result; var db = new TestModelContext(); var updateTestModel = db.TestModels.Where(x => x.Id == id).FirstOrDefault(); if (updateTestModel != null) { updateTestModel.Name = testModel.Name; updateTestModel.Location = testModel.Location; db.SaveChanges(); result = Request.CreateResponse(HttpStatusCode.OK, updateTestModel); result.Headers.Location = new Uri(Request.RequestUri.ToString()); } else { result = Request.CreateErrorResponse(HttpStatusCode.NotFound, "TestModel id = [" + id.ToString() + "] was not found."); } return(result); }
[HttpPost] // Can use custom method name then. public HttpResponseMessage Post([FromBody] TestModel testModel) { // data.Add(value); HttpResponseMessage result; try { var db = new TestModelContext(); db.TestModels.Add(testModel); db.SaveChanges(); result = Request.CreateResponse(HttpStatusCode.Created, testModel); result.Headers.Location = new Uri(Request.RequestUri + "/" + testModel.Id.ToString()); } catch (Exception exception) { result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception); } return(result); }
public static Guid CreateFullDbHierarchy() { Guid projectId; using (var db = new TestModelContext()) { var newProject = new Project { Id = Guid.NewGuid(), Name = "ProjectName", Description = "ProjectDescription" }; db.Projects.Add(newProject); projectId = newProject.Id; var newCredentialType = new CredentialType { Id = Guid.NewGuid(), Name = "CredType", RequiredValues = "None" }; db.CredentialTypes.Add(newCredentialType); var newCredentialsDefinition = new CredentialDefinition { Id = Guid.NewGuid(), Name = "Creds", DisplayName = "Creds", Project = newProject, Type = newCredentialType, }; db.CredentialDefinitions.Add(newCredentialsDefinition); newProject.CredentialDefinitions.Add(newCredentialsDefinition); var newEnvironment = new Models.Environment { Id = Guid.NewGuid(), Name = "EnvironmentName", Project = newProject, BaseUrl = "https://skywardapps.us" }; db.Environments.Add(newEnvironment); newProject.Environments.Add(newEnvironment); var newCredentials = new Credential { Id = Guid.NewGuid(), Environment = newEnvironment, Definition = newCredentialsDefinition, Values = new List <CredentialKeyValue> { new CredentialKeyValue { Id = Guid.NewGuid(), Key = "Key", Value = "Value", } } }; db.Credentials.Add(newCredentials); newEnvironment.Credentials.Add(newCredentials); db.SaveChanges(); } return(projectId); }
public ParentDtoController(TestModelContext context) { testModelContext = context; }
public ParentRespository(TestModelContext context, IMapper mapper) { this.autoMapper = mapper; this.testModelContext = context; }
public TestModelsController(TestModelContext context) { _context = context; }