mirror of
https://github.com/Biarity/Sieve.git
synced 2025-07-26 12:13:28 +02:00
Use Comment.Text instead of ValueObjects.Name
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using Sieve.Attributes;
|
||||
using SieveUnitTests.ValueObjects;
|
||||
|
||||
namespace SieveUnitTests.Entities
|
||||
{
|
||||
@@ -8,10 +7,6 @@ namespace SieveUnitTests.Entities
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public Name AuthorFirstName { get; set; }
|
||||
|
||||
public Name AuthorLastName { get; set; }
|
||||
|
||||
[Sieve(CanFilter = true, CanSort = true)]
|
||||
public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.UtcNow;
|
||||
|
||||
|
@@ -32,5 +32,6 @@ namespace SieveUnitTests.Entities
|
||||
public int OnlySortableViaFluentApi { get; set; }
|
||||
|
||||
public Comment TopComment { get; set; }
|
||||
public Comment FeaturedComment { get; set; }
|
||||
}
|
||||
}
|
||||
|
@@ -7,7 +7,6 @@ using Sieve.Models;
|
||||
using Sieve.Services;
|
||||
using SieveUnitTests.Entities;
|
||||
using SieveUnitTests.Services;
|
||||
using SieveUnitTests.ValueObjects;
|
||||
|
||||
namespace SieveUnitTests
|
||||
{
|
||||
@@ -32,7 +31,8 @@ namespace SieveUnitTests
|
||||
LikeCount = 100,
|
||||
IsDraft = true,
|
||||
CategoryId = null,
|
||||
TopComment = new Comment { Id = 0, Text = "A" }
|
||||
TopComment = new Comment { Id = 0, Text = "A1" },
|
||||
FeaturedComment = new Comment { Id = 4, Text = "A2" }
|
||||
},
|
||||
new Post() {
|
||||
Id = 1,
|
||||
@@ -40,14 +40,16 @@ namespace SieveUnitTests
|
||||
LikeCount = 50,
|
||||
IsDraft = false,
|
||||
CategoryId = 1,
|
||||
TopComment = new Comment { Id = 3, Text = "A" }
|
||||
TopComment = new Comment { Id = 3, Text = "B1" },
|
||||
FeaturedComment = new Comment { Id = 5, Text = "B2" }
|
||||
},
|
||||
new Post() {
|
||||
Id = 2,
|
||||
Title = "C",
|
||||
LikeCount = 0,
|
||||
CategoryId = 1,
|
||||
TopComment = new Comment { Id = 2, Text = "Z" }
|
||||
TopComment = new Comment { Id = 2, Text = "C1" },
|
||||
FeaturedComment = new Comment { Id = 6, Text = "C2" }
|
||||
},
|
||||
new Post() {
|
||||
Id = 3,
|
||||
@@ -55,7 +57,8 @@ namespace SieveUnitTests
|
||||
LikeCount = 3,
|
||||
IsDraft = true,
|
||||
CategoryId = 2,
|
||||
TopComment = new Comment { Id = 1, Text = "Z" }
|
||||
TopComment = new Comment { Id = 1, Text = "D1" },
|
||||
FeaturedComment = new Comment { Id = 7, Text = "D2" }
|
||||
},
|
||||
}.AsQueryable();
|
||||
|
||||
@@ -64,23 +67,17 @@ namespace SieveUnitTests
|
||||
new Comment() {
|
||||
Id = 0,
|
||||
DateCreated = DateTimeOffset.UtcNow.AddDays(-20),
|
||||
Text = "This is an old comment.",
|
||||
AuthorFirstName = new Name("FirstName1"),
|
||||
AuthorLastName = new Name("LastName1")
|
||||
Text = "This is an old comment."
|
||||
},
|
||||
new Comment() {
|
||||
Id = 1,
|
||||
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
|
||||
Text = "This is a fairly new comment.",
|
||||
AuthorFirstName = new Name("FirstName2"),
|
||||
AuthorLastName = new Name("LastName2")
|
||||
Text = "This is a fairly new comment."
|
||||
},
|
||||
new Comment() {
|
||||
Id = 2,
|
||||
DateCreated = DateTimeOffset.UtcNow,
|
||||
Text = "This is a brand new comment. ()",
|
||||
AuthorFirstName = new Name("FirstName3"),
|
||||
AuthorLastName = new Name("LastName3")
|
||||
Text = "This is a brand new comment. (Text in braces)"
|
||||
},
|
||||
}.AsQueryable();
|
||||
}
|
||||
@@ -350,10 +347,11 @@ namespace SieveUnitTests
|
||||
};
|
||||
|
||||
var result = _processor.Apply(model, _posts);
|
||||
Assert.AreEqual(2, result.Count());
|
||||
Assert.AreEqual(3, result.Count());
|
||||
var posts = result.ToList();
|
||||
Assert.IsTrue(posts[0].TopComment.Text.Contains("Z"));
|
||||
Assert.IsTrue(posts[1].TopComment.Text.Contains("Z"));
|
||||
Assert.IsTrue(posts[0].TopComment.Text.Contains("B"));
|
||||
Assert.IsTrue(posts[1].TopComment.Text.Contains("C"));
|
||||
Assert.IsTrue(posts[2].TopComment.Text.Contains("D"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -378,14 +376,19 @@ namespace SieveUnitTests
|
||||
{
|
||||
var model = new SieveModel()
|
||||
{
|
||||
Filters = "(firstName|lastName)@=*2",
|
||||
Filters = "(topc|featc)@=*2",
|
||||
};
|
||||
|
||||
var result = _processor.Apply(model, _comments);
|
||||
Assert.AreEqual(1, result.Count());
|
||||
var result = _processor.Apply(model, _posts);
|
||||
Assert.AreEqual(4, result.Count());
|
||||
|
||||
var comment = result.First();
|
||||
Assert.AreEqual(comment.Id, 1);
|
||||
model = new SieveModel()
|
||||
{
|
||||
Filters = "(topc|featc)@=*B",
|
||||
};
|
||||
|
||||
result = _processor.Apply(model, _posts);
|
||||
Assert.AreEqual(1, result.Count());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -31,13 +31,13 @@ namespace SieveUnitTests.Services
|
||||
mapper.Property<Post>(p => p.OnlySortableViaFluentApi)
|
||||
.CanSort();
|
||||
|
||||
mapper.Property<Comment>(c => c.AuthorFirstName.Value)
|
||||
mapper.Property<Post>(p => p.TopComment.Text)
|
||||
.CanFilter()
|
||||
.HasName("firstName");
|
||||
.HasName("topc");
|
||||
|
||||
mapper.Property<Comment>(c => c.AuthorLastName.Value)
|
||||
mapper.Property<Post>(p => p.FeaturedComment.Text)
|
||||
.CanFilter()
|
||||
.HasName("lastName");
|
||||
.HasName("featc");
|
||||
|
||||
return mapper;
|
||||
}
|
||||
|
@@ -1,43 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace SieveUnitTests.ValueObjects
|
||||
{
|
||||
public sealed class Name : IEquatable<Name>
|
||||
{
|
||||
public Name(string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
throw new InvalidOperationException("Invalid string!");
|
||||
}
|
||||
|
||||
if (value.Length > 50)
|
||||
{
|
||||
throw new InvalidOperationException("String exceeds maximum name length!");
|
||||
}
|
||||
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public string Value { get; private set; }
|
||||
|
||||
public bool Equals(Name other)
|
||||
{
|
||||
if (ReferenceEquals(null, other)) return false;
|
||||
if (ReferenceEquals(this, other)) return true;
|
||||
return string.Equals(Value, other.Value);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
return obj is Name && Equals((Name) obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return (Value != null ? Value.GetHashCode() : 0);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user