Skip to content

Commit 52e20a5

Browse files
committed
Test IDataReader abstraction
1 parent a140cd1 commit 52e20a5

File tree

9 files changed

+221
-2
lines changed

9 files changed

+221
-2
lines changed

DataAbstractions.Dapper.sln

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.28010.2041
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30413.136
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataAbstractions.Dapper", "src\DataAbstractions.Dapper\DataAbstractions.Dapper.csproj", "{146BC2F1-9A1C-4A68-B15F-BF42FE61228B}"
77
EndProject
8+
Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "DataAbstractions.Dapper.TestDb", "src\DataAbstractions.Dapper.TestDb\DataAbstractions.Dapper.TestDb.sqlproj", "{FCD154A2-591E-4706-851F-7E5A248E3902}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataAbstractions.Dapper.Tests", "src\DataAbstractions.Dapper.Tests\DataAbstractions.Dapper.Tests.csproj", "{98069EEB-35C9-4F96-B014-25E872545887}"
11+
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1014
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +19,16 @@ Global
1519
{146BC2F1-9A1C-4A68-B15F-BF42FE61228B}.Debug|Any CPU.Build.0 = Debug|Any CPU
1620
{146BC2F1-9A1C-4A68-B15F-BF42FE61228B}.Release|Any CPU.ActiveCfg = Release|Any CPU
1721
{146BC2F1-9A1C-4A68-B15F-BF42FE61228B}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{FCD154A2-591E-4706-851F-7E5A248E3902}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{FCD154A2-591E-4706-851F-7E5A248E3902}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{FCD154A2-591E-4706-851F-7E5A248E3902}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
25+
{FCD154A2-591E-4706-851F-7E5A248E3902}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{FCD154A2-591E-4706-851F-7E5A248E3902}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{FCD154A2-591E-4706-851F-7E5A248E3902}.Release|Any CPU.Deploy.0 = Release|Any CPU
28+
{98069EEB-35C9-4F96-B014-25E872545887}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{98069EEB-35C9-4F96-B014-25E872545887}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{98069EEB-35C9-4F96-B014-25E872545887}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{98069EEB-35C9-4F96-B014-25E872545887}.Release|Any CPU.Build.0 = Release|Any CPU
1832
EndGlobalSection
1933
GlobalSection(SolutionProperties) = preSolution
2034
HideSolutionNode = FALSE
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<Name>DataAbstractions.Dapper.TestDb</Name>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectVersion>4.1</ProjectVersion>
9+
<ProjectGuid>{fcd154a2-591e-4706-851f-7e5a248e3902}</ProjectGuid>
10+
<DSP>Microsoft.Data.Tools.Schema.Sql.Sql130DatabaseSchemaProvider</DSP>
11+
<OutputType>Database</OutputType>
12+
<RootPath>
13+
</RootPath>
14+
<RootNamespace>DataAbstractions.Dapper.TestDb</RootNamespace>
15+
<AssemblyName>DataAbstractions.Dapper.TestDb</AssemblyName>
16+
<ModelCollation>1033, CI</ModelCollation>
17+
<DefaultFileStructure>BySchemaAndSchemaType</DefaultFileStructure>
18+
<DeployToDatabase>True</DeployToDatabase>
19+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
20+
<TargetLanguage>CS</TargetLanguage>
21+
<AppDesignerFolder>Properties</AppDesignerFolder>
22+
<SqlServerVerification>False</SqlServerVerification>
23+
<IncludeCompositeObjects>True</IncludeCompositeObjects>
24+
<TargetDatabaseSet>True</TargetDatabaseSet>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<OutputPath>bin\Release\</OutputPath>
28+
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
29+
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<DefineDebug>false</DefineDebug>
33+
<DefineTrace>true</DefineTrace>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
38+
<OutputPath>bin\Debug\</OutputPath>
39+
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
40+
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
41+
<DebugSymbols>true</DebugSymbols>
42+
<DebugType>full</DebugType>
43+
<Optimize>false</Optimize>
44+
<DefineDebug>true</DefineDebug>
45+
<DefineTrace>true</DefineTrace>
46+
<ErrorReport>prompt</ErrorReport>
47+
<WarningLevel>4</WarningLevel>
48+
</PropertyGroup>
49+
<PropertyGroup>
50+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
51+
<!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
52+
<SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
53+
<VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
54+
</PropertyGroup>
55+
<Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
56+
<Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
57+
<ItemGroup>
58+
<Folder Include="Properties" />
59+
<Folder Include="Tables" />
60+
</ItemGroup>
61+
<ItemGroup>
62+
<Build Include="Tables\Company.sql" />
63+
<Build Include="Tables\Employee.sql" />
64+
<Build Include="Tables\Department.sql" />
65+
</ItemGroup>
66+
<ItemGroup>
67+
<PostDeploy Include="SeedScript.PostDeployment.sql" />
68+
</ItemGroup>
69+
<ItemGroup>
70+
<None Include="DataAbstractions.Dapper.TestDb.publish.xml" />
71+
</ItemGroup>
72+
</Project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Post-Deployment Script Template
3+
--------------------------------------------------------------------------------------
4+
This file contains SQL statements that will be appended to the build script.
5+
Use SQLCMD syntax to include a file in the post-deployment script.
6+
Example: :r .\myfile.sql
7+
Use SQLCMD syntax to reference a variable in the post-deployment script.
8+
Example: :setvar TableName MyTable
9+
SELECT * FROM [$(TableName)]
10+
--------------------------------------------------------------------------------------
11+
*/
12+
13+
14+
15+
USE [TestDb]
16+
GO
17+
18+
INSERT INTO [dbo].[Department]
19+
([Id]
20+
,[Name])
21+
VALUES
22+
(1, 'Support'),
23+
(2, 'Operations'),
24+
(3, 'Development')
25+
26+
GO
27+
28+
INSERT INTO [dbo].[Company]
29+
([Id]
30+
,[Name])
31+
VALUES
32+
(1, 'Mock Company'),
33+
(2, 'Bogus Company'),
34+
(3, 'Fake Company')
35+
GO
36+
37+
38+
INSERT INTO [dbo].[Employee]
39+
([Id]
40+
,[CompanyId]
41+
,[DepartmentId]
42+
,[Name]
43+
,[Title])
44+
VALUES
45+
(1, 1, 3, 'John Doe', 'Developer'),
46+
(2, 2, 1, 'Jane Doe', 'System Admin'),
47+
(3, 3, 2, 'Mike Smith', 'Account Manager')
48+
GO
49+
50+
51+
52+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE [dbo].[Company]
2+
(
3+
[Id] INT NOT NULL PRIMARY KEY,
4+
[Name] NVARCHAR(50) NOT NULL
5+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE [dbo].[Department]
2+
(
3+
[Id] INT NOT NULL PRIMARY KEY,
4+
[Name] NVARCHAR(50) NULL
5+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE [dbo].[Employee]
2+
(
3+
[Id] INT NOT NULL PRIMARY KEY,
4+
[CompanyId] INT NOT NULL,
5+
[DepartmentId] INT NOT NULL,
6+
[Name] NVARCHAR(50) NULL,
7+
[Title] NVARCHAR(50) NULL
8+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="FluentAssertions" Version="5.10.3" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
12+
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
13+
<PackageReference Include="xunit" Version="2.4.0" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
15+
<PackageReference Include="coverlet.collector" Version="1.2.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\DataAbstractions.Dapper\DataAbstractions.Dapper.csproj" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using FluentAssertions;
2+
using System.Data.SqlClient;
3+
using Xunit;
4+
5+
namespace DataAbstractions.Dapper.Tests
6+
{
7+
public class DataAccessorShould
8+
{
9+
private IDataAccessor _dataAccessor;
10+
public DataAccessorShould()
11+
{
12+
_dataAccessor = new DataAccessor(new SqlConnection("Data Source=localhost;Database=TestDb;User ID=sa;Password=t3st#database"));
13+
}
14+
15+
[Fact]
16+
public async void WorkWithQuerySingleAsync()
17+
{
18+
var result = await _dataAccessor.QuerySingleAsync<Company>("select * from Company where id = 1", commandType: System.Data.CommandType.Text);
19+
var expected = new Company { Id = 1, Name = "Mock Company" };
20+
result.Should().BeEquivalentTo(expected);
21+
}
22+
23+
[Fact]
24+
public async void WorkWithExecuteReaderAsync()
25+
{
26+
var reader = await _dataAccessor.ExecuteReaderAsync("select * from Company where id = 1");
27+
var dataReaderAccessor = _dataAccessor.GetDataReaderAbstraction(reader);
28+
var result = dataReaderAccessor.Parse<Company>();
29+
var expected = new Company { Id = 1, Name = "Mock Company" };
30+
result.Should().BeEquivalentTo(expected);
31+
}
32+
}
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace DataAbstractions.Dapper.Tests
2+
{
3+
public class Company
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
}
8+
}

0 commit comments

Comments
 (0)