Пример #1
0
        public void WrapAll_SmallCollection_WrappingIsDeferred()
        {
            IEnumerable <Person> People()
            {
                yield return(_person1);

                yield return(_person2);

                throw new Exception();
            }

            IWrapper wrapper    = WrapperBuilder.WithServer("http://api.example.com").Build();
            var      collection = wrapper.WrapAll <Person>(People());

            collection.Meta["count"] = 3;

            using (var e = collection.Data.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    Assert.Fail("Collection too small");
                }
                if (!e.MoveNext())
                {
                    Assert.Fail("Collection too small");
                }
                Assert.Throws <Exception>(() => e.MoveNext());
            }
        }
Пример #2
0
        public void WrapAll_EmptyCollection_CountZero()
        {
            Person[] people = new Person[] {};

            IWrapper wrapper    = WrapperBuilder.WithServer("http://api.example.com").Build();
            var      collection = wrapper.WrapAll <Person>(people);

            collection.Meta["count"] = 0;

            Assert.That(collection.Data.Count(), Is.EqualTo(0));
        }
Пример #3
0
        public void WrapAll_SmallCollection_CountMatches()
        {
            Person[] people = { _person1, _person2 };

            IWrapper wrapper    = WrapperBuilder.WithServer("http://api.example.com").Build();
            var      collection = wrapper.WrapAll <Person>(people);

            collection.Meta["count"] = people.Length;

            Assert.That(collection.Data.Count(), Is.EqualTo(2));
        }
Пример #4
0
        public void WithDefaultConfig_AllPolicyAsserts_NoException()
        {
            IWrapper wrapper = WrapperBuilder.WithServer(HttpsExampleCom).WithDefaultConfig(p =>
            {
                p.CamelCasedNames();
                p.HideDefaults();
                p.HyphenCasedTypes();
                p.PluralTypes();
                p.WithCanonicalLinks();
                p.WithSelfLinks();
            }).Build();

            Assert.That(wrapper.ServerPath, Is.EqualTo(HttpsExampleCom));
        }
Пример #5
0
        public void WrapAll_SmallCollection_TypeAndIdMatches()
        {
            Person[] people = { _person1, _person2 };

            IWrapper wrapper    = WrapperBuilder.WithServer("http://api.example.com").Build();
            var      collection = wrapper.WrapAll <Person>(people);

            var e0 = collection.Data.ElementAt(0);
            var e1 = collection.Data.ElementAt(1);

            Assert.That(e0.Type, Is.EqualTo("people"));
            Assert.That(e0.Id, Is.EqualTo("1"));
            Assert.That(e1.Type, Is.EqualTo("people"));
            Assert.That(e1.Id, Is.EqualTo("2"));
        }
Пример #6
0
        public void Errors_OneError_IsWrapped()
        {
            var wrapper  = WrapperBuilder.WithServer("").Build();
            var apiError = new ApiError("internal", "400", "detail");

            apiError.Source["pointer"] = "it/is/here";
            apiError.Source["header"]  = "and/here";
            var envelope = wrapper.Errors(new[] { apiError });

            var e0 = envelope.Errors.ElementAt(0);

            Assert.That(e0.Code, Is.EqualTo("internal"));
            Assert.That(e0.Status, Is.EqualTo("400"));
            Assert.That(e0.Detail, Is.EqualTo("detail"));
            Assert.That(e0.Source, Contains.Key("pointer"));
            Assert.That(e0.Source["header"], Is.EqualTo("and/here"));
        }
Пример #7
0
        public void Wrapper_WrapPerson_IdAndTypeSet(Person p)
        {
            Assume.That(p, Is.Not.Null);
            IWrapper wrapper = WrapperBuilder
                               .WithServer("http://api.example.com")
                               .WithDefaultConfig(null)
                               .WithTypeConfig <Person>(x => x.WithId("Id"))
                               .Build();

            var rep = wrapper.Wrap(p);
            Resource <Person> rp = rep.Data;

            var expectedType = typeof(Person).Name.ToPlural().ToHyphenCase(); // people

            Assert.That(rp.Type, Is.EqualTo(expectedType));
            var expectedId = p.Id.ToString();

            Assert.That(rp.Id, Is.EqualTo(expectedId));
        }
Пример #8
0
        public TodoController(TodoContext context, LinkGenerator linkGenerator)
        {
            _context       = context;
            _linkGenerator = linkGenerator;
            var path = ""; // TODO

            _wrapper = WrapperBuilder
                       .WithServer(path)
                       .WithDefaultConfig(p => p.HyphenCasedTypes())
                       .WithTypeConfig <TodoItem>(p => p.WithType("todo"))
                       .Build();

            if (_context.TodoItems.Count() == 0)
            {
                // Create a new TodoItems if collection is empty,
                // which means you can't delete all TodoItems.
                _context.TodoItems.Add(new TodoItem {
                    Name = "Item1"
                });
                _context.SaveChanges();
            }
        }
Пример #9
0
        public void WrapperBuilder_MinimalBuilder_ServerPathSet()
        {
            var wrapper = (Wrapper)WrapperBuilder.WithServer(HttpsExampleCom).Build();

            Assert.That(wrapper.ServerPath, Is.EqualTo(HttpsExampleCom));
        }
Пример #10
0
        public void Errors_NoErrors_Exception()
        {
            var wrapper = WrapperBuilder.WithServer("").Build();

            Assert.Throws <ArgumentException>(() => wrapper.Errors(new ApiError[0]));
        }