Skip to content

Commit

Permalink
Fix Trippin In Memory Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsking committed Aug 8, 2016
1 parent 74ecf80 commit 0f8a921
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,126 +37,140 @@ public void TestImperativeViewEntitySet()
[Fact]
public void TestCollectionOfPrimitivePropertyAccess()
{
TestGetPayloadContains("People(1)/Emails",
"\"@odata.context\":\"http://localhost:21248/api/Trippin/$metadata#Collection(Edm.String)\"");
TestGetPayloadContains("People(7)/Emails",
TestGetPayloadContains("People('russellwhyte')/Emails",
"\"@odata.context\":\"http://localhost:21248/");
TestGetPayloadContains("People('russellwhyte')/Emails",
"api/Trippin/$metadata#Collection(Edm.String)\"");

TestGetPayloadContains("People('russellwhyte')/Emails",
"\"value\":[");
}

[Fact]
public void TestCollectionOfComplexPropertyAccess()
{
TestGetPayloadContains("People(1)/Locations",
"\"@odata.context\":\"http://localhost:21248/api/Trippin/$metadata#Collection(" +
var reqStr = "People('russellwhyte')/AddressInfo";
TestGetPayloadContains(reqStr,
"\"@odata.context\":\"http://localhost:21248/");
TestGetPayloadContains(reqStr,
"api/Trippin/$metadata#Collection(" +
"Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location)\"");
}

[Fact]
public void TestCollectionOfEnumPropertyAccess()
{
TestGetPayloadContains("People(1)/Features",
"\"@odata.context\":\"http://localhost:21248/api/Trippin/$metadata#Collection(" +
var reqStr = "People('russellwhyte')/Features";
TestGetPayloadContains(reqStr,
"\"@odata.context\":\"http://localhost:21248/");
TestGetPayloadContains(reqStr,
"$metadata#Collection(" +
"Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature)\"");
}

[Fact]
public void TestEnumPropertyAccess()
{
TestGetPayloadContains("People(1)/FavoriteFeature",
"\"@odata.context\":\"http://localhost:21248/api/Trippin/$metadata#People(1)/FavoriteFeature");
var reqStr = "People('russellwhyte')/FavoriteFeature";
TestGetPayloadContains(reqStr,
"\"@odata.context\":\"http://localhost:21248/");
TestGetPayloadContains(reqStr,
"$metadata#" + reqStr);
}

[Fact]
public void TestRawValuedEnumPropertyAccess()
{
TestGetPayloadIs("People(1)/FavoriteFeature/$value", "Feature1");
TestGetPayloadIs("People('russellwhyte')/FavoriteFeature/$value", "Feature1");
}

[Fact]
public void TestCountCollectionOfStructuralProperty()
{
TestGetPayloadIs("People(1)/Emails/$count", "2");
TestGetPayloadIs("People(1)/Locations/$count", "2");
TestGetPayloadIs("People(1)/Features/$count", "2");
TestGetPayloadIs("People('russellwhyte')/Emails/$count", "2");
TestGetPayloadIs("People('russellwhyte')/AddressInfo/$count", "1");
}

/// <summary>
/// TODO t-pewang, this case cannot pass, since my navigation property doesn't auto expand
/// </summary>
[Fact]
public void TestAutoExpandedNavigationProperty()
{
TestGetPayloadContains("People", "\"Friends\":[");
}

[Theory]
// Single primitive property with null value
[InlineData("/People(5)/MiddleName", 204)]
// Single primitive property $value with null value
[InlineData("/People(5)/MiddleName/$value", 204)]
// Collection of primitive property with empty value
[InlineData("/People(5)/Emails", 200)]
// Collection of primitive property $value with null value, should throw exception
// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
[InlineData("/People(5)/Emails/$value", 404)]
// Single primitive property with null value
[InlineData("/People('willieashmore')/MiddleName", 204)]
// Single primitive property $value with null value
[InlineData("/People('willieashmore')/MiddleName/$value", 204)]
// Collection of primitive property with empty value
[InlineData("/People('willieashmore')/Emails", 200)]
//// Collection of primitive property $value with null value, should throw exception
//// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
//[InlineData("/People('willieashmore')/Emails/$value", 404)]
// Collection of primitive property with null collection
[InlineData("/People(7)/Emails", 200)]
[InlineData("/People('clydeguess')/Emails", 200)]
// single complex property with null value
[InlineData("/People(5)/HomeAddress", 204)]
[InlineData("/People('willieashmore')/HomeAddress", 204)]
// single complex property's propery and complex property has null value
[InlineData("/People(5)/HomeAddress/Address", 404)]
// single complex property's property with null value
[InlineData("/People(6)/HomeAddress/Address", 204)]
[InlineData("/People('willieashmore')/HomeAddress/Address", 404)]
// single complex property's property with null value
[InlineData("/People('clydeguess')/HomeAddress/Address", 204)]
// collection of complex property with empty collection value
[InlineData("/People(5)/Locations", 200)]
// collection of complex property's propery and collection of complex property has null value
// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
[InlineData("/People(5)/Locations/Address", 404)]
[InlineData("/People('willieashmore')/AddressInfo", 200)]
//// collection of complex property's propery and collection of complex property has null value
//// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
//[InlineData("/People(5)/AddressInfo/Address", 404)]
// Collection of complex property with null collection
[InlineData("/People(7)/Locations", 200)]
[InlineData("/People('clydeguess')/AddressInfo", 200)]
// single navigation property with null value
[InlineData("/People(5)/BestFriend", 204)]
[InlineData("/People('willieashmore')/BestFriend", 204)]
// single navigation property's propery and navigation property has null value
[InlineData("/People(5)/BestFriend/MiddleName", 404)]
[InlineData("/People('willieashmore')/BestFriend/MiddleName", 404)]
// single navigation property's property with null value
[InlineData("/People(6)/BestFriend/MiddleName", 204)]
[InlineData("/People('russellwhyte')/BestFriend/MiddleName", 204)]
// collection of navigation property with empty collection value
[InlineData("/People(5)/Friends", 200)]
[InlineData("/People('willieashmore')/Friends", 200)]
// collection of navigation property with null collection value
[InlineData("/People(7)/Trips", 200)]
// collection of navigation property's property and navigation property has null value
// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
[InlineData("/People(5)/Friends/MiddleName", 404)]
[InlineData("/People('clydeguess')/Trips", 200)]
//// collection of navigation property's property and navigation property has null value
//// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
//[InlineData("/People('willieashmore')/Friends/MiddleName", 404)]
public void QueryPropertyWithNullValueStatusCode(string url, int expectedCode)
{
TestGetStatusCodeIs(url, expectedCode);
}

[Theory]
// Single primitive property
[InlineData("/People(15)/MiddleName", 404)]
[InlineData("/People('NoneExist')/MiddleName", 404)]
// Single primitive property $value
[InlineData("/People(15)/MiddleName/$value", 404)]
// Collection of primitive property
[InlineData("/People(15)/Emails", 404)]
// Collection of primitive property $value
// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
[InlineData("/People(15)/Emails/$value", 404)]
[InlineData("/People('NoneExist')/MiddleName/$value", 404)]
// Collection of primitive property
[InlineData("/People('NoneExist')/Emails", 404)]
//// Collection of primitive property $value
//// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
//[InlineData("/People('NoneExist')/Emails/$value", 404)]
// single complex property
[InlineData("/People(15)/HomeAddress", 404)]
[InlineData("/People('NoneExist')/HomeAddress", 404)]
// single complex property's property
[InlineData("/People(15)/HomeAddress/Address", 404)]
[InlineData("/People('NoneExist')/HomeAddress/Address", 404)]
// collection of complex property
[InlineData("/People(15)/Locations", 404)]
// collection of complex property's propery
// TODO should be bad request 400 as this is not allowed?? 404 is returned by WebApi Route Match method
[InlineData("/People(15)/Locations/Address", 404)]
[InlineData("/People('NoneExist')/AddressInfo", 404)]
//// collection of complex property's propery
//// TODO should be bad request 400 as this is not allowed?? 404 is returned by WebApi Route Match method
//[InlineData("/People('NoneExist')/Locations/Address", 404)]
// single navigation property
[InlineData("/People(15)/BestFriend", 404)]
[InlineData("/People('NoneExist')/BestFriend", 404)]
// single navigation property's propery
[InlineData("/People(15)/BestFriend/MiddleName", 404)]
[InlineData("/People('NoneExist')/BestFriend/MiddleName", 404)]
// collection of navigation property
[InlineData("/People(15)/Friends", 404)]
// collection of navigation property's property
// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
[InlineData("/People(15)/Friends/MiddleName", 404)]
[InlineData("/People('NoneExist')/Friends", 404)]
//// collection of navigation property's property
//// TODO should be bad request 400 as this is not allowed, 404 is returned by WebApi Route Match method
//[InlineData("/People('NoneExist')/Friends/MiddleName", 404)]
public void QueryPropertyWithNonExistEntity(string url, int expectedCode)
{
TestGetStatusCodeIs(url, expectedCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public IQueryable<Person> People
}
}

public IQueryable<Person> NewComePeople
{
get
{
var datasource = _dataStoreManager.GetDataStoreInstance(Key);
return datasource?.People.AsQueryable();
}
}

public Person Me
{
get
Expand Down Expand Up @@ -177,7 +186,6 @@ public Airline GetFavoriteAirline(Person person)
return Airlines.Single(a => a.AirlineCode.Equals(favoriteAirlineCode));
}


/// <summary>
/// Bound Function, get the trips of one friend with userName
/// </summary>
Expand Down Expand Up @@ -427,15 +435,29 @@ private static void SetValues(object instance, Type type, IReadOnlyDictionary<st
if (!propertyInfo.PropertyType.IsInstanceOfType(value))
{
var dic = value as IReadOnlyDictionary<string, object>;
if (dic == null)
var col = value as System.Web.OData.EdmComplexObjectCollection;

if (dic != null)
{
value = Activator.CreateInstance(propertyInfo.PropertyType);
SetValues(value, propertyInfo.PropertyType, dic);
}
else if (col != null)
{
var realType = propertyInfo.PropertyType.GenericTypeArguments[0];
var valueType = typeof(Collection<>).MakeGenericType(realType);
value = Activator.CreateInstance(valueType);
foreach (var c in col)
{
value.GetType().GetMethod("Add").Invoke(value, new[] {c});
}
}
else
{
throw new NotSupportedException(string.Format(
CultureInfo.InvariantCulture,
propertyPair.Key));
}

value = Activator.CreateInstance(propertyInfo.PropertyType);
SetValues(value, propertyInfo.PropertyType, dic);
}

propertyInfo.SetValue(instance, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ public static class WebApiConfig
public static void Register(HttpConfiguration config)
{
RegisterTrippin(config, GlobalConfiguration.DefaultServer);
config.SetUseVerboseErrors(true);
config.MessageHandlers.Add(new ETagMessageHandler());
}

public static async void RegisterTrippin(
HttpConfiguration config, HttpServer server)
{
await config.MapRestierRoute<TrippinApi>(
"TrippinApi",
null, //"api/Trippin",
"TrippinApi",
"api/Trippin",
new RestierBatchHandler(server));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.OData.Builder;

namespace Microsoft.OData.Service.Sample.TrippinInMemory.Models
{
Expand All @@ -13,6 +14,14 @@ public enum PersonGender
Unknow
}

public enum Feature
{
Feature1,
Feature2,
Feature3,
Feature4
}

public class Person
{
[Key]
Expand All @@ -25,6 +34,8 @@ public class Person
[MaxLength(26), MinLength(1)]
public string LastName { get; set; }

public string MiddleName { get; set; }

public PersonGender Gender { get; set; }

public long? Age { get; set; }
Expand All @@ -33,10 +44,19 @@ public class Person

public ICollection<Location> AddressInfo { get; set; }

public Location HomeAddress { get; set; }

[AutoExpand]
public virtual ICollection<Person> Friends { get; set; }

public Person BestFriend { get; set; }

public virtual ICollection<Trip> Trips { get; set; }

public Feature FavoriteFeature { get; set; }

public virtual ICollection<Feature> Features { get; set; }

[ConcurrencyCheck]
public long Concurrency { get; set; }
}
Expand Down
Loading

0 comments on commit 0f8a921

Please sign in to comment.