Example #1
0
        public async Task Execute_StarWarsGetFriends_Test()
        {
            // arrange
            CancellationToken ct = new CancellationTokenSource(20_000).Token;

            using IWebHost host = TestServerHelper.CreateServer(
                      _ => { },
                      out var port);
            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddStarWarsGetFriendsClient()
            .ConfigureHttpClient(
                c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"))
            .ConfigureWebSocketClient(
                c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));

            IServiceProvider          services = serviceCollection.BuildServiceProvider();
            IStarWarsGetFriendsClient client   =
                services.GetRequiredService <IStarWarsGetFriendsClient>();

            // act
            IOperationResult <IGetHeroResult> result = await client.GetHero.ExecuteAsync(ct);

            // assert
            Assert.Equal("R2-D2", result.Data?.Hero?.Name);
            Assert.Collection(
                result.Data !.Hero !.Friends !.Nodes !,
                item => Assert.Equal("Luke Skywalker", item?.Name),
                item => Assert.Equal("Han Solo", item?.Name),
                item => Assert.Equal("Leia Organa", item?.Name));
        }
        public async Task Execute_StarWarsUnionList_Test()
        {
            // arrange
            using var cts = new CancellationTokenSource(20_000);

            using IWebHost host = TestServerHelper.CreateServer(
                      _ => { },
                      out var port);
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddHttpClient(
                StarWarsUnionListClient.ClientName,
                c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"));
            serviceCollection.AddWebSocketClient(
                StarWarsUnionListClient.ClientName,
                c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));
            serviceCollection.AddStarWarsUnionListClient();
            IServiceProvider        services = serviceCollection.BuildServiceProvider();
            StarWarsUnionListClient client   = services.GetRequiredService <StarWarsUnionListClient>();

            // act
            var result = await client.SearchHero.ExecuteAsync(cts.Token);

            // assert
            result.EnsureNoErrors();
            result.Data.MatchSnapshot();
        }
        public async Task Execute_StarWarsIntrospection_Test()
        {
            // arrange
            CancellationToken ct = new CancellationTokenSource(20_000).Token;

            using IWebHost host = TestServerHelper.CreateServer(
                      _ => { },
                      out var port);
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddHttpClient(
                StarWarsIntrospectionClient.ClientName,
                c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"));
            serviceCollection.AddWebSocketClient(
                StarWarsIntrospectionClient.ClientName,
                c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));
            serviceCollection.AddStarWarsIntrospectionClient();
            IServiceProvider            services = serviceCollection.BuildServiceProvider();
            StarWarsIntrospectionClient client   = services.GetRequiredService <StarWarsIntrospectionClient>();

            // act
            IOperationResult <IIntrospectionQueryResult> result =
                await client.IntrospectionQuery.ExecuteAsync(ct);

            // assert
            result.MatchSnapshot();
        }
Example #4
0
        public void Execute_MultiProfile_Test()
        {
            // arrange
            using IWebHost host = TestServerHelper.CreateServer(
                      _ => { },
                      out var port);
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddHttpClient(
                MultiProfileClient.ClientName,
                c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"));
            serviceCollection.AddWebSocketClient(
                MultiProfileClient.ClientName,
                c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));

            // act
            serviceCollection.AddMultiProfileClient(
                profile: MultiProfileClientProfileKind.Default);

            IServiceProvider   services = serviceCollection.BuildServiceProvider();
            MultiProfileClient client   = services.GetRequiredService <MultiProfileClient>();

            // assert
            Assert.NotNull(client);
        }
        public async Task Execute_AnyScalarDefaultSerialization_Test()
        {
            // arrange
            using var cts       = new CancellationTokenSource(20_000);
            using IWebHost host = TestServerHelper.CreateServer(
                      builder =>
            {
                builder.AddTypeExtension <QueryResolvers>();
            },
                      out var port);
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddHttpClient(
                AnyScalarDefaultSerializationClient.ClientName,
                c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"));
            serviceCollection.AddWebSocketClient(
                AnyScalarDefaultSerializationClient.ClientName,
                c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));
            serviceCollection.AddAnyScalarDefaultSerializationClient();
            IServiceProvider services = serviceCollection.BuildServiceProvider();
            AnyScalarDefaultSerializationClient client =
                services.GetRequiredService <AnyScalarDefaultSerializationClient>();

            // act
            IOperationResult <IGetJsonResult> result = await client.GetJson.ExecuteAsync(cts.Token);

            // assert
            Assert.Empty(result.Errors);
            result.Data?.Json.RootElement.ToString().MatchSnapshot();
        }
Example #6
0
        public async Task Watch_CacheFirst_StarWarsGetHero_Test()
        {
            // arrange
            CancellationToken ct = new CancellationTokenSource(20_000).Token;

            using IWebHost host = TestServerHelper.CreateServer(
                      _ => { },
                      out var port);
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddStarWarsGetHeroClient()
            .ConfigureHttpClient(
                c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"))
            .ConfigureWebSocketClient(
                c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));

            IServiceProvider      services = serviceCollection.BuildServiceProvider();
            StarWarsGetHeroClient client   = services.GetRequiredService <StarWarsGetHeroClient>();

            // act
            await client.GetHero.ExecuteAsync(ct);

            string?     name    = null;
            IDisposable session =
                client.GetHero
                .Watch(ExecutionStrategy.CacheFirst)
                .Subscribe(result =>
            {
                name = result.Data?.Hero?.Name;
            });

            while (name is null && !ct.IsCancellationRequested)
            {
                await Task.Delay(10, ct);
            }

            session.Dispose();

            // assert
            Assert.Equal("R2-D2", name);
        }
Example #7
0
        public async Task Update_Once()
        {
            // arrange
            CancellationToken ct = new CancellationTokenSource(20_000).Token;

            using IWebHost host = TestServerHelper.CreateServer(
                      _ => { },
                      out var port);
            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddStarWarsGetHeroClient()
            .ConfigureHttpClient(
                c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"))
            .ConfigureWebSocketClient(
                c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));

            IServiceProvider      services = serviceCollection.BuildServiceProvider();
            StarWarsGetHeroClient client   = services.GetRequiredService <StarWarsGetHeroClient>();

            await client.GetHero.ExecuteAsync(ct);

            // act
            var count = 0;

            using IDisposable session =
                      client.GetHero
                      .Watch(ExecutionStrategy.CacheAndNetwork)
                      .Subscribe(_ =>
            {
                count++;
            });

            await Task.Delay(1000, ct);

            // assert
            Assert.Equal(1, count);
        }
Example #8
0
        public async Task Watch_Interact_With_Persistence()
        {
            string fileName         = Path.GetTempFileName();
            string connectionString = "Data Source=" + fileName;

            File.Delete(fileName);

            try
            {
                {
                    // arrange
                    using var cts       = new CancellationTokenSource(100_20_000);
                    using IWebHost host = TestServerHelper.CreateServer(
                              _ => { },
                              out var port);
                    var serviceCollection = new ServiceCollection();

                    serviceCollection
                    .AddStarWarsGetHeroClient()
                    .ConfigureHttpClient(
                        c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"))
                    .ConfigureWebSocketClient(
                        c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"))
                    .AddSQLitePersistence(connectionString);

                    await using var services = serviceCollection.BuildServiceProvider();
                    await services.GetRequiredService <SQLitePersistence>().InitializeAsync();

                    var client        = services.GetRequiredService <StarWarsGetHeroClient>();
                    var storeAccessor =
                        services.GetRequiredService <StarWarsGetHeroClientStoreAccessor>();
                    var entityId = new EntityId("Droid", "2001");
                    await Task.Delay(250, cts.Token);

                    // act
                    await client.GetHero.ExecuteAsync(cts.Token);

                    string?     name    = null;
                    IDisposable session =
                        client.GetHero
                        .Watch(ExecutionStrategy.CacheFirst)
                        .Subscribe(result => name = result.Data?.Hero?.Name);

                    while (name is null && !cts.Token.IsCancellationRequested)
                    {
                        await Task.Delay(10, cts.Token);
                    }

                    var name1 = name;
                    name = null;

                    storeAccessor.EntityStore.Update(s =>
                    {
                        if (s.CurrentSnapshot.TryGetEntity(entityId, out DroidEntity? entity))
                        {
                            entity = new DroidEntity("NewName");
                            s.SetEntity(entityId, entity);
                        }
                    });

                    while (name is null && !cts.Token.IsCancellationRequested)
                    {
                        await Task.Delay(10, cts.Token);
                    }

                    var name2 = name;
                    name = null;


                    session.Dispose();

                    // assert
                    Assert.Equal("R2-D2", name1);
                    Assert.Equal("NewName", name2);

                    await Task.Delay(500, cts.Token);
                }
                {
                    // arrange
                    using var cts       = new CancellationTokenSource(100_20_000);
                    using IWebHost host = TestServerHelper.CreateServer(
                              _ => { },
                              out var port);
                    var serviceCollection = new ServiceCollection();

                    serviceCollection
                    .AddStarWarsGetHeroClient()
                    .ConfigureHttpClient(
                        c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"))
                    .ConfigureWebSocketClient(
                        c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"))
                    .AddSQLitePersistence(connectionString);

                    await using var services = serviceCollection.BuildServiceProvider();
                    await services.GetRequiredService <SQLitePersistence>().InitializeAsync();

                    var client = services.GetRequiredService <StarWarsGetHeroClient>();

                    // act
                    string?name = null;
                    client.GetHero
                    .Watch(ExecutionStrategy.CacheFirst)
                    .Subscribe(result =>
                    {
                        name = result.Data !.Hero !.Name;
                    });

                    while (name is null && !cts.Token.IsCancellationRequested)
                    {
                        await Task.Delay(10, cts.Token);
                    }

                    // assert
                    Assert.Equal("NewName", name);
                }
            }
            finally
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
            }
        }