|
| 1 | +/* |
| 2 | + * The contents of this file are subject to the Initial |
| 3 | + * Developer's Public License Version 1.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the |
| 5 | + * License. You may obtain a copy of the License at |
| 6 | + * https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt. |
| 7 | + * |
| 8 | + * Software distributed under the License is distributed on |
| 9 | + * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either |
| 10 | + * express or implied. See the License for the specific |
| 11 | + * language governing rights and limitations under the License. |
| 12 | + * |
| 13 | + * All Rights Reserved. |
| 14 | + */ |
| 15 | + |
| 16 | +//$Authors = Niek Schoemaker |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.Linq; |
| 21 | +using System.Reflection; |
| 22 | +using FirebirdSql.Data.FirebirdClient; |
| 23 | +using FirebirdSql.EntityFrameworkCore.Firebird.Design.Internal; |
| 24 | +using FirebirdSql.EntityFrameworkCore.Firebird.Storage.Internal; |
| 25 | +using Microsoft.EntityFrameworkCore.Design; |
| 26 | +using Microsoft.EntityFrameworkCore.Design.Internal; |
| 27 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 28 | +using Microsoft.EntityFrameworkCore.Metadata; |
| 29 | +using Microsoft.EntityFrameworkCore.Storage; |
| 30 | +using Microsoft.Extensions.DependencyInjection; |
| 31 | +using NUnit.Framework; |
| 32 | + |
| 33 | +namespace FirebirdSql.EntityFrameworkCore.Firebird.Tests.Design; |
| 34 | + |
| 35 | +#pragma warning disable EF1001 |
| 36 | +public class FbCSharpRuntimeAnnotationCodeGeneratorTests |
| 37 | +{ |
| 38 | + ServiceProvider _serviceProvider; |
| 39 | + IRelationalTypeMappingSource _typeMappingSource; |
| 40 | + ICSharpRuntimeAnnotationCodeGenerator _generator; |
| 41 | + |
| 42 | + [OneTimeSetUp] |
| 43 | + public void OneTimeSetUp() |
| 44 | + { |
| 45 | + var services = new ServiceCollection().AddEntityFrameworkDesignTimeServices(); |
| 46 | + new FbDesignTimeServices().ConfigureDesignTimeServices(services); |
| 47 | + _serviceProvider = services.BuildServiceProvider(); |
| 48 | + _typeMappingSource = _serviceProvider.GetRequiredService<IRelationalTypeMappingSource>(); |
| 49 | + _generator = _serviceProvider.GetRequiredService<ICSharpRuntimeAnnotationCodeGenerator>(); |
| 50 | + } |
| 51 | + |
| 52 | + [OneTimeTearDown] |
| 53 | + public void OneTimeTearDown() |
| 54 | + { |
| 55 | + _serviceProvider.Dispose(); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public void DesignTimeServicesUseFirebirdGenerator() |
| 60 | + { |
| 61 | + Assert.IsInstanceOf<FbCSharpRuntimeAnnotationCodeGenerator>(_generator); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void AllTypeMappingsHaveDefaultUsableByCompiledModel() |
| 66 | + { |
| 67 | + var mappingTypes = typeof(FbTypeMappingSource).Assembly.GetTypes() |
| 68 | + .Where(t => typeof(RelationalTypeMapping).IsAssignableFrom(t) && !t.IsAbstract) |
| 69 | + .ToList(); |
| 70 | + Assert.IsNotEmpty(mappingTypes); |
| 71 | + |
| 72 | + foreach (var mappingType in mappingTypes) |
| 73 | + { |
| 74 | + // Mirrors the requirements of CSharpRuntimeAnnotationCodeGenerator.CreateDefaultTypeMapping. |
| 75 | + var defaultProperty = mappingType.GetProperty("Default"); |
| 76 | + Assert.IsNotNull(defaultProperty, mappingType.Name); |
| 77 | + Assert.IsTrue(defaultProperty.GetMethod.IsStatic && defaultProperty.GetMethod.IsPublic, mappingType.Name); |
| 78 | + Assert.IsTrue(mappingType.IsAssignableFrom(defaultProperty.PropertyType), mappingType.Name); |
| 79 | + Assert.IsInstanceOf(mappingType, defaultProperty.GetValue(null), mappingType.Name); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + [TestCase(typeof(DateTime), "TIMESTAMP", null)] |
| 84 | + [TestCase(typeof(DateTime), "DATE", FbDbType.Date)] |
| 85 | + [TestCase(typeof(TimeSpan), "TIME", null)] |
| 86 | + [TestCase(typeof(string), "VARCHAR(50)", FbDbType.VarChar)] |
| 87 | + [TestCase(typeof(string), "CHAR(10)", FbDbType.Char)] |
| 88 | + [TestCase(typeof(string), "BLOB SUB_TYPE TEXT", null)] |
| 89 | + public void GeneratesFbDbTypeOnlyWhenItDiffersFromDefault(Type clrType, string storeType, FbDbType? expectedFbDbType) |
| 90 | + { |
| 91 | + var mapping = _typeMappingSource.FindMapping(clrType, storeType); |
| 92 | + |
| 93 | + var code = Generate(mapping); |
| 94 | + |
| 95 | + if (expectedFbDbType == null) |
| 96 | + { |
| 97 | + StringAssert.DoesNotContain("fbDbType", code); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + StringAssert.Contains($"property.TypeMapping = (({mapping.GetType().Name})property.TypeMapping).Clone(fbDbType: FbDbType.{expectedFbDbType})", code); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + [TestCase(typeof(DateTime), "DATE")] |
| 106 | + [TestCase(typeof(DateTime), "TIMESTAMP")] |
| 107 | + [TestCase(typeof(string), "VARCHAR(50)")] |
| 108 | + [TestCase(typeof(string), "CHAR(10)")] |
| 109 | + [TestCase(typeof(string), "BLOB SUB_TYPE TEXT")] |
| 110 | + [TestCase(typeof(TimeSpan), "TIME")] |
| 111 | + public void RecreatedMappingBehavesLikeOriginal(Type clrType, string storeType) |
| 112 | + { |
| 113 | + var mapping = _typeMappingSource.FindMapping(clrType, storeType); |
| 114 | + var fbDbType = (FbDbType)mapping.GetType().GetProperty("FbDbType").GetValue(mapping); |
| 115 | + |
| 116 | + // What the generated code does: Default.Clone(mappingInfo: ...), followed by Clone(fbDbType: ...) when needed. |
| 117 | + var defaultMapping = (RelationalTypeMapping)mapping.GetType().GetProperty("Default", BindingFlags.Public | BindingFlags.Static).GetValue(null); |
| 118 | + var recreated = defaultMapping.Clone(new RelationalTypeMappingInfo(storeTypeName: mapping.StoreType, size: mapping.Size, fixedLength: mapping.IsFixedLength, dbType: mapping.DbType)); |
| 119 | + recreated = (RelationalTypeMapping)recreated.GetType().GetMethod("Clone", [typeof(FbDbType)]).Invoke(recreated, [fbDbType]); |
| 120 | + |
| 121 | + var value = clrType == typeof(DateTime) ? new DateTime(2024, 5, 6, 7, 8, 9) : clrType == typeof(TimeSpan) ? new TimeSpan(7, 8, 9) : (object)"x"; |
| 122 | + using var command = new FbCommand(); |
| 123 | + var original = (FbParameter)mapping.CreateParameter(command, "p", value); |
| 124 | + var copy = (FbParameter)recreated.CreateParameter(command, "p", value); |
| 125 | + |
| 126 | + Assert.AreEqual(mapping.StoreType, recreated.StoreType); |
| 127 | + Assert.AreEqual(original.FbDbType, copy.FbDbType); |
| 128 | + // FbTimeSpanTypeMapping cannot generate literals at all: its format string leaves the '.' unescaped. |
| 129 | + if (clrType != typeof(TimeSpan)) |
| 130 | + { |
| 131 | + Assert.AreEqual(mapping.GenerateSqlLiteral(value), recreated.GenerateSqlLiteral(value)); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + string Generate(RelationalTypeMapping mapping) |
| 136 | + { |
| 137 | + var parameters = new CSharpRuntimeAnnotationCodeGeneratorParameters( |
| 138 | + "property", |
| 139 | + "TestEntityType", |
| 140 | + "TestNamespace", |
| 141 | + new IndentedStringBuilder(), |
| 142 | + new IndentedStringBuilder(), |
| 143 | + new HashSet<string>(), |
| 144 | + new Dictionary<string, object>(), |
| 145 | + new Dictionary<object, string>(), |
| 146 | + new Dictionary<ITypeBase, string>(), |
| 147 | + nullable: false, |
| 148 | + nativeAot: true); |
| 149 | + _generator.Create(mapping, parameters); |
| 150 | + return parameters.MainBuilder.ToString(); |
| 151 | + } |
| 152 | +} |
| 153 | +#pragma warning restore EF1001 |
0 commit comments