public async Task SearchById_GivenIdDoesNotReturnSearchResult_ReturnsNull()
        {
            SearchRepositorySettings searchRepositorySettings = new SearchRepositorySettings
            {
                SearchKey         = string.Empty,
                SearchServiceName = string.Empty
            };

            ISearchInitializer searchInitializer = Substitute.For <ISearchInitializer>();

            ISearchIndexClient searchIndexClient = Substitute.For <ISearchIndexClient>();

            AzureOperationResponse <DocumentSearchResult <SpecificationIndex> > documentSearchResult =
                new AzureOperationResponse <DocumentSearchResult <SpecificationIndex> >
            {
                Body = new DocumentSearchResult <SpecificationIndex>(null, null, null, null, null)
            };

            IDocumentsOperations documentsOperations = Substitute.For <IDocumentsOperations>();

            documentsOperations.SearchWithHttpMessagesAsync <SpecificationIndex>(Arg.Any <string>(), Arg.Any <SearchParameters>()).Returns(Task.FromResult(documentSearchResult));

            ISearchServiceClient searchServiceClient = Substitute.For <ISearchServiceClient>();

            searchIndexClient.Documents.Returns(documentsOperations);

            SearchRepository <SpecificationIndex> searchRepository = new SearchRepository <SpecificationIndex>(searchRepositorySettings, searchInitializer, searchServiceClient, searchIndexClient);

            string notFoundId = "notFound";

            SpecificationIndex specificationIndex = await searchRepository.SearchById(notFoundId);

            specificationIndex.Should().BeNull();
        }
コード例 #2
0
        public static async Task ForEachAsync <T>(
            this IDocumentsOperations operations,
            string searchText,
            Func <SearchResult <T>, bool> action,
            SearchParameters searchParameters         = null,
            SearchRequestOptions searchRequestOptions = null,
            CancellationToken cancellationToken       = default)
            where T : class
        {
            var documentSearchResult = await operations.SearchAsync <T>(searchText, searchParameters, searchRequestOptions, cancellationToken);

            foreach (var searchResult in documentSearchResult.Results)
            {
                if (!action(searchResult))
                {
                    return;
                }
            }

            while (documentSearchResult.ContinuationToken != null)
            {
                documentSearchResult = await operations.ContinueSearchAsync <T>(documentSearchResult.ContinuationToken, cancellationToken : cancellationToken);

                foreach (var searchResult in documentSearchResult.Results)
                {
                    if (!action(searchResult))
                    {
                        return;
                    }
                }
            }
        }
コード例 #3
0
 public DocumentsOperationsWrapper(
     IDocumentsOperations inner,
     ILogger <DocumentsOperationsWrapper> logger)
 {
     _inner  = inner ?? throw new ArgumentNullException(nameof(inner));
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 }
コード例 #4
0
 public AzureSearchResults(IDocumentsOperations docs, string query, int?maxResults = null)
 {
     if (string.IsNullOrWhiteSpace(query))
     {
         throw new ArgumentException("Value cannot be null or whitespace.", nameof(query));
     }
     _docs       = docs;
     _query      = query;
     _maxResults = maxResults;
 }
        public void SearchById_GivenIdReturnsSearchResult_TreatsMultipleResultsAsAnException()
        {
            string existingId = "existingId";

            SearchRepositorySettings searchRepositorySettings = new SearchRepositorySettings
            {
                SearchKey         = string.Empty,
                SearchServiceName = string.Empty
            };

            ISearchInitializer searchInitializer = Substitute.For <ISearchInitializer>();

            ISearchIndexClient searchIndexClient = Substitute.For <ISearchIndexClient>();

            Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex> specificationIndexSearchResult = new Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex>(new SpecificationIndex
            {
                Id = existingId
            });

            AzureOperationResponse <DocumentSearchResult <SpecificationIndex> > documentSearchResult =
                new AzureOperationResponse <DocumentSearchResult <SpecificationIndex> >
            {
                Body = new DocumentSearchResult <SpecificationIndex>(new[]
                {
                    specificationIndexSearchResult, new Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex>()
                },
                                                                     null,
                                                                     null,
                                                                     null,
                                                                     null)
            };

            IDocumentsOperations documentsOperations = Substitute.For <IDocumentsOperations>();

            documentsOperations
            .SearchWithHttpMessagesAsync <SpecificationIndex>(Arg.Is <string>(_ => _ == $"\"{existingId}\""),
                                                              Arg.Is <SearchParameters>(_ => _.SearchFields.SequenceEqual(new[]
            {
                "id"
            })))
            .Returns(Task.FromResult(documentSearchResult));

            ISearchServiceClient searchServiceClient = Substitute.For <ISearchServiceClient>();

            searchIndexClient.Documents.Returns(documentsOperations);

            SearchRepository <SpecificationIndex> searchRepository = new SearchRepository <SpecificationIndex>(searchRepositorySettings, searchInitializer, searchServiceClient, searchIndexClient);

            Func <Task <SpecificationIndex> > invocation = () => searchRepository.SearchById(existingId);

            invocation
            .Should()
            .Throw <FailedToQuerySearchException>();
        }
        public async Task SearchById_GivenIdReturnsSearchResult_ReturnsResults()
        {
            string existingId = "existingId";

            SearchRepositorySettings searchRepositorySettings = new SearchRepositorySettings
            {
                SearchKey         = string.Empty,
                SearchServiceName = string.Empty
            };

            ISearchInitializer searchInitializer = Substitute.For <ISearchInitializer>();

            ISearchIndexClient searchIndexClient = Substitute.For <ISearchIndexClient>();

            Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex> specificationIndexSearchResult = new Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex>(new SpecificationIndex
            {
                Id = existingId
            });

            AzureOperationResponse <DocumentSearchResult <SpecificationIndex> > documentSearchResult =
                new AzureOperationResponse <DocumentSearchResult <SpecificationIndex> >
            {
                Body = new DocumentSearchResult <SpecificationIndex>(new[]
                {
                    specificationIndexSearchResult
                },
                                                                     null,
                                                                     null,
                                                                     null,
                                                                     null)
            };

            IDocumentsOperations documentsOperations = Substitute.For <IDocumentsOperations>();

            documentsOperations
            .SearchWithHttpMessagesAsync <SpecificationIndex>(Arg.Is <string>(_ => _ == $"\"{existingId}\""),
                                                              Arg.Is <SearchParameters>(_ => _.SearchFields.SequenceEqual(new[]
            {
                "id"
            })))
            .Returns(Task.FromResult(documentSearchResult));

            ISearchServiceClient searchServiceClient = Substitute.For <ISearchServiceClient>();

            searchIndexClient.Documents.Returns(documentsOperations);

            SearchRepository <SpecificationIndex> searchRepository = new SearchRepository <SpecificationIndex>(searchRepositorySettings, searchInitializer, searchServiceClient, searchIndexClient);

            SpecificationIndex specificationIndex = await searchRepository.SearchById(existingId);

            specificationIndex.Should().NotBeNull();
            specificationIndex.Id.Should().Be(existingId);
        }
コード例 #7
0
 /// <summary>
 /// Initializes client properties.
 /// </summary>
 private void Initialize()
 {
     Documents       = new DocumentsOperations(this);
     BaseUri         = "https://{searchServiceName}.{searchDnsSuffix}/indexes('{indexName}')";
     ApiVersion      = "2017-11-11";
     SearchDnsSuffix = "search.windows.net";
     AcceptLanguage  = "en-US";
     LongRunningOperationRetryTimeout = 30;
     GenerateClientRequestId          = true;
     SerializationSettings            = new JsonSerializerSettings
     {
         Formatting            = Newtonsoft.Json.Formatting.Indented,
         DateFormatHandling    = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
         DateTimeZoneHandling  = Newtonsoft.Json.DateTimeZoneHandling.Utc,
         NullValueHandling     = Newtonsoft.Json.NullValueHandling.Ignore,
         ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize,
         ContractResolver      = new ReadOnlyJsonContractResolver(),
         Converters            = new List <JsonConverter>
         {
             new Iso8601TimeSpanConverter()
         }
     };
     DeserializationSettings = new JsonSerializerSettings
     {
         DateFormatHandling    = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
         DateTimeZoneHandling  = Newtonsoft.Json.DateTimeZoneHandling.Utc,
         NullValueHandling     = Newtonsoft.Json.NullValueHandling.Ignore,
         ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize,
         ContractResolver      = new ReadOnlyJsonContractResolver(),
         Converters            = new List <JsonConverter>
         {
             new Iso8601TimeSpanConverter()
         }
     };
     CustomInitialize();
     DeserializationSettings.Converters.Add(new CloudErrorJsonConverter());
 }
コード例 #8
0
 /// <summary>
 /// Initializes client properties.
 /// </summary>
 private void Initialize()
 {
     this.Documents      = new DocumentsOperations(this);
     this.BaseUri        = new Uri("http://localhost");
     this.ApiVersion     = "2015-02-28-Preview";
     this.AcceptLanguage = "en-US";
     this.LongRunningOperationRetryTimeout = 30;
     this.GenerateClientRequestId          = true;
     SerializationSettings = new JsonSerializerSettings
     {
         Formatting            = Formatting.Indented,
         DateFormatHandling    = DateFormatHandling.IsoDateFormat,
         DateTimeZoneHandling  = DateTimeZoneHandling.Utc,
         NullValueHandling     = NullValueHandling.Ignore,
         ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
         ContractResolver      = new ReadOnlyJsonContractResolver(),
         Converters            = new List <JsonConverter>
         {
             new Iso8601TimeSpanConverter()
         }
     };
     DeserializationSettings = new JsonSerializerSettings
     {
         DateFormatHandling    = DateFormatHandling.IsoDateFormat,
         DateTimeZoneHandling  = DateTimeZoneHandling.Utc,
         NullValueHandling     = NullValueHandling.Ignore,
         ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
         ContractResolver      = new ReadOnlyJsonContractResolver(),
         Converters            = new List <JsonConverter>
         {
             new Iso8601TimeSpanConverter()
         }
     };
     CustomInitialize();
     DeserializationSettings.Converters.Add(new CloudErrorJsonConverter());
 }
コード例 #9
0
 public AzureSearchResults(IDocumentsOperations docs, BooleanQuery luceneQuery, int?maxResults = null)
 {
     _docs        = docs;
     _luceneQuery = luceneQuery ?? throw new ArgumentNullException(nameof(luceneQuery));
     _maxResults  = maxResults;
 }