Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElizabethOkerio committed Sep 5, 2023
1 parent 4369f21 commit 3409e83
Showing 1 changed file with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -356,6 +357,52 @@ public void Bind_UsingEFQueryProvider_LowerCamelCasedModel_GeneratedExpression__
Assert.Null(innerInnerCustomer.Instance.Orders);
}

[Fact]
public void Bind_SelectAndExpand_WithNullProperties_DoesNotThrowException()
{
// Arrange
IQueryable<User> users;
string expand = "FileRefNavigation";

User user = new User
{
UserId = 1,
Name = "Alex",
Age = 35,
DataFileRef = null,
FileRefNavigation = null
};

users = new[] { user }.AsQueryable();
ODataQueryContext context = new ODataQueryContext(_model, typeof(User)) { RequestContainer = new MockServiceProvider() };

SelectExpandQueryOption selectExpand = new SelectExpandQueryOption(select: null, expand: expand, context: context);

QueryBinderContext queryBinderContext = new QueryBinderContext(_model, _settings, selectExpand.Context.ElementClrType)
{
NavigationSource = context.NavigationSource
};

queryBinderContext.QueryProvider = HandleNullPropagationOptionHelper.EntityFrameworkQueryProviderNamespace;

// Act
SelectExpandBinder binder = new SelectExpandBinder();
IQueryable queryable = binder.ApplyBind(users, selectExpand.SelectExpandClause, queryBinderContext);

// Assert
Assert.NotNull(queryable);

IEnumerator enumerator = queryable.GetEnumerator();
Assert.True(enumerator.MoveNext());
var usr = Assert.IsAssignableFrom<SelectExpandWrapper<User>>(enumerator.Current);
Assert.False(enumerator.MoveNext());
Assert.NotNull(usr.Instance);
Assert.Equal("Microsoft.AspNetCore.OData.Tests.Query.Expressions.User", usr.Instance.GetType().ToString());
IEnumerable<SelectExpandWrapper<DataFile>> fileRefsNavigations = usr.Container
.ToDictionary(PropertyMapper)["FileRefNavigation"] as IEnumerable<SelectExpandWrapper<DataFile>>;
Assert.Null(fileRefsNavigations);
}

[Fact]
public void Bind_GeneratedExpression_CheckNullObjectWithinChainProjectionByKey()
{
Expand Down Expand Up @@ -2095,6 +2142,7 @@ public static IEdmModel GetEdmModel()
builder.EntitySet<QueryOrder>("Orders");
builder.EntitySet<QueryCity>("Cities");
builder.EntitySet<QueryProduct>("Products");
builder.EntitySet<User>("Users");

customer.Collection.Function("IsUpgraded").Returns<bool>().Namespace="NS";
customer.Collection.Action("UpgradeAll").Namespace = "NS";
Expand Down Expand Up @@ -2305,4 +2353,27 @@ public enum QueryColor

Blue
}

public class User
{
[Key]
public int UserId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Age { get; set; }

//Navigations
[ForeignKey("FileRefNavigation")]
public int? DataFileRef { get; set; }
public DataFile? FileRefNavigation { get; set; }
}

public class DataFile
{
[Key]
public int FileId { get; set; }
[Required]
public string FileName { get; set; }
}
}

0 comments on commit 3409e83

Please sign in to comment.