From 2e5b5d4b126a7d0fb5787a76f56134a59f268a1e Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:22:44 -0800 Subject: [PATCH] fix tests and queryable project --- .../GenericlessQueryBuilder.cs | 87 ++++++++++--------- .../Expressions/ExpressionTester.cs | 8 +- 2 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/EdgeDB.Net.Queryable/GenericlessQueryBuilder.cs b/src/EdgeDB.Net.Queryable/GenericlessQueryBuilder.cs index b8bceca1..e65dc817 100644 --- a/src/EdgeDB.Net.Queryable/GenericlessQueryBuilder.cs +++ b/src/EdgeDB.Net.Queryable/GenericlessQueryBuilder.cs @@ -91,7 +91,9 @@ private TNode AddNode(NodeContext context, bool autoGenerated = false, Qu } #region Query methods + #region With + public GenericlessQueryBuilder With(object variables) { if (variables is null) @@ -110,7 +112,8 @@ public GenericlessQueryBuilder With(object variables) { var varName = QueryUtils.GenerateRandomVariableName(); _queryVariables.Add(varName, value); - _queryGlobals.Add(new QueryGlobal(property.Name, new SubQuery($"<{scalarInfo}>${varName}"))); + _queryGlobals.Add(new QueryGlobal(property.Name, + new SubQuery(writer => writer.QueryArgument(scalarInfo, varName)))); } else if (property.PropertyType.IsAssignableTo(typeof(IQueryBuilder))) { @@ -131,30 +134,32 @@ public GenericlessQueryBuilder With(object variables) var referenceValue = property.PropertyType.GetProperty("Value")!.GetValue(value); var jsonVarName = QueryUtils.GenerateRandomVariableName(); _queryVariables.Add(jsonVarName, DataTypes.Json.Serialize(referenceValue)); - _queryGlobals.Add(new QueryGlobal(property.Name, new SubQuery($"${jsonVarName}"), value)); + _queryGlobals.Add(new QueryGlobal(property.Name, + new SubQuery(writer => writer.QueryArgument("json", jsonVarName)), value)); } else - throw new InvalidOperationException($"Cannot serialize {property.Name}: No serialization strategy found for {property.PropertyType}"); + throw new InvalidOperationException( + $"Cannot serialize {property.Name}: No serialization strategy found for {property.PropertyType}"); } return this; } + #endregion #region For + public GenericlessQueryBuilder For(IEnumerable collection, LambdaExpression iterator, Type? type = null) { - AddNode(new ForContext(EnterType(type)) - { - Expression = iterator, - Set = collection - }); + AddNode(new ForContext(EnterType(type)) { Expression = iterator, Set = collection }); return this; } + #endregion #region Select + public GenericlessQueryBuilder Select(Type? type = null) { AddNode(new SelectContext(EnterType(type))); @@ -165,11 +170,7 @@ public GenericlessQueryBuilder Select(IShapeBuilder? shape, Type? type = null) { type ??= EnterType(type); - AddNode(new SelectContext(type) - { - Shape = shape, - IsFreeObject = type.IsAnonymousType() - }); + AddNode(new SelectContext(type) { Shape = shape, IsFreeObject = type.IsAnonymousType() }); return this; } @@ -180,24 +181,21 @@ public GenericlessQueryBuilder SelectExp(LambdaExpression expression, Type? type AddNode(new SelectContext(type) { - Expression = expression, - IncludeShape = false, - IsFreeObject = type.IsAnonymousType(), + Expression = expression, IncludeShape = false, IsFreeObject = type.IsAnonymousType(), }); return this; } + #endregion #region Insert + public GenericlessQueryBuilder Insert(object value, bool returnInsertedValue = true, Type? type = null) { type ??= EnterType(type); - var insertNode = AddNode(new InsertContext(type) - { - Value = value - }); + var insertNode = AddNode(new InsertContext(type) { Value = value }); if (returnInsertedValue) { @@ -206,17 +204,16 @@ public GenericlessQueryBuilder Insert(object value, bool returnInsertedValue = t return this; } + #endregion #region Update + public GenericlessQueryBuilder Update(LambdaExpression func, bool returnUpdatedValue = true, Type? type = null) { type ??= EnterType(type); - var updateNode = AddNode(new UpdateContext(type) - { - UpdateExpression = func - }); + var updateNode = AddNode(new UpdateContext(type) { UpdateExpression = func }); if (returnUpdatedValue) { @@ -225,18 +222,23 @@ public GenericlessQueryBuilder Update(LambdaExpression func, bool returnUpdatedV return this; } + #endregion #region Delete + public GenericlessQueryBuilder Delete(Type? type = null) { AddNode(new DeleteContext(EnterType(type))); return this; } + #endregion + #endregion #region Query node attributes + public GenericlessQueryBuilder Filter(LambdaExpression expression) { switch (CurrentUserNode) @@ -250,10 +252,12 @@ public GenericlessQueryBuilder Filter(LambdaExpression expression) default: throw new InvalidOperationException($"Cannot filter on a {CurrentUserNode}"); } + return this; } - public GenericlessQueryBuilder OrderBy(bool asc, LambdaExpression selector, OrderByNullPlacement? placement = null) + public GenericlessQueryBuilder OrderBy(bool asc, LambdaExpression selector, + OrderByNullPlacement? placement = null) { if (CurrentUserNode is not SelectNode selectNode) throw new InvalidOperationException($"Cannot order by on a {CurrentUserNode}"); @@ -357,13 +361,17 @@ public GenericlessQueryBuilder Else(Func IntrospectAndBuildAsync(IEdgeDBQueryable edgedb, CancellationToken token) { - if (_nodes.Any(x => x.RequiresIntrospection) || _queryGlobals.Any(x => x.Value is SubQuery subQuery && subQuery.RequiresIntrospection)) - _schemaInfo ??= await SchemaIntrospector.GetOrCreateSchemaIntrospectionAsync(edgedb, token).ConfigureAwait(false); + if (_nodes.Any(x => x.RequiresIntrospection) || + _queryGlobals.Any(x => x.Value is SubQuery subQuery && subQuery.RequiresIntrospection)) + _schemaInfo ??= await SchemaIntrospector.GetOrCreateSchemaIntrospectionAsync(edgedb, token) + .ConfigureAwait(false); var result = Build(); _nodes.Clear(); @@ -372,7 +380,8 @@ private async ValueTask IntrospectAndBuildAsync(IEdgeDBQueryable edg return result; } - internal BuiltQuery InternalBuild(bool includeGlobalsInQuery = true, Action? preFinalizerModifier = null) + internal BuiltQuery InternalBuild(bool includeGlobalsInQuery = true, + Action? preFinalizerModifier = null) { List query = new(); List> parameters = new(); @@ -391,15 +400,10 @@ internal BuiltQuery InternalBuild(bool includeGlobalsInQuery = true, Action x) - .DistinctBy(x => x.Key); + .SelectMany(x => x) + .DistinctBy(x => x.Key); // add any variables that might have been added by other builders in a sub-query context. variables = variables.Concat(_queryVariables.Where(x => !variables.Any(x => x.Key == x.Key))); @@ -436,8 +440,7 @@ internal BuiltQuery InternalBuild(bool includeGlobalsInQuery = true, Action x.Key, x => x.Value), - + .ToDictionary(x => x.Key, x => x.Value), Globals = !includeGlobalsInQuery ? _queryGlobals : null }; } @@ -453,16 +456,20 @@ public ValueTask BuildAsync(IEdgeDBQueryable edgedb, CancellationTok /// public BuiltQuery BuildWithGlobals(Action? preFinalizerModifier = null) => InternalBuild(false, preFinalizerModifier); + #endregion #region IQueryBuilder + IReadOnlyCollection IQueryBuilder.Nodes => _nodes.ToImmutableArray(); + IReadOnlyCollection IQueryBuilder.Globals => _queryGlobals.ToImmutableArray(); IReadOnlyDictionary IQueryBuilder.Variables => _queryVariables.ToImmutableDictionary(); + #endregion } } diff --git a/tests/EdgeDB.Tests.Integration/QueryBuilder/Expressions/ExpressionTester.cs b/tests/EdgeDB.Tests.Integration/QueryBuilder/Expressions/ExpressionTester.cs index e18b94f8..33b9ffb3 100644 --- a/tests/EdgeDB.Tests.Integration/QueryBuilder/Expressions/ExpressionTester.cs +++ b/tests/EdgeDB.Tests.Integration/QueryBuilder/Expressions/ExpressionTester.cs @@ -50,9 +50,10 @@ public static void AssertExpression( public static void AssertExpression(Func contextFactory, Expression expression, string expected) { - var result = ExpressionTranslator.ContextualTranslate(expression, contextFactory()); + var writer = new QueryStringWriter(); + ExpressionTranslator.ContextualTranslate(expression, contextFactory(), writer); - Assert.AreEqual(expected, result); + Assert.AreEqual(expected, writer.Compile().ToString()); } public static void AssertException( @@ -77,7 +78,8 @@ public static void AssertException( public static void AssertException(Func contextFactory, Expression expression) where TException : Exception { - Assert.ThrowsException(() => ExpressionTranslator.ContextualTranslate(expression, contextFactory())); + var writer = new QueryStringWriter(); + Assert.ThrowsException(() => ExpressionTranslator.ContextualTranslate(expression, contextFactory(), writer)); } } }