コード例 #1
0
        public async Task ShouldRankMakelaars()
        {
            var getAllOffersResult = new GetAllOffersResult()
            {
                Offers = new List <Offer>()
            };

            _fundaApiClient
            .Setup(c => c.GetAllOffers(It.IsAny <OfferTypes>(), It.IsAny <string>(), CancellationToken.None, It.IsAny <Action <GetAllOffersStatus> >()))
            .Returns(Task.FromResult(getAllOffersResult));

            var request = new TopMakalaarsQuery()
            {
                Location   = "Amsterdam",
                WithGarden = true
            };
            var offers = new List <Offer>();

            offers.AddRange(CreateOffers(1, "Test 1", 2));
            offers.AddRange(CreateOffers(2, "Test 2", 20));
            offers.AddRange(CreateOffers(3, "Test 3", 13));
            offers.AddRange(CreateOffers(4, "Test 4", 40));
            getAllOffersResult.Offers = offers;

            var result = await _handler.Handle(request, CancellationToken.None);

            var makelaars = result.Makelaars.ToList();

            Assert.AreEqual(4, makelaars[0].Id);
            Assert.AreEqual(2, makelaars[1].Id);
            Assert.AreEqual(3, makelaars[2].Id);
            Assert.AreEqual(1, makelaars[3].Id);
        }
コード例 #2
0
        private void PrintTopMakelaars(TopMakalaarsQuery query, TopMakelaarsResult result)
        {
            var caption = new StringBuilder($"Top {result.Makelaars.Count()} Makelaars - {query.Location}");

            if (query.WithGarden)
            {
                caption.Append(" with Garden");
            }

            if (result.Status == TopMakelaarsResultStatus.Error)
            {
                Console.WriteLine($"Error occurred: {result.Error}");
                return;
            }

            var header = $"| {"Nr",2} | {"Name",-50} | {"Count",5} |";

            Console.WriteLine(new string('-', header.Length));
            Console.WriteLine(caption);
            Console.WriteLine(new string('-', header.Length));
            Console.WriteLine(header);
            Console.WriteLine(new string('-', header.Length));
            foreach (var makelaar in result.Makelaars)
            {
                Console.WriteLine($"| {makelaar.Rank, 2} | {makelaar.Name, -50} | {makelaar.ListingCount, 5} |");
            }
            Console.WriteLine(new string('-', header.Length));
        }
コード例 #3
0
        public async Task ShouldTriggerStatusUpdate()
        {
            var getAllOffersResult = new GetAllOffersResult()
            {
                Offers = new List <Offer>()
            };

            _fundaApiClient
            .Setup(c => c.GetAllOffers(It.IsAny <OfferTypes>(), It.IsAny <string>(), CancellationToken.None, It.IsAny <Action <GetAllOffersStatus> >()))
            .Callback <OfferTypes?, string, CancellationToken, Action <GetAllOffersStatus> >((type, searchCommand, cancellationToken, statusUpdate) =>
            {
                statusUpdate(new GetAllOffersStatus()
                {
                    CurrentOffers = 10,
                    TotalOffers   = 999
                });
            })
            .Returns(Task.FromResult(getAllOffersResult));

            var lastStatus = default(GetAllOffersStatus);
            var request    = new TopMakalaarsQuery()
            {
                Location     = "Amsterdam",
                WithGarden   = true,
                StatusUpdate = status => { lastStatus = status; }
            };

            await _handler.Handle(request, CancellationToken.None);

            Assert.AreEqual(10, lastStatus.CurrentOffers);
            Assert.AreEqual(999, lastStatus.TotalOffers);
        }
コード例 #4
0
        public async Task Run(CancellationToken cancellationToken)
        {
            var query = new TopMakalaarsQuery()
            {
                Location     = "Amsterdam",
                WithGarden   = false,
                StatusUpdate = StatusUpdated
            };
            var result = await _mediator.Send <TopMakelaarsResult>(query, cancellationToken);

            PrintTopMakelaars(query, result);

            query = new TopMakalaarsQuery()
            {
                Location     = "Amsterdam",
                WithGarden   = true,
                StatusUpdate = StatusUpdated
            };
            result = await _mediator.Send <TopMakelaarsResult>(query, cancellationToken);

            PrintTopMakelaars(query, result);
        }
コード例 #5
0
        public async Task ShouldCorrectlyBuildRequest()
        {
            var getAllOffersResult = new GetAllOffersResult()
            {
                Offers = new List <Offer>()
            };
            var lastType          = default(OfferTypes?);
            var lastSearchCommand = default(string);

            _fundaApiClient
            .Setup(c => c.GetAllOffers(It.IsAny <OfferTypes>(), It.IsAny <string>(), CancellationToken.None, It.IsAny <Action <GetAllOffersStatus> >()))
            .Callback <OfferTypes?, string, CancellationToken, Action <GetAllOffersStatus> >((type, searchCommand, cancellationToken, statusUpdate) =>
            {
                lastType          = type;
                lastSearchCommand = searchCommand;
            })
            .Returns(Task.FromResult(getAllOffersResult));

            var request = new TopMakalaarsQuery()
            {
                Location   = "Amsterdam",
                WithGarden = true
            };

            await _handler.Handle(request, CancellationToken.None);

            Assert.AreEqual(OfferTypes.Koop, lastType);
            Assert.AreEqual("/Amsterdam/tuin/", lastSearchCommand);

            request = new TopMakalaarsQuery()
            {
                Location   = "Leiden",
                WithGarden = false
            };
            await _handler.Handle(request, CancellationToken.None);

            Assert.AreEqual(OfferTypes.Koop, lastType);
            Assert.AreEqual("/Leiden/", lastSearchCommand);
        }
コード例 #6
0
        public async Task ShouldLimitMakelaarsTo10()
        {
            var getAllOffersResult = new GetAllOffersResult()
            {
                Offers = new List <Offer>()
            };

            _fundaApiClient
            .Setup(c => c.GetAllOffers(It.IsAny <OfferTypes>(), It.IsAny <string>(), CancellationToken.None, It.IsAny <Action <GetAllOffersStatus> >()))
            .Returns(Task.FromResult(getAllOffersResult));

            var request = new TopMakalaarsQuery()
            {
                Location   = "Amsterdam",
                WithGarden = true
            };
            var offers = new List <Offer>();

            offers.AddRange(CreateOffers(1, "Test 1", 2));
            offers.AddRange(CreateOffers(2, "Test 2", 20));
            offers.AddRange(CreateOffers(3, "Test 3", 13));
            offers.AddRange(CreateOffers(4, "Test 4", 14));
            offers.AddRange(CreateOffers(5, "Test 5", 15));
            offers.AddRange(CreateOffers(6, "Test 6", 16));
            offers.AddRange(CreateOffers(7, "Test 7", 17));
            offers.AddRange(CreateOffers(8, "Test 8", 18));
            offers.AddRange(CreateOffers(9, "Test 9", 19));
            offers.AddRange(CreateOffers(10, "Test 10", 10));
            offers.AddRange(CreateOffers(11, "Test 11", 11));
            offers.AddRange(CreateOffers(12, "Test 12", 12));
            getAllOffersResult.Offers = offers;

            var result = await _handler.Handle(request, CancellationToken.None);

            var makelaars = result.Makelaars.ToList();

            Assert.AreEqual(10, makelaars.Count);
        }
コード例 #7
0
        public async Task ShouldHandleApiError()
        {
            var getAllOffersResult = new GetAllOffersResult()
            {
                Offers = new List <Offer>()
            };
            var error = new Exception();

            _fundaApiClient
            .Setup(c => c.GetAllOffers(It.IsAny <OfferTypes>(), It.IsAny <string>(), CancellationToken.None, It.IsAny <Action <GetAllOffersStatus> >()))
            .Throws(error);

            var request = new TopMakalaarsQuery()
            {
                Location   = "Amsterdam",
                WithGarden = true
            };

            var result = await _handler.Handle(request, CancellationToken.None);

            Assert.AreEqual(TopMakelaarsResultStatus.Error, result.Status);
            Assert.AreEqual(error, result.Error);
        }