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 IsEventEditableForTest()
        {
            var tcs = new TaskCompletionSource<Event>();
            string userId = "u1";
            string userId2 = "u2";
            Guid e1Id = new Guid("00000000-0000-0000-0000-000000000000");
            Guid e2Id = new Guid("00000000-0000-0000-0000-000000000001");
            Event e1 = new Event { Id = e1Id, Title = "New Event", OthersCanEdit = true };
            Event e2 = new Event { Id = e2Id, Title = "New Event2", OthersCanEdit = false, OrganizerId = userId };
            tcs.SetResult(e1);

            _eventRepository.Setup(mock => mock.GetEventInfo(e1Id)).Returns(tcs.Task);
            var task = await _userService.IsEventEditableFor(e1Id, userId);
            _eventRepository.Verify(mock => mock.GetEventInfo(e1Id), Times.Once());
            Assert.AreEqual(true, task, "This event should be editable, but the method IsEventEditable returns false.");

            var tcs2 = new TaskCompletionSource<Event>();
            tcs2.SetResult(e2);
            _eventRepository.Setup(mock => mock.GetEventInfo(e2Id)).Returns(tcs2.Task);
            var task2 = await _userService.IsEventEditableFor(e2Id, userId);
            _eventRepository.Verify(mock => mock.GetEventInfo(e2Id), Times.Once(), "Method GetEventInfo was not called or was called more than once (or its parameters were wrong).");
            Assert.AreEqual(true, task2, "This event should be editable, but the method IsEventEditable returns false.");

            _eventRepository.Setup(mock => mock.GetEventInfo(e2Id)).Returns(tcs2.Task);
            var task3 = await _userService.IsEventEditableFor(e2Id, userId2);
            _eventRepository.Verify(mock => mock.GetEventInfo(e2Id), Times.Exactly(2), "Method GetEventInfo was not called or was called more than once (or its parameters were wrong).");
            Assert.AreEqual(false, task3, "This event should not be editable, but the method IsEventEditable returns true.");
        }
        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 GetEventsCreatedByTest()
        {
            var tcs = new TaskCompletionSource<List<Event>>();
            string userId = "1";
            Event e1 = new Event { Title = "New Event", OrganizerId = userId };
            Event e2 = new Event { Title = "New Event2", OrganizerId = userId };
            List<Event> events = new List<Event> { e1, e2 };
            tcs.SetResult(events);

            _eventRepository.Setup(mock => mock.GetByOrganizer(userId)).Returns(tcs.Task);
            var task = await _userService.GetEventsCreatedBy(userId);
            _eventRepository.Verify(mock => mock.GetByOrganizer(userId), Times.Once(), "Method GetByOrganizer was not called or was called more than once (or its parameters were wrong).");
            CollectionAssert.AreEqual(events, task.ToList(), "The list of events returned by GetEventsCreatedBy was different than expected");
        }
        public async Task CreateEventAsyncTest()
        {
            var tcs = new TaskCompletionSource<Event>();
            string userId = "u1";
            Event e1 = new Event { Title = "New Event" };
            tcs.SetResult(e1);

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

            Assert.AreEqual(e1.Title, task.Title, "Title of the event is different that of the one added to DB.");
            Assert.AreEqual(userId, task.OrganizerId, "Organizer of the event is different that of the one added to DB.");
        }
        public async Task DisableEventAsyncTest()
        {
            var tcs = new TaskCompletionSource<Event>();
            Guid eId = new Guid();
            Event e1 = new Event { Id = eId, Title = "New Event" };
            tcs.SetResult(e1);

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

            Assert.AreEqual(e1.Disabled, true, "Event was not disabled");
        }
        public async Task GetDatesWithVotesTest()
        {
            var tcs = new TaskCompletionSource<List<TimeSlot>>();
            Guid eId = new Guid();
            Event e = new Event { Id = eId };
            TimeSlot t1 = new TimeSlot { Id = new Guid("00000000-0000-0000-0000-000000000000"), DateTime = DateTime.Now, Event = e, EventId = eId };
            TimeSlot t2 = new TimeSlot { Id = new Guid("00000000-0000-0000-0000-000000000001"), DateTime = DateTime.Now, Event = e, EventId = eId };
            List<TimeSlot> timeslots = new List<TimeSlot> { t1, t2 };
            tcs.SetResult(timeslots);

            _timeSlotRepository.Setup(mock => mock.GetTimeSlotWithVotesByEvent(eId)).Returns(tcs.Task);
            var task = await _eventDetailsService.GetDatesWithVotes(eId);
            _timeSlotRepository.Verify(mock => mock.GetTimeSlotWithVotesByEvent(eId), Times.Once(), "Method GetTimeSlotWithVotesByEvent was not called or was called more than once (or its parameters were wrong).");
            CollectionAssert.AreEqual(task.ToList(), timeslots, "Method GetDatesWithVotes returned different timeslots than expected");
        }
        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");
        }
        /// <summary>
        /// Update event and stores it to database in async operation. Places and TimeSlots which were removed from this Event are deleted from database with its votes 
        /// </summary>
        /// <param name="e">Event to update</param>
        /// <returns>Updated event</returns>
        public async Task<Event> UpdateEventAsync(Event e)
        {
            var fullEvent = await GetFullEventAsync(e.Id);
            e.CreatedOn = fullEvent.CreatedOn;
            e.OrganizerId = fullEvent.OrganizerId;

            foreach (var place in e.Places)
            {
                place.EventId = e.Id;
            }

            foreach (var timeSlot in e.TimeSlots)
            {
                timeSlot.EventId = e.Id;
            }

            var placeOrphans = fullEvent.Places.Select(pl => pl.Id).Except(e.Places.Select(p => p.Id)).ToList();
            var timeSlotOrphans = fullEvent.TimeSlots.Select(ts => ts.Id).Except(e.TimeSlots.Select(t => t.Id)).ToList();
            await DeleteEventPlaces(placeOrphans);
            await DeleteEventTimeSlots(timeSlotOrphans);
            return await _eventRepository.AddOrUpdate(e);
        }
 /// <summary>
 /// Create new event and stores it to database in async operation.
 /// </summary>
 /// <param name="e">Event to create. Event should not have filled Id</param>
 /// <param name="userId">Id of user who is creating this event</param>
 /// <returns>Created event with generated Id</returns>
 public async Task<Event> CreateEventAsync(Event e, string userId)
 {
     e.OrganizerId = userId;
     e.CreatedOn = DateTime.Now;
     return await _eventRepository.AddOrUpdate(e);
 }
Пример #12
0
        /// <summary>
        ///     Asynchronous method that adds or update en existed event based on event entity.
        /// </summary>
        /// <param name="ev">Event entity with related timeslots and places</param>
        /// <returns>Returns created event entity</returns>
        public virtual async Task<Event> AddOrUpdate(Event ev)
        {
            using (var context = EventPlannerContext.Get())
            {
                var entity = Mapper.Map<EventEntity>(ev);

                foreach (var t in entity.TimeSlots)
                {
                    context.Entry(t).State = (t.Id == default(Guid) ? EntityState.Added : EntityState.Modified);
                }

                foreach (var p in entity.Places)
                {
                    context.Entry(p).State = (p.Id == default(Guid) ? EntityState.Added : EntityState.Modified);
                }

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

                return Mapper.Map<Event>(entity);
            }
        }
        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");

        }