2018-02-10 01:26:32 +01:00
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Sieve.Models;
|
|
|
|
using Sieve.Services;
|
|
|
|
using SieveUnitTests.Entities;
|
|
|
|
using SieveUnitTests.Services;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Collections.Generic;
|
2018-02-13 23:43:33 +01:00
|
|
|
using Sieve.Exceptions;
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
|
|
namespace SieveUnitTests
|
|
|
|
{
|
|
|
|
[TestClass]
|
|
|
|
public class General
|
|
|
|
{
|
|
|
|
private SieveProcessor _processor;
|
|
|
|
private IQueryable<Post> _posts;
|
|
|
|
|
|
|
|
public General()
|
|
|
|
{
|
|
|
|
_processor = new SieveProcessor(new SieveOptionsAccessor(),
|
|
|
|
new SieveCustomSortMethods(),
|
|
|
|
new SieveCustomFilterMethods());
|
|
|
|
|
|
|
|
_posts = new List<Post>
|
|
|
|
{
|
|
|
|
new Post() {
|
|
|
|
Id = 0,
|
|
|
|
Title = "A",
|
2018-04-08 05:46:16 +02:00
|
|
|
LikeCount = 100,
|
|
|
|
IsDraft = true
|
2018-02-10 01:26:32 +01:00
|
|
|
},
|
|
|
|
new Post() {
|
|
|
|
Id = 1,
|
|
|
|
Title = "B",
|
2018-04-08 05:46:16 +02:00
|
|
|
LikeCount = 50,
|
|
|
|
IsDraft = false
|
2018-02-10 01:26:32 +01:00
|
|
|
},
|
|
|
|
new Post() {
|
|
|
|
Id = 2,
|
|
|
|
Title = "C",
|
|
|
|
LikeCount = 0
|
|
|
|
},
|
2018-04-20 10:46:04 +02:00
|
|
|
new Post() {
|
|
|
|
Id = 3,
|
|
|
|
Title = "3",
|
|
|
|
LikeCount = 3,
|
|
|
|
IsDraft = true
|
|
|
|
},
|
2018-02-10 01:26:32 +01:00
|
|
|
}.AsQueryable();
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void ContainsCanBeCaseInsensitive()
|
|
|
|
{
|
|
|
|
var model = new SieveModel()
|
|
|
|
{
|
|
|
|
Filters = "Title@=*a"
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
|
|
Assert.AreEqual(result.First().Id, 0);
|
|
|
|
Assert.IsTrue(result.Count() == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void ContainsIsCaseSensitive()
|
|
|
|
{
|
|
|
|
var model = new SieveModel()
|
|
|
|
{
|
|
|
|
Filters = "Title@=a",
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
|
|
Assert.IsTrue(result.Count() == 0);
|
|
|
|
}
|
|
|
|
|
2018-04-08 05:46:16 +02:00
|
|
|
[TestMethod]
|
|
|
|
public void CanFilterBools()
|
|
|
|
{
|
|
|
|
var model = new SieveModel()
|
|
|
|
{
|
|
|
|
Filters = "IsDraft==false"
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-04-08 05:46:16 +02:00
|
|
|
|
|
|
|
Assert.IsTrue(result.Count() == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void CanSortBools()
|
|
|
|
{
|
|
|
|
var model = new SieveModel()
|
|
|
|
{
|
|
|
|
Sorts = "-IsDraft"
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-04-08 05:46:16 +02:00
|
|
|
|
|
|
|
Assert.AreEqual(result.First().Id, 0);
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:26:32 +01:00
|
|
|
[TestMethod]
|
|
|
|
public void EqualsDoesntFailWithNonStringTypes()
|
|
|
|
{
|
|
|
|
var model = new SieveModel()
|
|
|
|
{
|
|
|
|
Filters = "LikeCount==50",
|
|
|
|
};
|
|
|
|
|
|
|
|
Console.WriteLine(model.FiltersParsed.First().Value);
|
|
|
|
Console.WriteLine(model.FiltersParsed.First().Operator);
|
|
|
|
Console.WriteLine(model.FiltersParsed.First().OperatorParsed);
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(result.First().Id, 1);
|
|
|
|
Assert.IsTrue(result.Count() == 1);
|
|
|
|
}
|
2018-02-11 04:35:33 +01:00
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void CustomFiltersWork()
|
|
|
|
{
|
|
|
|
var model = new SieveModel()
|
|
|
|
{
|
|
|
|
Filters = "Isnew",
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-02-11 04:35:33 +01:00
|
|
|
|
|
|
|
Assert.IsFalse(result.Any(p => p.Id == 0));
|
2018-04-20 10:46:04 +02:00
|
|
|
Assert.IsTrue(result.Count() == 3);
|
2018-02-11 04:35:33 +01:00
|
|
|
}
|
2018-02-13 23:43:33 +01:00
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void MethodNotFoundExceptionWork()
|
|
|
|
{
|
|
|
|
var model = new SieveModel()
|
|
|
|
{
|
|
|
|
Filters = "does not exist",
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
Assert.ThrowsException<SieveMethodNotFoundException>(() => _processor.Apply(model, _posts));
|
2018-02-13 23:43:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void IncompatibleMethodExceptionsWork()
|
|
|
|
{
|
|
|
|
var model = new SieveModel()
|
|
|
|
{
|
|
|
|
Filters = "TestComment",
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
Assert.ThrowsException<SieveIncompatibleMethodException>(() => _processor.Apply(model, _posts));
|
2018-02-13 23:43:33 +01:00
|
|
|
}
|
2018-04-20 10:46:04 +02:00
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
public void OrFilteringWorks()
|
|
|
|
{
|
|
|
|
var model = new SieveModel()
|
|
|
|
{
|
|
|
|
Filters = "(Title|LikeCount)==3",
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-04-20 10:46:04 +02:00
|
|
|
|
|
|
|
Assert.AreEqual(result.First().Id, 3);
|
|
|
|
Assert.IsTrue(result.Count() == 1);
|
|
|
|
}
|
2018-02-10 01:26:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//Sorts = "LikeCount",
|
|
|
|
//Page = 1,
|
|
|
|
//PageSize = 10
|
|
|
|
//
|