|  | 
| 2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. | 
| 3 | 3 | // See the LICENSE file in the project root for more information. | 
| 4 | 4 | 
 | 
|  | 5 | +using System; | 
|  | 6 | +using System.Text; | 
| 5 | 7 | using Xunit; | 
| 6 | 8 | 
 | 
| 7 | 9 | namespace Microsoft.Data.SqlClient.ManualTesting.Tests | 
| @@ -29,6 +31,64 @@ public static void CheckSupportUtf8ConnectionProperty() | 
| 29 | 31 |             } | 
| 30 | 32 |         } | 
| 31 | 33 | 
 | 
| 32 |  | -        // TODO: Write tests using UTF8 collations | 
|  | 34 | +        // skip creating database on Azure | 
|  | 35 | +        [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer), nameof(DataTestUtility.IsNotAzureSynapse))] | 
|  | 36 | +        public static void UTF8databaseTest() | 
|  | 37 | +        { | 
|  | 38 | +            const string letters = @"!\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f€\u0081‚ƒ„…†‡ˆ‰Š‹Œ\u008dŽ\u008f\u0090‘’“”•–—˜™š›œ\u009džŸ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"; | 
|  | 39 | +            string dbName = DataTestUtility.GetUniqueNameForSqlServer("UTF8databaseTest", false); | 
|  | 40 | +            string tblName = "Table1"; | 
|  | 41 | + | 
|  | 42 | +            SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString); | 
|  | 43 | +            builder.InitialCatalog = "master"; | 
|  | 44 | + | 
|  | 45 | +            using SqlConnection cn = new(builder.ConnectionString); | 
|  | 46 | +            cn.Open(); | 
|  | 47 | + | 
|  | 48 | +            try | 
|  | 49 | +            { | 
|  | 50 | +                PrepareDatabaseUTF8(cn, dbName, tblName, letters); | 
|  | 51 | + | 
|  | 52 | +                builder.InitialCatalog = dbName; | 
|  | 53 | +                using SqlConnection cnnTest = new(builder.ConnectionString); | 
|  | 54 | +                // creating a databse is a time consumer action and could be retried. | 
|  | 55 | +                SqlRetryLogicOption retryOption = new() { NumberOfTries = 3, DeltaTime = TimeSpan.FromMilliseconds(200) }; | 
|  | 56 | +                cnnTest.RetryLogicProvider = SqlConfigurableRetryFactory.CreateIncrementalRetryProvider(retryOption); | 
|  | 57 | +                cnnTest.Open(); | 
|  | 58 | + | 
|  | 59 | +                using SqlCommand cmd = cnnTest.CreateCommand(); | 
|  | 60 | +                cmd.CommandText = $"SELECT * FROM {tblName}"; | 
|  | 61 | + | 
|  | 62 | +                using SqlDataReader reader = cmd.ExecuteReader(); | 
|  | 63 | + | 
|  | 64 | +                Assert.True(reader.Read(), "The test table should have a row!"); | 
|  | 65 | +                object[] data = new object[1]; | 
|  | 66 | +                reader.GetSqlValues(data); | 
|  | 67 | +                Assert.Equal(letters, data[0].ToString()); | 
|  | 68 | +                reader.Close(); | 
|  | 69 | +                cnnTest.Close(); | 
|  | 70 | +            } | 
|  | 71 | +            finally | 
|  | 72 | +            { | 
|  | 73 | +                DataTestUtility.DropDatabase(cn, dbName); | 
|  | 74 | +            } | 
|  | 75 | +        } | 
|  | 76 | + | 
|  | 77 | +        private static void PrepareDatabaseUTF8(SqlConnection cnn, string dbName, string tblName, string letters) | 
|  | 78 | +        { | 
|  | 79 | +            StringBuilder sb = new(); | 
|  | 80 | + | 
|  | 81 | +            using SqlCommand cmd = cnn.CreateCommand(); | 
|  | 82 | + | 
|  | 83 | +            cmd.CommandText = $"CREATE DATABASE [{dbName}] COLLATE Latin1_General_100_CI_AS_SC_UTF8;"; | 
|  | 84 | +            cmd.ExecuteNonQuery(); | 
|  | 85 | + | 
|  | 86 | +            sb.AppendLine($"CREATE TABLE [{dbName}].dbo.[{tblName}] (col VARCHAR(7633) COLLATE Latin1_General_100_CI_AS_SC);"); | 
|  | 87 | +            sb.AppendLine($"INSERT INTO [{dbName}].dbo.[{tblName}] VALUES (@letters);"); | 
|  | 88 | + | 
|  | 89 | +            cmd.Parameters.Add(new SqlParameter("letters", letters)); | 
|  | 90 | +            cmd.CommandText = sb.ToString(); | 
|  | 91 | +            cmd.ExecuteNonQuery(); | 
|  | 92 | +        } | 
| 33 | 93 |     } | 
| 34 | 94 | } | 
0 commit comments