public void TestCreateFilter()
        {
            //setup
            var orgUnitTypeLocations = new OrgUnitType
                                            {
                                                Id = 1,
                                                Name = "Locations",
                                                IsEnabled = true,
                                                IsOutsideOfOrganization = false
                                            };

            var orgUnitTypeDepartments = new OrgUnitType
                                              {
                                                  Id = 2,
                                                  Name = "Departments",
                                                  IsEnabled = true,
                                                  IsOutsideOfOrganization = false
                                              };

            // orgUnit
            var orgUnit1 = new OrgUnit
                               {
                                   Id = 1,
                                   Name = "Hospital A"
                               };
            var outa = new List<OrgUnitTypeAssociation>
                           {
                               new OrgUnitTypeAssociation
                                   {
                                       Id = 1,
                                       OrgUnit = orgUnit1,
                                       OrgUnitType = orgUnitTypeLocations
                                   }
                           };
            orgUnit1.OrgUnitTypeAssociations = outa;

            // orgUnit
            var orgUnit2 = new OrgUnit
                               {
                                   Id = 2,
                                   Name = "Department A"
                               };
            outa = new List<OrgUnitTypeAssociation>
                       {
                           new OrgUnitTypeAssociation
                               {
                                   Id = 2,
                                   OrgUnit = orgUnit2,
                                   OrgUnitType = orgUnitTypeDepartments
                               }
                       };
            orgUnit2.OrgUnitTypeAssociations = outa;

            // orgUnit
            var orgUnit3 = new OrgUnit
                               {
                                   Id = 3,
                                   Name = "Hospital B"
                               };
            outa = new List<OrgUnitTypeAssociation>
                       {
                           new OrgUnitTypeAssociation
                               {
                                   Id = 3,
                                   OrgUnit = orgUnit3,
                                   OrgUnitType = orgUnitTypeLocations
                               }
                       };
            orgUnit3.OrgUnitTypeAssociations = outa;

            // orgUnit
            var orgUnit4 = new OrgUnit
                               {
                                   Id = 4,
                                   Name = "Department B"
                               };
            outa = new List<OrgUnitTypeAssociation>
                       {
                           new OrgUnitTypeAssociation
                               {
                                   Id = 4,
                                   OrgUnit = orgUnit4,
                                   OrgUnitType = orgUnitTypeDepartments
                               }
                       };
            orgUnit4.OrgUnitTypeAssociations = outa;

            var source = new List<ProviderCacheView>();

            var provider = new ProviderCacheView { Id = 1 };
            source.AddRange(provider.AddOrgUnit(orgUnit1));
            source.AddRange(provider.AddOrgUnit(orgUnit3));

            provider = new ProviderCacheView { Id = 2 };
            source.AddRange(provider.AddOrgUnit(orgUnit1));
            source.AddRange(provider.AddOrgUnit(orgUnit2));

            provider = new ProviderCacheView { Id = 3 };
            source.AddRange(provider.AddOrgUnit(orgUnit3));
            source.AddRange(provider.AddOrgUnit(orgUnit4));

            provider = new ProviderCacheView { Id = 4 };
            source.AddRange(provider.AddOrgUnit(orgUnit1));
            source.AddRange(provider.AddOrgUnit(orgUnit2));
            source.AddRange(provider.AddOrgUnit(orgUnit3));
            source.AddRange(provider.AddOrgUnit(orgUnit4));

            source.Add(new ProviderCacheView { Id = 5 });

            var builder = new OrgUnitFilterBuilder();

            //act
            var filters = builder.CreateFilters(source.AsQueryable());

            //assert
            Assert.IsNotNull(filters);
            var filterTypes = filters as SearchFilterType[] ?? filters.ToArray();
            Assert.AreEqual(2, filterTypes.Count());

            //locations
            var locationsFilter = filterTypes.Single(f => f.Name == "Locations");
            Assert.AreEqual(2, locationsFilter.FilterValues.Count());
            Assert.AreEqual(3, locationsFilter.FilterValues.ElementAt(0).Count);
            Assert.AreEqual(3, locationsFilter.FilterValues.ElementAt(1).Count);
            Assert.IsNotNullOrEmpty(locationsFilter.FilterValues.ElementAt(0).DisplayName);
            Assert.IsNotNullOrEmpty(locationsFilter.FilterValues.ElementAt(1).DisplayName);

            //departments
            var departmentsFilter = filterTypes.Single(f => f.Name == "Departments");
            Assert.AreEqual(2, departmentsFilter.FilterValues.Count());
            Assert.AreEqual(2, departmentsFilter.FilterValues.ElementAt(0).Count);
            Assert.AreEqual(2, departmentsFilter.FilterValues.ElementAt(1).Count);
            Assert.IsNotNullOrEmpty(departmentsFilter.FilterValues.ElementAt(0).DisplayName);
            Assert.IsNotNullOrEmpty(departmentsFilter.FilterValues.ElementAt(1).DisplayName);
        }
        public void TestCreateFilter()
        {
            //setup
            DateTime currentDate = new DateTime(2000, 1, 1);
            DateTime futureDate = new DateTime(2000, 2, 1);
            DateTime pastDate = new DateTime(1970, 1, 1);

            var orgUnitType_locations = new OrgUnitTypeCacheView()
            {
                Id = 1,
                Name = "Locations"
            };

            var orgUnitType_departments = new OrgUnitTypeCacheView()
            {
                Id = 2,
                Name = "Departments"
            };

            // orgUnit
            var orgUnit1 = new OrgUnitCacheView()
            {
                Id = 1,
                Name = "Hospital A"
            };

            orgUnit1.OrgUnitTypes.Add(orgUnitType_locations);

            // orgUnit
            var orgUnit2 = new OrgUnitCacheView()
            {
                Id = 2,
                Name = "Department A"
            };

            orgUnit2.OrgUnitTypes.Add(orgUnitType_departments);

            // orgUnit
            var orgUnit3 = new OrgUnitCacheView()
            {
                Id = 3,
                Name = "Hospital B"
            };

            orgUnit3.OrgUnitTypes.Add(orgUnitType_locations);

            // orgUnit
            var orgUnit4 = new OrgUnitCacheView()
            {
                Id = 4,
                Name = "Department B"
            };

            orgUnit4.OrgUnitTypes.Add(orgUnitType_departments);

            var source = new List<EventCacheView>();

            var Event = new EventCacheView() { Id = 1 };
            var eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit1;
            eventOccurence.OrgUnitId = orgUnit1.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = futureDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit3;
            eventOccurence.OrgUnitId = orgUnit3.Id;
            Event.Occurrences.Add(eventOccurence);
            source.Add(Event);

            Event = new EventCacheView() { Id = 2 };
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit1;
            eventOccurence.OrgUnitId = orgUnit1.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = futureDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit2;
            eventOccurence.OrgUnitId = orgUnit2.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = futureDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            source.Add(Event);

            Event = new EventCacheView() { Id = 3 };
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit3;
            eventOccurence.OrgUnitId = orgUnit3.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = futureDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit4;
            eventOccurence.OrgUnitId = orgUnit4.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = futureDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            source.Add(Event);

            Event = new EventCacheView() { Id = 4 };
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit1;
            eventOccurence.OrgUnitId = orgUnit1.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = futureDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit2;
            eventOccurence.OrgUnitId = orgUnit2.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = futureDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit3;
            eventOccurence.OrgUnitId = orgUnit3.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = futureDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit4;
            eventOccurence.OrgUnitId = orgUnit4.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = futureDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            eventOccurence = new EventOccurrenceCacheView();
            eventOccurence.OrgUnit = orgUnit4;
            eventOccurence.OrgUnitId = orgUnit4.Id;
            eventOccurence.OccurrenceDates.Add(new EventOccurrenceDateCacheView() { StartDate = pastDate, EventOccurrenceId = eventOccurence.Id });
            Event.Occurrences.Add(eventOccurence);
            source.Add(Event);

            Event = new EventCacheView() { Id = 5 };
            source.Add(Event);

            var builder = new OrgUnitFilterBuilder();
            var mockClock = new Mock<IClock>();
            mockClock.SetupGet(c => c.Now).Returns(currentDate);
            builder.Clock = mockClock.Object;

            //act
            var filters = builder.CreateFilters(source.AsQueryable());

            //assert
            Assert.IsNotNull(filters);
            Assert.AreEqual(2, filters.Count());

            //locations
            var locationsFilter = filters.Where(f => f.Name == "Locations").Single();
            Assert.AreEqual(2, locationsFilter.FilterValues.Count());
            Assert.AreEqual(3, locationsFilter.FilterValues.ElementAt(0).Count);
            Assert.AreEqual(3, locationsFilter.FilterValues.ElementAt(1).Count);
            Assert.IsNotNullOrEmpty(locationsFilter.FilterValues.ElementAt(0).DisplayName);
            Assert.IsNotNullOrEmpty(locationsFilter.FilterValues.ElementAt(1).DisplayName);

            //departments
            var departmentsFilter = filters.Where(f => f.Name == "Departments").Single();
            Assert.AreEqual(2, departmentsFilter.FilterValues.Count());
            Assert.AreEqual(2, departmentsFilter.FilterValues.ElementAt(0).Count);
            Assert.AreEqual(2, departmentsFilter.FilterValues.ElementAt(1).Count);
            Assert.IsNotNullOrEmpty(departmentsFilter.FilterValues.ElementAt(0).DisplayName);
            Assert.IsNotNullOrEmpty(departmentsFilter.FilterValues.ElementAt(1).DisplayName);
        }