public void AddRelationshipWhenCallGetWithSpecification()
        {
            Expression <Func <Entity, bool> > filter = entity => true;
            var entities = Builder <Entity> .CreateListOfSize(10)
                           .Build();

            var relationship = Relationship <Entity> .Create(entity => entity.SubEntities);

            relationship.Then <SubEntity>(s => s.Values);
            var mockedContext = new Mock <IDbContext>();

            mockedContext.Setup(context => context.Set <Entity>())
            .ReturnsDbSet(entities);
            var mockedSpecification = new Mock <ISpecification <Entity> >();

            mockedSpecification.SetupGet(specification => specification.Relationships)
            .Returns(new List <Relationship <Entity> > {
                relationship
            }.ToImmutableList);
            mockedSpecification.Setup(specification => specification.ToExpression())
            .Returns(filter);
            using var reader = new Reader(mockedContext.Object);

            var actualEntities = reader.Get(mockedSpecification.Object);

            actualEntities.Should().Equal(entities);
            mockedSpecification.Verify(specification => specification.Relationships, Times.Once);
        }
예제 #2
0
        public void DeserializeTest()
        {
            var jsonString = TestUtils.GetResource("Mastodon.API.Tests.Resources.get_relationship.json");
            var actual     = JsonConvert.DeserializeObject <Relationship>(jsonString);
            var expected   = Relationship.Create("29", true, true, true, true, true);

            Assert.AreEqual(expected, actual);
            actual.GetHashCode();
        }
예제 #3
0
        public Task <ValueOrError <Relationship <Cluster, User> > > AddUserToCluster(string cluster, string user, bool fromBackend = false)
        {
            var c = _clusters[cluster];
            var u = _users[user];

            var value = c.SetUser(u);

            _clusters.SetItem(cluster, value);
            return(Task.FromResult(ValueOrError.Create(Relationship.Create(value, u))));
        }
예제 #4
0
        public Task <ValueOrError <Relationship <Cluster, Source> > > RemoveSourceFromCluster(string cluster, string source, bool fromBackend = false)
        {
            var c = _clusters[cluster];
            var a = _sources[source];

            var value = c.RemoveSource(a);

            _clusters.SetItem(cluster, value);
            return(Task.FromResult(ValueOrError.Create(Relationship.Create(value, a))));
        }
예제 #5
0
        public async Task <ValueOrError <Relationship <Cluster, Source> > > RemoveSourceFromCluster(string cluster, string source, bool fromBackend = false)
        {
            var s = await _visibilityStore.RemoveSourceFromCluster(cluster, source, fromBackend);

            if (s.HasValue)
            {
                _clusterSourceOperations.OnNext(Delta.Create(Relationship.Create(s.Value.Primary, s.Value.Secondary), DeltaType.Removed, fromBackend));
            }

            return(s);
        }
예제 #6
0
        public async Task <ValueOrError <Relationship <Cluster, User> > > AddUserToCluster(string cluster, string user, bool fromBackend = false)
        {
            var s = await _visibilityStore.AddUserToCluster(cluster, user, fromBackend);

            if (s.HasValue)
            {
                _clusterUserOperations.OnNext(Delta.Create(Relationship.Create(s.Value.Primary, s.Value.Secondary), DeltaType.Added, fromBackend));
            }

            return(s);
        }
예제 #7
0
    public RelationshipBuilder AddRelationship()
    {
        if (Instance.IsValueObject)
        {
            throw new BuildException("Value Objects do not support relationships.");
        }
        var relation = Relationship.Create(Build, this);

        Instance.Relationships.Add(relation.Instance);
        return(relation);
    }
예제 #8
0
        private bool IsMatch(string type, string entity, string relatedEntity, string directRelationship)
        {
            IEnumerable <XElement> relationshipSchemas = Schema.Elements(SchemaVocab.Relationship).Where(r =>
                                                                                                         r.Attribute(SchemaVocab.Type).Value == type &&
                                                                                                         r.Attribute(SchemaVocab.Entity).Value == entity &&
                                                                                                         r.Attribute(SchemaVocab.RelatedEntity).Value == relatedEntity &&
                                                                                                         r.Elements(SchemaVocab.Property).Count() == 1);

            foreach (XElement relationshipSchema in relationshipSchemas)
            {
                if (Relationship.Create(relationshipSchema).DirectRelationships[0].ToString() == directRelationship)
                {
                    return(true);
                }
            }
            return(false);
        }
        private bool TryParseAsRelationship(JsonContract contract, object value, out object relationshipObj)
        {
            switch (contract.Converter)
            {
            case ResourceObjectConverter _:
                relationshipObj = Relationship.Create(value);
                return(true);

            case ResourceObjectListConverter _:
                relationshipObj = Relationship.Create(value as IEnumerable <object>);
                return(true);

            case ResourceRelationshipConverter _:
                relationshipObj = value ?? contract.DefaultCreator();
                return(true);

            default:
                relationshipObj = null;
                return(false);
            }
        }
        public void When_relationshp_with_resource_identifier_should_serialize()
        {
            var root = DocumentRoot.Create(new
            {
                Id     = "1234",
                Type   = "articles",
                Title  = "My Article",
                Author = new Relationship <ResourceIdentifier <Person> >()
                {
                    Data = new ResourceIdentifier <Person>()
                    {
                        Meta = new Meta {
                            { "primary-author", true }
                        },
                        Value = new Person
                        {
                            Id        = "333",
                            Type      = "people",
                            FirstName = "John",
                            LastName  = "Smith",
                            Twitter   = "jsmi",
                            Meta      = new Meta()
                            {
                                { "best-sellers", 4 }
                            }
                        }
                    },
                    Meta = new Meta {
                        { "self-published", true }
                    }
                },
                Comments = Relationship.Create(new List <ResourceIdentifier <Comment> >
                {
                    ResourceIdentifier.Create(new Comment()
                    {
                        Id   = "57",
                        Type = "comments",
                        Body = "I give it a 5 out of 7"
                    })
                })
            });

            var json         = JsonConvert.SerializeObject(root, settings);
            var expectedjson = @"{
                ""data"": {
                    ""id"": ""1234"",
                    ""type"": ""articles"",
                    ""attributes"": {
                        ""title"": ""My Article""
                    },
                    ""relationships"": {
                        ""author"": {
                            ""data"": { 
                                ""id"":""333"", 
                                ""type"":""people"",
                                ""meta"":{ ""primary-author"": true}
                            },
                            ""meta"": {""self-published"": true},
                        },
                        ""comments"": {
                            ""data"": [{ 
                                ""id"":""57"", 
                                ""type"":""comments"",
                            }]
                        }
                    }
                },
                ""included"" : [
                    {
                        ""id"": ""333"",
                        ""type"": ""people"",
                        ""attributes"":{
                            ""first-name"": ""John"",
                            ""last-name"": ""Smith"",
                            ""twitter"": ""jsmi""
                        },
                        ""meta"":{ ""best-sellers"": 4}

                    },
                    {
                        ""id"": ""57"",
                        ""type"": ""comments"",
                        ""attributes"":{
                            ""body"": ""I give it a 5 out of 7""
                        }
                    }
                ]
            }";

            Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance);
        }
예제 #11
0
        public void OneDeltaEachSourcePlusStartupRecap()
        {
            var scheduler = new TestScheduler();

            //Arrange
            var sut           = new RecapsQuery();
            var notifications = new List <NamedRecap>();

            //Act
            var user    = User.Create("u1");
            var source  = Source.Create("a1", "a1");
            var cluster = Cluster.Create("c1", new[] { user });

            var rca = Relationship.Create(cluster, source);
            var rcu = Relationship.Create(cluster, user);

            var clusterSourcesStream = scheduler.CreateColdObservable(
                Notification.CreateOnNext(Delta.Create(rca, DeltaType.Added)).RecordAt(1)
                );

            var clusterUsersStream = scheduler.CreateColdObservable(
                Notification.CreateOnNext(Delta.Create(rcu, DeltaType.Added)).RecordAt(2)
                );

            var errorsStream = scheduler.CreateColdObservable(
                Notification.CreateOnNext(new ErrorPayload(source.SourceId, new Error(), "e1", "")).RecordAt(5)
                );

            sut.Run(
                new ValueOrError <User>(user), new RunTargets
            {
                FrontendNotifier = new StubIFrontendNotifier
                {
                    RecapStringRecap = (n, r) =>
                    {
                        notifications.Add(new NamedRecap {
                            Name = n, Recap = r
                        });
                    }
                },
                ErrorsInbox = new StubIErrorsInbox
                {
                    GetErrorsStream = () => errorsStream
                },
                ErrorsBacklogReader = new StubIErrorsBacklogReader()
                {
                    GetSourcesRecapIEnumerableOfSource = apps => Task.FromResult(
                        new ValueOrError <Recap>(
                            new Recap(
                                DateTime.UtcNow,
                                Enumerable.Empty <Recap.Source>())))
                },
                VisibilityPersistor = new StubIVisibilityPersistor
                {
                    GetUserSourcesString = _ => Task.FromResult((IEnumerable <Source>) new[] { source })
                },
                VisibilityPublisher = new StubIVisibilityPublisher
                {
                    GetClusterSourcesSequence = () => clusterSourcesStream,
                    GetClusterUsersSequence   = () => clusterUsersStream
                }
            });


            //Asserts

            Assert.Equal(1, notifications.Count()); //first recap is received during the subscription

            scheduler.AdvanceBy(1);
            Assert.Equal(2, notifications.Count()); //1 for initial recap, 1 for adding app, 1 for adding user

            scheduler.AdvanceBy(1);
            Assert.Equal(3, notifications.Count()); //1 for initial recap, 1 for adding app, 1 for adding user

            scheduler.AdvanceBy(1);
        }
 protected override void OnAddRelation(AddRelationship <Animal> addRelationship)
 {
     base.OnAddRelation(addRelationship);
     addRelationship(Relationship <Animal> .Create(animal => animal.Family));
 }