public void Page_SortsDescAsRequested()
        {
            var r = new List <Role>()
            {
                new Role()
                {
                    Created = new DateTime(2011, 3, 14)
                },
                new Role()
                {
                    Created = new DateTime(2013, 3, 14)
                },
                new Role()
                {
                    Created = new DateTime(2016, 3, 14)
                },
            };
            var repo = GetRepoWithData(r, nameof(Page_SortsDescAsRequested));
            var a    = new Dictionary <string, dynamic>()
            {
                { "Created", (Expression <Func <Role, DateTime> >)(t => t.Created) }
            };
            var f = new SortFactory <Role, long>(new[] { new SortSpecification("Created", SortDirection.Descending) }, a);

            var results = repo.Page(Specification <Role> .All(), 0, 3, f);

            Assert.True(r.OrderByDescending(c => c.Created).SequenceEqual(results, new EqComparer <Role>()));
        }
        public void FindAll_Includes()
        {
            var repoM = GetRepoMock <V_MyView>(nameof(FindAll_Includes), setupIncl: true);

            repoM.Object.FindAll(Specification <V_MyView> .All(), includes: r => r.Id);
            repoM.Verify();
        }
        public void Page_SortsDescAsRequested()
        {
            var r = new List <V_MyView>()
            {
                new V_MyView()
                {
                    Id = "1"
                },
                new V_MyView()
                {
                    Id = "3"
                },
                new V_MyView()
                {
                    Id = "2"
                },
            };
            var repo = GetRepoWithData(r, nameof(Page_SortsDescAsRequested));
            var a    = new[] { new SortSpecification("Id", SortDirection.Descending), };
            var d    = new Dictionary <string, dynamic>()
            {
                { "Id", (Expression <Func <V_MyView, string> >)(t => t.Id) }
            };
            var f       = new SortFactory <V_MyView, string>(a, d);
            var results = repo.Page(Specification <V_MyView> .All(), f, 0, 3);

            Assert.True(r.OrderByDescending(c => c.Id).SequenceEqual(results, new ReadOnlyEqComparer <V_MyView>()));
        }
        public void Page_TrackingIfSpecified()
        {
            var repoM = GetRepoMock <Role>(nameof(Page_TrackingIfSpecified));

            repoM.Object.Page <Role>(Specification <Role> .All(), track: true);
            repoM.Verify(m => m.NoTracking(It.IsAny <DbSet <Role> >()), Times.Never);
        }
        public void Page_NoTrackingDefault()
        {
            var repoM = GetRepoMock <Role>(nameof(Page_NoTrackingDefault));

            repoM.Object.Page(Specification <Role> .All());
            repoM.Verify();
        }
Пример #6
0
        public void Delete_Saves() // save is required, given its a soft delete.
        {
            // Setup
            var order = new List <string>();
            var rm    = Mock.Get(Manager.Repo);

            rm.Setup(m => m.Delete(
                         It.IsAny <ISpecification <User> >(), true))
            .Callback <ISpecification <User>, bool, Expression <Func <User, object> >[]>
                ((c, b, e) => order.Add("delete"));
            var am = Mock.Get(Manager.Auth);

            am.Setup(a => a.GenerateFilterDelete()).Returns(Specification <User> .All());
            var sm = Mock.Get(Manager.Specs);

            sm.Setup(a => a.ById <User>(It.IsAny <long>())).Returns(Specification <User> .All());
            rm.Setup(m => m.Save(false))
            .Callback((bool b) => order.Add("save"));

            // Test
            Manager.Delete(1);

            // Verify
            Assert.AreEqual("delete", order.First());
            Assert.AreEqual("save", order.Last());
        }
        public void Page_Includes()
        {
            var repoM = GetRepoMock <Role>(nameof(Page_Includes));

            repoM.Object.Page <Role>(Specification <Role> .All(), includes: r => r.UserRoles);
            repoM.Verify();
        }
Пример #8
0
        public void Get_UsesId_ReturnsMapped()
        {
            // Setup
            long input        = 314;
            var  repoOutput   = new User();
            var  mapperOutput = new UserViewModel();
            var  specOutput   = Specification <User> .Start(c => true);

            Manager.Specs = Mock.Of <IUserSpecificationProvider>(
                sp => sp.ById <User>(It.Is <long>(i => i == input)) == specOutput);
            Mock.Get(Manager.Auth)
            .Setup(a => a.GenerateFilterGet())
            .Returns(Specification <User> .All());
            var rm = Mock.Get(Manager.Repo);

            rm.Setup(m => m.FindOne(
                         It.Is <ISpecification <User> >(o => o == specOutput),
                         It.IsAny <bool>(), It.IsAny <bool>()))
            .Returns(repoOutput)
            .Verifiable();
            var mm = Mock.Get(Manager.Mapper);

            mm.Setup(m => m.Map <IViewModel <User> >(It.Is <User>(i => i == repoOutput)))
            .Returns(mapperOutput)
            .Verifiable();

            // Test
            Manager.Get(input);

            // Verify
            mm.Verify();
            rm.Verify();
            Mock.Get(Manager.Specs).Verify();
        }
Пример #9
0
        public void Filter_UsesSpecAndPageParams()
        {
            // Setup
            ISpecification <User> actualSpec = null;
            int actualPage = 0, actualPageSize = 0;
            var expectedSpec = Specification <User> .All();

            int expectedPage = 1, expectedPageSize = 10;
            var rm = Mock.Get(Manager.Repo)
                     .Setup(m => m.Page(It.IsAny <ISpecification <User> >(),
                                        It.IsAny <int>(), It.IsAny <int>(), It.IsAny <ISortFactory <User, long> >(), false, false));

            Mock.Get(Manager.Auth)
            .Setup(a => a.GenerateFilterGet())
            .Returns(Specification <User> .All());
            var list = new List <User>().OrderBy(c => c.Created);

            rm.Returns(list);
            rm.Callback <
                ISpecification <User>,
                int, int,
                ISortFactory <User, long>,
                bool, bool,
                Expression <Func <User, object> >[]>(
                (s, p, ps, a, b, b1, i) => { actualSpec = s; actualPage = p; actualPageSize = ps; });
            rm.Verifiable();

            // Test
            Manager.Filter(expectedSpec, expectedPage, expectedPageSize, new SortSpecification[0]);

            // Verify
            Assert.AreEqual(expectedSpec, actualSpec);
            Assert.AreEqual(expectedPage, actualPage);
            Assert.AreEqual(expectedPageSize, actualPageSize);
        }
        public void Page_SortsAsRequested()
        {
            // Setup
            var corresp = new List <User>()
            {
                new User()
                {
                    Created = new DateTime(2016, 3, 14)
                },
                new User()
                {
                    Created = new DateTime(2013, 3, 14)
                },
                new User()
                {
                    Created = new DateTime(2011, 3, 14)
                },
            };
            var repo = GetRepoWithData(corresp, nameof(Page_SortsAsRequested));
            var a    = new Dictionary <string, dynamic>()
            {
                { "Created", (Expression <Func <User, DateTime> >)(t => t.Created) }
            };
            var f = new SortFactory <User, long>(new[] { new SortSpecification("Created", SortDirection.Ascending) }, a);

            // Test
            var results = repo.Page(Specification <User> .All(), 0, 3, f);

            // Assert
            Assert.True(corresp.OrderBy(c => c.Created).SequenceEqual(results, new EqComparer <User>()));
        }
        public void Page_Pages()
        {
            var r = new List <User>()
            {
                new User()
                {
                    Id = 1
                },
                new User()
                {
                    Id = 2
                },
                new User()
                {
                    Id = 3
                },
                new User()
                {
                    Id = 4
                },
                new User()
                {
                    Id = 5
                },
            };
            var repo = GetRepoWithData(r, nameof(Page_Pages));

            var results = repo.Page(Specification <User> .All(), 1, 2);

            Assert.True(results.First().Id == 3);
            Assert.True(results.ElementAt(1).Id == 4);
        }
Пример #12
0
 public void All_Specifications_ShouldBeFalse()
 {
     Specification <Entity> .All(_value1ShouldBe1, _value2ShouldBe2)
     .IsSatisfiedBy(new Entity {
         Value1 = 1, Value2 = 1
     });
 }
        public void FindAll_Includes()
        {
            QueryableExtensions.QueryableExtensions.Includer = Mock.Of <QueryableExtensions.IIncluder>();
            var repoM = GetRepoMock <Role>(nameof(FindOne_Includes));

            repoM.Object.FindAll <Role>(Specification <Role> .All(), includes: r => r.UserRoles);
            Mock.Get(QueryableExtensions.QueryableExtensions.Includer)
            .Verify(i => i.Include(It.IsAny <IQueryable <Role> >(), It.IsAny <Expression <Func <Role, object> > >()), Times.Once);
        }
Пример #14
0
        public void InvokeAll_ReturnAllSpecification()
        {
            var specification = MockComplexSpecification <int> .True();

            var expected = new AllSpecification <FakeType, int>(specification, true);

            var sut = Specification.All <FakeType, int>(specification);

            Assert.Equal(expected, sut, new SpecificationComparer());
        }
        public void AllIsAll()
        {
            var list = new List <TestModel>()
            {
                new TestModel(),
                new TestModel()
            };
            var filtered = list.Where(Specification <TestModel> .All().AsExpression().Compile());

            Assert.Equal(2, filtered.Count());
        }
Пример #16
0
        public void InvokeAllProperty_ReturnPropertySpecification()
        {
            var specification = MockComplexSpecification <int> .True();

            var expected = new PropertySpecification <FakeType, IEnumerable <int> >(
                ft => ft.Fourth, new AllSpecification <IEnumerable <int>, int>(specification, true));

            var sut = Specification.All <FakeType, int>(
                ft => ft.Fourth, specification);

            Assert.Equal(expected, sut, new SpecificationComparer());
        }
        public ISpecification <T> RolesByFilter <T>(FilterModel filter) where T : Role
        {
            var words = filter.Keywords;

            if (words == null)
            {
                return(Specification <T> .All());
            }
            return(Specification <T> .Start((T t) => words.All(kw =>
                                                               (t.Description.Contains(kw)) ||
                                                               (t.RoleName.Contains(kw))), words));
        }
Пример #18
0
        public ISpecification <T> UsersByFilter <T>(UserFilterModel filter) where T : User
        {
            var keywords = filter.Keywords;
            ISpecification <T> userSpec;

            if (keywords == null)
            {
                userSpec = Specification <T> .All();
            }
            else
            {
                userSpec = Specification <T> .Start(
                    t => keywords.All(
                        kw => kw == null || kw.Trim() == string.Empty ||
                        (t.Username != null && t.Username.Contains(kw)) ||
                        (t.First != null && t.First.Contains(kw)) ||
                        (t.Last != null && t.Last.Contains(kw))), keywords);
            }
            if (filter.Type == UserFilterModel.UserFilterType.Keywords)
            {
                return(userSpec);
            }
            else if (filter.Type == UserFilterModel.UserFilterType.Username)
            {
                var username = filter.Username;
                return(Specification <T> .Start((T t) => username == t.Username, username));
            }
            else if (filter.Type == UserFilterModel.UserFilterType.RoleAndKeywords)
            {
                var roleId = filter.RoleId;
                var role   = Specification <T> .Start(t => roleId == 0 || t.UserRoles.Any(ur => ur.RoleId == roleId), roleId);

                return(userSpec.And(role));
            }
            else if (filter.Type == UserFilterModel.UserFilterType.AnyRoleAndKeywords)
            {
                if (filter.RoleIds == null)
                {
                    return(userSpec);
                }

                var role = Specification <T> .None();

                foreach (var roleId in filter.RoleIds)
                {
                    role = role.Or(u => u.UserRoles.Any(ur => ur.RoleId == roleId));
                }

                userSpec.Metadata = filter.RoleIds;
                return(userSpec.And(role));
            }
            throw new NotImplementedException($"User filter type {filter.Type} has not been implemented.");
        }
        public void Page_Includes()
        {
            var a = new[] { new SortSpecification("Id", SortDirection.Descending), };
            var d = new Dictionary <string, dynamic>()
            {
                { "Id", (Expression <Func <V_MyView, string> >)(t => t.Id) }
            };
            var f     = new SortFactory <V_MyView, string>(a, d);
            var repoM = GetRepoMock <V_MyView>(nameof(Page_Includes));

            repoM.Object.Page(Specification <V_MyView> .All(), f, includes: r => r.Id);
            repoM.Verify();
        }
Пример #20
0
        /// <summary>Finds all the entities.</summary>
        /// <param name="includePaths">The dot-separated lists of related objects to return in the query results.</param>
        /// <returns>The found entities or an empty list if there were no results.</returns>
        public IList <T> FindEntities(string[] includePaths)
        {
            Guard.ArgumentIsNotNull(includePaths, nameof(includePaths));

            if (includePaths.Length == 0 || includePaths.Any(path => string.IsNullOrEmpty(path)))
            {
                throw new ArgumentException("The include paths cannot be empty ans cannot contain empty or null values.", "includePaths");
            }

            ISpecification <T> specification = Specification.All <T>();

            includePaths.ForEach(path => specification.Include(path));
            return(this.FindEntitiesCore(specification));
        }
        public void GenerateFilterUpdate_SameAsGet()
        {
            var filter = Specification <User> .All();

            var m = new Mock <UserAuthManager>(Mock.Of <IPrincipal>(), Mock.Of <ILogger>());

            m.Setup(c => c.GenerateFilterUpdate()).CallBase();
            m.Setup(c => c.GenerateFilterGet())
            .Returns(filter)
            .Verifiable();

            var f = m.Object.GenerateFilterUpdate();

            m.Verify();
            Assert.AreEqual(filter, f);
        }
        public void Page_Pages()
        {
            var r = new List <V_MyView>()
            {
                new V_MyView()
                {
                    Id = "1"
                },
                new V_MyView()
                {
                    Id = "2"
                },
                new V_MyView()
                {
                    Id = "3"
                },
                new V_MyView()
                {
                    Id = "4"
                },
                new V_MyView()
                {
                    Id = "5"
                },
            };
            var repo = GetRepoWithData(r, nameof(Page_Pages));
            var a    = new[] { new SortSpecification("Id", SortDirection.Ascending), };
            var d    = new Dictionary <string, dynamic>()
            {
                { "Id", (Expression <Func <V_MyView, string> >)(t => t.Id) }
            };
            var f = new SortFactory <V_MyView, string>(a, d);

            var results = repo.Page(Specification <V_MyView> .All(), f, 1, 2);

            Assert.Equal("3", results.First().Id);
            Assert.Equal("4", results.Skip(1).First().Id);
        }
Пример #23
0
        /**
         * The heart of your application truly begins, here.
         * Everything 'til now dealt with IoC setup, commandline args,
         * and the like.
         */
        public void Go()
        {
            // This template application just outputs a CSV from a
            // read-only data source (IDW?).

            var mappings = Repo.FindAll(Specification <User> .All());

            var csvConfig = new Configuration()
            {
                Delimiter      = "|",
                QuoteAllFields = true
            };

            // csvConfig.RegisterClassMap<WriteYourCodeAndEatItToo>();

            var path = Path.Combine(Options.Path, "my.csv");

            using (var writer = StreamWriterFactory.Overwrite(path))
            {
                var csv = CsvWriterFactory.Create(writer, csvConfig);
                csv.WriteRecords(mappings);
            }
        }
Пример #24
0
 public ISpecification <T> All() => Specification <T> .All();
Пример #25
0
 /// <summary>Finds all the entities.</summary>
 /// <returns>The found entities or an empty list if there were no results.</returns>
 public IList <T> FindEntities()
 {
     return(this.FindEntitiesCore(Specification.All <T>()));
 }