<commit_before>﻿using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;
using IdeaWeb.Data;
using IdeaWeb.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using NUnit.Framework;

namespace IdeaWeb.Integration.Tests.Controllers
{
    [TestFixture]
    public class HomeControllerTests
    {
        HttpClient _client;

        [OneTimeSetUp]
        public void OneTimeSetup()
        {
            var webHostBuilder = new WebHostBuilder()
                .UseContentRoot(CalculateRelativeContentRootPath())
                .UseEnvironment("Development")
                .UseStartup<TestStartup>();

            var server = new TestServer(webHostBuilder);

            _client = server.CreateClient();

            SeedDatabase(server);
        }

        private static void SeedDatabase(TestServer server)
        {
            var context = server.Host.Services.GetService<IdeaContext>();

            for (int i = 1; i <= 10; i++)
            {
                context.Add(new Idea
                {
                    Name = $"Idea name {i}",
                    Description = $"Description {i}",
                    Rating = i % 3 + 1
                });
            }
            context.SaveChanges();
        }

        /// <summary>
        /// With MVC, we need to set the content root to the project being tested
        /// so that the Razor views can be found
        /// </summary>
        string CalculateRelativeContentRootPath() =>
            Path.Combine(PlatformServices.Default.Application.ApplicationBasePath,
            @"..\..\..\..\IdeaWeb");

        [Test]
        public async Task HomePageContainsIdeas()
        {
            var response = await _client.GetAsync("/");
            response.EnsureSuccessStatusCode();

            var html = await response.Content.ReadAsStringAsync();

            var doc = new HtmlDocument();
            doc.LoadHtml(html);

            var ideas = doc.DocumentNode
                           .Descendants("span")
                           .Where(s => s.Attributes["class"].Value == "name");

            Assert.That(ideas.Any(), Is.True);<commit_msg>Commenting the integration tests<commit_after>﻿using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;
using IdeaWeb.Data;
using IdeaWeb.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using NUnit.Framework;

namespace IdeaWeb.Integration.Tests.Controllers
{
    [TestFixture]
    public class HomeControllerTests
    {
        HttpClient _client;

        [OneTimeSetUp]
        public void OneTimeSetup()
        {
            // ==================================================================
            // The configuration sets the content root so the Razor views will
            // be found, sets the environment and specifies a TestStartup to use
            // ==================================================================
            var webHostBuilder = new WebHostBuilder()
                .UseContentRoot(CalculateRelativeContentRootPath())
                .UseEnvironment("Development")
                .UseStartup<TestStartup>();

            // ================================================================
            // ================================================================
            var server = new TestServer(webHostBuilder);

            _client = server.CreateClient();

            SeedDatabase(server);
        }

        private static void SeedDatabase(TestServer server)
        {
            var context = server.Host.Services.GetService<IdeaContext>();

            for (int i = 1; i <= 10; i++)
            {
                context.Add(new Idea
                {
                    Name = $"Idea name {i}",
                    Description = $"Description {i}",
                    Rating = i % 3 + 1
                });
            }
            context.SaveChanges();
        }

        // =======================================================================
        // With MVC, we need to set the content root to the project being tested
        // so that the Razor views can be found
        // =======================================================================
        string CalculateRelativeContentRootPath() =>
            Path.Combine(PlatformServices.Default.Application.ApplicationBasePath,
            @"..\..\..\..\IdeaWeb");

        [Test]
        public async Task HomePageContainsIdeas()
        {
            // =======================================================================
            // Using the client created from the TestHost, you can make calls that
            // use all of the routing and configuration without going through a
            // browser or the network
            // =======================================================================
            var response = await _client.GetAsync("/");
            response.EnsureSuccessStatusCode();

            var html = await response.Content.ReadAsStringAsync();

            // =======================================================================
            // You can use the HtmlAgilityPack to parse the HTML for testing
            // =======================================================================
            var doc = new HtmlDocument();
            doc.LoadHtml(html);

            var ideas = doc.DocumentNode
                           .Descendants("span")
                           .Where(s => s.Attributes["class"].Value == "name");

            Assert.That(ideas.Any(), Is.True);
{'fraction_non_alphanumeric': 0.20132969655642688, 'fraction_numerical': 0.0017047391749062393, 'mean_word_length': 2.066910611604809, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5215357', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1264, 'n_words': 330}