<commit_before>﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AlphaDev.Core.Data.Sql.Tests.Integration
{
    using System.Data.SqlClient;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;

    using AlphaDev.Core.Data.Entties;
    using AlphaDev.Core.Data.Sql.Contexts;

    using FluentAssertions;

    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Infrastructure;
    using Microsoft.Extensions.Configuration;

    using Xunit;

    public class BlogContextTests : IDisposable
    {
        private readonly string connectionString;

        public BlogContextTests()
        {
            var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("connectionstrings.json", false, true).Build();

            connectionString = configuration.GetConnectionString("integration");

            var optionsBuilder = new DbContextOptionsBuilder();
            var options = optionsBuilder.UseSqlServer(connectionString).Options;

            using (var context = new DbContext(options))
            {
                context.Database.EnsureCreated();
            }
        }

        private void SeedBlogs()
        {
            var optionsBuilder = new DbContextOptionsBuilder();
            var options = optionsBuilder.UseSqlServer(connectionString).Options;

            using (var context = new DbContext(options))
            {
                Enumerable.Range(1, 10).ToList().ForEach(
                    i => context.Database.ExecuteSqlCommand($"INSERT INTO Blogs(Content, Title) VALUES('test {i}', 'Title {i}')"));
            }
        }

        [Fact]
        public void BlogsShouldReturnBlogs()
        {
            using (var context = GetBlogContext())
            {
                var blogs = context.Blogs;

                var blogsDictionary = blogs.Select(
                    blog => blog.GetType().GetProperties()
                        .ToDictionary(x => x.Name, x => x.GetGetMethod().Invoke(blog, null)));

                GetTable("Blogs").ShouldBeEquivalentTo(blogsDictionary);
            }
        }

        [Fact]
        public void BlogsShouldDeleteBlogs()
        {
            using (var context = GetBlogContext())
            {
                context.Blogs.RemoveRange(context.Blogs);
                context.SaveChanges();

                GetTable("Blogs").Should().BeEmpty();
            }
        }

        [Fact]
        public void BlogsShouldUpdateBlogs()
        {
            using (var context = GetBlogContext())
            {
                var targetBlog = context.Blogs.First();

                targetBlog.Title = "Updated Title";
                targetBlog.Content = "Updated Content";

                context.SaveChanges();

                GetTable("Blogs").First().ShouldBeEquivalentTo(
                    targetBlog.GetType().GetProperties().ToDictionary(
                        x => x.Name,
                        x => x.GetGetMethod().Invoke(targetBlog, null)));
            }
        }

        [Fact]
        public void BlogsShouldAddBlogWithCurrentDate()
        {
            using (var context = GetBlogContext())
            {
                var blog = new Blog
                               {
                                   Content = string.Empty,
                                   Title = string.Empty
                               };

                context.Blogs.Add(blog);

                context.SaveChanges();

                GetTable("Blogs").Last()["Created"].Should().BeOfType<DateTime>().Which.Should()
                    .BeCloseTo(DateTime.UtcNow, 1000).And.Subject.ShouldBeEquivalentTo(blog.Created);
            }
        }

        [Fact]
        public void BlogsShouldAddBlog()
        {
            using (var context = GetBlogContext())
            {
                var blog = new Blog
                               {
                                   Content = string.Empty,
                                   Title = string.Empty
                               };

                context.Blogs.Add(blog);

                context.SaveChanges();

                GetTable("Blogs").Should().HaveCount(11);
            }
        }

        private BlogContext GetBlogContext()
        {
            var context = new BlogContext(connectionString);
            context.Database.Migrate();

            SeedBlogs();

            return context;
        }

        private IEnumerable<IDictionary<string, object>> GetTable(string tableName)
        {
            using (var connection = new SqlConnection(connectionString))
            {
                using (var command = new SqlCommand($"SELECT * FROM {tableName}", connection))
                {
                    connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        yield return Enumerable.Range(0, reader.FieldCount)
                            .ToDictionary(reader.GetName, reader.GetValue);
                    }
                }
            }
        }

        public void Dispose()
        {
            var optionsBuilder = new DbContextOptionsBuilder();
            var options = optionsBuilder.UseSqlServer(connectionString).Options;

            using (var context = new DbContext(options))
            {
                context.Database.EnsureDeleted();
            }
        }
    }
}<commit_msg>use null when retrieving null data from database<commit_after>﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AlphaDev.Core.Data.Sql.Tests.Integration
{
    using System.Data.SqlClient;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;

    using AlphaDev.Core.Data.Entties;
    using AlphaDev.Core.Data.Sql.Contexts;

    using FluentAssertions;

    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Infrastructure;
    using Microsoft.Extensions.Configuration;

    using Xunit;

    public class BlogContextTests : IDisposable
    {
        private readonly string connectionString;

        public BlogContextTests()
        {
            var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("connectionstrings.json", false, true).Build();

            connectionString = configuration.GetConnectionString("integration");

            var optionsBuilder = new DbContextOptionsBuilder();
            var options = optionsBuilder.UseSqlServer(connectionString).Options;

            using (var context = new DbContext(options))
            {
                context.Database.EnsureCreated();
            }
        }

        private void SeedBlogs()
        {
            var optionsBuilder = new DbContextOptionsBuilder();
            var options = optionsBuilder.UseSqlServer(connectionString).Options;

            using (var context = new DbContext(options))
            {
                Enumerable.Range(1, 10).ToList().ForEach(
                    i => context.Database.ExecuteSqlCommand($"INSERT INTO Blogs(Content, Title) VALUES('test {i}', 'Title {i}')"));
            }
        }

        [Fact]
        public void BlogsShouldReturnBlogs()
        {
            using (var context = GetBlogContext())
            {
                var blogs = context.Blogs;

                var blogsDictionary = blogs.Select(
                    blog => blog.GetType().GetProperties()
                        .ToDictionary(x => x.Name, x => x.GetGetMethod().Invoke(blog, null)));

                GetTable("Blogs").ShouldBeEquivalentTo(blogsDictionary);
            }
        }

        [Fact]
        public void BlogsShouldDeleteBlogs()
        {
            using (var context = GetBlogContext())
            {
                context.Blogs.RemoveRange(context.Blogs);
                context.SaveChanges();

                GetTable("Blogs").Should().BeEmpty();
            }
        }

        [Fact]
        public void BlogsShouldUpdateBlogs()
        {
            using (var context = GetBlogContext())
            {
                var targetBlog = context.Blogs.First();

                targetBlog.Title = "Updated Title";
                targetBlog.Content = "Updated Content";

                context.SaveChanges();

                GetTable("Blogs").First().ShouldBeEquivalentTo(
                    targetBlog.GetType().GetProperties().ToDictionary(
                        x => x.Name,
                        x => x.GetGetMethod().Invoke(targetBlog, null)));
            }
        }

        [Fact]
        public void BlogsShouldAddBlogWithCurrentDate()
        {
            using (var context = GetBlogContext())
            {
                var blog = new Blog
                               {
                                   Content = string.Empty,
                                   Title = string.Empty
                               };

                context.Blogs.Add(blog);

                context.SaveChanges();

                GetTable("Blogs").Last()["Created"].Should().BeOfType<DateTime>().Which.Should()
                    .BeCloseTo(DateTime.UtcNow, 1000).And.Subject.ShouldBeEquivalentTo(blog.Created);
            }
        }

        [Fact]
        public void BlogsShouldAddBlog()
        {
            using (var context = GetBlogContext())
            {
                var blog = new Blog
                               {
                                   Content = string.Empty,
                                   Title = string.Empty
                               };

                context.Blogs.Add(blog);

                context.SaveChanges();

                GetTable("Blogs").Should().HaveCount(11);
            }
        }

        private BlogContext GetBlogContext()
        {
            var context = new BlogContext(connectionString);
            context.Database.Migrate();

            SeedBlogs();

            return context;
        }

        private IEnumerable<IDictionary<string, object>> GetTable(string tableName)
        {
            using (var connection = new SqlConnection(connectionString))
            {
                using (var command = new SqlCommand($"SELECT * FROM {tableName}", connection))
                {
                    connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        yield return Enumerable.Range(0, reader.FieldCount)
                            .ToDictionary(reader.GetName, i =>
                                {
                                    var value = reader.GetValue(i);
                                    return value == DBNull.Value ? null : value;
                                });
                    }
                }
            }
        }

        public void Dispose()
        {
            var optionsBuilder = new DbContextOptionsBuilder();
            var options = optionsBuilder.UseSqlServer(connectionString).Options;

            using (var context = new DbContext(options))
            {
                context.Database.EnsureDeleted();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09297829334739433, 'fraction_numerical': 0.0017576236927673784, 'mean_word_length': 1.4130619168787109, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '832934', 'n_tokens_mistral': 2764, 'n_tokens_neox': 2541, 'n_words': 505}