-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: Adds Computed Property SDK Support (#3761)
* Initial commit * Restored settings.json changes. * Update * Addressed comments; still need to be tested using Emulator. * Fixes after test run. * Ignored the computed property tests based on the sync this morning (to allow for preview release). * Suite0 fixes. * Test update. * Suite0 fixes
- Loading branch information
Showing
11 changed files
with
1,162 additions
and
26 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
Microsoft.Azure.Cosmos/src/Fluent/Settings/ComputedPropertiesDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
namespace Microsoft.Azure.Cosmos.Fluent | ||
{ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
|
||
/// <summary> | ||
/// Computed Properties fluent definition. | ||
/// </summary> | ||
/// <seealso cref="ComputedProperty"/> | ||
#if PREVIEW | ||
public | ||
#else | ||
internal | ||
#endif | ||
class ComputedPropertiesDefinition<T> | ||
{ | ||
private readonly Collection<ComputedProperty> computedProperties = new Collection<ComputedProperty>(); | ||
private readonly T parent; | ||
private readonly Action<Collection<ComputedProperty>> attachCallback; | ||
|
||
internal ComputedPropertiesDefinition( | ||
T parent, | ||
Action<Collection<ComputedProperty>> attachCallback) | ||
{ | ||
this.parent = parent; | ||
this.attachCallback = attachCallback; | ||
} | ||
|
||
/// <summary> | ||
/// Adds a computed property to the current <see cref="ComputedPropertiesDefinition{T}"/> | ||
/// </summary> | ||
/// <param name="name">Name of the computed property</param> | ||
/// <param name="query">Query for the computed property values</param> | ||
/// <returns>An instance of the current <see cref="ComputedPropertiesDefinition{T}"/></returns> | ||
public ComputedPropertiesDefinition<T> WithComputedProperty(string name, string query) | ||
{ | ||
this.computedProperties.Add( | ||
new ComputedProperty | ||
{ | ||
Name = name, | ||
Query = query | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Applies the current definition to the parent. | ||
/// </summary> | ||
/// <returns>An instance of the parent.</returns> | ||
public T Attach() | ||
{ | ||
this.attachCallback(this.computedProperties); | ||
return this.parent; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
Microsoft.Azure.Cosmos/src/Resource/Settings/ComputedProperty.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos | ||
{ | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
/// <summary> | ||
/// Represents a computed property definition in a Cosmos DB collection. | ||
/// </summary> | ||
#if PREVIEW | ||
public | ||
#else | ||
internal | ||
#endif | ||
sealed class ComputedProperty | ||
{ | ||
/// <summary> | ||
/// Gets or sets the name of the computed property. | ||
/// </summary> | ||
/// <value> | ||
/// The name of the computed property. | ||
/// </value> | ||
/// <remarks> | ||
/// Name of the computed property should be chosen such that it does not collide with any existing or future document properties. | ||
/// </remarks> | ||
[JsonProperty(PropertyName = "name")] | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the query for the computed property. | ||
/// </summary> | ||
/// <value> | ||
/// The query used to evaluate the value for the computed property. | ||
/// </value> | ||
/// <remarks> | ||
/// For example: | ||
/// SELECT VALUE LOWER(c.firstName) FROM c | ||
/// </remarks> | ||
[JsonProperty(PropertyName = "query")] | ||
public string Query { get; set; } | ||
|
||
/// <summary> | ||
/// This contains additional values for scenarios where the SDK is not aware of new fields. | ||
/// This ensures that if resource is read and updated none of the fields will be lost in the process. | ||
/// </summary> | ||
[JsonExtensionData] | ||
internal IDictionary<string, JToken> AdditionalProperties { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...osoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ComputedPropertyComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
namespace Microsoft.Azure.Cosmos | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
class ComputedPropertyComparer : IEqualityComparer<ComputedProperty> | ||
{ | ||
public static void AssertAreEqual(Collection<ComputedProperty> expected, Collection<ComputedProperty> actual) | ||
{ | ||
int expectedCount = expected?.Count ?? 0; | ||
int actualCount = actual?.Count ?? 0; | ||
Assert.AreEqual(expectedCount, actualCount); | ||
|
||
for (int i = 0; i < expectedCount; i++) | ||
{ | ||
AssertAreEqual(expected[i], actual[i]); | ||
} | ||
} | ||
|
||
public static void AssertAreEqual(ComputedProperty expected, ComputedProperty actual) | ||
{ | ||
ComputedPropertyComparer comparer = new ComputedPropertyComparer(); | ||
Assert.IsTrue(comparer.Equals(expected, actual), $"Expected: {ToString(expected)}{Environment.NewLine}Actual:{ToString(actual)}"); | ||
} | ||
|
||
private static string ToString(ComputedProperty computedProperty) => $@"""Name"":""{computedProperty.Name}"", ""Query"":""{computedProperty.Query}"""; | ||
|
||
public bool Equals(ComputedProperty x, ComputedProperty y) | ||
{ | ||
if (x == null) return y == null; | ||
if (y == null) return false; | ||
|
||
return (x.Name?.Equals(y.Name) == true) && | ||
(x.Query?.Equals(y.Query) == true); | ||
} | ||
|
||
public int GetHashCode([DisallowNull] ComputedProperty obj) | ||
{ | ||
return obj.GetHashCode(); | ||
} | ||
} | ||
} |
Oops, something went wrong.