<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;

using Kirkin.Mapping;
using Kirkin.Mapping.Data;

using Xunit;

namespace Kirkin.Tests.Mapping
{
    public class DataRecordMapperTests
    {
        private static readonly string ConnectionString = new SqlConnectionStringBuilder {
            DataSource = "MENZIES",
            InitialCatalog = "DemoStud2008Daily",
            IntegratedSecurity = true
        }.ToString();

        [Fact]
        public void PersonTest()
        {
            if (!Environment.MachineName.Equals("BABUSHKA", StringComparison.OrdinalIgnoreCase)) {
                return;
            }

            var stubs = new List<PersonStub>();

            using (var cn = new SqlConnection(ConnectionString))
            using (var cmd = new SqlCommand("SELECT TOP 10 * FROM Person ORDER BY PersonID", cn))
            {
                cn.Open();

                using (var reader = cmd.ExecuteReader())
                {
                    var config = new DataRecordToObjectMapperConfig<PersonStub>(reader);
                    var mapper = Mapper.CreateMapper(config);

                    while (reader.Read())
                    {
                        var person = mapper.Map(reader, new PersonStub());

                        stubs.Add(person);
                    }
                }
            }

            Assert.NotEmpty(stubs);
            Assert.True(stubs.All(s => !string.IsNullOrEmpty(s.DisplayName)), "All DisplayNames expected to be non-empty.");
            Assert.True(stubs.All(s => s.personID != 0), "All personIDs expected to be non-zero.");

            Debug.Print("Done.");
        }

        [Fact]
        public void SubstituteDBNull()
        {
            if (!Environment.MachineName.Equals("BABUSHKA", StringComparison.OrdinalIgnoreCase)) {
                return;
            }

            var nonNullableStubs = new List<PersonStub>();
            var nullableStubs = new List<NullablePersonStub>();

            using (var cn = new SqlConnection(ConnectionString))
            using (var cmd = new SqlCommand("SELECT TOP 10 NULL AS PersonID, DisplayName FROM Person ORDER BY PersonID", cn))
            {
                cn.Open();

                using (var reader = cmd.ExecuteReader())
                {
                    var nonNullableMapper = Mapper.CreateMapper(new DataRecordToObjectMapperConfig<PersonStub>(reader));
                    var nullableMapper = Mapper.CreateMapper(new DataRecordToObjectMapperConfig<NullablePersonStub>(reader));

                    while (reader.Read())
                    {
                        var nonNullablePerson = nonNullableMapper.Map(reader, new PersonStub { personID = 1 });

                        nonNullableStubs.Add(nonNullablePerson);

                        var nullablePerson = nullableMapper.Map(reader, new NullablePersonStub { PersonID = 1 });

                        nullableStubs.Add(nullablePerson);
                    }
                }
            }

            Assert.NotEmpty(nonNullableStubs);
            Assert.NotEmpty(nullableStubs);
            Assert.True(nonNullableStubs.All(s => s.personID == 0), "All personIDs expected to be zero.");
            Assert.True(nullableStubs.All(s => !s.PersonID.HasValue), "All PersonIDs expected to be NULL.");

            Debug.Print("Done.");
        }

        sealed class PersonStub
        {
            public int personID { get; set; } // Wrong case on purpose to test defaults.
            public string DisplayName { get; set; }
        }<commit_msg>DataRecordMapperTests: default(T) -> T? conversion bug test<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;

using Kirkin.Mapping;
using Kirkin.Mapping.Data;

using Xunit;

namespace Kirkin.Tests.Mapping
{
    public class DataRecordMapperTests
    {
        private static readonly string ConnectionString = new SqlConnectionStringBuilder {
            DataSource = "MENZIES",
            InitialCatalog = "DemoStud2008Daily",
            IntegratedSecurity = true
        }.ToString();

        [Fact]
        public void PersonTest()
        {
            if (!Environment.MachineName.Equals("BABUSHKA", StringComparison.OrdinalIgnoreCase)) {
                return;
            }

            var stubs = new List<PersonStub>();

            using (var cn = new SqlConnection(ConnectionString))
            using (var cmd = new SqlCommand("SELECT TOP 10 * FROM Person ORDER BY PersonID", cn))
            {
                cn.Open();

                using (var reader = cmd.ExecuteReader())
                {
                    var config = new DataRecordToObjectMapperConfig<PersonStub>(reader);
                    var mapper = Mapper.CreateMapper(config);

                    while (reader.Read())
                    {
                        var person = mapper.Map(reader, new PersonStub());

                        stubs.Add(person);
                    }
                }
            }

            Assert.NotEmpty(stubs);
            Assert.True(stubs.All(s => !string.IsNullOrEmpty(s.DisplayName)), "All DisplayNames expected to be non-empty.");
            Assert.True(stubs.All(s => s.personID != 0), "All personIDs expected to be non-zero.");

            Debug.Print("Done.");
        }

        [Fact]
        public void SubstituteDBNull()
        {
            if (!Environment.MachineName.Equals("BABUSHKA", StringComparison.OrdinalIgnoreCase)) {
                return;
            }

            var nonNullableStubs = new List<PersonStub>();
            var nullableStubs = new List<NullablePersonStub>();

            using (var cn = new SqlConnection(ConnectionString))
            using (var cmd = new SqlCommand("SELECT TOP 10 NULL AS PersonID, DisplayName FROM Person ORDER BY PersonID", cn))
            {
                cn.Open();

                using (var reader = cmd.ExecuteReader())
                {
                    var nonNullableMapper = Mapper.CreateMapper(new DataRecordToObjectMapperConfig<PersonStub>(reader));
                    var nullableMapper = Mapper.CreateMapper(new DataRecordToObjectMapperConfig<NullablePersonStub>(reader));

                    while (reader.Read())
                    {
                        var nonNullablePerson = nonNullableMapper.Map(reader, new PersonStub { personID = 1 });

                        nonNullableStubs.Add(nonNullablePerson);

                        var nullablePerson = nullableMapper.Map(reader, new NullablePersonStub { PersonID = 1 });

                        nullableStubs.Add(nullablePerson);
                    }
                }
            }

            Assert.NotEmpty(nonNullableStubs);
            Assert.NotEmpty(nullableStubs);
            Assert.True(nonNullableStubs.All(s => s.personID == 0), "All personIDs expected to be zero.");
            Assert.True(nullableStubs.All(s => !s.PersonID.HasValue), "All PersonIDs expected to be NULL.");

            Debug.Print("Done.");
        }

        [Fact]
        public void MapDefaultOfTToNullableT()
        {
            if (!Environment.MachineName.Equals("BABUSHKA", StringComparison.OrdinalIgnoreCase)) {
                return;
            }

            var stubs = new List<NullablePersonStub>();

            using (var cn = new SqlConnection(ConnectionString))
            using (var cmd = new SqlCommand("SELECT TOP 10 0 AS PersonID, DisplayName FROM Person ORDER BY PersonID", cn))
            {
                cn.Open();

                using (var reader = cmd.ExecuteReader())
                {
                    var mapper = Mapper.CreateMapper(new DataRecordToObjectMapperConfig<NullablePersonStub>(reader));

                    while (reader.Read())
                    {
                        var nullablePerson = mapper.Map(reader, new NullablePersonStub { PersonID = 1 });

                        stubs.Add(nullablePerson);
                    }
                }
            }

            Assert.NotEmpty(stubs);
            Assert.True(stubs.All(s => s.PersonID.HasValue && s.PersonID.Value == 0), "All PersonIDs expected to be NULL.");

            Debug.Print("Done.");
        }

        sealed class PersonStub
        {
            public int personID { get; set; } // Wrong case on purpose to test defaults.
            public string DisplayName { get; set; }
        }
{'fraction_non_alphanumeric': 0.09466275659824047, 'fraction_numerical': 0.003401759530791789, 'mean_word_length': 1.7239616613418531, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, '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': '3132037', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2047, 'n_words': 496}