<commit_before>﻿using AllReady.Models;
using AllReady.ViewModels;
using AllReady.Controllers;
using Microsoft.Data.Entity;
using Microsoft.Framework.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace AllReady.UnitTests
{
    public class ActivityApiControllerTest
    {
        private static IServiceProvider _serviceProvider;
        private static bool populatedData = false;
        
        public ActivityApiControllerTest()
        {
            if (_serviceProvider == null)
            {
                var services = new ServiceCollection();

                services.AddEntityFramework()
                    .AddInMemoryStore()
                    .AddDbContext<AllReadyContext>(options => options.UseInMemoryStore());
                _serviceProvider = services.BuildServiceProvider();
            }
        }

        [Fact]
        public void GetAllActivities()
        {
            // Arrange
            ActivityApiController controller = GetActivityController();

            // Act
            var activities = new List<ActivityViewModel>(controller.Get());

            // Assert
            Assert.Equal(10, activities.Count());
        }

        [Fact]
        public void GetSingleActivity()
        {
            // Arrange
            ActivityApiController controller = GetActivityController();

            // Act
            int recordId = 5;
            var activityViewModel = controller.Get(recordId);

            // Assert
            Assert.Equal(activityViewModel.Id, recordId);
            Assert.Equal(activityViewModel.CampaignName, string.Format(TestActivityModelProvider.CampaignNameFormat, recordId));
            Assert.Equal(activityViewModel.CampaignId, recordId);
            Assert.Equal(activityViewModel.Description, string.Format(TestActivityModelProvider.ActivityDescriptionFormat, recordId));
            Assert.Equal(activityViewModel.EndDateTime, DateTime.MaxValue.ToUniversalTime());
            Assert.Equal(activityViewModel.StartDateTime, DateTime.MinValue.ToUniversalTime());
        }

        [Fact]
        public void PostSingleActivity()
        {
            // Arrange
            ActivityApiController controller = GetActivityController();

            //Act
            ActivityViewModel toPost = new ActivityViewModel()
            {
                Id = 11,
                Title = "the answer to life the universe and everything",
                CampaignId = 1,
                TenantId = 1,
                StartDateTime = DateTime.Today,
                EndDateTime = DateTime.Today.AddMonths(1),
                Location = new LocationViewModel()
                {
                    Address1 = "123 Happy Street",
                    City = "Madison",
                    State = "WI",
                    PostalCode = new PostalCodeGeo() { PostalCode = "53711" },
                    Country = "country gets ignored right now"
                }
            };

            controller.Post(toPost);
            var viewModelFromDb = controller.Get(toPost.Id);

            // Assert
            Assert.NotEqual(viewModelFromDb, toPost);
            Assert.Equal(toPost.Id, viewModelFromDb.Id);
            Assert.Equal(toPost.CampaignId, viewModelFromDb.CampaignId);
            Assert.Equal(string.Format(TestActivityModelProvider.CampaignNameFormat, toPost.CampaignId), viewModelFromDb.CampaignName);
            Assert.Null(viewModelFromDb.Description);
            Assert.Equal(toPost.EndDateTime, viewModelFromDb.EndDateTime);
            Assert.Equal(toPost.StartDateTime, viewModelFromDb.StartDateTime);
            Assert.Equal("US", viewModelFromDb.Location.Country);
            Assert.Equal(toPost.Location.Address1, viewModelFromDb.Location.Address1);
            Assert.Equal(toPost.Location.City, viewModelFromDb.Location.City);
        }

        #region Helper Methods

        private ActivityApiController GetActivityController()
        {
            var allReadyContext = _serviceProvider.GetService<AllReadyContext>();
            var allReadyDataAccess = new AllReadyDataAccessEF7(allReadyContext);
            var controller = new ActivityApiController(allReadyDataAccess, null, null);
            PopulateData(allReadyContext);
            return controller;
        }

        private void PopulateData(DbContext context)
        {
            if (!populatedData)
            {
                var activities = TestActivityModelProvider.GetActivities();

                foreach (var activity in activities)
                {
                    context.Add(activity);
                    context.Add(activity.Campaign);
                    context.Add(activity.Tenant);
                }
                context.SaveChanges();
                populatedData = true;<commit_msg>Ensure ActivityApiController tests can run in any order<commit_after>﻿using AllReady.Models;
using AllReady.ViewModels;
using AllReady.Controllers;
using Microsoft.Data.Entity;
using Microsoft.Framework.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace AllReady.UnitTests
{
    public class ActivityApiControllerTest
    {
        private static IServiceProvider _serviceProvider;
        private static bool populatedData = false;
        private static int activitiesAdded = 0;
        
        public ActivityApiControllerTest()
        {
            if (_serviceProvider == null)
            {
                var services = new ServiceCollection();

                services.AddEntityFramework()
                    .AddInMemoryStore()
                    .AddDbContext<AllReadyContext>(options => options.UseInMemoryStore());
                _serviceProvider = services.BuildServiceProvider();
            }
        }

        [Fact]
        public void GetAllActivities()
        {
            // Arrange
            ActivityApiController controller = GetActivityController();

            // Act
            var activities = new List<ActivityViewModel>(controller.Get());

            // Assert
            Assert.Equal(activitiesAdded, activities.Count());
        }

        [Fact]
        public void GetSingleActivity()
        {
            // Arrange
            ActivityApiController controller = GetActivityController();

            // Act
            int recordId = 5;
            var activityViewModel = controller.Get(recordId);

            // Assert
            Assert.Equal(activityViewModel.Id, recordId);
            Assert.Equal(activityViewModel.CampaignName, string.Format(TestActivityModelProvider.CampaignNameFormat, recordId));
            Assert.Equal(activityViewModel.CampaignId, recordId);
            Assert.Equal(activityViewModel.Description, string.Format(TestActivityModelProvider.ActivityDescriptionFormat, recordId));
            Assert.Equal(activityViewModel.EndDateTime, DateTime.MaxValue.ToUniversalTime());
            Assert.Equal(activityViewModel.StartDateTime, DateTime.MinValue.ToUniversalTime());
        }

        [Fact]
        public void PostSingleActivity()
        {
            // Arrange
            ActivityApiController controller = GetActivityController();

            //Act
            ActivityViewModel toPost = new ActivityViewModel()
            {
                Id = 11,
                Title = "the answer to life the universe and everything",
                CampaignId = 1,
                TenantId = 1,
                StartDateTime = DateTime.Today,
                EndDateTime = DateTime.Today.AddMonths(1),
                Location = new LocationViewModel()
                {
                    Address1 = "123 Happy Street",
                    City = "Madison",
                    State = "WI",
                    PostalCode = new PostalCodeGeo() { PostalCode = "53711" },
                    Country = "country gets ignored right now"
                }
            };

            controller.Post(toPost);
            activitiesAdded++;
            var viewModelFromDb = controller.Get(toPost.Id);

            // Assert
            Assert.NotEqual(viewModelFromDb, toPost);
            Assert.Equal(toPost.Id, viewModelFromDb.Id);
            Assert.Equal(toPost.CampaignId, viewModelFromDb.CampaignId);
            Assert.Equal(string.Format(TestActivityModelProvider.CampaignNameFormat, toPost.CampaignId), viewModelFromDb.CampaignName);
            Assert.Null(viewModelFromDb.Description);
            Assert.Equal(toPost.EndDateTime, viewModelFromDb.EndDateTime);
            Assert.Equal(toPost.StartDateTime, viewModelFromDb.StartDateTime);
            Assert.Equal("US", viewModelFromDb.Location.Country);
            Assert.Equal(toPost.Location.Address1, viewModelFromDb.Location.Address1);
            Assert.Equal(toPost.Location.City, viewModelFromDb.Location.City);
        }

        #region Helper Methods

        private ActivityApiController GetActivityController()
        {
            var allReadyContext = _serviceProvider.GetService<AllReadyContext>();
            var allReadyDataAccess = new AllReadyDataAccessEF7(allReadyContext);
            var controller = new ActivityApiController(allReadyDataAccess, null, null);
            PopulateData(allReadyContext);
            return controller;
        }

        private void PopulateData(DbContext context)
        {
            if (!populatedData)
            {
                var activities = TestActivityModelProvider.GetActivities();

                foreach (var activity in activities)
                {
                    context.Add(activity);
                    context.Add(activity.Campaign);
                    context.Add(activity.Tenant);
                    activitiesAdded++;
                }
                context.SaveChanges();
                populatedData = true;
{'fraction_non_alphanumeric': 0.08077118214104516, 'fraction_numerical': 0.0039573820395738205, 'mean_word_length': 1.936829558998808, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '7487878', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2210, 'n_words': 441}