Ejemplo n.º 1
0
                public async ValueTask <bool> MoveNextAsync()
                {
                    _cancellationToken.ThrowIfCancellationRequested();

                    if (_jsonReader == null)
                    {
                        if (_query == null)
                        {
                            _query = _cosmosClient.CreateQuery(_containerId, _cosmosSqlQuery);
                        }

                        if (!_query.HasMoreResults)
                        {
                            Current = default;
                            return(false);
                        }

                        _responseMessage = await _query.ReadNextAsync(_cancellationToken);

                        if (!_responseMessage.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException(CosmosStrings.QueryFailed(_responseMessage.StatusCode, _responseMessage.ErrorMessage));
                        }

                        _responseStream = _responseMessage.Content;
                        _reader         = new StreamReader(_responseStream);
                        _jsonReader     = new JsonTextReader(_reader);

                        while (_jsonReader.Read())
                        {
                            if (_jsonReader.TokenType == JsonToken.StartObject)
                            {
                                while (_jsonReader.Read())
                                {
                                    if (_jsonReader.TokenType == JsonToken.StartArray)
                                    {
                                        goto ObjectFound;
                                    }
                                }
                            }
                        }

                        ObjectFound :;
                    }

                    while (_jsonReader.Read())
                    {
                        if (_jsonReader.TokenType == JsonToken.StartObject)
                        {
                            Current = new JsonSerializer().Deserialize <JObject>(_jsonReader);
                            return(true);
                        }
                    }

                    await DisposeAsync();

                    return(await MoveNextAsync());
                }
        public async Task Add_update_delete_query_throws_if_no_container()
        {
            await using var testDatabase = CosmosTestStore.CreateInitialized(DatabaseName + "Empty");
            var options = Fixture.CreateOptions(testDatabase);

            var customer = new Customer {
                Id = 42, Name = "Theon"
            };

            using (var context = new CustomerContext(options))
            {
                context.Add(customer);

                Assert.StartsWith(CosmosStrings.CreateItemFailed("NotFound", "Message: {\"Errors\":[\"Resource Not Found\"]}")[..^ 1],
                                  (await Assert.ThrowsAsync <InvalidOperationException>(() => context.SaveChangesAsync())).Message);
            }

            using (var context = new CustomerContext(options))
            {
                context.Add(customer).State = EntityState.Modified;

                Assert.StartsWith(CosmosStrings.ReplaceItemFailed("NotFound", "Message: {\"Errors\":[\"Resource Not Found\"]}")[..^ 1],
                                  (await Assert.ThrowsAsync <InvalidOperationException>(() => context.SaveChangesAsync())).Message);
            }

            using (var context = new CustomerContext(options))
            {
                context.Add(customer).State = EntityState.Deleted;

                Assert.StartsWith(CosmosStrings.DeleteItemFailed("NotFound", "Message: {\"Errors\":[\"Resource Not Found\"]}")[..^ 1],
                                  (await Assert.ThrowsAsync <InvalidOperationException>(() => context.SaveChangesAsync())).Message);
            }

            using (var context = new CustomerContext(options))
            {
                Assert.StartsWith(CosmosStrings.QueryFailed("NotFound", "Message: {\"Errors\":[\"Resource Not Found\"]}")[..^ 1],
                                  (await Assert.ThrowsAsync <InvalidOperationException>(() => context.Set <Customer>().SingleAsync())).Message);
            }
        }