|
| 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; |
| 6 | +using Microsoft.Spark.E2ETest.Utils; |
| 7 | +using Microsoft.Spark.Extensions.Hyperspace.Index; |
| 8 | +using Microsoft.Spark.Sql; |
| 9 | +using Microsoft.Spark.UnitTest.TestUtils; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.Spark.Extensions.Hyperspace.E2ETest |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Test suite for Hyperspace index management APIs. |
| 16 | + /// </summary> |
| 17 | + [Collection(Constants.HyperspaceTestContainerName)] |
| 18 | + public class HyperspaceTests : IDisposable |
| 19 | + { |
| 20 | + private readonly SparkSession _spark; |
| 21 | + private readonly TemporaryDirectory _hyperspaceSystemDirectory; |
| 22 | + private readonly Hyperspace _hyperspace; |
| 23 | + |
| 24 | + // Fields needed for sample DataFrame. |
| 25 | + private readonly DataFrame _sampleDataFrame; |
| 26 | + private readonly string _sampleIndexName; |
| 27 | + private readonly IndexConfig _sampleIndexConfig; |
| 28 | + |
| 29 | + public HyperspaceTests(HyperspaceFixture fixture) |
| 30 | + { |
| 31 | + _spark = fixture.SparkFixture.Spark; |
| 32 | + _hyperspaceSystemDirectory = new TemporaryDirectory(); |
| 33 | + _spark.Conf().Set("spark.hyperspace.system.path", _hyperspaceSystemDirectory.Path); |
| 34 | + _hyperspace = new Hyperspace(_spark); |
| 35 | + |
| 36 | + _sampleDataFrame = _spark.Read() |
| 37 | + .Option("header", true) |
| 38 | + .Option("delimiter", ";") |
| 39 | + .Csv("Resources\\people.csv"); |
| 40 | + _sampleIndexName = "sample_dataframe"; |
| 41 | + _sampleIndexConfig = new IndexConfig(_sampleIndexName, new[] { "job" }, new[] { "name" }); |
| 42 | + _hyperspace.CreateIndex(_sampleDataFrame, _sampleIndexConfig); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Clean up the Hyperspace system directory in between tests. |
| 47 | + /// </summary> |
| 48 | + public void Dispose() |
| 49 | + { |
| 50 | + _hyperspaceSystemDirectory.Dispose(); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Test the method signatures for all Hyperspace APIs. |
| 55 | + /// </summary> |
| 56 | + [SkipIfSparkVersionIsLessThan(Versions.V2_4_0)] |
| 57 | + public void TestSignatures() |
| 58 | + { |
| 59 | + // Indexes API. |
| 60 | + Assert.IsType<DataFrame>(_hyperspace.Indexes()); |
| 61 | + |
| 62 | + // Delete and Restore APIs. |
| 63 | + _hyperspace.DeleteIndex(_sampleIndexName); |
| 64 | + _hyperspace.RestoreIndex(_sampleIndexName); |
| 65 | + |
| 66 | + // Refresh API. |
| 67 | + _hyperspace.RefreshIndex(_sampleIndexName); |
| 68 | + |
| 69 | + // Cancel API. |
| 70 | + Assert.Throws<Exception>(() => _hyperspace.Cancel(_sampleIndexName)); |
| 71 | + |
| 72 | + // Explain API. |
| 73 | + _hyperspace.Explain(_sampleDataFrame, true); |
| 74 | + _hyperspace.Explain(_sampleDataFrame, true, s => Console.WriteLine(s)); |
| 75 | + |
| 76 | + // Delete and Vacuum APIs. |
| 77 | + _hyperspace.DeleteIndex(_sampleIndexName); |
| 78 | + _hyperspace.VacuumIndex(_sampleIndexName); |
| 79 | + |
| 80 | + // Enable and disable Hyperspace. |
| 81 | + Assert.IsType<SparkSession>(_spark.EnableHyperspace()); |
| 82 | + Assert.IsType<SparkSession>(_spark.DisableHyperspace()); |
| 83 | + Assert.IsType<bool>(_spark.IsHyperspaceEnabled()); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Test E2E functionality of index CRUD APIs. |
| 88 | + /// </summary> |
| 89 | + [SkipIfSparkVersionIsLessThan(Versions.V2_4_0)] |
| 90 | + public void TestIndexCreateAndDelete() |
| 91 | + { |
| 92 | + // Should be one active index. |
| 93 | + DataFrame indexes = _hyperspace.Indexes(); |
| 94 | + Assert.Equal(1, indexes.Count()); |
| 95 | + Assert.Equal(_sampleIndexName, indexes.SelectExpr("name").First()[0]); |
| 96 | + Assert.Equal(States.Active, indexes.SelectExpr("state").First()[0]); |
| 97 | + |
| 98 | + // Delete the index then verify it has been deleted. |
| 99 | + _hyperspace.DeleteIndex(_sampleIndexName); |
| 100 | + indexes = _hyperspace.Indexes(); |
| 101 | + Assert.Equal(1, indexes.Count()); |
| 102 | + Assert.Equal(States.Deleted, indexes.SelectExpr("state").First()[0]); |
| 103 | + |
| 104 | + // Restore the index to active state and verify it is back. |
| 105 | + _hyperspace.RestoreIndex(_sampleIndexName); |
| 106 | + indexes = _hyperspace.Indexes(); |
| 107 | + Assert.Equal(1, indexes.Count()); |
| 108 | + Assert.Equal(States.Active, indexes.SelectExpr("state").First()[0]); |
| 109 | + |
| 110 | + // Delete and vacuum the index, then verify it is gone. |
| 111 | + _hyperspace.DeleteIndex(_sampleIndexName); |
| 112 | + _hyperspace.VacuumIndex(_sampleIndexName); |
| 113 | + Assert.Equal(0, _hyperspace.Indexes().Count()); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Test that the explain API generates the expected string. |
| 118 | + /// </summary> |
| 119 | + [SkipIfSparkVersionIsLessThan(Versions.V2_4_0)] |
| 120 | + public void TestExplainAPI() |
| 121 | + { |
| 122 | + // Run a query that hits the index. |
| 123 | + DataFrame queryDataFrame = _sampleDataFrame |
| 124 | + .Where("job == 'Developer'") |
| 125 | + .Select("name"); |
| 126 | + |
| 127 | + string explainString = string.Empty; |
| 128 | + _hyperspace.Explain(queryDataFrame, true, s => explainString = s); |
| 129 | + Assert.False(string.IsNullOrEmpty(explainString)); |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Index states used in testing. |
| 134 | + /// </summary> |
| 135 | + private static class States |
| 136 | + { |
| 137 | + public const string Active = "ACTIVE"; |
| 138 | + public const string Deleted = "DELETED"; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments