public new void SetUp()
        {
            var store = GetDocumentStore();

            store.Initialize();
            IndexCreation.CreateIndexes(typeof(Record).Assembly, store);
            WaitForIndexing(store);
            ReusableDocumentStore = store;

            Db = ReusableDocumentStore.OpenSession();

            var record1 = QueryTestHelper.SimpleRecord().With(m => { m.Gemini.Title = "sea"; });
            var record2 = QueryTestHelper.SimpleRecord().With(m => { m.Gemini.Title = "seabirds"; });
            var record3 = QueryTestHelper.SimpleRecord().With(m => { m.Gemini.Title = "birds"; });
            var record4 = QueryTestHelper.SimpleRecord().With(m => { m.Gemini.Title = "coastal birds"; });
            var record5 = QueryTestHelper.SimpleRecord().With(m => { m.Gemini.Title = "1234"; });
            var record6 = QueryTestHelper.SimpleRecord().With(m => { m.Gemini.Title = "sea1234"; });
            var record7 = QueryTestHelper.SimpleRecord().With(m => { m.Gemini.Title = "sea 1234"; });

            Db.Store(record1);
            Db.Store(record2);
            Db.Store(record3);
            Db.Store(record4);
            Db.Store(record5);
            Db.Store(record6);
            Db.Store(record7);
            Db.SaveChanges();
            WaitForIndexing(ReusableDocumentStore);
        }
        private IDocumentSession GetDbForFilterTests()
        {
            var store = new InMemoryDatabaseHelper().Create();

            using (var db = store.OpenSession())
            {
                var record1 = QueryTestHelper.SimpleRecord().With(m =>
                {
                    m.Gemini.Title        = "spreadsheet record";
                    m.Gemini.DataFormat   = "Microsoft Excel for Windows";
                    m.Gemini.ResourceType = "publication";
                    m.Manager             = new UserInfo {
                        DisplayName = "cathy test [email protected]"
                    };
                });
                var record2 = QueryTestHelper.SimpleRecord().With(m =>
                {
                    m.Gemini.Title        = "database record";
                    m.Gemini.DataFormat   = "Database";
                    m.Gemini.ResourceType = "nonGeographicDataset";
                    m.Manager             = new UserInfo {
                        DisplayName = "pete test [email protected]"
                    };
                });
                var record3 = QueryTestHelper.SimpleRecord().With(m =>
                {
                    m.Gemini.Title        = "geospatial record 1";
                    m.Gemini.DataFormat   = "ESRI Arc/View ShapeFile";
                    m.Gemini.ResourceType = "dataset";
                    m.Manager             = new UserInfo {
                        DisplayName = "pete test"
                    };
                });
                var record4 = QueryTestHelper.SimpleRecord().With(m =>
                {
                    m.Gemini.Title        = "geospatial record 2";
                    m.Gemini.DataFormat   = "Geospatial (vector polygon)";
                    m.Gemini.ResourceType = "service";
                    m.Manager             = new UserInfo {
                        DisplayName = "*****@*****.**"
                    };
                });
                var record5 = QueryTestHelper.SimpleRecord().With(m =>
                {
                    m.Gemini.Title        = "record with no data format";
                    m.Gemini.DataFormat   = null;
                    m.Gemini.ResourceType = "dataset";
                });

                db.Store(record1);
                db.Store(record2);
                db.Store(record3);
                db.Store(record4);
                db.Store(record5);
                db.SaveChanges();

                return(db);
            }
        }
        public void should_return_correct_result_count_for_keywords(string keyword, int expected)
        {
            var input = QueryTestHelper.EmptySearchInput().With(q => q.F = new FilterOptions {
                Keywords = new[] { keyword }
            });
            var output = new RecordQueryer(Db).Search(input);

            output.Total.Should().Be(expected);
        }
        public void test_order_matters_in_exact_search()
        {
            var helper = new RecordQueryer(Db);

            var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = @"""birds coastal""");

            var results = helper.Search(input).Results;

            results.Count.Should().Be(0);
        }
        public void test_exact_search_with_no_matches()
        {
            var helper = new RecordQueryer(Db);

            var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = @"""coast""");

            var results = helper.Search(input).Results;

            results.Count.Should().Be(0);
        }
        public void test_exact_search_where_title_has_multiple_terms()
        {
            var helper = new RecordQueryer(Db);

            var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = @"""coastal""");

            var results = helper.Search(input).Results;

            results.Count.Should().Be(1);
            results.ToList()[0].Title.Should().Be("<b>coastal</b> birds");
        }
        public void filter_by_no_formats_test()
        {
            var queryer = new RecordQueryer(Db);
            var input   = QueryTestHelper.EmptySearchInput().With(x => x.F = new FilterOptions {
                DataFormats = new string[0]
            });

            var results = queryer.Search(input).Results;

            results.Count.Should().Be(5);
        }
        public void test_number_only_search()
        {
            var helper  = new RecordQueryer(Db);
            var input   = QueryTestHelper.EmptySearchInput().With(x => x.Q = "123");
            var results = helper.Search(input).Results;

            results.Count.Should().Be(3);
            results.ToList()[0].Title.Should().Be("<b>123</b>4");
            results.ToList()[1].Title.Should().Be("sea<b>123</b>4");
            results.ToList()[2].Title.Should().Be("sea <b>123</b>4");
        }
        public void test_exact_search_term_with_normal_search_term()
        {
            var helper = new RecordQueryer(Db);

            var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = @"""coastal"" bird");

            var results = helper.Search(input).Results;

            results.Count.Should().Be(1);
            results.ToList()[0].Title.Should().Be("<b>coastal</b> <b>birds</b>");
        }
        public void test_simple_search_with_numbers_and_letters()
        {
            var helper = new RecordQueryer(Db);
            var input  = QueryTestHelper.EmptySearchInput().With(x => x.Q = "sea12");

            var results = helper.Search(input).Results;

            results.Count.Should().Be(3);
            results.ToList()[0].Title.Should().Be("<b>sea</b>1234");
            results.ToList()[1].Title.Should().Be("<b>sea</b>");
            results.ToList()[2].Title.Should().Be("<b>sea</b> 1234");
        }
        public void test_exact_search_is_case_insensitive()
        {
            var helper = new RecordQueryer(Db);

            var input = QueryTestHelper.EmptySearchInput().With(x => { x.Q = @"""BIRDS"""; x.O = SortOptions.TitleAZ; });

            var results = helper.Search(input).Results;

            results.Count.Should().Be(2);
            results.ToList()[0].Title.Should().Be("<b>birds</b>");
            results.ToList()[1].Title.Should().Be("coastal <b>birds</b>");
        }
        public void test_exact_search_of_phrase()
        {
            using (var db = GetDbForSortTests())
            {
                var helper = new RecordQueryer(db);

                var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = @"""coastal birds""");

                var results = helper.Search(input).Results;
                results.Count.Should().Be(1);
                results.ToList()[0].Title.Should().Be("<b>coastal birds</b>");
            }
        }
        [Ignore] //Only works if we decide to AND search terms instead of ORing
        public void test_simple_search_with_multiple_terms()
        {
            using (var db = GetDbForSortTests())
            {
                var helper = new RecordQueryer(db);

                var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = "coastal bird");

                var results = helper.Search(input).Results;
                results.Count.Should().Be(1);
                results.ToList()[0].Title.Should().Be("<b>coastal</b> <b>birds</b>");
            }
        }
        public void test_exact_search_when_title_has_multiple_terms_and_digits()
        {
            var helper = new RecordQueryer(Db);

            var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = @"""sea""");

            var results = helper.Search(input).Results;

            results.Count.Should().Be(3);
            results.ToList()[0].Title.Should().Be("<b>sea</b>");
            results.ToList()[1].Title.Should().Be("<b>sea</b>1234");
            results.ToList()[2].Title.Should().Be("<b>sea</b> 1234");
        }
        public void test_simple_search()
        {
            var helper = new RecordQueryer(Db);

            var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = "sea");

            var results = helper.Search(input).Results;

            results.Count.Should().Be(4);
            results.ToList()[0].Title.Should().Be("<b>sea</b>");
            results.ToList()[1].Title.Should().Be("<b>sea</b>1234");
            results.ToList()[2].Title.Should().Be("<b>sea</b> 1234");
            results.ToList()[3].Title.Should().Be("<b>sea</b>birds");
        }
        public void filter_by_email_test()
        {
            var queryer = new RecordQueryer(Db);
            var input   = QueryTestHelper.EmptySearchInput().With(x =>
            {
                x.Q = "record";
                x.F = new FilterOptions {
                    Manager = "jncc.gov.uk"
                };
            });

            var results = queryer.Search(input).Results;

            results.Count.Should().Be(3);
        }
        [Ignore] //Only works if we decide to AND search terms instead of ORing
        public void test_simple_search_with_multiple_terms_and_numbers()
        {
            using (var db = GetDbForSortTests())
            {
                var helper = new RecordQueryer(db);

                var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = "sea 34");

                var results = helper.Search(input).Results;
                results.Count.Should().Be(3);
                results.ToList()[0].Title.Should().Be("<b>sea</b>1234");
                results.ToList()[1].Title.Should().Be("<b>sea</b> 1234");
                results.ToList()[2].Title.Should().Be("<b>sea</b>");
            }
        }
        public void test_search_is_case_insensitive()
        {
            using (var db = GetDbForSortTests())
            {
                var helper = new RecordQueryer(db);

                var input = QueryTestHelper.EmptySearchInput().With(x => { x.Q = "BIRDS"; x.O = SortOptions.TitleAZ; });

                var results = helper.Search(input).Results;
                results.Count.Should().Be(3);
                results.ToList()[0].Title.Should().Be("<b>birds</b>");
                results.ToList()[1].Title.Should().Be("coastal <b>birds</b>");
                results.ToList()[2].Title.Should().Be("sea<b>birds</b>");
            }
        }
        public void filter_by_other_formats_test()
        {
            var queryer = new RecordQueryer(Db);
            var input   = QueryTestHelper.EmptySearchInput().With(x =>
            {
                x.Q = "record";
                x.F = new FilterOptions {
                    DataFormats = new[] { "Other" }
                };
            });

            var results = queryer.Search(input).Results;

            results.Count.Should().Be(1);
            results.Any(r => r.Title == "<b>record</b> with no data format").Should().BeTrue();
        }
        public void filter_by_multiple_resource_types()
        {
            var queryer = new RecordQueryer(Db);
            var input   = QueryTestHelper.EmptySearchInput().With(x =>
            {
                x.Q = "record";
                x.F = new FilterOptions {
                    ResourceTypes = new[] { "publication", "nonGeographicDataset" }
                };
            });

            var results = queryer.Search(input).Results;

            results.Count.Should().Be(2);
            results.Any(r => r.Title == "spreadsheet <b>record</b>").Should().BeTrue();
            results.Any(r => r.Title == "database <b>record</b>").Should().BeTrue();
        }
        public void filter_by_one_resource_type()
        {
            var queryer = new RecordQueryer(Db);
            var input   = QueryTestHelper.EmptySearchInput().With(x =>
            {
                x.Q = "record";
                x.F = new FilterOptions {
                    ResourceTypes = new [] { "Dataset" }
                };
            });

            var results = queryer.Search(input).Results;

            results.Count.Should().Be(2);
            results.Any(r => r.Title == "geospatial <b>record</b> 1").Should().BeTrue();
            results.Any(r => r.Title == "<b>record</b> with no data format").Should().BeTrue();
        }
        public void filter_user_with_name_and_email_test()
        {
            var queryer = new RecordQueryer(Db);
            var input   = QueryTestHelper.EmptySearchInput().With(x =>
            {
                x.Q = "record";
                x.F = new FilterOptions {
                    Manager = "cathy test"
                };
            });

            var results = queryer.Search(input).Results;

            results.Count.Should().Be(2);
            results.Any(r => r.Title == "spreadsheet <b>record</b>").Should().BeTrue();
            results.Any(r => r.Title == "geospatial <b>record</b> 2").Should().BeTrue();
        }
        public void filter_by_multiple_formats_test()
        {
            var queryer = new RecordQueryer(Db);
            var input   = QueryTestHelper.EmptySearchInput().With(x =>
            {
                x.Q = "record";
                x.F = new FilterOptions {
                    DataFormats = new[] { "Spreadsheet", "Database" }
                };
            });

            var results = queryer.Search(input).Results;

            results.Count.Should().Be(2);
            results.Any(r => r.Title == "spreadsheet <b>record</b>").Should().BeTrue();
            results.Any(r => r.Title == "database <b>record</b>").Should().BeTrue();
        }
        public void filter_by_format_test_with_no_query()
        {
            var queryer = new RecordQueryer(Db);
            var input   = QueryTestHelper.EmptySearchInput().With(x =>
            {
                x.Q = "";
                x.F = new FilterOptions {
                    DataFormats = new[] { "Geospatial" }
                };
            });

            var results = queryer.Search(input).Results;

            results.Count.Should().Be(2);
            results.Any(r => r.Title == "geospatial record 1").Should().BeTrue();
            results.Any(r => r.Title == "geospatial record 2").Should().BeTrue();
        }
        public void test_exact_search_checks_abstract_too()
        {
            var store = new InMemoryDatabaseHelper().Create();
            var db    = store.OpenSession();

            using (db)
            {
                var record1 = QueryTestHelper.SimpleRecord().With(m =>
                {
                    m.Gemini.Title    = "marine conservation zone";
                    m.Gemini.Abstract = "population analysis data for zone";
                });
                var record2 = QueryTestHelper.SimpleRecord().With(m =>
                {
                    m.Gemini.Title    = "habitat data";
                    m.Gemini.Abstract = "marine conservation analysis data";
                });

                db.Store(record1);
                db.Store(record2);
                db.SaveChanges();

                var helper = new RecordQueryer(db);

                var input = QueryTestHelper.EmptySearchInput().With(x => x.Q = @"""marine conservation""");

                var results = helper.Search(input).Results;
                results.Count.Should().Be(2);
                results.ToList()[0].Title.Should().Be("<b>marine conservation</b> zone");
                results.ToList()[1].Title.Should().Be("habitat data");

                input = QueryTestHelper.EmptySearchInput().With(x => x.Q = @"""analysis data""");

                results = helper.Search(input).Results;
                results.Count.Should().Be(2);
                results.ToList()[0].Title.Should().Be("marine conservation zone");
                results.ToList()[1].Title.Should().Be("habitat data");

                input = QueryTestHelper.EmptySearchInput().With(x => x.Q = @"""population analysis""");

                results = helper.Search(input).Results;
                results.Count.Should().Be(1);
                results.ToList()[0].Title.Should().Be("marine conservation zone");
            }
        }
        private IDocumentSession GetDbForSortTests()
        {
            var store = new InMemoryDatabaseHelper().Create();
            var db    = store.OpenSession();

            var record1 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title = "sea";
            });
            var record2 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title = "seabirds";
            });
            var record3 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title = "birds";
            });
            var record4 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title = "coastal birds";
            });
            var record5 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title = "1234";
            });
            var record6 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title = "sea1234";
            });
            var record7 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title = "sea 1234";
            });

            db.Store(record1);
            db.Store(record2);
            db.Store(record3);
            db.Store(record4);
            db.Store(record5);
            db.Store(record6);
            db.Store(record7);
            db.SaveChanges();

            return(db);
        }
        public void filter_user_with_email_test()
        {
            using (var db = GetDbForFilterTests())
            {
                var queryer = new RecordQueryer(db);
                var input   = QueryTestHelper.EmptySearchInput().With(x =>
                {
                    x.Q = "record";
                    x.F = new FilterOptions {
                        Manager = "*****@*****.**"
                    };
                });

                var results = queryer.Search(input).Results;
                results.Count.Should().Be(2);
                results.Any(r => r.Title == "spreadsheet <b>record</b>").Should().BeTrue();
                results.Any(r => r.Title == "geospatial <b>record</b> 2").Should().BeTrue();
            }
        }
        public void filter_by_multiple_formats_including_other_test()
        {
            using (var db = GetDbForFilterTests())
            {
                var queryer = new RecordQueryer(db);
                var input   = QueryTestHelper.EmptySearchInput().With(x =>
                {
                    x.Q = "record";
                    x.F = new FilterOptions {
                        DataFormats = new[] { "Other", "Database" }
                    };
                });

                var results = queryer.Search(input).Results;
                results.Count.Should().Be(2);
                results.Any(r => r.Title == "<b>record</b> with no data format").Should().BeTrue();
                results.Any(r => r.Title == "database <b>record</b>").Should().BeTrue();
            }
        }
        public void filter_by_format_test()
        {
            using (var db = GetDbForFilterTests())
            {
                var queryer = new RecordQueryer(db);
                var input   = QueryTestHelper.EmptySearchInput().With(x =>
                {
                    x.Q = "record";
                    x.F = new FilterOptions {
                        DataFormats = new[] { "Geospatial" }
                    };
                });

                var results = queryer.Search(input).Results;
                results.Count.Should().Be(2);
                results.Any(r => r.Title == "geospatial <b>record</b> 1").Should().BeTrue();
                results.Any(r => r.Title == "geospatial <b>record</b> 2").Should().BeTrue();
            }
        }
        public new void SetUp()
        {
            var store = GetDocumentStore();

            store.Initialize();
            IndexCreation.CreateIndexes(typeof(Record).Assembly, store);
            WaitForIndexing(store);
            ReusableDocumentStore = store;

            Db = ReusableDocumentStore.OpenSession();

            var record1 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title        = "spreadsheet record";
                m.Gemini.DataFormat   = "Microsoft Excel for Windows";
                m.Gemini.ResourceType = "publication";
                m.Manager             = new UserInfo {
                    DisplayName = "cathy test [email protected]"
                };
            });
            var record2 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title        = "database record";
                m.Gemini.DataFormat   = "Database";
                m.Gemini.ResourceType = "nonGeographicDataset";
                m.Manager             = new UserInfo {
                    DisplayName = "pete test [email protected]"
                };
            });
            var record3 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title        = "geospatial record 1";
                m.Gemini.DataFormat   = "ESRI Arc/View ShapeFile";
                m.Gemini.ResourceType = "dataset";
                m.Manager             = new UserInfo {
                    DisplayName = "pete test"
                };
            });
            var record4 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title        = "geospatial record 2";
                m.Gemini.DataFormat   = "Geospatial (vector polygon)";
                m.Gemini.ResourceType = "service";
                m.Manager             = new UserInfo {
                    DisplayName = "*****@*****.**"
                };
            });
            var record5 = QueryTestHelper.SimpleRecord().With(m =>
            {
                m.Gemini.Title        = "record with no data format";
                m.Gemini.DataFormat   = null;
                m.Gemini.ResourceType = "dataset";
            });

            Db.Store(record1);
            Db.Store(record2);
            Db.Store(record3);
            Db.Store(record4);
            Db.Store(record5);
            Db.SaveChanges();
            WaitForIndexing(ReusableDocumentStore);
        }