|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +#if NETFX_CORE |
| 5 | +using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; |
| 6 | +using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; |
| 7 | +using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; |
| 8 | +using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; |
| 9 | +#else |
| 10 | +using NUnit.Framework; |
| 11 | +#endif |
| 12 | + |
| 13 | +namespace SQLite.Tests |
| 14 | +{ |
| 15 | + [TestFixture] |
| 16 | + public class NestedObjectPropertyMappingTest |
| 17 | + { |
| 18 | + SQLiteConnection db; |
| 19 | + |
| 20 | + int fooId = 1; |
| 21 | + int fooReferenceId = 3; |
| 22 | + string fooName = "Foo Table"; |
| 23 | + string fooDescription = "The main table."; |
| 24 | + string fooReferenceName = "Referenced table."; |
| 25 | + string fooReferenceDescription = "A table referenced by the main table"; |
| 26 | + |
| 27 | + class FooTable |
| 28 | + { |
| 29 | + [PrimaryKey] |
| 30 | + public int Id { get; set; } |
| 31 | + |
| 32 | + public string FooName { get; set; } |
| 33 | + |
| 34 | + public string FooDescription { get; set; } |
| 35 | + |
| 36 | + public int FooTableReferenceId { get; set; } |
| 37 | + } |
| 38 | + |
| 39 | + class FooTableReference |
| 40 | + { |
| 41 | + [PrimaryKey] |
| 42 | + public int Id { get; set; } |
| 43 | + |
| 44 | + public string ReferenceName { get; set; } |
| 45 | + |
| 46 | + public string ReferenceDescription { get; set; } |
| 47 | + } |
| 48 | + |
| 49 | + class FooDTO |
| 50 | + { |
| 51 | + public int Id { get; set; } |
| 52 | + |
| 53 | + public string Name { get; set; } |
| 54 | + |
| 55 | + public string Description { get; set; } |
| 56 | + |
| 57 | + public FooNestedObject NestedObject { get; set; } |
| 58 | + } |
| 59 | + |
| 60 | + class FooNestedObject |
| 61 | + { |
| 62 | + public int Id { get; set; } |
| 63 | + |
| 64 | + public string NestedName { get; set; } |
| 65 | + |
| 66 | + public string NestedDescription { get; set; } |
| 67 | + |
| 68 | + public FooNestedObject NestedChild { get; set; } |
| 69 | + } |
| 70 | + |
| 71 | + public NestedObjectPropertyMappingTest() |
| 72 | + { |
| 73 | + SetupDb (); |
| 74 | + } |
| 75 | + |
| 76 | + void SetupDb() |
| 77 | + { |
| 78 | + db = new TestDb (); |
| 79 | + |
| 80 | + db.CreateTable<FooTable> (); |
| 81 | + db.CreateTable<FooTableReference> (); |
| 82 | + |
| 83 | + db.Insert (new FooTable { |
| 84 | + Id = fooId, |
| 85 | + FooName = fooName, |
| 86 | + FooDescription = fooDescription, |
| 87 | + FooTableReferenceId = fooReferenceId |
| 88 | + }); |
| 89 | + db.Insert (new FooTableReference { |
| 90 | + Id = fooReferenceId, |
| 91 | + ReferenceName = fooReferenceName, |
| 92 | + ReferenceDescription = fooReferenceDescription, |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + [Test] |
| 97 | + public void SelectNestedObject () |
| 98 | + { |
| 99 | + string query = |
| 100 | + $" select" + |
| 101 | + $" fooTable.{nameof (FooTable.Id)} as '{nameof (FooDTO.Id)}'," + |
| 102 | + $" fooTable.{nameof (FooTable.FooName)} as '{nameof (FooDTO.Name)}'," + |
| 103 | + $" fooTable.{nameof (FooTable.FooDescription)} as '{nameof (FooDTO.Description)}'," + |
| 104 | + $" fooTableReference.{nameof (FooTableReference.Id)} as '{nameof (FooDTO.NestedObject)}.{nameof (FooNestedObject.Id)}'," + |
| 105 | + $" fooTableReference.{nameof (FooTableReference.ReferenceName)} as '{nameof (FooDTO.NestedObject)}.{nameof (FooNestedObject.NestedName)}'," + |
| 106 | + $" fooTableReference.{nameof (FooTableReference.ReferenceDescription)} as '{nameof (FooDTO.NestedObject)}.{nameof (FooNestedObject.NestedDescription)}'" + |
| 107 | + $" from {nameof (FooTable)} as fooTable" + |
| 108 | + $" join {nameof (FooTableReference)} fooTableReference on fooTable.{nameof (FooTable.FooTableReferenceId)} = fooTableReference.{nameof (FooTableReference.Id)}" + |
| 109 | + $" where fooTable.{nameof (FooTable.Id)} = {fooId}"; |
| 110 | + var dtoArray = db.Query<FooDTO> (query); |
| 111 | + var dto = dtoArray[0]; |
| 112 | + |
| 113 | + Assert.AreEqual (dto.Id, fooId); |
| 114 | + Assert.AreEqual (dto.Name, fooName); |
| 115 | + Assert.AreEqual (dto.Description, fooDescription); |
| 116 | + Assert.IsNotNull (dto.NestedObject); |
| 117 | + Assert.AreEqual (dto.NestedObject.Id, fooReferenceId); |
| 118 | + Assert.AreEqual (dto.NestedObject.NestedName, fooReferenceName); |
| 119 | + Assert.AreEqual (dto.NestedObject.NestedDescription, fooReferenceDescription); |
| 120 | + } |
| 121 | + |
| 122 | + [Test] |
| 123 | + public void SelectNestedObjectChildObjectProperty () |
| 124 | + { |
| 125 | + string query = |
| 126 | + $" select" + |
| 127 | + $" fooTable.{nameof (FooTable.Id)} as '{nameof (FooDTO.Id)}'," + |
| 128 | + $" fooTable.{nameof (FooTable.FooName)} as '{nameof (FooDTO.Name)}'," + |
| 129 | + $" fooTable.{nameof (FooTable.FooDescription)} as '{nameof (FooDTO.Description)}'," + |
| 130 | + $" fooTableReference.{nameof (FooTableReference.Id)} as '{nameof (FooDTO.NestedObject)}.{nameof (FooNestedObject.Id)}'," + |
| 131 | + $" fooTableReference.{nameof (FooTableReference.ReferenceName)} as '{nameof (FooDTO.NestedObject)}.{nameof (FooNestedObject.NestedName)}'," + |
| 132 | + $" fooTableReference.{nameof (FooTableReference.ReferenceDescription)} as '{nameof (FooDTO.NestedObject)}.{nameof (FooNestedObject.NestedChild)}.{nameof (FooNestedObject.NestedDescription)}'" + |
| 133 | + $" from {nameof (FooTable)} as fooTable" + |
| 134 | + $" join {nameof (FooTableReference)} fooTableReference on fooTable.{nameof (FooTable.FooTableReferenceId)} = fooTableReference.{nameof (FooTableReference.Id)}" + |
| 135 | + $" where fooTable.{nameof (FooTable.Id)} = {fooId}"; |
| 136 | + var dtoArray = db.Query<FooDTO> (query); |
| 137 | + var dto = dtoArray[0]; |
| 138 | + |
| 139 | + Assert.AreEqual (dto.Id, fooId); |
| 140 | + Assert.AreEqual (dto.Name, fooName); |
| 141 | + Assert.AreEqual (dto.Description, fooDescription); |
| 142 | + Assert.IsNotNull (dto.NestedObject); |
| 143 | + Assert.AreEqual (dto.NestedObject.Id, fooReferenceId); |
| 144 | + Assert.AreEqual (dto.NestedObject.NestedName, fooReferenceName); |
| 145 | + |
| 146 | + //Does not map to a nested object of a nested object - only goes one level deep. |
| 147 | + Assert.IsNull (dto.NestedObject.NestedChild); |
| 148 | + } |
| 149 | + |
| 150 | + [Test] |
| 151 | + public void SelectNonExistentNestedObjectProperty () |
| 152 | + { |
| 153 | + string query = |
| 154 | + $" select" + |
| 155 | + $" fooTable.{nameof (FooTable.Id)} as '{nameof (FooDTO.Id)}'," + |
| 156 | + $" fooTable.{nameof (FooTable.FooName)} as '{nameof (FooDTO.Name)}'," + |
| 157 | + $" fooTable.{nameof (FooTable.FooDescription)} as '{nameof (FooDTO.Description)}'," + |
| 158 | + $" fooTableReference.{nameof (FooTableReference.Id)} as '{nameof (FooDTO.NestedObject)}.{nameof (FooNestedObject.Id)}'," + |
| 159 | + $" fooTableReference.{nameof (FooTableReference.ReferenceName)} as '{nameof (FooDTO.NestedObject)}.{nameof (FooNestedObject.NestedName)}'," + |
| 160 | + $" fooTableReference.{nameof (FooTableReference.ReferenceDescription)} as '{nameof (FooDTO.NestedObject)}.Foo'" + |
| 161 | + $" from {nameof (FooTable)} as fooTable" + |
| 162 | + $" join {nameof (FooTableReference)} fooTableReference on fooTable.{nameof (FooTable.FooTableReferenceId)} = fooTableReference.{nameof (FooTableReference.Id)}" + |
| 163 | + $" where fooTable.{nameof (FooTable.Id)} = {fooId}"; |
| 164 | + var dtoArray = db.Query<FooDTO> (query); |
| 165 | + var dto = dtoArray[0]; |
| 166 | + |
| 167 | + Assert.AreEqual (dto.Id, fooId); |
| 168 | + Assert.AreEqual (dto.Name, fooName); |
| 169 | + Assert.AreEqual (dto.Description, fooDescription); |
| 170 | + Assert.IsNotNull (dto.NestedObject); |
| 171 | + Assert.AreEqual (dto.NestedObject.Id, fooReferenceId); |
| 172 | + Assert.AreEqual (dto.NestedObject.NestedName, fooReferenceName); |
| 173 | + Assert.IsNull (dto.NestedObject.NestedDescription); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
0 commit comments