using System; using System.Collections.Generic; using System.Linq; using Luticate2.Utils.Dbo; using Luticate2.Utils.Dbo.Result; using Test.Utils.DataAccess; using Test.Utils.Dbo.PkBigSerial; using Test.Utils.Dbo.PkGuid; using Xunit; namespace Test.Utils.EfCrubDataAccess { public class LuEfCreateDataAccessTest { [Fact] public void TestAddMultiple1() { Tests.TestRealDb(context => { var dbos = new List { new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }, new PkBigSerialAddDbo { SomeInt = 21, SomeText = "24" } }; var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.Add(dbos, enumerable => enumerable); Assert.Equal(LuStatus.Success, res.Status); foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair(dbo, serialDbo))) { Assert.NotNull(dbo.Key); Assert.NotNull(dbo.Value); Assert.NotEqual(0, dbo.Value.Id); Assert.Equal(dbo.Key.SomeText, dbo.Value.SomeText); Assert.Equal(dbo.Key.SomeInt, dbo.Value.SomeInt); var get = service.GetSingleById(dbo.Value.Id); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal(dbo.Key.SomeText, get.Data.SomeText); Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt); Assert.Equal(dbo.Value.Id, get.Data.Id); } }); } [Fact] public void TestAddMultiple2() { Tests.TestRealDb(context => { var dbos = new List { new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }, new PkBigSerialAddDbo { SomeInt = 21, SomeText = "24" } }; var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.Id)); Assert.Equal(LuStatus.Success, res.Status); foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair(dbo, serialDbo))) { Assert.NotNull(dbo.Key); Assert.NotNull(dbo.Value); Assert.NotEqual(0, dbo.Value); var get = service.GetSingleById(dbo.Value); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal(dbo.Key.SomeText, get.Data.SomeText); Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt); Assert.Equal(dbo.Value, get.Data.Id); } }); } [Fact] public void TestAddMultiple3() { Tests.TestRealDb(context => { var dbos = new List { new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }, new PkBigSerialAddDbo { SomeInt = 21, SomeText = "24" } }; var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.CreatedAt)); Assert.Equal(LuStatus.Success, res.Status); foreach (var dbo in res.Data) { Assert.NotEqual(default(DateTime), dbo); } }); } [Fact] public void TestAddMultiple4() { Tests.TestRealDb(context => { var dbos = new List { new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }, new PkBigSerialAddDbo { SomeInt = 21, SomeText = "42" } }; var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.CreatedAt)); Assert.Equal(LuStatus.DbError, res.Status); }); } [Fact] public void TestAddSingle1() { Tests.TestRealDb(context => { var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.Add(new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }, dbo => dbo); Assert.Equal(LuStatus.Success, res.Status); Assert.NotEqual(0, res.Data.Id); Assert.Equal("42", res.Data.SomeText); Assert.Equal(42, res.Data.SomeInt); var get = service.GetSingleById(res.Data.Id); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal("42", get.Data.SomeText); Assert.Equal(42, get.Data.SomeInt); Assert.Equal(res.Data.Id, get.Data.Id); }); } [Fact] public void TestAddSingle2() { Tests.TestRealDb(context => { var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.Add(new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }, dbo => dbo.Id); Assert.Equal(LuStatus.Success, res.Status); Assert.NotEqual(0, res.Data); var get = service.GetSingleById(res.Data); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal("42", get.Data.SomeText); Assert.Equal(42, get.Data.SomeInt); Assert.Equal(res.Data, get.Data.Id); }); } [Fact] public void TestAddSingle3() { Tests.TestRealDb(context => { var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.Add(new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }, dbo => dbo.CreatedAt); Assert.Equal(LuStatus.Success, res.Status); Assert.NotEqual(default(DateTime), res.Data); }); } [Fact] public void TestAddGuidMultiple1() { Tests.TestRealDb(context => { var dbos = new List { new PkGuidAddDbo { SomeInt = 42, SomeText = "42" }, new PkGuidAddDbo { SomeInt = 21, SomeText = "24" } }; var service = new LuUtilsPkGuidDataAccess(context); var res = service.AddId(dbos); Assert.Equal(LuStatus.Success, res.Status); foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair(dbo, serialDbo))) { Assert.NotNull(dbo.Key); Assert.NotNull(dbo.Value); Assert.NotEqual(new Guid().ToString(), dbo.Value); var get = service.GetSingleById(dbo.Value); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal(dbo.Key.SomeText, get.Data.SomeText); Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt); Assert.Equal(dbo.Value, get.Data.Id); } }); } [Fact] public void TestAddGuidSingle1() { Tests.TestRealDb(context => { var service = new LuUtilsPkGuidDataAccess(context); var res = service.AddId(new PkGuidAddDbo { SomeInt = 42, SomeText = "42" }); Assert.Equal(LuStatus.Success, res.Status); Assert.NotEqual(new Guid().ToString(), res.Data); var get = service.GetSingleById(res.Data); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal("42", get.Data.SomeText); Assert.Equal(42, get.Data.SomeInt); Assert.Equal(res.Data, get.Data.Id); }); } [Fact] public void TestAddIdMultiple1() { Tests.TestRealDb(context => { var dbos = new List { new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }, new PkBigSerialAddDbo { SomeInt = 21, SomeText = "24" } }; var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.AddId(dbos); Assert.Equal(LuStatus.Success, res.Status); foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair(dbo, serialDbo))) { Assert.NotNull(dbo.Key); Assert.NotNull(dbo.Value); Assert.NotEqual(0, dbo.Value); var get = service.GetSingleById(dbo.Value); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal(dbo.Key.SomeText, get.Data.SomeText); Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt); Assert.Equal(dbo.Value, get.Data.Id); } }); } [Fact] public void TestAddIdSingle1() { Tests.TestRealDb(context => { var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.AddId(new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }); Assert.Equal(LuStatus.Success, res.Status); Assert.NotEqual(0, res.Data); var get = service.GetSingleById(res.Data); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal("42", get.Data.SomeText); Assert.Equal(42, get.Data.SomeInt); Assert.Equal(res.Data, get.Data.Id); }); } [Fact] public void TestAddDboMultiple1() { Tests.TestRealDb(context => { var dbos = new List { new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }, new PkBigSerialAddDbo { SomeInt = 21, SomeText = "24" } }; var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.AddDbo(dbos); Assert.Equal(LuStatus.Success, res.Status); foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair(dbo, serialDbo))) { Assert.NotNull(dbo.Key); Assert.NotNull(dbo.Value); Assert.NotEqual(0, dbo.Value.Id); var get = service.GetSingleById(dbo.Value.Id); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal(dbo.Key.SomeText, get.Data.SomeText); Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt); Assert.Equal(dbo.Value.Id, get.Data.Id); } }); } [Fact] public void TestAddDboSingle1() { Tests.TestRealDb(context => { var service = new LuUtilsPkBigSerialDataAccess(context); var res = service.AddDbo(new PkBigSerialAddDbo { SomeInt = 42, SomeText = "42" }); Assert.Equal(LuStatus.Success, res.Status); Assert.NotEqual(0, res.Data.Id); var get = service.GetSingleById(res.Data.Id); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal("42", get.Data.SomeText); Assert.Equal(42, get.Data.SomeInt); Assert.Equal(res.Data.Id, get.Data.Id); }); } } }