Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kenny Burns 173 posts 305 karma points
    Jun 15, 2020 @ 12:17
    Kenny Burns
    0

    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

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jun 16, 2020 @ 08:26
    Ismail Mayat
    1

    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:

    /// <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.

    Regards

    Ismail

  • Kenny Burns 173 posts 305 karma points
    Jun 16, 2020 @ 14:54
    Kenny Burns
    0

    Ismail - thank you, that's really helpful!

    I will have a look at this and let you know how I get on.

Please Sign in or register to post replies

Write your reply to:

Draft