<commit_before>﻿using System;
using System.Collections.Generic;
using ApprovalTests.Reporters;
using HeterogeneousDataSources.LoadLinkExpressions;
using HeterogeneousDataSources.Tests.Shared;
using NUnit.Framework;
using RC.Testing;

namespace HeterogeneousDataSources.Tests {
    [UseReporter(typeof(DiffReporter))]
    [TestFixture]
    public class NestedLinkedSourceTests
    {
        private LoadLinkProtocolFactory<NestedContent, int> _loadLinkProtocolFactory;

        [SetUp]
        public void SetUp() {
            _loadLinkProtocolFactory = new LoadLinkProtocolFactory<NestedContent, int>(
                loadLinkExpressions: new List<ILoadLinkExpression>{
                    new RootLoadLinkExpression<NestedLinkedSource, NestedContent, int>(),
                    new NestedLinkedSourceLoadLinkExpression<NestedLinkedSource, PersonLinkedSource, Person, string>(
                        linkedSource => linkedSource.Model.AuthorDetailId,
                        (linkedSource, reference) => linkedSource.AuthorDetail = reference),
                    new ReferenceLoadLinkExpression<NestedLinkedSource,Person, string>(
                        linkedSource => linkedSource.Model.ClientSummaryId,
                        (linkedSource, reference) => linkedSource.ClientSummary = reference),
                    new ReferenceLoadLinkExpression<PersonLinkedSource,Image, string>(
                        linkedSource => linkedSource.Model.SummaryImageId,
                        (linkedSource, reference) => linkedSource.SummaryImage = reference)
                },
                getReferenceIdFunc: reference => reference.Id,
                fakeReferenceTypeForLoadingLevel: new[] {
                        new List<Type>{typeof(int)},
                        new List<Type>{typeof(NestedContent)},
                        new List<Type>{typeof(Person)},
                        new List<Type>{typeof(Image)},
                }
            );
        }

        [Test]
        public void LoadLink_NestedLinkedSource()
        {
            var sut = _loadLinkProtocolFactory.Create(
                new NestedContent{
                    Id = 1,
                    AuthorDetailId = "32",
                    ClientSummaryId = "33"
                }
            );

            var actual = sut.LoadLink<NestedLinkedSource>(1);

            ApprovalsExt.VerifyPublicProperties(actual);
        }

        [Test]
        public void LoadLink_DifferendKindOfPersonInSameRootLinkedSource_ShouldNotLoadImageFromClientSummary() {
            var sut = _loadLinkProtocolFactory.Create(
                new NestedContent {
                    Id = 1,
                    AuthorDetailId = "32",
                    ClientSummaryId = "666" //Image repository throws an exception for "person-img-666" 
                }
            );

            sut.LoadLink<NestedLinkedSource>(1);

            //stle: improve this by allowing test visibility on which image id was resolved
            //assert that does not throw
        }

        [Test]
        public void LoadLink_NestedLinkedSourceWithoutReferenceId_ShouldLinkNull() {
            var sut = _loadLinkProtocolFactory.Create(
                new NestedContent {
                    Id = 1,
                    AuthorDetailId = null,
                    ClientSummaryId = "33"
                }
            );

            var actual = sut.LoadLink<NestedLinkedSource>(1);

            Assert.That(actual.AuthorDetail, Is.Null);
        }

        [Test]
        public void LoadLink_NestedLinkedSourceCannotBeResolved_ShouldLinkNull() {
            var sut = _loadLinkProtocolFactory.Create(
                new NestedContent {
                    Id = 1,
                    AuthorDetailId = "cannot-be-resolved",
                    ClientSummaryId = "33"
                }
            );

            var actual = sut.LoadLink<NestedLinkedSource>(1);

            Assert.That(actual.AuthorDetail, Is.Null);
        }

        [Test]
        public void LoadLink_NestedLinkedSourceRootCannotBeResolved_ShouldReturnNullAsRoot() {
            var sut = _loadLinkProtocolFactory.Create(null);

            var actual = sut.LoadLink<NestedLinkedSource>(1);

            Assert.That(actual, Is.Null);
        }
    }


    public class NestedLinkedSource:ILinkedSource<NestedContent>
    {
        public NestedContent Model { get; set; }
        public PersonLinkedSource AuthorDetail { get; set; }
        public Person ClientSummary { get; set; }
    }

    public class NestedContent {
        public int Id { get; set; }
        public string AuthorDetailId { get; set; }
        public string ClientSummaryId { get; set; }
    }

    public class PersonLinkedSource: ILinkedSource<Person>
    {
        public Person Model { get; set; }
        public Image SummaryImage{ get; set; }
    }
}
<commit_msg>Remove confusion, id is not a loaded type<commit_after>﻿using System;
using System.Collections.Generic;
using ApprovalTests.Reporters;
using HeterogeneousDataSources.LoadLinkExpressions;
using HeterogeneousDataSources.Tests.Shared;
using NUnit.Framework;
using RC.Testing;

namespace HeterogeneousDataSources.Tests {
    [UseReporter(typeof(DiffReporter))]
    [TestFixture]
    public class NestedLinkedSourceTests
    {
        private LoadLinkProtocolFactory<NestedContent, int> _loadLinkProtocolFactory;

        [SetUp]
        public void SetUp() {
            _loadLinkProtocolFactory = new LoadLinkProtocolFactory<NestedContent, int>(
                loadLinkExpressions: new List<ILoadLinkExpression>{
                    new RootLoadLinkExpression<NestedLinkedSource, NestedContent, int>(),
                    new NestedLinkedSourceLoadLinkExpression<NestedLinkedSource, PersonLinkedSource, Person, string>(
                        linkedSource => linkedSource.Model.AuthorDetailId,
                        (linkedSource, reference) => linkedSource.AuthorDetail = reference),
                    new ReferenceLoadLinkExpression<NestedLinkedSource,Person, string>(
                        linkedSource => linkedSource.Model.ClientSummaryId,
                        (linkedSource, reference) => linkedSource.ClientSummary = reference),
                    new ReferenceLoadLinkExpression<PersonLinkedSource,Image, string>(
                        linkedSource => linkedSource.Model.SummaryImageId,
                        (linkedSource, reference) => linkedSource.SummaryImage = reference)
                },
                getReferenceIdFunc: reference => reference.Id,
                fakeReferenceTypeForLoadingLevel: new[] {
                        new List<Type>{typeof(NestedContent)},
                        new List<Type>{typeof(Person)},
                        new List<Type>{typeof(Image)},
                }
            );
        }

        [Test]
        public void LoadLink_NestedLinkedSource()
        {
            var sut = _loadLinkProtocolFactory.Create(
                new NestedContent{
                    Id = 1,
                    AuthorDetailId = "32",
                    ClientSummaryId = "33"
                }
            );

            var actual = sut.LoadLink<NestedLinkedSource>(1);

            ApprovalsExt.VerifyPublicProperties(actual);
        }

        [Test]
        public void LoadLink_DifferendKindOfPersonInSameRootLinkedSource_ShouldNotLoadImageFromClientSummary() {
            var sut = _loadLinkProtocolFactory.Create(
                new NestedContent {
                    Id = 1,
                    AuthorDetailId = "32",
                    ClientSummaryId = "666" //Image repository throws an exception for "person-img-666" 
                }
            );

            sut.LoadLink<NestedLinkedSource>(1);

            //stle: improve this by allowing test visibility on which image id was resolved
            //assert that does not throw
        }

        [Test]
        public void LoadLink_NestedLinkedSourceWithoutReferenceId_ShouldLinkNull() {
            var sut = _loadLinkProtocolFactory.Create(
                new NestedContent {
                    Id = 1,
                    AuthorDetailId = null,
                    ClientSummaryId = "33"
                }
            );

            var actual = sut.LoadLink<NestedLinkedSource>(1);

            Assert.That(actual.AuthorDetail, Is.Null);
        }

        [Test]
        public void LoadLink_NestedLinkedSourceCannotBeResolved_ShouldLinkNull() {
            var sut = _loadLinkProtocolFactory.Create(
                new NestedContent {
                    Id = 1,
                    AuthorDetailId = "cannot-be-resolved",
                    ClientSummaryId = "33"
                }
            );

            var actual = sut.LoadLink<NestedLinkedSource>(1);

            Assert.That(actual.AuthorDetail, Is.Null);
        }

        [Test]
        public void LoadLink_NestedLinkedSourceRootCannotBeResolved_ShouldReturnNullAsRoot() {
            var sut = _loadLinkProtocolFactory.Create(null);

            var actual = sut.LoadLink<NestedLinkedSource>(1);

            Assert.That(actual, Is.Null);
        }
    }


    public class NestedLinkedSource:ILinkedSource<NestedContent>
    {
        public NestedContent Model { get; set; }
        public PersonLinkedSource AuthorDetail { get; set; }
        public Person ClientSummary { get; set; }
    }

    public class NestedContent {
        public int Id { get; set; }
        public string AuthorDetailId { get; set; }
        public string ClientSummaryId { get; set; }
    }

    public class PersonLinkedSource: ILinkedSource<Person>
    {
        public Person Model { get; set; }
        public Image SummaryImage{ get; set; }
    }
}

{'fraction_non_alphanumeric': 0.07530089496965332, 'fraction_numerical': 0.005143503754757741, 'mean_word_length': 1.8644667059516795, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6199421', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2267, 'n_words': 493}