public void ApplyTo_OnSingleEntity_WithUnTypedContext_Throws_InvalidOperation() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataQueryContext context = new ODataQueryContext(model.Model, model.Customer); SelectExpandQueryOption selectExpand = new SelectExpandQueryOption(select: "ID", expand: null, context: context, queryTranslator: null); object entity = new object(); // Act & Assert Assert.Throws<NotSupportedException>(() => selectExpand.ApplyTo(entity, new ODataQuerySettings()), "The query option is not bound to any CLR type. 'ApplyTo' is only supported with a query option bound to a CLR type."); }
public void Ctor_SetsProperty_RawExpand() { // Arrange string expandValue = "expand"; _model.Model.SetAnnotationValue<ClrTypeAnnotation>(_model.Customer, new ClrTypeAnnotation(typeof(Customer))); ODataQueryContext context = new ODataQueryContext(_model.Model, typeof(Customer)); // Act SelectExpandQueryOption result = new SelectExpandQueryOption(select: null, expand: expandValue, context: context); // Assert Assert.Equal(expandValue, result.RawExpand); }
public void Ctor_SetsProperty_RawSelect() { // Arrange string selectValue = "select"; _model.Model.SetAnnotationValue<ClrTypeAnnotation>(_model.Customer, new ClrTypeAnnotation(typeof(Customer))); ODataQueryContext context = new ODataQueryContext(_model.Model, typeof(Customer)); // Act SelectExpandQueryOption result = new SelectExpandQueryOption(selectValue, expand: null, context: context, queryTranslator: null); // Assert Assert.Equal(selectValue, result.RawSelect); }
public void SelectExpandClause_Property_ParsesRawSelectAndRawExpand() { // Arrange IEdmModel model = _model.Model; _model.Model.SetAnnotationValue<ClrTypeAnnotation>(_model.Customer, new ClrTypeAnnotation(typeof(Customer))); ODataQueryContext context = new ODataQueryContext(model, typeof(Customer)); SelectExpandQueryOption option = new SelectExpandQueryOption("ID,Name,Orders", "Orders", context); // Act SelectExpandClause selectExpandClause = option.SelectExpandClause; // Assert Assert.NotNull(selectExpandClause.Selection); Assert.NotNull(selectExpandClause.Expansion); }
public void ApplyTo_OnQueryable_WithUnTypedContext_Throws_InvalidOperation() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataQueryContext context = new ODataQueryContext(model.Model, model.Customer); SelectExpandQueryOption selectExpand = new SelectExpandQueryOption(select: "ID", expand: null, context: context); IQueryable queryable = new Mock<IQueryable>().Object; // Act & Assert Assert.Throws<NotSupportedException>(() => selectExpand.ApplyTo(queryable, new ODataQuerySettings()), "The query option is not bound to any CLR type. 'ApplyTo' is only supported with a query option bound to a CLR type."); }
public void Property_SelectExpandClause_WorksWithUnTypedContext() { // Arrange CustomersModelWithInheritance model = new CustomersModelWithInheritance(); ODataQueryContext context = new ODataQueryContext(model.Model, model.Customer); SelectExpandQueryOption selectExpand = new SelectExpandQueryOption(select: "ID", expand: null, context: context); // Act & Assert Assert.NotNull(selectExpand.SelectExpandClause); }
public void SelectExpandCaluse_ThrowsODataException_InvalidQuery(string select, string expand, string error) { // Arrange IEdmModel model = _model.Model; _model.Model.SetAnnotationValue<ClrTypeAnnotation>(_model.Customer, new ClrTypeAnnotation(typeof(Customer))); ODataQueryContext context = new ODataQueryContext(model, typeof(Customer)); SelectExpandQueryOption option = new SelectExpandQueryOption(select, expand, context); // Act Assert.Throws<ODataException>( () => option.SelectExpandClause.ToString(), error); }
public void SelectExpandClause_CanParse_ModelBuiltForQueryable(string select, string expand) { // Arrange HttpConfiguration config = new HttpConfiguration(); config.Services.Replace(typeof(IAssembliesResolver), new TestAssemblyResolver()); ODataConventionModelBuilder builder = new ODataConventionModelBuilder(new HttpConfiguration(), isQueryCompositionMode: true); builder.Entity<Customer>(); IEdmModel model = builder.GetEdmModel(); ODataQueryContext context = new ODataQueryContext(model, typeof(Customer)); SelectExpandQueryOption option = new SelectExpandQueryOption(select, expand, context); // Act & Assert Assert.DoesNotThrow(() => option.SelectExpandClause.ToString()); }
/// <summary> /// Initializes a new instance of the <see cref="ODataQueryOptions"/> class based on the incoming request and some metadata information from /// the <see cref="ODataQueryContext"/>. /// </summary> /// <param name="context">The <see cref="ODataQueryContext"/> which contains the <see cref="IEdmModel"/> and some type information.</param> /// <param name="request">The incoming request message.</param> public ODataQueryOptions(ODataQueryContext context, HttpRequestMessage request) { if (context == null) { throw Error.ArgumentNull("context"); } if (request == null) { throw Error.ArgumentNull("request"); } if (request.GetConfiguration() != null) { _assembliesResolver = request.GetConfiguration().Services.GetAssembliesResolver(); } // fallback to the default assemblies resolver if none available. _assembliesResolver = _assembliesResolver ?? new DefaultAssembliesResolver(); // remember the context and request Context = context; Request = request; // Parse the query from request Uri RawValues = new ODataRawQueryOptions(); IEnumerable <KeyValuePair <string, string> > queryParameters = request.GetQueryNameValuePairs(); foreach (KeyValuePair <string, string> kvp in queryParameters) { switch (kvp.Key) { case "$filter": RawValues.Filter = kvp.Value; ThrowIfEmpty(kvp.Value, "$filter"); Filter = new FilterQueryOption(kvp.Value, context); break; case "$orderby": RawValues.OrderBy = kvp.Value; ThrowIfEmpty(kvp.Value, "$orderby"); OrderBy = new OrderByQueryOption(kvp.Value, context); break; case "$top": RawValues.Top = kvp.Value; ThrowIfEmpty(kvp.Value, "$top"); Top = new TopQueryOption(kvp.Value, context); break; case "$skip": RawValues.Skip = kvp.Value; ThrowIfEmpty(kvp.Value, "$skip"); Skip = new SkipQueryOption(kvp.Value, context); break; case "$select": RawValues.Select = kvp.Value; break; case "$inlinecount": RawValues.InlineCount = kvp.Value; ThrowIfEmpty(kvp.Value, "$inlinecount"); InlineCount = new InlineCountQueryOption(kvp.Value, context); break; case "$expand": RawValues.Expand = kvp.Value; break; case "$format": RawValues.Format = kvp.Value; break; case "$skiptoken": RawValues.SkipToken = kvp.Value; break; default: // we don't throw if we can't recognize the query break; } } if (RawValues.Select != null || RawValues.Expand != null) { SelectExpand = new SelectExpandQueryOption(RawValues.Select, RawValues.Expand, context); } Validator = new ODataQueryValidator(); }
public void SelectExpandClause_Property_ParsesRawSelectAndRawExpand() { // Arrange IEdmModel model = _model.Model; _model.Model.SetAnnotationValue<ClrTypeAnnotation>(_model.Customer, new ClrTypeAnnotation(typeof(Customer))); ODataQueryContext context = new ODataQueryContext(model, typeof(Customer)); SelectExpandQueryOption option = new SelectExpandQueryOption("ID,Name,Orders", "Orders", context, queryTranslator: null); // Act SelectExpandClause selectExpandClause = option.SelectExpandClause; // Assert Assert.NotEmpty(selectExpandClause.SelectedItems.OfType<PathSelectItem>()); Assert.NotEmpty(selectExpandClause.SelectedItems.OfType<ExpandedNavigationSelectItem>()); }