|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Data; |
| 7 | +using Azure.DataApiBuilder.Auth; |
| 8 | +using Azure.DataApiBuilder.Config; |
| 9 | +using Azure.DataApiBuilder.Config.DatabasePrimitives; |
| 10 | +using Azure.DataApiBuilder.Config.ObjectModel; |
| 11 | +using Azure.DataApiBuilder.Core.Models; |
| 12 | +using Azure.DataApiBuilder.Core.Resolvers; |
| 13 | +using Azure.DataApiBuilder.Core.Services; |
| 14 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 15 | +using Moq; |
| 16 | + |
| 17 | +namespace Azure.DataApiBuilder.Service.Tests.UnitTests |
| 18 | +{ |
| 19 | + [TestClass] |
| 20 | + public class SqlExecuteStructureUnitTests |
| 21 | + { |
| 22 | + [TestMethod] |
| 23 | + public void ConfigDefaultDateTimeParameterIsResolvedAsDateTime() |
| 24 | + { |
| 25 | + const string entityName = "GetRecordsByDate"; |
| 26 | + const string parameterName = "YearEndDate"; |
| 27 | + const string envVarName = "year-end"; |
| 28 | + DateTime expectedDate = new(year: 2024, month: 6, day: 30, hour: 0, minute: 0, second: 0, kind: DateTimeKind.Utc); |
| 29 | + |
| 30 | + Environment.SetEnvironmentVariable(envVarName, "2024-06-30"); |
| 31 | + try |
| 32 | + { |
| 33 | + Assert.IsTrue(RuntimeConfigLoader.TryParseConfig( |
| 34 | + GetStoredProcedureConfigWithEnvDefault(), |
| 35 | + out RuntimeConfig runtimeConfig, |
| 36 | + replacementSettings: new DeserializationVariableReplacementSettings( |
| 37 | + azureKeyVaultOptions: null, |
| 38 | + doReplaceEnvVar: true, |
| 39 | + doReplaceAkvVar: false))); |
| 40 | + |
| 41 | + StoredProcedureDefinition storedProcedureDefinition = new(); |
| 42 | + storedProcedureDefinition.Parameters.Add( |
| 43 | + parameterName, |
| 44 | + new ParameterDefinition |
| 45 | + { |
| 46 | + SystemType = typeof(DateTime), |
| 47 | + DbType = DbType.DateTime, |
| 48 | + HasConfigDefault = true, |
| 49 | + ConfigDefaultValue = runtimeConfig.Entities[entityName].Source.Parameters[0].Default |
| 50 | + }); |
| 51 | + |
| 52 | + DatabaseStoredProcedure storedProcedure = new(schemaName: "dbo", tableName: "get_records_by_date") |
| 53 | + { |
| 54 | + SourceType = EntitySourceType.StoredProcedure, |
| 55 | + StoredProcedureDefinition = storedProcedureDefinition |
| 56 | + }; |
| 57 | + |
| 58 | + Mock<ISqlMetadataProvider> metadataProvider = new(); |
| 59 | + metadataProvider.SetupGet(x => x.EntityToDatabaseObject).Returns(new Dictionary<string, DatabaseObject> |
| 60 | + { |
| 61 | + { entityName, storedProcedure } |
| 62 | + }); |
| 63 | + metadataProvider.Setup(x => x.GetStoredProcedureDefinition(entityName)).Returns(storedProcedureDefinition); |
| 64 | + metadataProvider.Setup(x => x.GetSourceDefinition(entityName)).Returns(storedProcedureDefinition); |
| 65 | + |
| 66 | + SqlExecuteStructure executeStructure = new( |
| 67 | + entityName, |
| 68 | + metadataProvider.Object, |
| 69 | + Mock.Of<IAuthorizationResolver>(), |
| 70 | + null, |
| 71 | + new Dictionary<string, object?>()); |
| 72 | + |
| 73 | + string dbConnectionParameterName = (string)executeStructure.ProcedureParameters[parameterName]; |
| 74 | + Assert.AreEqual(expectedDate, executeStructure.Parameters[dbConnectionParameterName].Value); |
| 75 | + } |
| 76 | + finally |
| 77 | + { |
| 78 | + Environment.SetEnvironmentVariable(envVarName, null); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static string GetStoredProcedureConfigWithEnvDefault() |
| 83 | + { |
| 84 | + return @" |
| 85 | + { |
| 86 | + ""data-source"": { |
| 87 | + ""database-type"": ""mssql"", |
| 88 | + ""connection-string"": ""Server=tcp:127.0.0.1,1433;Persist Security Info=False;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=False;Connection Timeout=5;"" |
| 89 | + }, |
| 90 | + ""entities"": { |
| 91 | + ""GetRecordsByDate"": { |
| 92 | + ""source"": { |
| 93 | + ""object"": ""get_records_by_date"", |
| 94 | + ""type"": ""stored-procedure"", |
| 95 | + ""parameters"": { |
| 96 | + ""YearEndDate"": ""@env('year-end')"" |
| 97 | + } |
| 98 | + }, |
| 99 | + ""permissions"": [ |
| 100 | + { |
| 101 | + ""role"": ""anonymous"", |
| 102 | + ""actions"": [ |
| 103 | + { |
| 104 | + ""action"": ""execute"" |
| 105 | + } |
| 106 | + ] |
| 107 | + } |
| 108 | + ] |
| 109 | + } |
| 110 | + } |
| 111 | + }"; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments