Skip to content

Commit

Permalink
Update Cosmos SDK
Browse files Browse the repository at this point in the history
Refactor some tests
  • Loading branch information
AndriySvyryd committed Jun 11, 2019
1 parent 4d5df55 commit be28e3a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PropertyGroup Label="Dependencies from nuget.org">
<SystemThreadingTasksExtensionsVersion>4.5.2</SystemThreadingTasksExtensionsVersion>
<CastleCorePackageVersion>4.2.1</CastleCorePackageVersion>
<MicrosoftAzureCosmosPackageVersion>3.0.0.1-preview</MicrosoftAzureCosmosPackageVersion>
<MicrosoftAzureCosmosPackageVersion>3.0.0.17-preview</MicrosoftAzureCosmosPackageVersion>
<MicrosoftCodeAnalysisCSharpPackageVersion>2.8.0</MicrosoftCodeAnalysisCSharpPackageVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>2.8.0</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
<mod_spatialitePackageVersion>4.3.0.1</mod_spatialitePackageVersion>
Expand Down
51 changes: 25 additions & 26 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CosmosClientWrapper : IDisposable

private static readonly string _userAgent = " Microsoft.EntityFrameworkCore.Cosmos/" + ProductInfo.GetVersion();
public static readonly JsonSerializer Serializer = new JsonSerializer();
private string _region;
private readonly string _region;

static CosmosClientWrapper()
{
Expand All @@ -57,21 +57,19 @@ public CosmosClientWrapper(
}

private CosmosClient Client =>
_client
?? (_client = new CosmosClient(
BuildCosmosConfiguration()));
_client ??= new CosmosClient(BuildCosmosConfiguration());

private CosmosConfiguration BuildCosmosConfiguration()
private CosmosClientOptions BuildCosmosConfiguration()
{
var configuration = new CosmosConfiguration(_endPoint, _authKey)
var configuration = new CosmosClientOptions(_endPoint, _authKey)
{
UserAgentSuffix = _userAgent,
ConnectionMode = ConnectionMode.Direct
ApplicationName = _userAgent,
ConnectionMode = ConnectionMode.Direct,
};

if (_region != null)
{
configuration = configuration.UseCurrentRegion(_region);
configuration.ApplicationRegion = _region;
}

return configuration;
Expand All @@ -96,7 +94,7 @@ public async Task<bool> CreateDatabaseIfNotExistsOnceAsync(
object __,
CancellationToken cancellationToken = default)
{
var response = await Client.Databases.CreateDatabaseIfNotExistsAsync(_databaseId, cancellationToken: cancellationToken);
var response = await Client.CreateDatabaseIfNotExistsAsync(_databaseId, cancellationToken: cancellationToken);

return response.StatusCode == HttpStatusCode.Created;
}
Expand All @@ -119,7 +117,7 @@ public async Task<bool> DeleteDatabaseOnceAsync(
object __,
CancellationToken cancellationToken = default)
{
var response = await Client.Databases[_databaseId].DeleteAsync(cancellationToken: cancellationToken);
var response = await Client.GetDatabase(_databaseId).DeleteAsync(cancellationToken: cancellationToken);

return response.StatusCode == HttpStatusCode.NoContent;
}
Expand Down Expand Up @@ -147,8 +145,7 @@ private async Task<bool> CreateContainerIfNotExistsOnceAsync(
(string ContainerId, string PartitionKey) parameters,
CancellationToken cancellationToken = default)
{
var response = await Client.Databases[_databaseId].Containers
.CreateContainerIfNotExistsAsync(
var response = await Client.GetDatabase(_databaseId).CreateContainerIfNotExistsAsync(
new CosmosContainerSettings(parameters.ContainerId, "/" + parameters.PartitionKey), cancellationToken: cancellationToken);

return response.StatusCode == HttpStatusCode.Created;
Expand Down Expand Up @@ -182,10 +179,10 @@ private async Task<bool> CreateItemOnceAsync(
using (var jsonWriter = new JsonTextWriter(writer))
{
JsonSerializer.Create().Serialize(jsonWriter, parameters.Document);
await jsonWriter.FlushAsync();
await jsonWriter.FlushAsync(cancellationToken);

var items = Client.Databases[_databaseId].Containers[parameters.ContainerId].Items;
using (var response = await items.CreateItemStreamAsync("0", stream, new CosmosItemRequestOptions(), cancellationToken))
var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
using (var response = await container.CreateItemStreamAsync(PartitionKey.NonePartitionKeyValue, stream, null, cancellationToken))
{
return response.StatusCode == HttpStatusCode.Created;
}
Expand Down Expand Up @@ -222,10 +219,11 @@ private async Task<bool> ReplaceItemOnceAsync(
using (var jsonWriter = new JsonTextWriter(writer))
{
JsonSerializer.Create().Serialize(jsonWriter, parameters.Document);
await jsonWriter.FlushAsync();
await jsonWriter.FlushAsync(cancellationToken);

var items = Client.Databases[_databaseId].Containers[parameters.ContainerId].Items;
using (var response = await items.ReplaceItemStreamAsync("0", parameters.ItemId, stream, null, cancellationToken))
var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
using (var response = await container.ReplaceItemStreamAsync(
PartitionKey.NonePartitionKeyValue, parameters.ItemId, stream, null, cancellationToken))
{
return response.StatusCode == HttpStatusCode.OK;
}
Expand Down Expand Up @@ -255,8 +253,8 @@ public async Task<bool> DeleteItemOnceAsync(
(string ContainerId, string DocumentId) parameters,
CancellationToken cancellationToken = default)
{
var items = Client.Databases[_databaseId].Containers[parameters.ContainerId].Items;
using (var response = await items.DeleteItemStreamAsync("0", parameters.DocumentId, null, cancellationToken))
var items = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
using (var response = await items.DeleteItemStreamAsync(PartitionKey.NonePartitionKeyValue, parameters.DocumentId, null, cancellationToken))
{
return response.StatusCode == HttpStatusCode.NoContent;
}
Expand All @@ -280,18 +278,18 @@ public IAsyncEnumerable<JObject> ExecuteSqlQueryAsync(
return new DocumentAsyncEnumerable(this, containerId, query);
}

private CosmosResultSetIterator CreateQuery(
private FeedIterator CreateQuery(
string containerId,
CosmosSqlQuery query)
{
var items = Client.Databases[_databaseId].Containers[containerId].Items;
var container = Client.GetDatabase(_databaseId).GetContainer(containerId);
var queryDefinition = new CosmosSqlQueryDefinition(query.Query);
foreach (var parameter in query.Parameters)
{
queryDefinition.UseParameter(parameter.Name, parameter.Value);
}

return items.CreateItemQueryAsStream(queryDefinition, "0");
return container.CreateItemQueryStream(queryDefinition, maxConcurrency: 1, PartitionKey.NonePartitionKeyValue);
}

private class DocumentEnumerable : IEnumerable<JObject>
Expand All @@ -316,7 +314,7 @@ public DocumentEnumerable(

private class Enumerator : IEnumerator<JObject>
{
private CosmosResultSetIterator _query;
private FeedIterator _query;
private Stream _responseStream;
private StreamReader _reader;
private JsonTextReader _jsonReader;
Expand Down Expand Up @@ -430,9 +428,10 @@ public DocumentAsyncEnumerable(

public IAsyncEnumerator<JObject> GetAsyncEnumerator(CancellationToken cancellationToken = default)
=> new AsyncEnumerator(this, cancellationToken);

private class AsyncEnumerator : IAsyncEnumerator<JObject>
{
private CosmosResultSetIterator _query;
private FeedIterator _query;
private Stream _responseStream;
private StreamReader _reader;
private JsonTextReader _jsonReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public static IServiceCollection AddEntityFrameworkInMemoryDatabase([NotNull] th
.TryAdd<IEntityQueryableTranslatorFactory, InMemoryEntityQueryableTranslatorFactory>()
.TryAdd<IShapedQueryOptimizerFactory, InMemoryShapedQueryOptimizerFactory>()


.TryAdd<ISingletonOptions, IInMemorySingletonOptions>(p => p.GetService<IInMemorySingletonOptions>())
.TryAdd<ITypeMappingSource, InMemoryTypeMappingSource>()
.TryAddProviderSpecificServices(
Expand Down
11 changes: 5 additions & 6 deletions test/EFCore.Cosmos.FunctionalTests/CosmosEndToEndTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Azure.Cosmos;
using Microsoft.EntityFrameworkCore.Cosmos.TestUtilities;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.EntityFrameworkCore.TestUtilities.Xunit;
using Newtonsoft.Json.Linq;
using Xunit;

Expand Down Expand Up @@ -284,17 +283,17 @@ public void Can_use_non_persisted_properties()

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

using (var context = new UnmpappedCustomerContext(options))
using (var context = new UnmappedCustomerContext(options))
{
context.Database.EnsureCreated();

var entry = context.Add(customer);
context.Add(customer);

context.SaveChanges();
Assert.Equal("Theon", customer.Name);
}

using (var context = new UnmpappedCustomerContext(options))
using (var context = new UnmappedCustomerContext(options))
{
var customerFromStore = context.Set<Customer>().Single();

Expand All @@ -308,9 +307,9 @@ public void Can_use_non_persisted_properties()
}
}

public class UnmpappedCustomerContext : CustomerContext
public class UnmappedCustomerContext : CustomerContext
{
public UnmpappedCustomerContext(DbContextOptions dbContextOptions)
public UnmappedCustomerContext(DbContextOptions dbContextOptions)
: base(dbContextOptions)
{
}
Expand Down
3 changes: 2 additions & 1 deletion test/EFCore.Cosmos.FunctionalTests/NestedDocumentsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ public virtual void Can_add_collection_dependent_to_owner()
var json = context.Entry(people[1]).Property<JObject>("__jObject").CurrentValue;
var jsonAddress = (JObject)((JArray)json["Stored Addresses"])[0];
Assert.Equal("Second", jsonAddress[nameof(Address.Street)]);
// Uncomment when issue #13578 is fixed
//Assert.Equal(2, jsonAddress["unmappedId"]);
//Assert.Equal(2, jsonAddress.Count);

addresses = people[2].Addresses.ToList();
Assert.Equal(3, addresses.Count);
Expand Down Expand Up @@ -375,7 +377,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
eb => eb.OwnsMany(v => v.Addresses, b =>
{
b.ForCosmosToProperty("Stored Addresses");
b.HasKey(v => new { v.Street, v.City });
}));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Cosmos.Configuration
// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
{
public class CosmosDbContextOptionsExtensionsTests
{
Expand Down

0 comments on commit be28e3a

Please sign in to comment.