Skip to content

Commit

Permalink
Merge pull request #8 from DCCS-IT-Business-Solutions/optional_sort_page
Browse files Browse the repository at this point in the history
sorting and paging now optional v. 1.2.0
  • Loading branch information
stephanmeissner authored Jun 26, 2020
2 parents 07398bd + b322641 commit 532685d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
61 changes: 56 additions & 5 deletions DCCS.Data.Source.Tests/ResultTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,33 @@ public void Should_map_data()
}

[Test]
public void Should_bootstrap_from_provided_data()
public void Should_bootstrap_from_provided_enumerable()
{
var data = new Faker<Dummy>().Generate(13);
var ps = new Params { Count = 10, Page = 1 };
var sut = new Result<Dummy>(ps, data.AsEnumerable(), 99);
var total = 99;
var sut = new Result<Dummy>(ps, data.AsEnumerable(), total);

Assert.AreEqual(sut.Count, data.Count());
Assert.AreEqual(sut.Desc, ps.Desc);
Assert.AreEqual(sut.OrderBy, ps.OrderBy);
Assert.AreEqual(sut.Page, ps.Page);
Assert.AreEqual(sut.Total, 99);
Assert.AreEqual(sut.Total, total);
}

[Test]
public void Should_not_sort_provided_enumerable()
{
// Arrage
var data = new Faker<Dummy>().Generate(13).OrderByDescending(d => d.Name);
var ps = new Params { Count = 10, Page = 1 };

// Act
var sut = new Result<Dummy>(ps, data.AsEnumerable(), data.Count());

// Assert
Assert.AreEqual(sut.Data.First(), data.First());
Assert.AreEqual(sut.Total, data.Count());
}


Expand All @@ -106,9 +122,9 @@ public void Should_order_data()
data.Add(new Dummy { Name = "1" });
data.Add(new Dummy { Name = "7" });
var ps = new Params { OrderBy = "name" };
var sut = new Result<Dummy>(ps, data.AsQueryable());
var sut = new Result<Dummy>(ps, data.AsQueryable());
var sorted = sut.Data.ToArray();

Assert.AreEqual("1", sorted[0].Name);
Assert.AreEqual("2", sorted[1].Name);
Assert.AreEqual("7", sorted[2].Name);
Expand Down Expand Up @@ -136,5 +152,40 @@ public void Should_fail_for_invalid_order()
}

}

[Test]
public void Should_not_sort_if_unchecked()
{
// Arrange
var data = new List<Dummy>();
data.Add(new Dummy { Name = "2" });
data.Add(new Dummy { Name = "1" });
data.Add(new Dummy { Name = "7" });
var ps = new Params { OrderBy = "name" };
// Act
var sut = new Result<Dummy>(ps, data.AsQueryable(), sort: false);

// Assert
var sorted = sut.Data.ToArray();

Assert.AreEqual("2", sorted[0].Name);
Assert.AreEqual("1", sorted[1].Name);
Assert.AreEqual("7", sorted[2].Name);

}

[Test]
public void Should_not_page_if_unchecked()
{
// Arrange
var data = new Faker<Dummy>().Generate(13);
var ps = new Params { Page = 2, Count = 10 };
// Act
var sut = new Result<Dummy>(ps, data.AsQueryable(), page: false);

// Assert
Assert.AreEqual(data.Count(), sut.Data.Count());
}

}
}
2 changes: 1 addition & 1 deletion DCCS.Data.Source/DCCS.Data.Source.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Mario te Best, Stephan Meißner</Authors>
<Company>DCCS GmbH</Company>
<Version>1.1.2</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions DCCS.Data.Source/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace DCCS.Data.Source
{
public class Result<T> : ResultWithoutTotal<T>
{
public Result(Params ps, IQueryable<T> data = null) : base(ps, data)
public Result(Params ps, IQueryable<T> data = null, bool sort = true, bool page = true) : base(ps, data, sort, page)
{
}

Expand All @@ -17,7 +17,7 @@ public Result(Params ps, IEnumerable<T> data, int total) : base(ps, data)
Total = total;
}

public override void SetData(IQueryable<T> data)
public override void SetData(IQueryable<T> data, bool sort, bool page)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
Expand All @@ -26,7 +26,7 @@ public override void SetData(IQueryable<T> data)
{
Count = Math.Min(Count.Value, Total);
}
base.SetData(data);
base.SetData(data, sort: sort, page: page);
}

public int Total { get; set; }
Expand Down
15 changes: 9 additions & 6 deletions DCCS.Data.Source/ResultWithoutTotal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ public class ResultWithoutTotal<T> : Params
public ResultWithoutTotal(Params ps) : base(ps)
{ }

public ResultWithoutTotal(Params ps, IQueryable<T> data) : base(ps)
public ResultWithoutTotal(Params ps, IQueryable<T> data, bool sort = true, bool page = true) : base(ps)
{
if (data != null)
SetData(data);
{
SetData(data, sort: sort, page: page);
}
}

public ResultWithoutTotal(Params ps, IEnumerable<T> data) : base(ps)
Expand All @@ -26,7 +28,7 @@ public ResultWithoutTotal(Params ps, IEnumerable<T> data) : base(ps)
Count = Data.Count();
}

public virtual void SetData(IQueryable<T> data)
public virtual void SetData(IQueryable<T> data, bool sort, bool page)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
Expand All @@ -36,8 +38,9 @@ public virtual void SetData(IQueryable<T> data)
}
else
{
var result = Sort(data);
result = Paging(result);
var result = data;
if (sort) { result = Sort(result); }
if (page) { result = Paging(result); }
Data = result.ToArray();
}
}
Expand Down Expand Up @@ -85,7 +88,7 @@ protected IQueryable<T> Paging(IQueryable<T> data)
IQueryable<T> tempresult = null; // Wird für "Kann diese Seite überhaupt angezeigt werden" benötigt
if (Page.HasValue)
{
if (!Count.HasValue)
if (!Count.HasValue)
throw new ArgumentNullException($"With specified {nameof(Page)} is the {nameof(Count)} required");

//Manuel 24.10.2016
Expand Down

0 comments on commit 532685d

Please sign in to comment.