|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using Google.Protobuf.WellKnownTypes; |
| 8 | +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
| 9 | +using Microsoft.EntityFrameworkCore.TestUtilities; |
| 10 | +using ProtoTest; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Microsoft.EntityFrameworkCore |
| 14 | +{ |
| 15 | + public abstract class GrpcTestBase<TFixture> : IClassFixture<TFixture> |
| 16 | + where TFixture : GrpcTestBase<TFixture>.GrpcFixtureBase |
| 17 | + { |
| 18 | + protected GrpcTestBase(TFixture fixture) |
| 19 | + => Fixture = fixture; |
| 20 | + |
| 21 | + protected TFixture Fixture { get; } |
| 22 | + |
| 23 | + protected List<EntityTypeMapping> ExpectedMappings |
| 24 | + => new() |
| 25 | + { |
| 26 | + new() |
| 27 | + { |
| 28 | + Name = "PostTag", |
| 29 | + TableName = "PostTag", |
| 30 | + PrimaryKey = |
| 31 | + "Key: PostTag (Dictionary<string, object>).PostsInTagDataPostId, PostTag (Dictionary<string, object>).TagsInPostDataTagId PK", |
| 32 | + Properties = |
| 33 | + { |
| 34 | + "Property: PostTag (Dictionary<string, object>).PostsInTagDataPostId (no field, int) Indexer Required PK FK AfterSave:Throw", |
| 35 | + "Property: PostTag (Dictionary<string, object>).TagsInPostDataTagId (no field, int) Indexer Required PK FK Index AfterSave:Throw", |
| 36 | + }, |
| 37 | + Indexes = { "{'TagsInPostDataTagId'} ", }, |
| 38 | + FKs = |
| 39 | + { |
| 40 | + "ForeignKey: PostTag (Dictionary<string, object>) {'PostsInTagDataPostId'} -> Post {'PostId'} Cascade", |
| 41 | + "ForeignKey: PostTag (Dictionary<string, object>) {'TagsInPostDataTagId'} -> Tag {'TagId'} Cascade", |
| 42 | + }, |
| 43 | + }, |
| 44 | + new() |
| 45 | + { |
| 46 | + Name = "ProtoTest.Author", |
| 47 | + TableName = "Author", |
| 48 | + PrimaryKey = "Key: Author.AuthorId PK", |
| 49 | + Properties = |
| 50 | + { |
| 51 | + "Property: Author.AuthorId (authorId_, int) Required PK AfterSave:Throw ValueGenerated.OnAdd", |
| 52 | + "Property: Author.DateCreated (dateCreated_, Timestamp)", |
| 53 | + "Property: Author.Name (name_, string)", |
| 54 | + }, |
| 55 | + }, |
| 56 | + new() |
| 57 | + { |
| 58 | + Name = "ProtoTest.Post", |
| 59 | + TableName = "Post", |
| 60 | + PrimaryKey = "Key: Post.PostId PK", |
| 61 | + Properties = |
| 62 | + { |
| 63 | + "Property: Post.PostId (postId_, int) Required PK AfterSave:Throw ValueGenerated.OnAdd", |
| 64 | + "Property: Post.AuthorId (authorId_, int) Required FK Index", |
| 65 | + "Property: Post.DateCreated (dateCreated_, Timestamp)", |
| 66 | + "Property: Post.PostStat (postStat_, PostStatus) Required", |
| 67 | + "Property: Post.Title (title_, string)", |
| 68 | + }, |
| 69 | + Indexes = { "{'AuthorId'} ", }, |
| 70 | + FKs = { "ForeignKey: Post {'AuthorId'} -> Author {'AuthorId'} ToPrincipal: PostAuthor Cascade", }, |
| 71 | + Navigations = { "Navigation: Post.PostAuthor (postAuthor_, Author) ToPrincipal Author", }, |
| 72 | + SkipNavigations = |
| 73 | + { |
| 74 | + "SkipNavigation: Post.TagsInPostData (tagsInPostData_, RepeatedField<Tag>) CollectionTag Inverse: PostsInTagData", |
| 75 | + }, |
| 76 | + }, |
| 77 | + new() |
| 78 | + { |
| 79 | + Name = "ProtoTest.Tag", |
| 80 | + TableName = "Tag", |
| 81 | + PrimaryKey = "Key: Tag.TagId PK", |
| 82 | + Properties = |
| 83 | + { |
| 84 | + "Property: Tag.TagId (tagId_, int) Required PK AfterSave:Throw ValueGenerated.OnAdd", |
| 85 | + "Property: Tag.Name (name_, string)", |
| 86 | + }, |
| 87 | + SkipNavigations = |
| 88 | + { |
| 89 | + "SkipNavigation: Tag.PostsInTagData (postsInTagData_, RepeatedField<Post>) CollectionPost Inverse: TagsInPostData", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + [ConditionalFact] |
| 95 | + public void Can_build_Grpc_model() |
| 96 | + { |
| 97 | + using var context = Fixture.CreateContext(); |
| 98 | + |
| 99 | + var entityTypeMappings = context.Model.GetEntityTypes().Select(e => new EntityTypeMapping(e)).ToList(); |
| 100 | + EntityTypeMapping.AssertEqual(ExpectedMappings, entityTypeMappings); |
| 101 | + } |
| 102 | + |
| 103 | + [ConditionalFact] |
| 104 | + public void Can_query_Grpc_model() |
| 105 | + { |
| 106 | + using var context = Fixture.CreateContext(); |
| 107 | + |
| 108 | + var post = context.Set<Post>().Include(e => e.PostAuthor).Include(e => e.TagsInPostData).Single(); |
| 109 | + |
| 110 | + Assert.Equal("Arthur's post", post.Title); |
| 111 | + Assert.Equal(new DateTime(2021, 9, 3, 12, 10, 0, DateTimeKind.Utc), post.DateCreated.ToDateTime()); |
| 112 | + Assert.Equal(PostStatus.Published, post.PostStat); |
| 113 | + Assert.Equal("Arthur", post.PostAuthor.Name); |
| 114 | + Assert.Equal(new DateTime(1973, 9, 3, 12, 10, 0, DateTimeKind.Utc), post.PostAuthor.DateCreated.ToDateTime()); |
| 115 | + |
| 116 | + Assert.Equal(2, post.TagsInPostData.Count); |
| 117 | + Assert.Contains("Puppies", post.TagsInPostData.Select(e => e.Name).ToList()); |
| 118 | + Assert.Contains("Kittens", post.TagsInPostData.Select(e => e.Name).ToList()); |
| 119 | + Assert.Same(post, post.TagsInPostData.First().PostsInTagData.First()); |
| 120 | + Assert.Same(post, post.TagsInPostData.Skip(1).First().PostsInTagData.First()); |
| 121 | + } |
| 122 | + |
| 123 | + public class GrpcContext : PoolableDbContext |
| 124 | + { |
| 125 | + public GrpcContext(DbContextOptions options) |
| 126 | + : base(options) |
| 127 | + { |
| 128 | + } |
| 129 | + |
| 130 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 131 | + { |
| 132 | + var timeStampConverter = new ValueConverter<Timestamp, DateTime>( |
| 133 | + v => v.ToDateTime(), |
| 134 | + v => new DateTime(v.Ticks, DateTimeKind.Utc).ToTimestamp()); |
| 135 | + |
| 136 | + modelBuilder.Entity<Author>().Property(e => e.DateCreated).HasConversion(timeStampConverter); |
| 137 | + modelBuilder.Entity<Post>().Property(e => e.DateCreated).HasConversion(timeStampConverter); |
| 138 | + modelBuilder.Entity<Tag>(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public abstract class GrpcFixtureBase : SharedStoreFixtureBase<GrpcContext> |
| 143 | + { |
| 144 | + protected override string StoreName { get; } = "GrpcTest"; |
| 145 | + |
| 146 | + protected override void Seed(GrpcContext context) |
| 147 | + { |
| 148 | + var post = new Post |
| 149 | + { |
| 150 | + DateCreated = Timestamp.FromDateTime(new DateTime(2021, 9, 3, 12, 10, 0, DateTimeKind.Utc)), |
| 151 | + Title = "Arthur's post", |
| 152 | + PostAuthor = new Author |
| 153 | + { |
| 154 | + DateCreated = Timestamp.FromDateTime(new DateTime(1973, 9, 3, 12, 10, 0, DateTimeKind.Utc)), Name = "Arthur" |
| 155 | + }, |
| 156 | + PostStat = PostStatus.Published, |
| 157 | + TagsInPostData = { new Tag { Name = "Kittens" }, new Tag { Name = "Puppies" } } |
| 158 | + }; |
| 159 | + |
| 160 | + context.Add(post); |
| 161 | + |
| 162 | + context.SaveChanges(); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments