|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Xunit; |
| 9 | +using Xunit.Abstractions; |
| 10 | + |
| 11 | +namespace Microsoft.Data.SqlClient.ManualTesting.Tests.SQL.VectorTest |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Provides parameterized test data for backward compatibility tests that exchange |
| 15 | + /// float16 vector data as varchar(max) JSON strings. |
| 16 | + /// </summary> |
| 17 | + public static class Float16VarcharVectorTestData |
| 18 | + { |
| 19 | + // Values chosen to be exactly representable in IEEE-754 binary16 (float16), |
| 20 | + // so JSON round-trips through a vector(N, float16) column without precision loss. |
| 21 | + public static readonly float[] TestData = { 1.0f, 2.0f, 3.0f }; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Generates test cases for all 4 SqlParameter construction patterns x 2 value types (non-null + null). |
| 25 | + /// Each case yields: [int pattern, string jsonOrNull, float[] expectedData] |
| 26 | + /// where jsonOrNull is null when testing DBNull insertion. |
| 27 | + /// </summary> |
| 28 | + public static IEnumerable<object[]> GetVarcharVectorInsertTestData() |
| 29 | + { |
| 30 | + string json = JsonSerializer.Serialize(TestData); |
| 31 | + |
| 32 | + // Pattern 1-4 with non-null JSON value |
| 33 | + yield return new object[] { 1, json, TestData }; |
| 34 | + yield return new object[] { 2, json, TestData }; |
| 35 | + yield return new object[] { 3, json, TestData }; |
| 36 | + yield return new object[] { 4, json, TestData }; |
| 37 | + |
| 38 | + // Pattern 1-4 with null value |
| 39 | + yield return new object[] { 1, null, null }; |
| 40 | + yield return new object[] { 2, null, null }; |
| 41 | + yield return new object[] { 3, null, null }; |
| 42 | + yield return new object[] { 4, null, null }; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public sealed class Float16VectorTypeBackwardCompatibilityTests : VectorBackwardCompatTestBase |
| 47 | + { |
| 48 | + public Float16VectorTypeBackwardCompatibilityTests(ITestOutputHelper output) |
| 49 | + : base(output, columnDefinition: "vector(3, float16)", namePrefix: "VectorF16") |
| 50 | + { |
| 51 | + } |
| 52 | + |
| 53 | + protected override float[] GetPrepareTestValues(int i) => |
| 54 | + new float[] { i + 1, i + 2, i + 3 }; |
| 55 | + |
| 56 | + #region Insert Tests |
| 57 | + |
| 58 | + [ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSqlVectorFloat16Supported))] |
| 59 | + [MemberData(nameof(Float16VarcharVectorTestData.GetVarcharVectorInsertTestData), MemberType = typeof(Float16VarcharVectorTestData), DisableDiscoveryEnumeration = true)] |
| 60 | + public void TestVectorDataInsertionAsVarchar(int pattern, string jsonValue, float[] expectedData) |
| 61 | + => InsertAndValidateAsVarchar(pattern, jsonValue, expectedData); |
| 62 | + |
| 63 | + [ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSqlVectorFloat16Supported))] |
| 64 | + [MemberData(nameof(Float16VarcharVectorTestData.GetVarcharVectorInsertTestData), MemberType = typeof(Float16VarcharVectorTestData), DisableDiscoveryEnumeration = true)] |
| 65 | + public async Task TestVectorDataInsertionAsVarcharAsync(int pattern, string jsonValue, float[] expectedData) |
| 66 | + => await InsertAndValidateAsVarcharAsync(pattern, jsonValue, expectedData); |
| 67 | + |
| 68 | + #endregion |
| 69 | + |
| 70 | + #region Stored Procedure Tests |
| 71 | + |
| 72 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSqlVectorFloat16Supported))] |
| 73 | + public void TestStoredProcParamsForVectorAsVarchar() |
| 74 | + => StoredProcRoundTrip(Float16VarcharVectorTestData.TestData); |
| 75 | + |
| 76 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSqlVectorFloat16Supported))] |
| 77 | + public async Task TestStoredProcParamsForVectorAsVarcharAsync() |
| 78 | + => await StoredProcRoundTripAsync(Float16VarcharVectorTestData.TestData); |
| 79 | + |
| 80 | + #endregion |
| 81 | + |
| 82 | + #region Bulk Copy Tests |
| 83 | + |
| 84 | + [ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSqlVectorFloat16Supported))] |
| 85 | + [InlineData(1)] |
| 86 | + [InlineData(2)] |
| 87 | + public void TestSqlBulkCopyForVectorAsVarchar(int bulkCopySourceMode) |
| 88 | + => BulkCopyRoundTrip(bulkCopySourceMode, Float16VarcharVectorTestData.TestData); |
| 89 | + |
| 90 | + [ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSqlVectorFloat16Supported))] |
| 91 | + [InlineData(1)] |
| 92 | + [InlineData(2)] |
| 93 | + public async Task TestSqlBulkCopyForVectorAsVarcharAsync(int bulkCopySourceMode) |
| 94 | + => await BulkCopyRoundTripAsync(bulkCopySourceMode, Float16VarcharVectorTestData.TestData); |
| 95 | + |
| 96 | + #endregion |
| 97 | + |
| 98 | + #region Prepared Statement Tests |
| 99 | + |
| 100 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSqlVectorFloat16Supported))] |
| 101 | + public void TestInsertVectorsAsVarcharWithPrepare() |
| 102 | + => PreparedInsertRoundTrip(); |
| 103 | + |
| 104 | + #endregion |
| 105 | + } |
| 106 | +} |
0 commit comments