Skip to content

Commit

Permalink
Lucene unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
reddyashish committed Aug 1, 2023
1 parent b4c05fb commit 206add1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
47 changes: 27 additions & 20 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
using DynamoServices;
using Greg;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ProtoCore;
Expand Down Expand Up @@ -928,8 +927,16 @@ protected DynamoModel(IStartConfiguration config)

CustomNodeManager = new CustomNodeManager(NodeFactory, MigrationManager, LibraryServices);

LuceneSearchUtility = new LuceneSearchUtility(this);
LuceneSearchUtility.InitializeLuceneConfig(LuceneConfig.NodesIndexingDirectory);
if (IsTestMode)
{
LuceneSearchUtility = new LuceneSearchUtility(this);
LuceneSearchUtility.InitializeLuceneConfig(string.Empty, LuceneSearchUtility.LuceneStorage.RAM);
}
else
{
LuceneSearchUtility = new LuceneSearchUtility(this);
LuceneSearchUtility.InitializeLuceneConfig(LuceneConfig.NodesIndexingDirectory);
}

InitializeCustomNodeManager();

Expand Down Expand Up @@ -1518,19 +1525,19 @@ private void InitializeIncludedNodes()
//WhenHomeWorkspaceIsFocusedInputAndOutputNodesAreMissingFromSearch
//WhenStartingDynamoInputAndOutputNodesAreNolongerMissingFromSearch
// New index process from Lucene, adding missing nodes: Code Block, Input and Output
//var ele = AddNodeTypeToSearch(outputData);
//if (ele != null)
//{
// AddNodeTypeToSearchIndex(ele, iDoc);
//}

//ele = AddNodeTypeToSearch(symbolData);
//if (ele != null)
//{
// AddNodeTypeToSearchIndex(ele, iDoc);
//}

var ele = AddNodeTypeToSearch(cbnData);
var ele = AddNodeTypeToSearch(outputData);
if (ele != null)
{
AddNodeTypeToSearchIndex(ele, iDoc);
}

ele = AddNodeTypeToSearch(symbolData);
if (ele != null)
{
AddNodeTypeToSearchIndex(ele, iDoc);
}

ele = AddNodeTypeToSearch(cbnData);
if (ele != null)
{
AddNodeTypeToSearchIndex(ele, iDoc);
Expand Down Expand Up @@ -1569,8 +1576,8 @@ private void InitializeNodeLibrary()

// Import Zero Touch libs
var functionGroups = LibraryServices.GetAllFunctionGroups();
if (!IsTestMode)
AddZeroTouchNodesToSearch(functionGroups);

AddZeroTouchNodesToSearch(functionGroups);
#if DEBUG_LIBRARY
DumpLibrarySnapshot(functionGroups);
#endif
Expand Down Expand Up @@ -3248,9 +3255,9 @@ private NodeModelSearchElement AddNodeTypeToSearch(TypeLoadData typeLoadData)
/// </summary>
/// <param name="node">node info that will be indexed</param>
/// <param name="doc">Lucene document in which the node info will be indexed</param>
private void AddNodeTypeToSearchIndex(NodeSearchElement node, Document doc)
internal void AddNodeTypeToSearchIndex(NodeSearchElement node, Document doc)
{
if (IsTestMode) return;
//if (IsTestMode) return;
if (LuceneSearchUtility.addedFields == null) return;

LuceneSearchUtility.SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.FullCategoryName), node.FullCategoryName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Dynamo.Graph;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.CustomNodes;
using Dynamo.Interfaces;
using Dynamo.Models;

namespace Dynamo.Search.SearchElements
{
Expand Down
16 changes: 10 additions & 6 deletions src/DynamoCoreWpf/ViewModels/Search/SearchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,13 @@ internal SearchViewModel(NodeSearchModel model)
InitializeCore();
}

internal SearchViewModel(NodeSearchModel model, DynamoViewModel dynamoViewModel)
{
Model = model;
InitializeCore();
LuceneSearchUtility = dynamoViewModel.Model.LuceneSearchUtility;
}

/// <summary>
/// Dispose function
/// </summary>
Expand Down Expand Up @@ -875,7 +882,7 @@ public void SearchAndUpdateResults(string query)
return;

//Passing the second parameter as true will search using Lucene.NET
var foundNodes = DynamoModel.IsTestMode? Search(query) : Search(query, true);
var foundNodes = Search(query, true);
searchResults = new List<NodeSearchElementViewModel>(foundNodes);

FilteredResults = searchResults;
Expand Down Expand Up @@ -937,7 +944,7 @@ internal IEnumerable<NodeSearchElementViewModel> Search(string search, IEnumerab
/// <param name="useLucene"> Temporary flag that will be used for searching using Lucene.NET </param>
internal IEnumerable<NodeSearchElementViewModel> Search(string search, bool useLucene)
{
if (useLucene)
if (LuceneSearchUtility != null)
{
//The DirectoryReader and IndexSearcher have to be assigned after commiting indexing changes and before executing the Searcher.Search() method, otherwise new indexed info won't be reflected
LuceneSearchUtility.dirReader = LuceneSearchUtility.writer?.GetReader(applyAllDeletes: true);
Expand Down Expand Up @@ -982,10 +989,7 @@ internal IEnumerable<NodeSearchElementViewModel> Search(string search, bool useL
}
return candidates;
}
else
{
return Search(search);
}
return null;
}

/// <summary>
Expand Down
19 changes: 14 additions & 5 deletions test/DynamoCoreWpfTests/SearchViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Dynamo.Configuration;
using Dynamo.Search;
using Dynamo.Search.SearchElements;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using Dynamo.Wpf.ViewModels;
using NUnit.Framework;
Expand All @@ -15,12 +16,13 @@ internal class SearchViewModelTests : DynamoViewModelUnitTest
{
private static NodeSearchModel model;
private static SearchViewModel viewModel;
private static LuceneSearchUtility LuceneSearchUtility;

[SetUp]
public void Init()
{
model = new NodeSearchModel();
viewModel = new SearchViewModel(model);
viewModel = new SearchViewModel(model, ViewModel);
}

[Test]
Expand Down Expand Up @@ -794,23 +796,30 @@ public void CollapseAllTest()

#region Helpers

private static NodeSearchElement CreateCustomNode(string name, string category,
private NodeSearchElement CreateCustomNode(string name, string category,
string description = "", string path = "")
{
var element = new CustomNodeSearchElement(null,
new CustomNodeInfo(Guid.NewGuid(), name, category, description, path));

var iDoc = ViewModel.Model.LuceneSearchUtility.InitializeIndexDocumentForNodes();
if (element != null)
{
ViewModel.Model.AddNodeTypeToSearchIndex(element, iDoc);
}


return element;
}

private static NodeSearchElementViewModel CreateCustomNodeViewModel(string name, string category,
private NodeSearchElementViewModel CreateCustomNodeViewModel(string name, string category,
string description = "", string path = "")
{
var element = CreateCustomNode(name, category, description, path);
return new NodeSearchElementViewModel(element, null);
}

private static NodeSearchElementViewModel CreateCustomNodeViewModel(NodeSearchElement element)
private NodeSearchElementViewModel CreateCustomNodeViewModel(NodeSearchElement element)
{
return new NodeSearchElementViewModel(element, null);
}
Expand All @@ -819,7 +828,7 @@ private static NodeSearchElementViewModel CreateCustomNodeViewModel(NodeSearchEl
/// Build complex tree.
/// </summary>
/// <returns>All members of built tree.</returns>
private static IEnumerable<NodeSearchElementViewModel> PrepareTestTree()
private IEnumerable<NodeSearchElementViewModel> PrepareTestTree()
{
// Next tree will be built.
// Used blocks: ─, │, ┌, ┐, ┤, ├, ┴, ┬.
Expand Down
6 changes: 3 additions & 3 deletions test/DynamoCoreWpfTests/WorkspaceSaving.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ public void CustomNodeSaveAsAddsNewCustomNodeToSearch()
ViewModel.CurrentSpaceViewModel.InCanvasSearchViewModel.SearchAndUpdateResults("Constant2");
Assert.AreEqual(originalNumElements + 1, ViewModel.Model.SearchModel.NumElements);

Assert.AreEqual(2, ViewModel.CurrentSpaceViewModel.InCanvasSearchViewModel.FilteredResults.Count());
Assert.AreEqual(2, ViewModel.CurrentSpaceViewModel.InCanvasSearchViewModel.FilteredResults.OfType<CustomNodeSearchElementViewModel>().Count());

var res1 = ViewModel.CurrentSpaceViewModel.InCanvasSearchViewModel.FilteredResults.ElementAt(0);
var res2 = ViewModel.CurrentSpaceViewModel.InCanvasSearchViewModel.FilteredResults.ElementAt(1);
Expand All @@ -1060,7 +1060,7 @@ public void CustomNodeSaveAsAddsNewCustomNodeToSearch()
var node2 = res2.Model as CustomNodeSearchElement;

Assert.IsTrue((node1.ID == oldId && node2.ID == newId) ||
(node1.ID == newId && node2.ID == oldId));
(node1.ID == newId && node2.ID == oldId));

}

Expand Down Expand Up @@ -1123,7 +1123,7 @@ public void CustomNodeSaveAsAddsNewCustomNodeToSearchAndItCanBeRefactoredWhilePr
var examplePath = Path.Combine(TestDirectory, @"core\custom_node_saving", "Constant2.dyf");
ViewModel.OpenCommand.Execute(examplePath);

var oldId = (model.CurrentWorkspace as CustomNodeWorkspaceModel).CustomNodeDefinition.FunctionId;
var oldId = (model.CurrentWorkspace as CustomNodeWorkspaceModel).CustomNodeDefinition.FunctionId;

CustomNodeWorkspaceModel nodeWorkspace;
Assert.IsTrue(model.CustomNodeManager.TryGetFunctionWorkspace(oldId, true, out nodeWorkspace));
Expand Down

0 comments on commit 206add1

Please sign in to comment.