mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-18 14:41:21 +08:00
Made the code unit testable and added unit tests for the indexer plugin
This commit is contained in:
parent
69758b80a5
commit
ca916deda9
@ -7,9 +7,9 @@ namespace Wox.Plugin.Indexer.SearchHelper
|
||||
{
|
||||
public class WindowsSearchAPI
|
||||
{
|
||||
private OleDbConnection conn;
|
||||
private OleDbCommand command;
|
||||
private OleDbDataReader WDSResults;
|
||||
public OleDbConnection conn;
|
||||
public OleDbCommand command;
|
||||
public OleDbDataReader WDSResults;
|
||||
|
||||
public IEnumerable<SearchResult> ExecuteQuery(ISearchQueryHelper queryHelper, string keyword)
|
||||
{
|
||||
@ -43,7 +43,8 @@ namespace Wox.Plugin.Indexer.SearchHelper
|
||||
}
|
||||
}
|
||||
|
||||
public void setQueryHelper(ref ISearchQueryHelper queryHelper, string pattern)
|
||||
|
||||
public void ModifyQueryHelper(ref ISearchQueryHelper queryHelper, string pattern)
|
||||
{
|
||||
// convert file pattern if it is not '*'. Don't create restriction for '*' as it includes all files.
|
||||
if (pattern != "*")
|
||||
@ -63,7 +64,7 @@ namespace Wox.Plugin.Indexer.SearchHelper
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<SearchResult> Search(string keyword, string pattern = "*", int maxCount = 100)
|
||||
public void InitQueryHelper(out ISearchQueryHelper queryHelper, int maxCount)
|
||||
{
|
||||
// This uses the Microsoft.Search.Interop assembly
|
||||
CSearchManager manager = new CSearchManager();
|
||||
@ -72,7 +73,7 @@ namespace Wox.Plugin.Indexer.SearchHelper
|
||||
ISearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
|
||||
|
||||
// Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer
|
||||
ISearchQueryHelper queryHelper = catalogManager.GetQueryHelper();
|
||||
queryHelper = catalogManager.GetQueryHelper();
|
||||
|
||||
// Set the number of results we want. Don't set this property if all results are needed.
|
||||
queryHelper.QueryMaxResults = maxCount;
|
||||
@ -85,9 +86,13 @@ namespace Wox.Plugin.Indexer.SearchHelper
|
||||
|
||||
// Set sorting order
|
||||
queryHelper.QuerySorting = "System.DateModified DESC";
|
||||
}
|
||||
|
||||
setQueryHelper(ref queryHelper, pattern);
|
||||
|
||||
public IEnumerable<SearchResult> Search(string keyword, string pattern = "*", int maxCount = 100)
|
||||
{
|
||||
ISearchQueryHelper queryHelper;
|
||||
InitQueryHelper(out queryHelper, maxCount);
|
||||
ModifyQueryHelper(ref queryHelper, pattern);
|
||||
return ExecuteQuery(queryHelper, keyword);
|
||||
}
|
||||
}
|
||||
|
@ -9,9 +9,10 @@
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Wox.Plugin.Indexer</RootNamespace>
|
||||
<AssemblyName>Wox.Plugin.Indexer</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
|
145
src/modules/launcher/Wox.Test/Plugins/WindowsIndexerTest.cs
Normal file
145
src/modules/launcher/Wox.Test/Plugins/WindowsIndexerTest.cs
Normal file
@ -0,0 +1,145 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.OleDb;
|
||||
using Microsoft.Search.Interop;
|
||||
using Wox.Plugin.Indexer.SearchHelper;
|
||||
|
||||
namespace Wox.Test.Plugins
|
||||
{
|
||||
|
||||
[TestFixture]
|
||||
public class WindowsIndexerTest
|
||||
{
|
||||
private WindowsSearchAPI _api = new WindowsSearchAPI();
|
||||
|
||||
[Test]
|
||||
public void InitQueryHelper_ShouldInitialize_WhenFunctionIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
int maxCount = 10;
|
||||
ISearchQueryHelper queryHelper = null;
|
||||
|
||||
// Act
|
||||
_api.InitQueryHelper(out queryHelper, maxCount);
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(queryHelper);
|
||||
Assert.AreEqual(queryHelper.QueryMaxResults, maxCount);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternIsAsterisk()
|
||||
{
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "*";
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
_api.ModifyQueryHelper(ref queryHelper, pattern);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("LIKE"));
|
||||
Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternContainsAsterisk()
|
||||
{
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt*^&)";
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
_api.ModifyQueryHelper(ref queryHelper, pattern);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("LIKE"));
|
||||
Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternContainsPercent()
|
||||
{
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt%^&)";
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
_api.ModifyQueryHelper(ref queryHelper, pattern);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("LIKE"));
|
||||
Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternContainsUnderScore()
|
||||
{
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt_^&)";
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
_api.ModifyQueryHelper(ref queryHelper, pattern);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("LIKE"));
|
||||
Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternContainsQuestionMark()
|
||||
{
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt?^&)";
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
_api.ModifyQueryHelper(ref queryHelper, pattern);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("LIKE"));
|
||||
Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternDoesNotContainSplSymbols()
|
||||
{
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
String pattern = "tt^&)bc";
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
|
||||
// Act
|
||||
_api.ModifyQueryHelper(ref queryHelper, pattern);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("LIKE"));
|
||||
Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("Contains"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExecuteQuery_ShouldDisposeAllConnections_AfterFunctionCall()
|
||||
{
|
||||
// Arrange
|
||||
ISearchQueryHelper queryHelper;
|
||||
_api.InitQueryHelper(out queryHelper, 10);
|
||||
_api.ModifyQueryHelper(ref queryHelper, "*");
|
||||
string keyword = "test";
|
||||
|
||||
// Act
|
||||
_api.ExecuteQuery(queryHelper, keyword);
|
||||
|
||||
// Assert
|
||||
Assert.IsNull(_api.conn);
|
||||
Assert.IsNull(_api.command);
|
||||
Assert.IsNull(_api.WDSResults);
|
||||
}
|
||||
}
|
||||
}
|
@ -38,6 +38,7 @@
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\SolutionAssemblyInfo.cs">
|
||||
@ -45,10 +46,15 @@
|
||||
</Compile>
|
||||
<Compile Include="FuzzyMatcherTest.cs" />
|
||||
<Compile Include="Plugins\PluginInitTest.cs" />
|
||||
<Compile Include="Plugins\WindowsIndexerTest.cs" />
|
||||
<Compile Include="QueryBuilderTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Plugins\Wox.Plugin.Indexer\Wox.Plugin.Indexer.csproj">
|
||||
<Project>{63c3cea8-51fe-472e-b97c-b58f8b17dd51}</Project>
|
||||
<Name>Wox.Plugin.Indexer</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Wox.Core\Wox.Core.csproj">
|
||||
<Project>{B749F0DB-8E75-47DB-9E5E-265D16D0C0D2}</Project>
|
||||
<Name>Wox.Core</Name>
|
||||
@ -72,6 +78,9 @@
|
||||
<PackageReference Include="NUnit3TestAdapter">
|
||||
<Version>3.15.1</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="tlbimp-Microsoft.Search.Interop">
|
||||
<Version>1.0.0</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
Loading…
Reference in New Issue
Block a user