Ejemplo n.º 1
0
        public PolymorphicSubLinkedSourceTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <LinkedSource>()
            .LoadLinkPolymorphic(
                linkedSource => linkedSource.Model.Target,
                linkedSource => linkedSource.Target,
                link => link.Type,
                includes => includes.Include <PdfReferenceLinkedSource>().AsNestedLinkedSourceFromModel(
                    "pdf",
                    link => link,
                    (linkedSource, referenceIndex, childLinkedSource) =>
                    childLinkedSource.Contextualization = "From the level below:" + linkedSource.Model.Id
                    )
                .Include <WebPageReferenceLinkedSource>().AsNestedLinkedSourceFromModel(
                    "web-page",
                    link => link.GetAsBlogPostReference()
                    )
                );

            loadLinkProtocolBuilder.For <WebPageReferenceLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.ImageId,
                linkedSource => linkedSource.Image);

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
        public SideEffectBetweenNestedLinkedSourceTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <LinkedSource>()
            .LoadLinkNestedLinkedSourceById(
                linkedSource => linkedSource.Model.PersonId,
                linkedSource => linkedSource.Person
                )
            .LoadLinkNestedLinkedSourceById(
                linkedSource => linkedSource.Model.PersonGroupId,
                linkedSource => linkedSource.PersonGroup
                );
            loadLinkProtocolBuilder.For <PersonGroupLinkedSource>()
            .LoadLinkReferencesByIds(
                linkedSource => linkedSource.Model.PersonIds,
                linkedSource => linkedSource.People);

            loadLinkProtocolBuilder.For <PersonLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);
            _sut = (LoadLinkProtocol)loadLinkProtocolBuilder.Build(
                () => null //not required
                );
        }
        public PolymorphicList_WithDependenciesBetweenItemsTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <LinkedSource>()
            .LoadLinkPolymorphicList(
                linkedSource => linkedSource.Model.PolyLinks,
                linkedSource => linkedSource.PolyLinks,
                link => link.Type,
                includes => includes.Include <Image>().AsReferenceById(
                    "image",
                    link => link.Id
                    )
                .Include <PersonLinkedSource>().AsNestedLinkedSourceById(
                    "person",
                    link => link.Id
                    )
                );
            loadLinkProtocolBuilder.For <PersonLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 4
0
        public PolymorphicSubLinkedSourcesTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <WithPolymorphicSubLinkedSource>()
            .LoadLinkPolymorphicList(
                linkedSource => linkedSource.Model.Subs,
                linkedSource => linkedSource.Subs,
                link => link.GetType(),
                includes => includes.Include <SubContentWithImageLinkedSource>().AsNestedLinkedSourceFromModel(
                    typeof(SubContentWithImage),
                    link => (SubContentWithImage)link,
                    (linkedSource, referenceIndex, childLinkedSource) =>
                    childLinkedSource.Contextualization = linkedSource.Model.Contextualizations[referenceIndex]
                    )
                .Include <SubContentWithoutReferencesLinkedSource>().AsNestedLinkedSourceFromModel(
                    typeof(SubContentWithoutReferences),
                    link => (SubContentWithoutReferences)link,
                    (linkedSource, referenceIndex, childLinkedSource) =>
                    childLinkedSource.Contextualization = linkedSource.Model.Contextualizations[referenceIndex]
                    )
                );

            loadLinkProtocolBuilder.For <SubContentWithImageLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.ImageId,
                linkedSource => linkedSource.Image);

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 5
0
        public void CreateLoadLinkConfig_WithCycleCausedByIndirectLinkedSources_ShouldThrow()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <IndirectCycleLinkedSourceA>()
            .LoadLinkNestedLinkedSourceById(
                linkedSource => linkedSource.Model.ModelBId,
                linkedSource => linkedSource.ModelB
                );
            loadLinkProtocolBuilder.For <IndirectCycleLinkedSourceB>()
            .LoadLinkNestedLinkedSourceById(
                linkedSource => linkedSource.Model.ModelCId,
                linkedSource => linkedSource.ModelC
                );
            loadLinkProtocolBuilder.For <IndirectCycleLinkedSourceC>()
            .LoadLinkNestedLinkedSourceById(
                linkedSource => linkedSource.Model.ModelAId,
                linkedSource => linkedSource.ModelA
                );

            Action act = () => loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());

            var exception = Assert.Throws <InvalidOperationException>(act);

            exception.Message.Should().BeOneOf(
                $"Recursive dependency detected for type {{ {nameof(IndirectCycleLinkedSourceA)} }}.",
                $"Recursive dependency detected for type {{ {nameof(IndirectCycleLinkedSourceB)} }}.",
                $"Recursive dependency detected for type {{ {nameof(IndirectCycleLinkedSourceC)} }}.");
        }
Ejemplo n.º 6
0
        public PolymorphicMixtedTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <LinkedSource>()
            .LoadLinkPolymorphic(
                linkedSource => linkedSource.Model.TargetReference,
                linkedSource => linkedSource.Target,
                link => link.GetType(),
                includes => includes.Include <Person>().AsReferenceById(
                    typeof(int),
                    link => link.ToString()
                    )
                .Include <PersonLinkedSource>().AsNestedLinkedSourceById(
                    typeof(string),
                    link => link.ToString()
                    )
                .Include <PersonLinkedSource>().AsNestedLinkedSourceFromModel(
                    typeof(Person),
                    link => (Person)link
                    )
                );
            loadLinkProtocolBuilder.For <PersonLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 7
0
        public PolymorphicTest()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <LinkedSource>()
            .LoadLinkPolymorphic(
                linkedSource => linkedSource.Model.PolyRef,
                linkedSource => linkedSource.Poly,
                link => link.Kind,
                includes => includes.Include <PersonLinkedSource>().AsNestedLinkedSourceById(
                    "person-nested",
                    link => (string)link.Value)
                .Include <PersonLinkedSource>().AsNestedLinkedSourceFromModel(
                    "person-sub",
                    link => (Person)link.Value)
                .Include <Image>().AsReferenceById(
                    "img",
                    link => (string)link.Value)
                );
            loadLinkProtocolBuilder.For <PersonLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);
            _sut = (LoadLinkProtocol)loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 8
0
        public SimplestRootLinkedSourceTest()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <LinkedSource>();
            _sut = (LoadLinkProtocol)loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 9
0
        public PolymorphicNestedLinkedSourcesTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <WithNestedPolymorphicContentsLinkedSource>()
            .LoadLinkPolymorphicList(
                linkedSource => linkedSource.Model.ContentContextualizations,
                linkedSource => linkedSource.Contents,
                link => link.ContentType,
                includes => includes.Include <PersonWithoutContextualizationLinkedSource>().AsNestedLinkedSourceById(
                    "person",
                    link => (string)link.Id)
                .Include <ImageWithContextualizationLinkedSource>().AsNestedLinkedSourceById(
                    "image",
                    link => (string)link.Id,
                    (linkedSource, referenceIndex, childLinkedSource) =>
            {
                var contextualization = linkedSource.Model.ContentContextualizations[referenceIndex];
                childLinkedSource.ContentContextualization = contextualization;
            }
                    )
                );

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 10
0
        public DebugModeTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <BlogPostLinkedSource>()
            .LoadLinkNestedLinkedSourceById(
                linkedSource => linkedSource.Model.AuthorId,
                linkedSource => linkedSource.Author)
            .LoadLinkNestedLinkedSourceById(
                linkedSource => linkedSource.Model.MediaId,
                linkedSource => linkedSource.Media);
            loadLinkProtocolBuilder.For <PersonLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);
            loadLinkProtocolBuilder.For <MediaLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);

            var blogPostConfig = new ReferenceTypeConfig <BlogPost, string>(
                ids => ids.Select(GetBlogPost),
                reference => reference.Id
                );

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub(blogPostConfig));
        }
Ejemplo n.º 11
0
        public GenericPieTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <StringPieLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.PieContent,
                linkedSource => linkedSource.SummaryImage);

            loadLinkProtocolBuilder.For <IntPieLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.PieContent,
                linkedSource => linkedSource.SummaryImage);

            _sut = loadLinkProtocolBuilder.Build(() =>
                                                 new ReferenceLoaderStub(
                                                     new ReferenceTypeConfig <Pie <string>, string>(
                                                         ids => new PieRepository <string>().GetByPieContentIds(ids),
                                                         reference => reference.Id
                                                         ),
                                                     new ReferenceTypeConfig <Pie <int>, string>(
                                                         ids => new PieRepository <int>().GetByPieContentIds(ids),
                                                         reference => reference.Id
                                                         )
                                                     )
                                                 );
        }
        public LoadLinkExpressionOverridingTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <SingleReferenceLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource =>
            {
                throw new Exception("Not overridden!");
#pragma warning disable 162
                return(linkedSource.Model.SummaryImageId);

#pragma warning restore 162
            },
                linkedSource => linkedSource.SummaryImage
                );

            //override
            loadLinkProtocolBuilder.For <SingleReferenceLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId + "-overridden",
                linkedSource => linkedSource.SummaryImage
                );

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 13
0
        public LoadLinkPrimitiveModelTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <PrimitiveLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model,
                linkedSource => linkedSource.Media);
            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 14
0
        public LoadLinkModelTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <SingleReferenceLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);
            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 15
0
        public async Task LoadLink_LinkedSourceWithoutLoadLinkExpressionAtRoot_ShouldThrow()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();
            var sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());

            Func <Task> act = async() => await sut.LoadLink <PersonLinkedSource>().ByIdAsync("dont-care");

            var ex = await Assert.ThrowsAsync <InvalidOperationException>(act);

            Assert.Contains("PersonLinkedSource", ex.Message);
            Assert.Contains("root linked source", ex.Message);
        }
Ejemplo n.º 16
0
        public OptionalReferenceTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <LinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.MediaId,
                linkedSource => linkedSource.Media
                );

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 17
0
        public LoadLinkByIdsTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <PersonLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);

            _referenceLoaderStub = new ReferenceLoaderStub();
            _sut = loadLinkProtocolBuilder.Build(() => _referenceLoaderStub);
        }
Ejemplo n.º 18
0
        public void CreateLoadLinkConfig_WithCycleCausedByReference_ShouldNotThrow()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <CycleInReferenceLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.ParentId,
                linkedSource => linkedSource.Parent);

            var loadLinkProtocol = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());

            Assert.NotNull(loadLinkProtocol);
        }
Ejemplo n.º 19
0
        public ContextualizationFromRootTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <PersonContextualizedLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource =>
                linkedSource.Contextualization?.SummaryImageId ??
                linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
        public ImageWithRatiosCustomLoadLinkTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <WithImageLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.ImageUrl,
                linkedSource => linkedSource.Image);

            _sut = loadLinkProtocolBuilder.Build(() =>
                                                 new ReferenceLoaderStub(new ImageReferenceTypeConfigWorkAround())
                                                 );
        }
Ejemplo n.º 21
0
        public MultipleReferencesTypeTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <MultipleReferencesTypeLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage)
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.AuthorId,
                linkedSource => linkedSource.Author);

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 22
0
        private static ILoadLinkProtocol BuildLoadLinkProtocol()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.ApplyConventions(
                new List <Type> {
                typeof(LinkedSource)
            },
                new List <ILoadLinkExpressionConvention> {
                new LoadLinkByNullableValueTypeIdWhenIdSuffixMatches()
            }
                );
            return(loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub()));
        }
Ejemplo n.º 23
0
        public ReferenceTest()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <LinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.PersonOneId,
                linkedSource => linkedSource.PersonOne)
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.PersonTwoId,
                linkedSource => linkedSource.PersonTwo
                );
            _sut = (LoadLinkProtocol)loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
        /// <summary>
        /// Build a new <see cref="ILoadLinkProtocol"/> using assemblies as an entry point
        /// to find <see cref="ILoadLinkProtocolConfig"/> and types for which to apply the <paramref name="conventions"/>.
        /// </summary>
        public static ILoadLinkProtocol Build(
            this LoadLinkProtocolBuilder loadLinkProtocolBuilder,
            Func <IReferenceLoader> createReferenceLoader,
            IEnumerable <Assembly> assemblies,
            IList <ILoadLinkExpressionConvention> conventions)
        {
            loadLinkProtocolBuilder.ApplyConventions(
                assemblies,
                conventions
                );
            loadLinkProtocolBuilder.ApplyLoadLinkProtocolConfigs(assemblies);

            return(loadLinkProtocolBuilder.Build(createReferenceLoader));
        }
Ejemplo n.º 25
0
        public async Task LoadLink_ShouldDisposeLoader()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <PersonLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);
            var sut            = new ReferenceLoaderStub();
            var loadLinkConfig = loadLinkProtocolBuilder.Build(() => sut);

            await loadLinkConfig.LoadLink <PersonLinkedSource>().ByIdAsync("dont-care");

            Assert.True(sut.IsDisposed);
        }
Ejemplo n.º 26
0
 public LoadLinkProtocolFixture()
 {
     try
     {
         var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();
         LoadLinkProtocol = loadLinkProtocolBuilder.Build(
             () => new FakeReferenceLoader(),
             new [] { Assembly.GetExecutingAssembly() },
             LoadLinkExpressionConvention.Default);
     }
     catch (Exception e)
     {
         Assert.Null(e);
     }
 }
        private static ILoadLinkProtocol BuildLoadLinkProtocol()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.ApplyConventions(
                new List <Type> {
                typeof(LinkedSource), typeof(MasterLinkedSource)
            },
                new List <ILoadLinkExpressionConvention> {
                new LoadLinkNestedLinkedSourceListFromModelWhenNameMatches()
            }
                );

            return(loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub()));
        }
Ejemplo n.º 28
0
        public void CreateLoadLinkConfig_WithCycleWithinLinkedSource_ShouldThrow()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <SelfReferencingLinkedSource>()
            .LoadLinkNestedLinkedSourceById(
                linkedSource => linkedSource.Model.ParentId,
                linkedSource => linkedSource.Parent);

            Action act = () => loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());

            var exception = Assert.Throws <InvalidOperationException>(act);

            Assert.Equal($"Recursive dependency detected for type {{ {nameof(SelfReferencingLinkedSource)} }}.", exception.Message);
        }
Ejemplo n.º 29
0
        public NestedLinkedSourcesTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <NestedLinkedSources>()
            .LoadLinkNestedLinkedSourcesByIds(
                linkedSource => linkedSource.Model.AuthorIds,
                linkedSource => linkedSource.Authors);
            loadLinkProtocolBuilder.For <PersonLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage);

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }
Ejemplo n.º 30
0
        public ManyReferencesTests()
        {
            var loadLinkProtocolBuilder = new LoadLinkProtocolBuilder();

            loadLinkProtocolBuilder.For <ManyReferencesLinkedSource>()
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.SummaryImageId,
                linkedSource => linkedSource.SummaryImage)
            .LoadLinkReferenceById(
                linkedSource => linkedSource.Model.AuthorImageId,
                linkedSource => linkedSource.AuthorImage)
            .LoadLinkReferencesByIds(
                linkedSource => linkedSource.Model.FavoriteImageIds,
                linkedSource => linkedSource.FavoriteImages);

            _sut = loadLinkProtocolBuilder.Build(() => new ReferenceLoaderStub());
        }