Пример #1
0
        public void GetIncludedModels__Given_NullHasManyReference__Then_ReturnModels()
        {
            var subject = CreateSubject(
                typeof(CircularReferenceA),
                typeof(CircularReferenceB),
                typeof(CircularReferenceC));

            var a = new CircularReferenceA();
            var b = new CircularReferenceB();
            var c = new CircularReferenceC();

            a.B = b;

            b.A = a;
            b.C = c;

            c.A        = a;
            c.PrimaryD = null;
            c.AllDs    = null;

            var result = subject.IncludedModelsCreate(a);

            Assert.NotNull(result);
            Assert.NotEmpty(result);

            Assert.Equal(3, result.Count());

            Assert.Contains(a, result);
            Assert.Contains(b, result);
            Assert.Contains(c, result);
        }
Пример #2
0
        public void BuildQuery__Given_Target__When_ExpressionHasExtension__Then_InvokeMethodsAndAddFilter
            (Guid[] ids)
        {
            var a  = new CircularReferenceA();
            var b  = new CircularReferenceB();
            var c  = new CircularReferenceC();
            var ds = ids.Select(x => new CircularReferenceD()).ToArray();

            a.B     = b;
            b.C     = c;
            c.A     = a;
            c.AllDs = ds;

            var expectedId = ds.First().Id;

            var mockQueryContext = new Mock <IQueryContext>();

            var session = Mock.Of <IQuerySession>();

            var mockTarget = new Mock <RemoteQueryable <AllPropertyTypes> >(session, Mock.Of <IQueryProvider>());

            mockTarget
            .Setup(x => x.BuildQuery())
            .Returns(mockQueryContext.Object);

            Expression <Func <AllPropertyTypes, bool> > predicate =
                x => x.StringProperty == a.B.C.AllDs.First().Id.ToString();

            var subject = CreateSubject(
                session,
                mockTarget.Object,
                predicate);

            var result = subject.BuildQuery();

            Assert.Same(mockQueryContext.Object, result);

            mockQueryContext.Verify(x => x.AppendFilter("allPropertyTypes", $"stringProperty[eq]'{expectedId}'"),
                                    Times.Once);
        }
Пример #3
0
        public void BuildQuery__Given_Target__When_ExpressionIsCrazyChain__Then_InvokeMethodsAndAddFilter
            (Guid id)
        {
            var a = new CircularReferenceA();
            var b = new CircularReferenceB();
            var c = new CircularReferenceC();
            var d = new CircularReferenceD {
                Id = id
            };

            a.B        = b;
            b.C        = c;
            c.A        = a;
            c.PrimaryD = d;

            var mockQueryContext = new Mock <IQueryContext>();

            var session = Mock.Of <IQuerySession>();

            var mockTarget = new Mock <RemoteQueryable <AllPropertyTypes> >(session, Mock.Of <IQueryProvider>());

            mockTarget
            .Setup(x => x.BuildQuery())
            .Returns(mockQueryContext.Object);

            Expression <Func <AllPropertyTypes, bool> > predicate = x => x.StringProperty == a.B.C.PrimaryD.Id.ToString();

            var subject = CreateSubject(
                session,
                mockTarget.Object,
                predicate);

            var result = subject.BuildQuery();

            Assert.Same(mockQueryContext.Object, result);

            mockQueryContext.Verify(x => x.AppendFilter("allPropertyTypes", $"stringProperty[eq]'{d.Id}'"), Times.Once);
        }
Пример #4
0
        public void GetIncludedModels__Given_Model__When_RelatedNull__Then_ReturnModels()
        {
            var subject = CreateSubject(
                typeof(CircularReferenceA),
                typeof(CircularReferenceB),
                typeof(CircularReferenceC),
                typeof(CircularReferenceD));

            var a = new CircularReferenceA();
            var b = new CircularReferenceB();
            var c = new CircularReferenceC();

            var ds = Enumerable.Range(0, 3)
                     .Select(x => new CircularReferenceD())
                     .ToArray();

            a.B = b;

            b.A = a;
            b.C = c;

            c.A        = a;
            c.PrimaryD = ds.First();
            c.AllDs    = ds;

            var result = subject.IncludedModelsCreate(a);

            Assert.NotNull(result);
            Assert.NotEmpty(result);

            Assert.Equal(6, result.Count());

            Assert.Contains(a, result);
            Assert.Contains(b, result);
            Assert.Contains(c, result);
            Assert.All(ds, d => Assert.Contains(d, result));
        }
Пример #5
0
        public async Task Create__Given_ComplexModel__Then_PostAndCacheModel
            (Guid modelId)
        {
            var uri = "http://www.test.com/";

            var a = new CircularReferenceA {
                Id = modelId
            };
            var b = new CircularReferenceB();
            var c = new CircularReferenceC();

            a.B = b;
            b.A = a;
            b.C = c;
            c.A = a;

            var expectedRequest = new HttpRequestMessage(HttpMethod.Post, new Uri(uri));

            var mockRequestBuilder = new Mock <IHttpRequestBuilder>();

            mockRequestBuilder
            .Setup(x => x.CreateResource(It.IsAny <ResourceRootSingle>()))
            .Callback <ResourceRootSingle>((resource) =>
            {
                Assert.Equal(modelId, resource.Data.Id);
                Assert.Null(resource.Data.Attributes);

                Assert.NotNull(resource.Data.Relationships);
                Assert.Equal(1, resource.Data.Relationships.Count);

                Assert.NotEmpty(resource.Included);
                Assert.Equal(2, resource.Included.Count());

                Assert.Null(resource.Links);
                Assert.Null(resource.Meta);
            })
            .ReturnsAsync(expectedRequest);

            var mockHandler = new MockRequestHandler();

            mockHandler.Setup(
                new Uri(uri),
                request =>
            {
                Assert.Same(expectedRequest, request);
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.Accepted)));
            });

            var mockCacheProvider = new Mock <ICacheProvider>();

            var modelRegistry = CreateModelRegistry(
                typeof(CircularReferenceA),
                typeof(CircularReferenceB),
                typeof(CircularReferenceC));

            var subject = CreateSubject(
                mockHandler,
                mockRequestBuilder.Object,
                mockCacheProvider.Object,
                modelRegistry);

            var result = await subject.Create(a);

            Assert.NotNull(result);
            Assert.Same(a, result);

            mockCacheProvider
            .Verify(x => x.Update(It.IsAny <Guid>(), It.IsAny <CircularReferenceA>()), Times.Once);
            mockCacheProvider
            .Verify(x => x.Update(It.IsAny <Guid>(), It.IsAny <CircularReferenceB>()), Times.Once);
            mockCacheProvider
            .Verify(x => x.Update(It.IsAny <Guid>(), It.IsAny <CircularReferenceC>()), Times.Once);
        }