Ejemplo n.º 1
0
        public async Task LoadSingleKeyNull()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);

            // act
            Task <string> Verify() => loader.LoadAsync(default(string) !, CancellationToken.None);

            // assert
            await Assert.ThrowsAsync <ArgumentNullException>("key", Verify);
        }
Ejemplo n.º 2
0
        public void DisposeNoExceptionNobatchingAndCaching()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(batchScheduler, fetch);

            // act
            Action verify = () => loader.Dispose();

            // assert
            Assert.Null(Record.Exception(verify));
        }
Ejemplo n.º 3
0
        public async Task LoadParamsKeysNull()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);

            // act
            Task <IReadOnlyList <string> > Verify() => loader.LoadAsync(default(string[]) !);

            // assert
            await Assert.ThrowsAsync <ArgumentNullException>("keys", (Func <Task <IReadOnlyList <string> > >) Verify);
        }
Ejemplo n.º 4
0
        public void DisposeNoExceptionNoBatchingAndCaching()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);

            // act
            void Verify() => loader.Dispose();

            // assert
            Assert.Null(Record.Exception(Verify));
        }
        public void SetNoException()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);
            var key            = "Foo";

            // act
            void Verify() => loader.Set(key, null !);

            // assert
            Assert.Throws <ArgumentNullException>(Verify);
        }
        public void SetKeyNull()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);
            var value          = "Bar";

            // act
            void Verify() => loader.Set(null !, value);

            // assert
            Assert.Throws <ArgumentNullException>("key", (Action)Verify);
        }
Ejemplo n.º 7
0
        public async Task IDataLoaderLoadCollectionKeysNull()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var           batchScheduler             = new ManualBatchScheduler();
            IDataLoader   loader = new DataLoader <string, string>(batchScheduler, fetch);
            List <object> keys   = null;

            // act
            Func <Task <IReadOnlyList <object> > > verify = () => loader.LoadAsync(keys);

            // assert
            await Assert.ThrowsAsync <ArgumentNullException>("keys", verify);
        }
Ejemplo n.º 8
0
        public async Task LoadSingleKeyNull()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var    batchScheduler = new ManualBatchScheduler();
            var    loader         = new DataLoader <string, string>(batchScheduler, fetch);
            string key            = null;

            // act
            Func <Task <string> > verify = () => loader.LoadAsync(key);

            // assert
            await Assert.ThrowsAsync <ArgumentNullException>("key", verify);
        }
Ejemplo n.º 9
0
        public async Task IDataLoaderLoadCollectionZeroKeys()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var         batchScheduler = new ManualBatchScheduler();
            IDataLoader loader         = new DataLoader <string, string>(batchScheduler, fetch);
            var         keys           = new List <object>();

            // act
            IReadOnlyList <object> loadResult = await loader.LoadAsync(keys);

            // assert
            Assert.Empty(loadResult);
        }
        public void IDataLoaderLoadCollectionNoException()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers
                                                       .CreateFetch <string, string>("Bar");
            var         batchScheduler = new ManualBatchScheduler();
            IDataLoader loader         = new DataLoader <string, string>(batchScheduler, fetch);

            // act
            Action verify = () => loader.LoadAsync(new List <object>());

            // assert
            Assert.Null(Record.Exception(verify));
        }
Ejemplo n.º 11
0
        public void IDataLoaderRemoveNoException()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var         batchScheduler = new ManualBatchScheduler();
            IDataLoader loader         = new DataLoader <string, string>(batchScheduler, fetch);
            object      key            = "Foo";

            // act
            Action verify = () => loader.Remove(key);

            // assert
            Assert.Null(Record.Exception(verify));
        }
        public void LoadSingleNoException()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers
                                                       .CreateFetch <string, string>("Bar");
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(batchScheduler, fetch);
            var key            = "Foo";

            // act
            Action verify = () => loader.LoadAsync(key);

            // assert
            Assert.Null(Record.Exception(verify));
        }
        public void SetNoException()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var    batchScheduler = new ManualBatchScheduler();
            var    loader         = new DataLoader <string, string>(batchScheduler, fetch);
            var    key            = "Foo";
            string value          = null;

            // act
            Action verify = () => loader.Set(key, value);

            // assert
            Assert.Null(Record.Exception(verify));
        }
Ejemplo n.º 14
0
        public void SetKeyNull()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var    batchScheduler = new ManualBatchScheduler();
            var    loader         = new DataLoader <string, string>(batchScheduler, fetch);
            string key            = null;
            var    value          = Task.FromResult("Foo");

            // act
            Action verify = () => loader.Set(key, value);

            // assert
            Assert.Throws <ArgumentNullException>("key", verify);
        }
Ejemplo n.º 15
0
        public void IDataLoaderSetNoException()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var           batchScheduler             = new ManualBatchScheduler();
            IDataLoader   loader = new DataLoader <string, string>(batchScheduler, fetch);
            object        key    = "Foo";
            Task <object> value  = Task.FromResult <object>("Bar");

            // act
            Action verify = () => loader.Set(key, value);

            // assert
            Assert.Null(Record.Exception(verify));
        }
Ejemplo n.º 16
0
        public void IDataLoaderSetValueNull()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var           batchScheduler             = new ManualBatchScheduler();
            IDataLoader   loader = new DataLoader <string, string>(batchScheduler, fetch);
            object        key    = "Foo";
            Task <object> value  = null;

            // act
            Action verify = () => loader.Set(key, value);

            // assert
            Assert.Throws <ArgumentNullException>("value", verify);
        }
Ejemplo n.º 17
0
        public void IDataLoaderRemoveKeyNull()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var         batchScheduler = new ManualBatchScheduler();
            IDataLoader loader         = new DataLoader <string, string>(batchScheduler, fetch);
            object      key            = null;

            loader.Set("Foo", Task.FromResult((object)"Bar"));

            // act
            Action verify = () => loader.Remove(key);

            // assert
            Assert.Throws <ArgumentNullException>("key", verify);
        }
Ejemplo n.º 18
0
        public async Task LoadCollectionZeroKeys()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(batchScheduler, fetch);
            var keys           = new List <string>();

            // act
            Task <IReadOnlyList <string> > loadResult = loader.LoadAsync(keys);

            // assert
            await Task.Delay(25);

            batchScheduler.Dispatch();
            Assert.Empty(await loadResult);
        }
Ejemplo n.º 19
0
        public async Task LoadSingleResult()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = CreateFetch <string, string>("Bar");
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);
            var key            = "Foo";

            // act
            Task <string> loadResult = loader.LoadAsync(key);

            // assert
            await Task.Delay(25);

            batchScheduler.Dispatch();
            (await loadResult).MatchSnapshot();
        }
        public async Task SetNewCacheEntry()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);
            var key            = "Foo";
            var value          = "Bar";

            // act
            loader.Set(key, value);

            // assert
            var loadResult = await loader.LoadAsync(key).ConfigureAwait(false);

            Assert.Equal(value, loadResult);
        }
Ejemplo n.º 21
0
        public async Task IDataLoaderLoadParamsResult()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers
                                                       .CreateFetch <string, string>("Bar");
            var         batchScheduler = new ManualBatchScheduler();
            IDataLoader loader         = new DataLoader <string, string>(batchScheduler, fetch);
            var         keys           = new object[] { "Foo" };

            // act
            Task <IReadOnlyList <object> > loadResult = loader.LoadAsync(keys);

            // assert
            await Task.Delay(25);

            batchScheduler.Dispatch();
            (await loadResult).MatchSnapshot();
        }
Ejemplo n.º 22
0
        public async Task IDataLoaderLoadSingleResult()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers
                                                       .CreateFetch <string, string>("Bar");
            var         batchScheduler = new ManualBatchScheduler();
            IDataLoader loader         = new DataLoader <string, string>(batchScheduler, fetch);
            object      key            = "Foo";

            // act
            Task <object> loadResult = loader.LoadAsync(key);

            // assert
            await Task.Delay(25);

            batchScheduler.Dispatch();
            (await loadResult).MatchSnapshot();
        }
Ejemplo n.º 23
0
        public async Task LoadCollectionResult()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = CreateFetch <string, string>("Bar");
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);
            var keys           = new List <string> {
                "Foo"
            };

            // act
            Task <IReadOnlyList <string> > loadResult = loader.LoadAsync(keys, CancellationToken.None);

            batchScheduler.Dispatch();

            // assert
            (await loadResult).MatchSnapshot();
        }
Ejemplo n.º 24
0
        public async Task LoadKeyAndValueCountNotEquel()
        {
            // arrange
            InvalidOperationException expectedException = Errors
                                                          .CreateKeysAndValuesMustMatch(4, 3);
            Result <string> expectedResult = "Bar";
            var             repository     = new Dictionary <string, string>
            {
                { "Foo", "Bar" },
                { "Bar", "Baz" },
                { "Baz", "Foo" }
            };
            FetchDataDelegate <string, string> fetch =
                async(keys, cancellationToken) =>
            {
                var values = new List <Result <string> >();

                foreach (var key in keys)
                {
                    if (repository.ContainsKey(key))
                    {
                        values.Add(repository[key]);
                    }
                }

                return(await Task.FromResult(values));
            };
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(batchScheduler, fetch);
            var requestKeys    = new [] { "Foo", "Bar", "Baz", "Qux" };

            // act
            Func <Task> verify = () => loader.LoadAsync(requestKeys);

            // assert
            Task <InvalidOperationException> task = Assert
                                                    .ThrowsAsync <InvalidOperationException>(verify);

            batchScheduler.Dispatch();

            InvalidOperationException actualException = await task;

            Assert.Equal(expectedException.Message, actualException.Message);
        }
        public async Task SetTwice()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);
            var key            = "Foo";
            var first          = "Bar";
            var second         = "Baz";

            // act
            loader.Set(key, first);
            loader.Set(key, second);

            // assert
            var loadResult = await loader.LoadAsync(key).ConfigureAwait(false);

            Assert.Equal(first, loadResult);
        }
Ejemplo n.º 26
0
        public void IDataLoaderSetNewCacheEntry()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var cache          = new TaskCache(10);
            var options        = new DataLoaderOptions <string>
            {
                Cache = cache
            };
            IDataLoader   loader = new DataLoader <string, string>(batchScheduler, fetch, options);
            object        key    = "Foo";
            Task <object> value  = Task.FromResult <object>("Bar");

            // act
            loader.Set(key, value);

            // assert
            Assert.Equal(1, cache.Usage);
        }
Ejemplo n.º 27
0
        public void ClearAllEntries()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var cache          = new TaskCache(10);
            var options        = new DataLoaderOptions {
                Cache = cache
            };
            var loader = new DataLoader <string, string>(fetch, batchScheduler, options);

            loader.Set("Foo", Task.FromResult("Bar"));
            loader.Set("Bar", Task.FromResult("Baz"));

            // act
            loader.Clear();

            // assert
            Assert.Equal(0, cache.Usage);
        }
Ejemplo n.º 28
0
        public async Task LoadSingleErrorResult()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var loader         = new DataLoader <string, string>(fetch, batchScheduler);
            var key            = "Foo";

            // act
            Task <string> Verify() => loader.LoadAsync(key, CancellationToken.None);

            // assert
            Task <InvalidOperationException> task = Assert
                                                    .ThrowsAsync <InvalidOperationException>((Func <Task <string> >)Verify);

            await Task.Delay(25);

            batchScheduler.Dispatch();

            await task;
        }
Ejemplo n.º 29
0
        public void IDataLoaderRemoveEntry()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var batchScheduler = new ManualBatchScheduler();
            var cache          = new TaskCache(10);
            var options        = new DataLoaderOptions <string>
            {
                Cache = cache
            };
            IDataLoader loader = new DataLoader <string, string>(batchScheduler, fetch, options);
            object      key    = "Foo";

            loader.Set(key, Task.FromResult((object)"Bar"));

            // act
            loader.Remove(key);

            // assert
            Assert.Equal(0, cache.Usage);
        }
Ejemplo n.º 30
0
        public async Task IDataLoaderLoadSingleErrorResult()
        {
            // arrange
            FetchDataDelegate <string, string> fetch = TestHelpers.CreateFetch <string, string>();
            var         batchScheduler = new ManualBatchScheduler();
            IDataLoader loader         = new DataLoader <string, string>(batchScheduler, fetch);
            object      key            = "Foo";

            // act
            Func <Task <object> > verify = () => loader.LoadAsync(key);

            // assert
            Task <InvalidOperationException> task = Assert
                                                    .ThrowsAsync <InvalidOperationException>(verify);

            await Task.Delay(25);

            batchScheduler.Dispatch();

            await task;
        }