public void WithSimpleProperty_maps_properly() { //Arrange var builder = new ConfigurationBuilder(); var post = new Post() { Title = "test" }; //Act builder .Resource<Post, PostsController>() .WithSimpleProperty(p => p.Title); var configuration = builder.Build(); var mapping = configuration.GetMapping(typeof(Post)); //Assert Assert.Equal(mapping.PropertyGetters.Count, 1); Assert.Equal(mapping.PropertySetters.Count, 1); var getter = mapping.PropertyGetters.Single().Value; var setter = mapping.PropertySetters.Single().Value; Assert.Equal(((string)getter(post)), "test"); setter(post, "works"); Assert.Equal(post.Title, "works"); }
public PostBuilder() { this.post = new Post() { Id = 1, Title = "Post Title 1", }; }
public void For_existing_id_properties_returns_proper_expression() { // Arrange var convention = new SimpleLinkedIdConvention(); var post = new Post {AuthorId = 4}; // Act var expression = convention.GetIdExpression((Post p) => p.Author); // Assert Assert.NotNull(expression); var compiled = expression.Compile(); Assert.Equal(compiled(post), 4); }
public void WithIdSelector_maps_properly() { //Arrange var builder = new ConfigurationBuilder(); var post = new Post { Id = 4 }; //Act builder .Resource<Post, PostsController>() .WithIdSelector(p => p.Id); var configuration = builder.Build(); var mapping = configuration.GetMapping(typeof(Post)); //Assert Assert.NotNull(mapping.IdGetter); Assert.Equal(mapping.IdGetter(post), 4); }
public void TestWithIdSelectorForMultipleTypes() { //Arrange var resourceConfigurationForAuthor = configurationBuilder.Resource<Author>(); var resourceConfigurationForPost = configurationBuilder.Resource<Post>(); const int authorId = 5; const int postId = 6; var author = new Author() { Id = authorId }; var post = new Post() { Id = postId }; //Act resourceConfigurationForAuthor.WithIdSelector(a => a.Id); resourceConfigurationForPost.WithIdSelector(p => p.Id); //Assert var authorResult = (int)resourceConfigurationForAuthor.ConstructedMetadata.IdGetter.Invoke(author); authorResult.ShouldEqual(authorId); var postResult = (int)resourceConfigurationForPost.ConstructedMetadata.IdGetter.Invoke(post); postResult.ShouldEqual(postId); }
public void TestWithIdSelectorForMultipleTypes() { //Arrange var resourceConfigurationForAuthor = configurationBuilder.Resource<Author, AuthorsController>(); var resourceConfigurationForPost = configurationBuilder.Resource<Post, PostsController>(); const int authorId = 5; const int postId = 6; var author = new Author() { Id = authorId }; var post = new Post() { Id = postId }; //Act resourceConfigurationForAuthor.WithIdSelector(a => a.Id); resourceConfigurationForPost.WithIdSelector(p => p.Id); //Assert var authorResult = (int)resourceConfigurationForAuthor.BuiltResourceMapping.IdGetter.Invoke(author); Assert.Equal(authorResult, authorId); var postResult = (int)resourceConfigurationForPost.BuiltResourceMapping.IdGetter.Invoke(post); Assert.Equal(postResult, postId); }
public void WithLinkedResource_maps_properly() { //Arrange var builder = new ConfigurationBuilder(); builder .WithConvention(new CamelCaseLinkNameConvention()) .WithConvention(new PluralizedCamelCaseTypeConvention()) .WithConvention(new SimpleLinkedIdConvention()); var post = new Post(); var author = new Author { Posts = new List<Post> { post } }; post.Author = author; post.AuthorId = 4; //Act builder .Resource<Post, PostsController>() .WithLinkedResource(p => p.Author); builder .Resource<Author, AuthorsController>() .WithLinkedResource(a => a.Posts); var configuration = builder.Build(); var postMapping = configuration.GetMapping(typeof(Post)); var authorMapping = configuration.GetMapping(typeof(Author)); //Assert Assert.Equal(postMapping.Relationships.Count, 1); var linkToAuthor = postMapping.Relationships.Single(); Assert.False(linkToAuthor.IsCollection); Assert.Equal(linkToAuthor.RelationshipName, "author"); Assert.Equal(linkToAuthor.ParentType, typeof(Post)); Assert.Equal(linkToAuthor.RelatedBaseType, typeof(Author)); Assert.Same(linkToAuthor.RelatedResource(post), author); Assert.Equal(linkToAuthor.RelatedResourceId(post), 4); Assert.Same(linkToAuthor.ResourceMapping, authorMapping); Assert.Equal(authorMapping.Relationships.Count, 1); var linkToPosts = authorMapping.Relationships.Single(); Assert.True(linkToPosts.IsCollection); Assert.Equal(linkToPosts.RelationshipName, "posts"); Assert.Equal(linkToPosts.ParentType, typeof(Author)); Assert.Equal(linkToPosts.RelatedBaseType, typeof(Post)); Assert.Same(linkToPosts.RelatedResource(author), author.Posts); Assert.Null(linkToPosts.RelatedResourceId); Assert.Same(linkToPosts.ResourceMapping, postMapping); }
public void WithComplexObjectTest() { //Arrange const int authorId = 5; const string authorName = "Valentin"; const int postId = 6; const string postTitle = "The measure of a man"; const string commentBody = "Comment body"; const int commentId = 7; var author = new Author() { Id = authorId, Name = authorName }; var post = new Post() { Id = postId, Title = postTitle, Author = author }; var comment = new Comment() { Id = commentId, Body = commentBody, Post = post }; post.Replies = new List<Comment>() { comment }; author.Posts = new List<Post>() { post }; var configurationBuilder = new ConfigurationBuilder(); //Act var resourceConfigurationForPost = configurationBuilder .Resource<Post, PostsController>() .WithSimpleProperty(p => p.Title) .WithIdSelector(p => p.Id) .WithLinkedResource(p => p.Replies); var resourceConfigurationForAuthor = configurationBuilder .Resource<Author, AuthorsController>() .WithSimpleProperty(a => a.Name) .WithIdSelector(a => a.Id) .WithLinkedResource(a => a.Posts); var resourceConfigurationForComment = configurationBuilder .Resource<Comment, CommentsController>() .WithIdSelector(c => c.Id) .WithSimpleProperty(c => c.Body); var result = configurationBuilder.Build(); //Assert Assert.Equal(resourceConfigurationForPost.BuiltResourceMapping.Relationships.Count, 1); Assert.Equal(resourceConfigurationForAuthor.BuiltResourceMapping.Relationships.Count, 1); configurationBuilder.ResourceConfigurationsByType.All( r => r.Value.BuiltResourceMapping.Relationships.All(l => l.ResourceMapping != null)); var authorLinks = configurationBuilder.ResourceConfigurationsByType[ resourceConfigurationForAuthor.BuiltResourceMapping.ResourceRepresentationType].BuiltResourceMapping.Relationships; Assert.Equal(authorLinks.Count, 1); Assert.Equal(authorLinks[0].RelationshipName, "posts"); Assert.Equal(authorLinks[0].ResourceMapping.PropertyGetters.Count, 1); }
public void WithAllSimpleProperties_maps_properly() { //Arrange var builder = new ConfigurationBuilder(); builder .WithConvention(new DefaultPropertyScanningConvention()); const string testTitle = "test"; var post = new Post { Id = 4, Title = testTitle }; //Act builder .Resource<Post, PostsController>() .WithAllSimpleProperties(); var configuration = builder.Build(); var postMapping = configuration.GetMapping(typeof(Post)); //Assert Assert.NotNull(postMapping.IdGetter); Assert.Equal(postMapping.IdGetter(post), 4); Assert.Equal(postMapping.PropertyGetters.Count, 2); Assert.Equal(postMapping.PropertySetters.Count, 2); Assert.Equal(postMapping.PropertyGetters["title"](post), testTitle); Assert.True(postMapping.PropertyGetters.ContainsKey("authorId")); }
public void TestWithSimplePropertyMultipleTypes() { //Arrange const int authorId = 5; const string authorName = "Valentin"; const int postId = 6; const string postTitle = "The measure of a man"; const string postTitleModifed = "Modified"; var author = new Author() { Id = authorId, Name = authorName }; var post = new Post() { Id = postId, Title = postTitle }; //Act var resourceConfigurationForPost = configurationBuilder .Resource<Post, PostsController>() .WithSimpleProperty(p => p.Title) .WithIdSelector(p => p.Id); var resourceConfigurationForAuthor = configurationBuilder .Resource<Author, AuthorsController>() .WithSimpleProperty(a => a.Name) .WithIdSelector(a => a.Id); //Assert var result = resourceConfigurationForAuthor.BuiltResourceMapping; AssertResourceConfigurationHasValuesForWithSimpleProperty(resourceConfigurationForAuthor); Assert.Contains("author", result.ResourceType); Assert.Equal(result.PropertyGetters["name"].Invoke(author), authorName); Assert.Equal(result.IdGetter.Invoke(author), authorId); AssertResourceConfigurationHasValuesForWithSimpleProperty(resourceConfigurationForPost); result = resourceConfigurationForPost.BuiltResourceMapping; Assert.Contains("post", result.ResourceType); Assert.Equal(result.PropertyGetters["title"].Invoke(post), postTitle); resourceConfigurationForPost.BuiltResourceMapping.PropertySetters["title"].Invoke(post, postTitleModifed); Assert.Equal(post.Title,postTitleModifed); Assert.Equal(result.PropertyGetters["title"].Invoke(post), postTitleModifed); }
public void TestWithSimplePropertyMultipleTypes() { //Arrange const int authorId = 5; const string authorName = "Valentin"; const int postId = 6; const string postTitle = "The measure of a man"; const string postTitleModifed = "Modified"; var author = new Author() { Id = authorId, Name = authorName }; var post = new Post() { Id = postId, Title = postTitle }; //Act var resourceConfigurationForPost = configurationBuilder .Resource<Post>() .WithSimpleProperty(p => p.Title) .WithIdSelector(p => p.Id); var resourceConfigurationForAuthor = configurationBuilder .Resource<Author>() .WithSimpleProperty(a => a.Name) .WithIdSelector(a => a.Id); //Assert AssertResourceConfigurationHasValuesForWithSimpleProperty(resourceConfigurationForAuthor); resourceConfigurationForAuthor.ConstructedMetadata.ResourceType.ShouldContain(typeof(Author).Name.ToLower()); resourceConfigurationForAuthor.ConstructedMetadata.PropertyGetters["name"].Invoke(author).ShouldEqual(authorName); resourceConfigurationForAuthor.ConstructedMetadata.IdGetter.Invoke(author).ShouldEqual(authorId); AssertResourceConfigurationHasValuesForWithSimpleProperty(resourceConfigurationForPost); resourceConfigurationForPost.ConstructedMetadata.ResourceType.ShouldContain(typeof(Post).Name.ToLower()); resourceConfigurationForPost.ConstructedMetadata.PropertyGetters["title"].Invoke(post).ShouldEqual(postTitle); resourceConfigurationForPost.ConstructedMetadata.PropertySetters["title"].Invoke(post, postTitleModifed); post.Title.ShouldEqual(postTitleModifed); resourceConfigurationForPost.ConstructedMetadata.PropertyGetters["title"].Invoke(post).ShouldEqual(postTitleModifed); }