public async Task GetTotalNumberOfVotersForEventTest()
        {
            Guid eId = new Guid();
            Event e = new Event { Id = eId };
            VoteForPlace vp1 = new VoteForPlace { Id = new Guid("00000000-0000-0000-0000-000000000000"), UserId = "0" };
            VoteForPlace vp2 = new VoteForPlace { Id = new Guid("00000000-0000-0000-0000-000000000001"), UserId = "1" };
            Place p1 = new Place { Id = new Guid("00000000-0000-0000-0000-000000000000"), Event = e, EventId = eId, VotesForPlace = new List<VoteForPlace> { vp1, vp2 } };
            Place p2 = new Place { Id = new Guid("00000000-0000-0000-0000-000000000001"), Event = e, EventId = eId };
            List<Place> places = new List<Place> { p1, p2 };
            VoteForDate vd1 = new VoteForDate { Id = new Guid("00000000-0000-0000-0000-000000000000"), UserId = "1" };
            VoteForDate vd2 = new VoteForDate { Id = new Guid("00000000-0000-0000-0000-000000000001"), UserId = "2" };
            TimeSlot t1 = new TimeSlot { Id = new Guid("00000000-0000-0000-0000-000000000000"), Event = e, EventId = eId, VotesForDate = new List<VoteForDate> { vd1, vd2 } };
            TimeSlot t2 = new TimeSlot { Id = new Guid("00000000-0000-0000-0000-000000000001"), Event = e, EventId = eId };
            List<TimeSlot> timeslots = new List<TimeSlot> { t1, t2 };
            var tcs1 = new TaskCompletionSource<IList<Place>>();
            tcs1.SetResult(places);
            var tcs2 = new TaskCompletionSource<IList<TimeSlot>>();
            tcs2.SetResult(timeslots);

            _eventDetailsService.Setup(mock => mock.GetPlacesWithVotes(eId)).Returns(tcs1.Task);
            _eventDetailsService.Setup(mock => mock.GetDatesWithVotes(eId)).Returns(tcs2.Task);
            var task = await _votingService.GetTotalNumberOfVotersForEvent(eId);
            _eventDetailsService.Verify(mock => mock.GetPlacesWithVotes(eId), Times.Once(), "Method GetPlacesWithVotes was not called or was called more than once (or its parameters were wrong).");
            _eventDetailsService.Verify(mock => mock.GetDatesWithVotes(eId), Times.Once(), "Method GetPlacesWithVotes was not called or was called more than once (or its parameters were wrong).");

            Assert.AreEqual(3, task, "Returned number of voters is different to the actual value");

        }
        public async Task UpdateEventAsyncTest()
        {
            var tcs = new TaskCompletionSource<Event>();
            Guid eId = new Guid();
            Event e1 = new Event { Id = eId, Title = "New Event" };
            tcs.SetResult(e1);


            Place p = new Place { Id = new Guid() };
            TimeSlot t = new TimeSlot { Id = new Guid(), DateTime = DateTime.Now };
            Event e2 = new Event { Id = eId, Title = "New title", Places = new List<Place> { p }, TimeSlots = new List<TimeSlot> { t } };
            var tcs2 = new TaskCompletionSource<Event>();
            tcs2.SetResult(e2);

            _eventRepository.Setup(mock => mock.GetFullEvent(eId)).Returns(tcs.Task);
            _eventRepository.Setup(mock => mock.AddOrUpdate(e2)).Returns(tcs2.Task);
            var task = await _eventManagementService.UpdateEventAsync(e2);


            _eventRepository.Verify(mock => mock.GetFullEvent(eId), Times.Once(), "Method GetFullEvent was not called or was called more than once (or its parameters were wrong).");
            _eventRepository.Verify(mock => mock.AddOrUpdate(e2), Times.Once(), "Method AddOrUpdate was not called or was called more than once (or its parameters were wrong).");



            Assert.AreEqual(e2.Title, task.Title, "Title of the event was not updated.");
            Assert.AreEqual(task.Places.ToList()[0], p, "List of places was not updated.");
            Assert.AreEqual(task.TimeSlots.ToList()[0], t, "List of timeslots was not updated.");
        }
        public async Task GetPlacesWithVotesTest()
        {
            var tcs = new TaskCompletionSource<List<Place>>();
            Guid eId = new Guid();
            Event e = new Event { Id = eId };
            Place p1 = new Place { Id = new Guid("00000000-0000-0000-0000-000000000000"), EventId = eId, Event = e };
            Place p2 = new Place { Id = new Guid("00000000-0000-0000-0000-000000000001"), EventId = eId, Event = e };
            List<Place> places = new List<Place> { p1, p2 };
            tcs.SetResult(places);

            _placeRepository.Setup(mock => mock.GetPlaceWithVotesByEvent(eId)).Returns(tcs.Task);
            var task = await _eventDetailsService.GetPlacesWithVotes(eId);
            _placeRepository.Verify((mock => mock.GetPlaceWithVotesByEvent(eId)), Times.Once(), "Method GetPlaceWithVotesByEvent was not called or was called more than once (or its parameters were wrong).");
            CollectionAssert.AreEqual(task.ToList(), places, "Method GetPlacesWithVotes returned different Places than expected");
        }
        public async Task GetVotesForPlaceAsyncTest()
        {
            Guid eId = new Guid();
            Event e = new Event { Id = eId };
            Place p1 = new Place { Id = new Guid("00000000-0000-0000-0000-000000000001"), EventId = eId, Event = e };
            Place p2 = new Place { Id = new Guid("00000000-0000-0000-0000-000000000002"), EventId = eId, Event = e };
            VoteForPlace vp1 = new VoteForPlace { Id = new Guid("00000000-0000-0000-0000-000000000000"), PlaceId = p1.Id, Place = p1 };
            VoteForPlace vp2 = new VoteForPlace { Id = new Guid("00000000-0000-0000-0000-000000000002"), PlaceId = p1.Id, Place = p1 };
            VoteForPlace vp3 = new VoteForPlace { Id = new Guid("00000000-0000-0000-0000-000000000003"), PlaceId = p2.Id, Place = p2 };
            var tcs = new TaskCompletionSource<List<VoteForPlace>>();
            tcs.SetResult(new List<VoteForPlace> { vp1, vp2, vp3 });
            _voteForPlaceRepository.Setup(mock => mock.GetAll()).Returns(tcs.Task);
            var task = await _votingService.GetVotesForPlaceAsync(eId, p1.Id);
            _voteForPlaceRepository.Verify(mock => mock.GetAll(), Times.Once(), "Method GetAll was not called or was called more than once (or its parameters were wrong).");

            CollectionAssert.AreEqual(new List<VoteForPlace> { vp1, vp2 }, task.ToList(), "Returned list of votes is not correct");
        }
Пример #5
0
        /// <summary>
        ///     Asynchronous method that adds or update existed place based on place entity.
        /// </summary>
        /// <param name="place">Place entity</param>
        /// <returns>Returns created place entity</returns>
        public async Task<Place> AddOrUpdate(Place place)
        {
            using (var context = EventPlannerContext.Get())
            {
                var entity = Mapper.Map<PlaceEntity>(place);

                context.Places.AddOrUpdate(entity);
                await context.SaveChangesAsync();

                return Mapper.Map<Place>(entity);
            }
        }
        public async Task SubmitVoteForPlaceTest()
        {
            Place p1 = new Place { Id = new Guid("00000000-0000-0000-0000-000000000001") };
            VoteForPlace vp1 = new VoteForPlace { Id = new Guid("00000000-0000-0000-0000-000000000000"), PlaceId = p1.Id, Place = p1 };
            var tcs = new TaskCompletionSource<VoteForPlace>();
            tcs.SetResult(vp1);
            _voteForPlaceRepository.Setup(mock => mock.AddOrUpdate(vp1)).Returns(tcs.Task);
            var task = await _votingService.SubmitVoteForPlace(vp1);
            _voteForPlaceRepository.Verify(mock => mock.AddOrUpdate(vp1), Times.Once(), "Method AddOrUpdate was not called or was called more than once (or its parameters were wrong).");

            Assert.AreEqual(task, vp1, "Vote was not submitted correctly");
        }
        public async Task GetFullEventAsyncTest()
        {
            var tcs = new TaskCompletionSource<Event>();
            Place p = new Place { Id = new Guid() };
            TimeSlot t = new TimeSlot { Id = new Guid(), DateTime = DateTime.Now };
            Guid eId = new Guid();
            Event e1 = new Event { Id = eId, Title = "New Event", Places = new List<Place> { p }, TimeSlots = new List<TimeSlot> { t } };
            tcs.SetResult(e1);

            _eventRepository.Setup(mock => mock.GetFullEvent(eId)).Returns(tcs.Task);
            var task = await _eventManagementService.GetFullEventAsync(eId);
            _eventRepository.Verify(mock => mock.GetFullEvent(eId), Times.Once(), "Method GetFullEvent was not called or was called more than once (or its parameters were wrong).");

            Assert.AreEqual(task, e1, "GetFullEvent did not return the expected event.");
            Assert.AreEqual(task.Places.ToList()[0], p, "GetFullEvent returned event with wrong list of places.");
            Assert.AreEqual(task.TimeSlots.ToList()[0], t, "GetFullEvent returned event with wrong list of timeslots");

        }