|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Xunit; |
| 4 | +using System.Linq; |
| 5 | +using System.Security.Cryptography; |
| 6 | + |
| 7 | +namespace LiteDB.Tests.Issues |
| 8 | +{ |
| 9 | + |
| 10 | + public class Issue1865_Tests |
| 11 | + { |
| 12 | + public class Project : BaseEntity |
| 13 | + { |
| 14 | + public List<BaseEntity> Points { get; set; } = new List<BaseEntity>(); |
| 15 | + } |
| 16 | + |
| 17 | + public class Point : BaseEntity |
| 18 | + { |
| 19 | + public BaseEntity Project { get; set; } |
| 20 | + public BaseEntity Parent { get; set; } |
| 21 | + public DateTime Start { get; internal set; } |
| 22 | + public DateTime End { get; internal set; } |
| 23 | + } |
| 24 | + |
| 25 | + public class BaseEntity |
| 26 | + { |
| 27 | + public ObjectId Id { get; set; } = ObjectId.NewObjectId(); |
| 28 | + public string Name { get; set; } |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void Incluced_document_types_should_be_reald() |
| 33 | + { |
| 34 | + BsonMapper.Global.Entity<Point>().DbRef(p => p.Project, "activity"); |
| 35 | + BsonMapper.Global.Entity<Point>().DbRef(p => p.Parent, "activity"); |
| 36 | + BsonMapper.Global.Entity<Project>().DbRef(p => p.Points, "activity"); |
| 37 | + |
| 38 | + //BsonMapper.Global.ResolveCollectionName = (s) => "activity"; |
| 39 | + |
| 40 | + using var _database = new LiteDatabase(":memory:"); |
| 41 | + var projectsCol = _database.GetCollection<Project>("activity"); |
| 42 | + var pointsCol = _database.GetCollection<Point>("activity"); |
| 43 | + |
| 44 | + var project = new Project() { Name = "Project" }; |
| 45 | + var point1 = new Point { Parent = project, Project = project, Name = "Point 1", Start = DateTime.Now, End = DateTime.Now.AddDays(2) }; |
| 46 | + var point2 = new Point { Parent = point1, Project = project, Name = "Point 2", Start = DateTime.Now, End = DateTime.Now.AddDays(2) }; |
| 47 | + |
| 48 | + project.Points.Add(point1); |
| 49 | + project.Points.Add(point2); |
| 50 | + |
| 51 | + pointsCol.Insert(point1); |
| 52 | + pointsCol.Insert(point2); |
| 53 | + projectsCol.Insert(project); |
| 54 | + |
| 55 | + |
| 56 | + var p1 = pointsCol |
| 57 | + .FindById(point1.Id); |
| 58 | + Assert.Equal(typeof(Project), p1.Parent.GetType()); |
| 59 | + Assert.Equal(typeof(Project), p1.Project.GetType()); |
| 60 | + |
| 61 | + var p2 = pointsCol |
| 62 | + .FindById(point2.Id); |
| 63 | + Assert.Equal(typeof(Point), p2.Parent.GetType()); |
| 64 | + Assert.Equal(typeof(Project), p2.Project.GetType()); |
| 65 | + |
| 66 | + var prj = projectsCol |
| 67 | + .FindById(project.Id); |
| 68 | + Assert.Equal(typeof(Point), prj.Points[0].GetType()); |
| 69 | + |
| 70 | + p1 = pointsCol |
| 71 | + .Include(p => p.Parent).Include(p => p.Project) |
| 72 | + .FindById(point1.Id); |
| 73 | + Assert.Equal(typeof(Project), p1.Parent.GetType()); |
| 74 | + Assert.Equal(typeof(Project), p1.Project.GetType()); |
| 75 | + |
| 76 | + p2 = pointsCol |
| 77 | + .Include(p => p.Parent).Include(p => p.Project) |
| 78 | + .FindById(point2.Id); |
| 79 | + Assert.Equal(typeof(Point), p2.Parent.GetType()); |
| 80 | + Assert.Equal(typeof(Project), p2.Project.GetType()); |
| 81 | + |
| 82 | + prj = projectsCol |
| 83 | + .Include(p => p.Points) |
| 84 | + .FindById(project.Id); |
| 85 | + Assert.Equal(typeof(Point), prj.Points[0].GetType()); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments