public async Task Read_By_Where_Condition() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange await connection.Create(new City() { CityCode = "PUP", CityName = "Portsmouth", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "SOU", CityName = "Southampton", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "BOU", CityName = "Bournemouth", Area = "Dorset" }); IEnumerable <City> cities = await connection.ReadList <City>(new { Area = "Hampshire" }); Assert.AreEqual(2, cities.Count()); } }
public async Task Insert_With_Composite_Key_Two_Assigned_And_One_Sequential() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Act AssignedPairAndSequential oneOneOne = await connection.Create(new AssignedPairAndSequential() { FirstAssignedId = 1, SecondAssignedId = 1, Heading = "One" }); AssignedPairAndSequential oneOneTwo = await connection.Create(new AssignedPairAndSequential() { FirstAssignedId = 1, SecondAssignedId = 1, Heading = "Two" }); AssignedPairAndSequential oneTwoOne = await connection.Create(new AssignedPairAndSequential() { FirstAssignedId = 1, SecondAssignedId = 2, Heading = "One" }); // Assert Assert.AreEqual(1, oneOneOne.SequentialId); Assert.AreEqual(2, oneOneTwo.SequentialId); Assert.AreEqual(1, oneTwoOne.SequentialId); } }
public async Task Read_Paged_With_Where_Dictionary() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange await connection.Create(new City() { CityCode = "PUP", CityName = "Portsmouth", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "SOU", CityName = "Southampton", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "BAS", CityName = "Basingstoke", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "WIN", CityName = "Winchester", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "PTR", CityName = "Petersfield", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "CHI", CityName = "Chichester", Area = "West Sussex" }); await connection.Create(new City() { CityCode = "BOG", CityName = "Bognor Regis", Area = "West Sussex" }); await connection.Create(new City() { CityCode = "BOU", CityName = "Bournemouth", Area = "Dorset" }); // Act PagedList <City> pagedList = await connection.ReadList <City>( new Dictionary <string, object>() { { "Area", "Hampshire" } }, null, 4, 1); // Assert Assert.AreEqual(4, pagedList.Rows.Count()); Assert.AreEqual(false, pagedList.HasPrevious); Assert.AreEqual(true, pagedList.HasNext); Assert.AreEqual(2, pagedList.TotalPages); Assert.AreEqual(5, pagedList.TotalRows); } }
public static IDataContext GetDataContext(Type dataContextType) { // check whether we are dealing with a type that implement IDataContext if (typeof(IDataContext).IsAssignableFrom(dataContextType) == false) { throw new ArgumentException($"Type {dataContextType.Name} does not implement IDataContext."); } // return the data context for the given type if (typeof(InMemoryDataContext).IsAssignableFrom(dataContextType)) { return(new InMemoryDataContext()); } else if (typeof(SqlServerDataContext).IsAssignableFrom(dataContextType)) { // create the test database LocalDbTestHelper.CreateTestDatabase(TestContext.CurrentContext.Test.FullName); // return data context return(new SqlServerDataContext(LocalDbTestHelper.GetTestConnectionString(TestContext.CurrentContext.Test.FullName))); } else { throw new ArgumentException($"Type {dataContextType} is not supported, add support in {nameof(DataContextTestHelper)}.cs"); } }
public async Task Delete_List_Of_Entities() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange await connection.Create <City>(new City() { CityCode = "BAS", CityName = "Basingstoke", Area = "Hampshire" }); await connection.Create <City>(new City() { CityCode = "PUP", CityName = "Portsmouth", Area = "Hampshire" }); await connection.Create <City>(new City() { CityCode = "BOU", CityName = "Bournemouth", Area = "Dorset" }); await connection.Create <City>(new City() { CityCode = "HAV", CityName = "Havant", Area = "Hampshire" }); // Act await connection.DeleteList <City>(new { Area = "Hampshire" }); // Assert Assert.AreEqual(0, (await connection.ReadList <City>(new { Area = "Hampshire" })).Count()); Assert.AreEqual(1, (await connection.ReadAll <City>()).Count()); } }
public async Task Delete_List_No_Conditions() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange await connection.Create <City>(new City() { CityCode = "BAS", CityName = "Basingstoke", Area = "Hampshire" }); await connection.Create <City>(new City() { CityCode = "PUP", CityName = "Portsmouth", Area = "Hampshire" }); await connection.Create <City>(new City() { CityCode = "BOU", CityName = "Bournemouth", Area = "Dorset" }); await connection.Create <City>(new City() { CityCode = "HAV", CityName = "Havant", Area = "Hampshire" }); // Act / Assert Assert.ThrowsAsync <ArgumentException>(async() => { await connection.DeleteList <City>(null); }); Assert.ThrowsAsync <ArgumentException>(async() => { await connection.DeleteList <City>(new object()); }); } }
public async Task Update_With_Datestamp() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange DateStamp row = await connection.Create(new DateStamp() { Name = "Key", Value = "Value" }) .ConfigureAwait(false); // sleep so that insert date and update date will be different when update called Thread.Sleep(TimeSpan.FromMilliseconds(100)); // Act DateTime updateDate = DateTime.Now; DateStamp updatedRow = await connection.Update <DateStamp>(new { row.Name, Value = "New Value" }) .ConfigureAwait(false); // Assert Assert.AreEqual(row.Name, updatedRow.Name); Assert.AreEqual("New Value", updatedRow.Value); Assert.AreEqual(0, (row.InsertDate - updatedRow.InsertDate).Seconds); Assert.AreNotEqual(row.InsertDate, updatedRow.UpdateDate); Assert.That(updatedRow.UpdateDate, Is.EqualTo(updateDate).Within(TimeSpan.FromSeconds(1))); } }
public async Task Update_Editable_Properties() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange City city = await connection.Create <City>(new City() { CityCode = "BAS", CityName = "Basingstoke", Area = "Hampshire" }); // Act City basVegas = await connection.Update <City>(new { city.CityId, CityCode = "BV", CityName = "Bas Vegas!", Area = "The Strip" }); Assert.AreEqual(city.CityId, basVegas.CityId); Assert.AreEqual("BV", basVegas.CityCode); Assert.AreEqual("The Strip", basVegas.Area); Assert.AreEqual("Bas Vegas!", basVegas.CityName); } }
public async Task Read_Paged_Last_Page() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange - seed some data await connection.Create(new City() { CityCode = "PUP", CityName = "Portsmouth", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "SOU", CityName = "Southampton", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "BAS", CityName = "Basingstoke", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "WIN", CityName = "Winchester", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "PTR", CityName = "Petersfield", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "CHI", CityName = "Chichester", Area = "West Sussex" }); await connection.Create(new City() { CityCode = "BOG", CityName = "Bognor Regis", Area = "West Sussex" }); await connection.Create(new City() { CityCode = "BOU", CityName = "Bournemouth", Area = "Dorset" }); // Arrange - read the first page to see how many pages there are int lastPage = (await connection.ReadList <City>(null, null, 3, 1)).TotalPages; // Act - read the last page PagedList <City> pagedList = await connection.ReadList <City>(null, null, 3, lastPage); // Assert Assert.AreEqual(pagedList.TotalRows - ((lastPage - 1) * 3), pagedList.Rows.Count()); Assert.AreEqual(true, pagedList.HasPrevious); Assert.AreEqual(false, pagedList.HasNext); Assert.AreEqual(lastPage, pagedList.TotalPages); } }
public async Task Read_Paged_With_Where_Dictionary_And_OrderBy_Dictionary() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange await connection.Create(new City() { CityCode = "PUP", CityName = "Portsmouth", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "SOU", CityName = "Southampton", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "BAS", CityName = "Basingstoke", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "WIN", CityName = "Winchester", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "PTR", CityName = "Petersfield", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "CHI", CityName = "Chichester", Area = "West Sussex" }); await connection.Create(new City() { CityCode = "BOG", CityName = "Bognor Regis", Area = "West Sussex" }); await connection.Create(new City() { CityCode = "BOU", CityName = "Bournemouth", Area = "Dorset" }); // Act PagedList <City> pagedList = await connection.ReadList <City>( new Dictionary <string, object>() { { "Area", "Hampshire" } }, new Dictionary <string, SortOrder>() { { "CityName", SortOrder.Descending } }, 5, 1); // Assert string[] expectedOrder = { "Winchester", "Southampton", "Portsmouth", "Petersfield", "Basingstoke" }; Assert.IsTrue(pagedList.Rows.Select(x => x.CityName).SequenceEqual(expectedOrder)); } }
public static void DeleteDataContext(Type dataContextType) { // check whether we are dealing with a type that we need to dispose of if (typeof(SqlServerDataContext).IsAssignableFrom(dataContextType)) { // create the test database LocalDbTestHelper.DeleteTestDatabase(TestContext.CurrentContext.Test.FullName); } }
public async Task Read_Paged_With_Invalid_OrderBy_Dictionary() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange await connection.Create(new City() { CityCode = "PUP", CityName = "Portsmouth", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "SOU", CityName = "Southampton", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "BAS", CityName = "Basingstoke", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "WIN", CityName = "Winchester", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "PTR", CityName = "Petersfield", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "CHI", CityName = "Chichester", Area = "West Sussex" }); await connection.Create(new City() { CityCode = "BOG", CityName = "Bognor Regis", Area = "West Sussex" }); await connection.Create(new City() { CityCode = "BOU", CityName = "Bournemouth", Area = "Dorset" }); // Act / Assert Assert.ThrowsAsync <ArgumentException>(async() => { await connection.ReadList <City>(null, new Dictionary <string, SortOrder> { { "County", SortOrder.Descending } }, 1, 1); }); } }
public async Task Insert_For_Soft_Delete() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Act SoftDelete softDelete = await connection.Create <SoftDelete>(new SoftDelete()).ConfigureAwait(false); // Assert Assert.Greater(softDelete.SoftDeleteId, 0); Assert.AreEqual(1, softDelete.RecordStatus); } }
public async Task Update_Soft_Delete_Column() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange SoftDelete softDelete = await connection.Create <SoftDelete>(new SoftDelete()).ConfigureAwait(false); // Act / Assert Assert.ThrowsAsync <ArgumentException>(async() => { await connection.Update <SoftDelete>(new { softDelete.SoftDeleteId, RecordsStatus = 999 }); }); } }
public async Task Insert_With_Assigned_Key() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Act CityManual city = await connection.Create <CityManual>(new CityManual() { CityCode = "BRI", CityName = "Brighton" }).ConfigureAwait(false); // Assert Assert.AreEqual("BRI", city.CityCode); Assert.AreEqual("Brighton", city.CityName); } }
public async Task Delete_Entity_Single_Typed_Argument() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange City city = await connection.Create <City>(new City() { CityCode = "BAS", CityName = "Basingstoke", Area = "Hampshire" }); // Act await connection.Delete <City>(city.CityId); // Assert Assert.IsNull(await connection.Read <City>(new { city.CityId })); } }
public async Task Insert_With_Identity() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Act City city = await connection.Create <City>(new City() { CityCode = "BRI", CityName = "Brighton", Area = "Sussex" }) .ConfigureAwait(false); // Assert Assert.Greater(city.CityId, 0); Assert.AreEqual("BRI", city.CityCode); Assert.AreEqual("Brighton", city.CityName); Assert.AreEqual("Sussex", city.Area); } }
public async Task Insert_With_Datestamp() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Act DateTime now = DateTime.Now; DateStamp row = await connection.Create(new DateStamp() { Name = "Key", Value = "Value" }) .ConfigureAwait(false); // Assert Assert.AreEqual("Key", row.Name); Assert.AreEqual("Value", row.Value); Assert.AreEqual(row.InsertDate, row.UpdateDate); Assert.That(row.InsertDate, Is.EqualTo(now).Within(TimeSpan.FromSeconds(1))); } }
public async Task Read_All_With_Assigned() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange await connection.Create(new CityManual() { CityCode = "PUP", CityName = "Portsmouth" }); await connection.Create(new CityManual() { CityCode = "NYC", CityName = "New York City" }); // Act IEnumerable <CityManual> cities = await connection.ReadAll <CityManual>(); // Assert Assert.AreEqual(2, cities.Count()); } }
public async Task Read_By_Id_With_Assigned_Single_Typed_Argument() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange await connection.Create(new CityManual() { CityCode = "PUP", CityName = "Portsmouth" }); await connection.Create(new CityManual() { CityCode = "NYC", CityName = "New York City" }); // Act CityManual city = await connection.Read <CityManual>("NYC"); // Assert Assert.AreEqual("NYC", city.CityCode); Assert.AreEqual("New York City", city.CityName); } }
public async Task Read_All_With_Identity() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange await connection.Create(new City() { CityCode = "PUP", CityName = "Portsmouth", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "BOU", CityName = "Bournemouth", Area = "Dorset" }); // Act IEnumerable <City> cities = await connection.ReadAll <City>(); // Assert Assert.AreEqual(2, cities.Count()); Assert.Greater(cities.ElementAt(0).CityId, 0); Assert.Greater(cities.ElementAt(1).CityId, cities.ElementAt(0).CityId); } }
public async Task Update_Read_Only_Column() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange ReadOnly readOnly = await connection.Create <ReadOnly>(new ReadOnly() { Editable = "Hello", ReadOnlyProperty = "World" }); // Act readOnly = await connection.Update <ReadOnly>(new { readOnly.SequentialId, Editable = "Goodbye", ReadOnlyProperty = "Yesterday" }); // Assert Assert.AreEqual("Goodbye", readOnly.Editable); Assert.AreEqual("World", readOnly.ReadOnlyProperty); } }
public async Task Read_By_Id_With_Identity_Property_Bag() { using (IDbConnection connection = LocalDbTestHelper.OpenTestConnection(TestContext.CurrentContext.Test.FullName)) { // Arrange City pup = await connection.Create(new City() { CityCode = "PUP", CityName = "Portsmouth", Area = "Hampshire" }); await connection.Create(new City() { CityCode = "NYC", CityName = "New York City", Area = "New York" }); // Act City city = await connection.Read <City>(new { pup.CityId }); // Assert Assert.AreEqual(pup.CityId, city.CityId); Assert.AreEqual("PUP", city.CityCode); Assert.AreEqual("Portsmouth", city.CityName); } }
public void TearDown() { LocalDbTestHelper.DeleteTestDatabase(TestContext.CurrentContext.Test.FullName); }
public void Setup() { LocalDbTestHelper.CreateTestDatabase(TestContext.CurrentContext.Test.FullName); }