<commit_before>﻿using System;
using AllReady.Areas.Admin.ViewModels.Task;
using AllReady.Areas.Admin.ViewModels.Validators.Task;
using AllReady.Features.Events;
using AllReady.Models;
using AllReady.Providers;
using MediatR;
using Moq;
using Xunit;

namespace AllReady.UnitTest.Areas.Admin.ViewModels.Validators.Task
{
    public class TaskEditViewModelValidatorShould
    {
        [Fact]
        public async System.Threading.Tasks.Task SendEventByIdQueryWithCorrectEventId()
        {
            var model = new EditViewModel { EventId = 1 };
            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(new Event { Campaign = new Campaign() });

            var sut = new TaskEditViewModelValidator(mediator.Object);
            await sut.Validate(model);

            mediator.Verify(x => x.SendAsync(It.Is<EventByEventIdQuery>(y => y.EventId == model.EventId)), Times.Once);
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnCorrectErrorWhenEndDateTimeIsLessThanStartDateTime()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event {
                Campaign = new Campaign { TimeZoneId = "UTC" },
                TimeZoneId = "UTC"};
            
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);

            var validator = new TaskEditViewModelValidator(mockMediator.Object);
            var errors = await validator.Validate(new EditViewModel
            {
                StartDateTime = now,
                EndDateTime = now.AddMinutes(-30)
            });

            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "EndDateTime").Value, "End date cannot be earlier than the start date");
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnCorrectErrorWhenModelsStartDateTimeIsLessThanParentEventStartDate()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC", }, TimeZoneId = "UTC", StartDateTime = now.AddDays(1), EndDateTime = now.AddDays(-1) };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);
            
            var validator = new TaskEditViewModelValidator(mockMediator.Object);
            var errors = await validator.Validate(new EditViewModel());

            Assert.True(errors.Exists(x => x.Key.Equals("StartDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "StartDateTime").Value, "Start date cannot be earlier than the event start date " + @event.StartDateTime.ToString("d"));
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnCorrectErrorWhenModelsEndDateTimeIsGreaterThanParentEventStartDate()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC", },
                StartDateTime = now,
                EndDateTime = now.AddDays(-1) };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);

            var validator = new TaskEditViewModelValidator(mediator.Object);
            var errors = await validator.Validate(new EditViewModel
            {
                EndDateTime = now.AddDays(-1).AddMinutes(15)
            });

            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "EndDateTime").Value, "End date cannot be later than the event end date " + @event.EndDateTime.ToString("d"));
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnCorrectErrorWhenItineraryTaskWithStartAndEndDatesNotOnSameDay()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC", }, StartDateTime = now, EndDateTime = now.AddDays(1), EventType = EventType.Itinerary };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);

            var validator = new TaskEditViewModelValidator(mediator.Object);
            var errors = await validator.Validate(new EditViewModel
            {
                StartDateTime = now.AddDays(-1),
                EndDateTime = now
            });

            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "EndDateTime").Value, "For itinerary events the task end date must occur on the same day as the start date. Tasks cannot span multiple days");
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnNoErrorForNonItineraryTaskWhenModelsDatesAreValid()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC" }, TimeZoneId = "UTC", StartDateTime = now, EndDateTime = now.AddDays(1), EventType = EventType.Rally };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);

            
            var validator = new TaskEditViewModelValidator(mockMediator.Object);
            var errors = await validator.Validate(new EditViewModel
            {
                StartDateTime = now.AddMinutes(30),
                EndDateTime = now.AddDays(1).AddMinutes(-30)
            });

            Assert.True(errors.Count == 0);
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnNoErrorForItineraryTaskWhenModelsDatesAreValid()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC", }, StartDateTime = now, EndDateTime = now.AddDays(1), TimeZoneId = "UTC", EventType = EventType.Itinerary };

            var mockMediator = new Mock<IMediator>();<commit_msg>Fix for unit test that failed at certain times of day<commit_after>﻿using System;
using AllReady.Areas.Admin.ViewModels.Task;
using AllReady.Areas.Admin.ViewModels.Validators.Task;
using AllReady.Features.Events;
using AllReady.Models;
using AllReady.Providers;
using MediatR;
using Moq;
using Xunit;

namespace AllReady.UnitTest.Areas.Admin.ViewModels.Validators.Task
{
    public class TaskEditViewModelValidatorShould
    {
        [Fact]
        public async System.Threading.Tasks.Task SendEventByIdQueryWithCorrectEventId()
        {
            var model = new EditViewModel { EventId = 1 };
            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(new Event { Campaign = new Campaign() });

            var sut = new TaskEditViewModelValidator(mediator.Object);
            await sut.Validate(model);

            mediator.Verify(x => x.SendAsync(It.Is<EventByEventIdQuery>(y => y.EventId == model.EventId)), Times.Once);
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnCorrectErrorWhenEndDateTimeIsLessThanStartDateTime()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event {
                Campaign = new Campaign { TimeZoneId = "UTC" },
                TimeZoneId = "UTC"};
            
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);

            var validator = new TaskEditViewModelValidator(mockMediator.Object);
            var errors = await validator.Validate(new EditViewModel
            {
                StartDateTime = now,
                EndDateTime = now.AddMinutes(-30)
            });

            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "EndDateTime").Value, "End date cannot be earlier than the start date");
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnCorrectErrorWhenModelsStartDateTimeIsLessThanParentEventStartDate()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC", }, TimeZoneId = "UTC", StartDateTime = now.AddDays(1), EndDateTime = now.AddDays(-1) };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);
            
            var validator = new TaskEditViewModelValidator(mockMediator.Object);
            var errors = await validator.Validate(new EditViewModel());

            Assert.True(errors.Exists(x => x.Key.Equals("StartDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "StartDateTime").Value, "Start date cannot be earlier than the event start date " + @event.StartDateTime.ToString("d"));
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnCorrectErrorWhenModelsEndDateTimeIsGreaterThanParentEventStartDate()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC", },
                StartDateTime = now,
                EndDateTime = now.AddDays(-1) };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);

            var validator = new TaskEditViewModelValidator(mediator.Object);
            var errors = await validator.Validate(new EditViewModel
            {
                EndDateTime = now.AddDays(-1).AddMinutes(15)
            });

            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "EndDateTime").Value, "End date cannot be later than the event end date " + @event.EndDateTime.ToString("d"));
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnCorrectErrorWhenItineraryTaskWithStartAndEndDatesNotOnSameDay()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC", }, StartDateTime = now, EndDateTime = now.AddDays(1), EventType = EventType.Itinerary };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);

            var validator = new TaskEditViewModelValidator(mediator.Object);
            var errors = await validator.Validate(new EditViewModel
            {
                StartDateTime = now.AddDays(-1),
                EndDateTime = now
            });

            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
            Assert.Equal(errors.Find(x => x.Key == "EndDateTime").Value, "For itinerary events the task end date must occur on the same day as the start date. Tasks cannot span multiple days");
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnNoErrorForNonItineraryTaskWhenModelsDatesAreValid()
        {
            var now = DateTimeOffset.Now;

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC" }, TimeZoneId = "UTC", StartDateTime = now, EndDateTime = now.AddDays(1), EventType = EventType.Rally };

            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(x => x.SendAsync(It.IsAny<EventByEventIdQuery>())).ReturnsAsync(@event);

            
            var validator = new TaskEditViewModelValidator(mockMediator.Object);
            var errors = await validator.Validate(new EditViewModel
            {
                StartDateTime = now.AddMinutes(30),
                EndDateTime = now.AddDays(1).AddMinutes(-30)
            });

            Assert.True(errors.Count == 0);
        }

        [Fact]
        public async System.Threading.Tasks.Task ReturnNoErrorForItineraryTaskWhenModelsDatesAreValid()
        {
            var now = new DateTimeOffset(2016, 10, 4, 8, 0, 0, TimeSpan.FromSeconds(0));

            var @event = new Event { Campaign = new Campaign { TimeZoneId = "UTC", }, StartDateTime = now, EndDateTime = now.AddDays(1), TimeZoneId = "UTC", EventType = EventType.Itinerary };

            var mockMediator = new Mock<IMediator>();
{'fraction_non_alphanumeric': 0.11053677932405567, 'fraction_numerical': 0.00389662027833002, 'mean_word_length': 2.4913936701832315, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5049755', 'n_tokens_mistral': 3475, 'n_tokens_neox': 3315, 'n_words': 742}