public async Task TestInsertSubquery() { var dr = await CreateDriver(); var subq = Builders <MyModel> .Select(x => x.Name) .Where(x => x.Id == 1) .AsSubquery(); var obj = new MyModel2 { }; await dr.Insert(obj) .UseField(x => x.Name1, subq) .ExecuteAsync(); var res = await dr.From <MyModel2>() .Select(x => x.Name1) .ToListAsync(); Assert.Collection(res, x => Assert.Equal("subtest1", x), x => Assert.Equal("subtest2", x), x => Assert.Equal("subtest3", x), x => Assert.Equal("subtest4", x), x => Assert.Equal("test1", x)); }
public MyModel2 Visit(MyModel2 model) { model.CompanyName = new StringFixVisitor().Visit(model.CompanyName); model.Region = new StringFixVisitor().Visit(model.Region); model.Address = new CompanyAddressFixVisitor().Visit(model.Address); return(model); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Propery1,Propery2,Propery3,Propery4")] MyModel2 myModel2) { if (id != myModel2.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(myModel2); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MyModel2Exists(myModel2.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(myModel2)); }
private void ReadWithMap2(MyModel2 mm2, DtoTestViewModelExtra vm) { //DefineMappers2(); //var mapper2 = (PropBagMapperCustom<MyModel2, DtoTestViewModelExtra>)_autoMappers.GetMapperToUse(_mapperKey2); //DtoTestViewModelExtra tt = (DtoTestViewModelExtra)mapper2.MapToDestination(mm2, vm); }
public async Task <IActionResult> Create([Bind("Id,Propery1,Propery2,Propery3,Propery4")] MyModel2 myModel2) { if (ModelState.IsValid) { _context.Add(myModel2); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(myModel2)); }
private void BtnRead2_Click(object sender, RoutedEventArgs e) { MyModel2 mm2 = new MyModel2 { ProductId = Guid.NewGuid(), Amount = 1451, Size = 17.8 }; ReadWithMap2(mm2, OurData); }
public ActionResult zagrebPost(MyModel1 request) { string ulaznaDatoteka = (string)TempData["Metro"]; // Mapa metro = new Mapa("ZagrebMetro", ulaznaDatoteka); List <Ruta> Lista = new List <Ruta>() { new Ruta(new Kvart("MAKSIMIR"), new Kvart("SIGET"), 5), new Ruta(new Kvart("SIGET"), new Kvart("SPANSKO"), 4), new Ruta(new Kvart("SPANSKO"), new Kvart("MEDVESCAK"), 8), new Ruta(new Kvart("MEDVESCAK"), new Kvart("SPANSKO"), 8), new Ruta(new Kvart("MEDVESCAK"), new Kvart("DUBRAVA"), 6), new Ruta(new Kvart("MAKSIMIR"), new Kvart("MEDVESCAK"), 5), new Ruta(new Kvart("SPANSKO"), new Kvart("DUBRAVA"), 2), new Ruta(new Kvart("DUBRAVA"), new Kvart("SIGET"), 3), new Ruta(new Kvart("MAKSIMIR"), new Kvart("DUBRAVA"), 7) }; //List<Ruta> Lista = metro.KonstrukcijaRuta(); List <Ruta> r = new List <Ruta>(); foreach (Ruta ru in Lista) { r.Add(ru); } MyModel1 model = new MyModel1() { stations = new string[] { "" } }; model.stations = request.stations; MyModel2 model2 = new MyModel2() { distance = 0 }; model2.distance = model.DuljinaPuta(r); return(View("zagrebPost")); //return Json(model2 ,JsonRequestBehavior.AllowGet); }
public void InsertRefId() { var obj = new MyModel2 { Name1 = "A" }; var q = Builders <MyModel2> .Insert(obj) .UseField(x => x.Name1) .UsePreviousInsertId <MyModel>(x => x.ModelId, x => x.Id); Utils.AssertRawQuery(q, @"INSERT INTO ""public"".model2(name1,model_id) VALUES ('A',currval(pg_get_serial_sequence('model','id'))) RETURNING ""id"";"); }
public void InsertIntoVariable() { var obj = new MyModel2 { Name1 = "A" }; var q = Builders <MyModel2> .Insert(obj) .UseField(x => x.Name1) .IntoVariable("insert_id1"); Utils.AssertRawQuery(q, @"INSERT INTO ""public"".model2(name1) VALUES ('A') RETURNING ""id""; SELECT set_config('vars.insert_id1', lastval()::text, true);"); }
public async Task TestInsertSubqueryFailMultipleRows() { var dr = await CreateDriver(); var subq = Builders <MyModel> .Select(x => x.Name) .AsSubquery(); var obj = new MyModel2 { }; await Assert.ThrowsAsync <Npgsql.PostgresException>(() => dr.Insert(obj) .UseField(x => x.Name1, subq) .ExecuteAsync()); }
public void InsertWithSubquery() { var subq = Builders <MyModel> .Select(x => x.Name) .Where(x => x.Id == 1) .AsSubquery(); var obj = new MyModel2 { }; var q = Builders <MyModel2> .Insert(obj) .UseField(x => x.Name1, subq); Utils.AssertRawQuery(q, @"INSERT INTO ""public"".model2(name1) VALUES ((SELECT ""name"" FROM model WHERE (""id"") = (1))) RETURNING ""id"";"); }
public void InsertWithSubqueryOptional() { var subq = Builders <MyModel> .Select(x => x.Id) .Where(x => x.Name == "subtest4") .AsSubquery(); var obj = new MyModel2 { }; var q = Builders <MyModel2> .Insert(obj) .UseField(x => x.ModelId, subq); Utils.AssertRawQuery(q, @"INSERT INTO ""public"".model2(model_id) VALUES ((SELECT ""id"" FROM model WHERE (""name"") = ('subtest4'))) RETURNING ""id"";"); }
public void metro5PostTest() { //Arange MyModel4 request1 = new MyModel4() { stations = new List <Kvart> { new Kvart("MAKSIMIR"), new Kvart("SPANSKO") } }; MyModel2 expected1 = new MyModel2() { distance = 9 }; MyModel4 request2 = new MyModel4() { stations = new List <Kvart> { new Kvart("MAKSIMIR"), new Kvart("SPANSKO") } }; MyModel2 expected2 = new MyModel2() { distance = 9 }; METRO5Controller controller = new METRO5Controller(); //Act // JsonResult result1 = controller.metro5Post(request1) as JsonResult; // string Json1 = new JavaScriptSerializer().Serialize(result1.Data); string expectedJson1 = new JavaScriptSerializer().Serialize(expected1); // Assert // Assert.IsNotNull(result1); // Assert.AreEqual(expectedJson1, Json1); //JsonResult result2 = controller.metro5Post(request2) as JsonResult; //string Json2 = new JavaScriptSerializer().Serialize(result2.Data); string expectedJson2 = new JavaScriptSerializer().Serialize(expected2); // Assert //Assert.IsNotNull(result2); // Assert.AreEqual(expectedJson2, Json2); }
public void PocoConstructorParameterOnPropertyTest() { // For this test, the value would have already been resolved by a PropertyValueConverter to its intended type. var value = new MyModel2("foo"); var property = new Mocks.PublishedContentPropertyMock("myProperty", value, true); var content = new Mocks.PublishedContentMock { Properties = new[] { property } }; var model = content.As <MyModel3>(); Assert.That(model, Is.Not.Null); Assert.That(model, Is.InstanceOf <MyModel3>()); Assert.That(model.MyProperty, Is.Not.Null); Assert.That(model.MyProperty, Is.InstanceOf <MyModel2>()); Assert.That(model.MyProperty, Is.EqualTo(value)); }
public async Task TestInsertRefField() { var dr = await CreateDriver(); var obj = new MyModel() { Name = "new", }; var obj2 = new MyModel2() { Name1 = "new", }; var b = dr.CreateTransactionBatch(); b.Insert(obj) .Schedule(); b.Insert <MyModel2>() .AddObject(obj2) .AddObject(obj2) .AddObject(obj2) .UseField(x => x.Name1) .UsePreviousInsertId <MyModel>(x => x.ModelId, x => x.Id) .Schedule(); await b.Execute(); var rows = await dr.From <MyModel2>().Select(x => x.ModelId).ToListAsync(); Assert.Collection(rows, x => Assert.Equal(1, x), x => Assert.Equal(1, x), x => Assert.Equal(2, x), x => Assert.Equal(4, x), x => Assert.Equal(4, x), x => Assert.Equal(4, x), x => Assert.Equal(4, x)); }
public JsonResult metro7Post() { string ulaznaDatoteka = (string)TempData["Metro"]; // Mapa metro = new Mapa("ZagrebMetro", ulaznaDatoteka); List <Ruta> Lista = new List <Ruta>() { new Ruta(new Kvart("MAKSIMIR"), new Kvart("SIGET"), 5), new Ruta(new Kvart("SIGET"), new Kvart("SPANSKO"), 4), new Ruta(new Kvart("SPANSKO"), new Kvart("MEDVESCAK"), 8), new Ruta(new Kvart("MEDVESCAK"), new Kvart("SPANSKO"), 8), new Ruta(new Kvart("MEDVESCAK"), new Kvart("DUBRAVA"), 6), new Ruta(new Kvart("MAKSIMIR"), new Kvart("MEDVESCAK"), 5), new Ruta(new Kvart("SPANSKO"), new Kvart("DUBRAVA"), 2), new Ruta(new Kvart("DUBRAVA"), new Kvart("SIGET"), 3), new Ruta(new Kvart("MAKSIMIR"), new Kvart("DUBRAVA"), 7) }; //List<Ruta> Lista = metro.KonstrukcijaRuta(); List <Ruta> r = new List <Ruta>(); foreach (Ruta ru in Lista) { r.Add(ru); } MyModel2 model2 = new MyModel2() { distance = 0 }; //model2.NajkracaRuta(Lista, model) //return View("zagrebPost"); return(Json(model2, JsonRequestBehavior.AllowGet)); }
public void zagrebPostTest() { //Arange MyModel1 request = new MyModel1() { stations = new string[] { "MAKSIMIR", "DUBRAVA" } }; MyModel2 expected = new MyModel2() { distance = 7 }; zagrebmetroController controller = new zagrebmetroController(); //Act JsonResult result = controller.zagrebPost(request) as JsonResult; string Json = new JavaScriptSerializer().Serialize(result.Data); string expectedJson = new JavaScriptSerializer().Serialize(expected); // Assert Assert.IsNotNull(result); Assert.AreEqual(expectedJson, Json); }
public void BindPropertyDoesNothingIfValueProviderContainsNoEntryForProperty() { // Arrange MyModel2 model = new MyModel2() { IntReadWrite = 3 }; ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ValueProvider = new Dictionary<string, ValueProviderResult>() }; PropertyDescriptor pd = TypeDescriptor.GetProperties(model)["IntReadWrite"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper(); // Act helper.PublicBindProperty(null, bindingContext, pd); // Assert Assert.AreEqual(3, model.IntReadWrite, "Property should not have been changed."); }
public void BindPropertyReturnsIfOnPropertyValidatingReturnsFalse() { // Arrange ControllerContext controllerContext = new Mock<ControllerContext>().Object; MyModel2 model = new MyModel2() { IntReadWrite = 3 }; ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ValueProvider = new Dictionary<string, ValueProviderResult>() { { "IntReadWrite", new ValueProviderResult("42", "42", null) } } }; PropertyDescriptor pd = TypeDescriptor.GetProperties(model)["IntReadWrite"]; Mock<DefaultModelBinderHelper> mockHelper = new Mock<DefaultModelBinderHelper>() { CallBase = true }; mockHelper.Expect(b => b.PublicOnPropertyValidating(controllerContext, bindingContext, pd, 42)).Returns(false); mockHelper.Expect(b => b.PublicSetProperty(controllerContext, bindingContext, pd, 42)).Never(); mockHelper.Expect(b => b.PublicOnPropertyValidated(controllerContext, bindingContext, pd, 42)).Never(); DefaultModelBinderHelper helper = mockHelper.Object; // Act helper.PublicBindProperty(controllerContext, bindingContext, pd); // Assert Assert.AreEqual(3, model.IntReadWrite, "Property should not have been changed."); mockHelper.Verify(); }
public void BindPropertySetsPropertyToNullIfUserLeftTextEntryFieldBlankForOptionalValue() { // Arrange MyModel2 model = new MyModel2() { NullableIntReadWrite = 8 }; ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ValueProvider = new Dictionary<string, ValueProviderResult>() { { "NullableIntReadWrite", null } } }; Mock<IModelBinder> mockInnerBinder = new Mock<IModelBinder>(); mockInnerBinder.Expect(b => b.BindModel(null, It.IsAny<ModelBindingContext>())).Returns((object)null); PropertyDescriptor pd = TypeDescriptor.GetProperties(model)["NullableIntReadWrite"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper() { Binders = new ModelBinderDictionary() { { typeof(int?), mockInnerBinder.Object } } }; // Act helper.PublicBindProperty(null, bindingContext, pd); // Assert Assert.AreEqual(0, bindingContext.ModelState.Count, "Should not have been an error."); Assert.AreEqual(null, model.NullableIntReadWrite, "Property should not have been updated."); }
public void BindPropertyUpdatesPropertyOnFailureIfInnerBinderReturnsNonNullObject() { // Arrange MyModel2 model = new MyModel2() { IntReadWriteNonNegative = 8 }; ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ValueProvider = new Dictionary<string, ValueProviderResult>() { { "IntReadWriteNonNegative", null } } }; Mock<IModelBinder> mockInnerBinder = new Mock<IModelBinder>(); mockInnerBinder .Expect(b => b.BindModel(null, It.IsAny<ModelBindingContext>())) .Returns( delegate(ControllerContext cc, ModelBindingContext bc) { bc.ModelState.AddModelError("IntReadWriteNonNegative", "Some error text."); return 4; }); PropertyDescriptor pd = TypeDescriptor.GetProperties(model)["IntReadWriteNonNegative"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper() { Binders = new ModelBinderDictionary() { { typeof(int), mockInnerBinder.Object } } }; // Act helper.PublicBindProperty(null, bindingContext, pd); // Assert Assert.AreEqual(false, bindingContext.ModelState.IsValidField("IntReadWriteNonNegative"), "Error should have propagated."); Assert.AreEqual(1, bindingContext.ModelState["IntReadWriteNonNegative"].Errors.Count, "Wrong number of errors."); Assert.AreEqual("Some error text.", bindingContext.ModelState["IntReadWriteNonNegative"].Errors[0].ErrorMessage, "Wrong error text."); Assert.AreEqual(4, model.IntReadWriteNonNegative, "Property should have been updated."); }
public void BindPropertyUpdatesPropertyOnSuccess() { // Arrange // Effectively, this is just testing updating a single property named "IntReadWrite" ControllerContext controllerContext = new Mock<ControllerContext>().Object; MyModel2 model = new MyModel2() { IntReadWrite = 3 }; ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelName = "foo", ModelState = new ModelStateDictionary() { { "blah", new ModelState() } }, ValueProvider = new Dictionary<string, ValueProviderResult>() { { "foo.IntReadWrite", null } } }; Mock<IModelBinder> mockInnerBinder = new Mock<IModelBinder>(); mockInnerBinder .Expect(b => b.BindModel(It.IsAny<ControllerContext>(), It.IsAny<ModelBindingContext>())) .Returns( delegate(ControllerContext cc, ModelBindingContext bc) { Assert.AreEqual(controllerContext, cc, "ControllerContext was not forwarded correctly."); Assert.AreEqual(3, bc.Model, "Original model was not forwarded correctly."); Assert.AreEqual(typeof(int), bc.ModelType, "Model type was not forwarded correctly."); Assert.AreEqual("foo.IntReadWrite", bc.ModelName, "Model name was not forwarded correctly."); Assert.AreEqual(new ModelBindingContext().PropertyFilter, bc.PropertyFilter, "Property filter property should not have been set."); Assert.AreEqual(bindingContext.ModelState, bc.ModelState, "ModelState was not forwarded correctly."); Assert.AreEqual(bindingContext.ValueProvider, bc.ValueProvider, "Value provider was not forwarded correctly."); return 4; }); PropertyDescriptor pd = TypeDescriptor.GetProperties(model)["IntReadWrite"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper() { Binders = new ModelBinderDictionary() { { typeof(int), mockInnerBinder.Object } } }; // Act helper.PublicBindProperty(controllerContext, bindingContext, pd); // Assert Assert.AreEqual(4, model.IntReadWrite, "Property should have been updated."); }