I did something similar in v8 but not exactly with Examine. So the tests were for gettings tags based on searches using simplefactedsearch. I did look at how to do it with examine but could not figure out how to pass in in memory lucene index. My base code for lucene based tests looks like:
/// <summary>
/// used to create an in memory lucene index that we can use for tests
/// </summary>
public class LuceneTestBase
{
protected IndexReader Reader;
protected Directory Dir;
protected rss Rss;
[OneTimeSetUp]
public void Init()
{
BuildFeed();
Dir = new RAMDirectory();
IndexWriter writer = null;
using (writer = new IndexWriter(Dir, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED))
{
int i = 1;
foreach (var item in Rss.channel.item)
{
AddDocWithIndex(writer,i,item);
i++;
}
}
Reader = IndexReader.Open(Dir,true);
}
private void BuildFeed()
{
var feedService = Substitute.For<IFeedService>();
feedService.GetFeed().Returns(Properties.Resources.SampleHighqFeed);
var builder = new HiqhQFeedBuilder(feedService);
Rss = builder.BuildRss();
}
private void AddDocWithIndex(IndexWriter writer, int index,rssChannelItem item)
{
var doc = new Document();
doc.Add(new Field("__IndexType", "content ", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("content", item.encoded.StripHtml(), Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("id", "" + index, Field.Store.YES, Field.Index.ANALYZED));
var tags = GetTags(item.category, "onlineservices/FRWB - Topics");
foreach (var tagRaw in tags)
{
foreach (var tag in tagRaw.Split(','))
{
doc.Add(new Field("tag",tag,Field.Store.YES, Field.Index.NOT_ANALYZED));
}
}
writer.AddDocument(doc);
}
private IEnumerable<string> GetTags(List<rssChannelItemCategory> feedItemCategory, string query)
{
var items = from item in feedItemCategory
where item.domain == query
select item.Value;
return items;
}
[OneTimeTearDown]
public void TearDown()
{
Dir.Dispose();
Reader.Dispose();
}
}
And my class methods take in a reader which I can pass in. As I say I did try and see how to do it with examine but couldnt figure it out. Take a look at umbraco or examine source code as tests in the codebase use in memory lucene.
Umbraco 8 - nUnit testing and Examine
Hello,
I am trying to create a unit test against a method which uses an examine query.
Has anyone managed to create a mock examine index in v8?
I came across this article: https://blog.gravypower.net/2013/10/28/mocking-examine-an-umbraco-index-story/ - but this is going back a few years now. :)
Thanks,
Kenny
Kenny,
I did something similar in v8 but not exactly with Examine. So the tests were for gettings tags based on searches using simplefactedsearch. I did look at how to do it with examine but could not figure out how to pass in in memory lucene index. My base code for lucene based tests looks like:
And my class methods take in a reader which I can pass in. As I say I did try and see how to do it with examine but couldnt figure it out. Take a look at umbraco or examine source code as tests in the codebase use in memory lucene.
Regards
Ismail
Ismail - thank you, that's really helpful!
I will have a look at this and let you know how I get on.
is working on a reply...