[TestFixture] public class TwoLeftJoins { void ExecuteTwoLeftJoins(bool isSmart) { var dataModel = new TestDataModel {Settings = {IsSmart = isSmart}}; const int carId = 100; dataModel.CAR.AddObject(new CAR{CAR_ID = carId}); dataModel.CAR.AddObject(new CAR{CAR_ID = carId + 1}); var cars = (from car in dataModel.CAR join door in dataModel.DOOR on car.CAR_ID equals door.CAR_ID into joinedDoor from door in joinedDoor.DefaultIfEmpty() join doorHandle in dataModel.DOOR_HANDLE on door.DOOR_ID equals doorHandle.DOOR_ID into joinedDoorHandle from doorHandle in joinedDoorHandle.DefaultIfEmpty() select car).ToList(); Assert.AreEqual(2, cars.Count); Assert.AreEqual(carId, cars.First().CAR_ID); } [Test] public void TwoLeftJoinsShouldThrow() { Assert.Throws<NullReferenceException>(() => ExecuteTwoLeftJoins(false)); } [Test] public void SmartTwoLeftJoinsShouldNotThrow() { ExecuteTwoLeftJoins(true); } }
[TestFixture] public class Contains { public void ExecuteContains(bool isSmart) { var dataModel = new TestDataModel { Settings = { IsSmart = isSmart } }; new[] { new DOOR_HANDLE {DOOR_HANDLE_ID = 1, MATERIAL_ID = 1}, new DOOR_HANDLE {DOOR_HANDLE_ID = 2, MATERIAL_ID = 2}, new DOOR_HANDLE {DOOR_HANDLE_ID = 3, MATERIAL_ID = null} } .ForEach(dataModel.DOOR_HANDLE.AddObject); var doorHandleIds = new List<int>{1,2}; var doorHandles = (from doorHandle in dataModel.DOOR_HANDLE where doorHandleIds.Contains(doorHandle.MATERIAL_ID.Value) select doorHandle).ToList(); Assert.AreEqual(2, doorHandles.Count); } [Test] public void ContainsShouldFail() { Assert.Throws<InvalidOperationException>(() => ExecuteContains(false)); } [Test] public void SmartContainsShouldSuccess() { ExecuteContains(true); } }
[TestFixture] public class SumFromEmptyTable { void ExecuteSumFromEmptyTable(bool isSmart) { var dataModel = new TestDataModel {Settings = {IsSmart = isSmart}}; int sum = dataModel.CAR.Sum(x => x.CAR_ID); } [Test] public void SmartSumShouldThrow() { Assert.Throws<InvalidOperationException>(() => ExecuteSumFromEmptyTable(true)); } [Test] public void SumShouldNotThrow() { ExecuteSumFromEmptyTable(false); } void ExecuteNullableSumFromEmptyTable(bool isSmart) { var dataModel = new TestDataModel { Settings = { IsSmart = isSmart } }; int? sum = dataModel.DOOR_HANDLE.Sum(x => x.MATERIAL_ID); Assert.AreEqual(null, sum); } [Test] public void NullableSumShouldFail() { Assert.Throws<AssertionException>(() => ExecuteNullableSumFromEmptyTable(false)); } [Test] public void NullableSmartSumShouldSuccess() { ExecuteNullableSumFromEmptyTable(true); } }
[TestFixture] public class NullComparison { void ExecuteNullComparison(bool isSmart) { var dataModel = new TestDataModel { Settings = { IsSmart = isSmart } }; new[] { new DOOR_HANDLE {DOOR_HANDLE_ID = 1, MATERIAL_ID = 1, MANUFACTURER_ID = 1}, // <---- new DOOR_HANDLE {DOOR_HANDLE_ID = 2, MATERIAL_ID = 2, MANUFACTURER_ID = 2}, // |-- this is only pair new DOOR_HANDLE {DOOR_HANDLE_ID = 3, MATERIAL_ID = 1, MANUFACTURER_ID = 1}, // <---- new DOOR_HANDLE {DOOR_HANDLE_ID = 4, MATERIAL_ID = 5, MANUFACTURER_ID = null}, new DOOR_HANDLE {DOOR_HANDLE_ID = 5, MATERIAL_ID = 5, MANUFACTURER_ID = null}, new DOOR_HANDLE {DOOR_HANDLE_ID = 6, MATERIAL_ID = null, MANUFACTURER_ID = null}, new DOOR_HANDLE {DOOR_HANDLE_ID = 7, MATERIAL_ID = null, MANUFACTURER_ID = null} } .ForEach(x => dataModel.DOOR_HANDLE.AddObject(x)); var handlePairsWithSameMaterialAndManufacturer = (from handle in dataModel.DOOR_HANDLE join anotherHandle in dataModel.DOOR_HANDLE on handle.MATERIAL_ID equals anotherHandle.MATERIAL_ID where handle.MANUFACTURER_ID == anotherHandle.MANUFACTURER_ID && handle.DOOR_HANDLE_ID < anotherHandle.DOOR_HANDLE_ID select new {handle, anotherHandle}).ToList(); Assert.AreEqual(1, handlePairsWithSameMaterialAndManufacturer.Count); var pair = handlePairsWithSameMaterialAndManufacturer.First(); Assert.AreEqual(1, pair.handle.MATERIAL_ID); Assert.AreEqual(pair.handle.MATERIAL_ID, pair.anotherHandle.MATERIAL_ID); Assert.AreEqual(1, pair.handle.MANUFACTURER_ID); Assert.AreEqual(pair.handle.MANUFACTURER_ID, pair.anotherHandle.MANUFACTURER_ID); } [Test] public void NullComparisonShouldFail() { Assert.Throws<AssertionException>(() => ExecuteNullComparison(false)); } [Test] public void SmartNullComparisonShouldSuccess() { ExecuteNullComparison(true); } }
public System.Linq.Expressions.Expression Expression { get { return _collection.AsQueryable<T>().ToTestable().Expression; } } public IQueryProvider Provider { get { return _collection.AsQueryable<T>().ToTestable().Provider; } }
public Expression Expression { get { return _settings.IsSmart ? _collection.AsQueryable().ToTestable().Expression : _collection.AsQueryable().Expression; } } public IQueryProvider Provider { get { return _settings.IsSmart ? _collection.AsQueryable().ToTestable().Provider : _collection.AsQueryable().Provider; } }
Source: https://habr.com/ru/post/273801/
All Articles