-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
longxianghui
committed
Sep 29, 2019
1 parent
2750b35
commit 025a060
Showing
30 changed files
with
1,614 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.28307.705 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leo.Chimp", "src\Leo.Chimp\Leo.Chimp.csproj", "{AE57FE9E-110F-4BBD-AF58-243A37E334C3}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leo.Chimp.Test", "src\Leo.Chimp.Test\Leo.Chimp.Test.csproj", "{1CF7DFC0-5C7A-4E9C-B5A8-0C9609004F23}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{AE57FE9E-110F-4BBD-AF58-243A37E334C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{AE57FE9E-110F-4BBD-AF58-243A37E334C3}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{AE57FE9E-110F-4BBD-AF58-243A37E334C3}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{AE57FE9E-110F-4BBD-AF58-243A37E334C3}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{1CF7DFC0-5C7A-4E9C-B5A8-0C9609004F23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{1CF7DFC0-5C7A-4E9C-B5A8-0C9609004F23}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{1CF7DFC0-5C7A-4E9C-B5A8-0C9609004F23}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{1CF7DFC0-5C7A-4E9C-B5A8-0C9609004F23}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {D936E593-6A06-4989-8708-382308D741CF} | ||
EndGlobalSection | ||
EndGlobal |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
-- ---------------------------- | ||
-- Table structure for school | ||
-- ---------------------------- | ||
DROP TABLE IF EXISTS `school`; | ||
CREATE TABLE `school` ( | ||
`Id` char(36) NOT NULL, | ||
`Name` varchar(255) NOT NULL, | ||
PRIMARY KEY (`Id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
-- ---------------------------- | ||
-- Table structure for student | ||
-- ---------------------------- | ||
DROP TABLE IF EXISTS `student`; | ||
CREATE TABLE `student` ( | ||
`Id` varchar(50) NOT NULL, | ||
`Name` varchar(255) DEFAULT NULL, | ||
`SchoolId` int(11) DEFAULT NULL, | ||
`Birthday` datetime DEFAULT NULL, | ||
PRIMARY KEY (`Id`) USING BTREE | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Dapper; | ||
using Leo.Chimp.Test.Entities; | ||
using Leo.Chimp.Test.Repositories; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Leo.Chimp.Test | ||
{ | ||
public class DapperTest | ||
{ | ||
private readonly IUnitOfWork _unitOfWork; | ||
private readonly ISchoolRepository _schoolRepository; | ||
|
||
public DapperTest() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddKing(opt => { opt.UseMySql("server = 10.0.0.146;database=king;uid=root;password=123456;"); }); | ||
//services.AddKing(opt => { opt.UseSqlServer("Server=10.0.0.99;Database=king;Uid=sa;Pwd=Fuluerp123"); }); | ||
var sp = services.BuildServiceProvider(); | ||
_unitOfWork = sp.GetRequiredService<IUnitOfWork>(); | ||
_schoolRepository = sp.GetRequiredService<ISchoolRepository>(); | ||
} | ||
|
||
[Fact] | ||
public async Task QueryAsync() | ||
{ | ||
var schools = await _unitOfWork.QueryAsync<School>("select * from school"); | ||
Assert.True(schools.Any()); | ||
} | ||
|
||
[Fact] | ||
public async Task QueryPagedListAsync() | ||
{ | ||
var schools = await _unitOfWork.QueryPagedListAsync<School>(1, 10, "select * from school order by id"); | ||
Assert.True(schools.Item.Any()); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAsync() | ||
{ | ||
var school = new School | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "school" | ||
}; | ||
|
||
await _unitOfWork.ExecuteAsync("insert school(id,name) values(@Id,@Name)", | ||
school); | ||
|
||
var newSchool = await _unitOfWork.QueryAsync<School>("select * from school where id =@id", | ||
new { Id = school.Id }); | ||
|
||
Assert.True(school.Name == newSchool.First().Name); | ||
} | ||
|
||
[Fact] | ||
public async Task GetConnection() | ||
{ | ||
//可以直接使用dapper | ||
var schools = await _unitOfWork.GetConnection().QueryAsync("select * from school"); | ||
Assert.True(schools.Any()); | ||
} | ||
|
||
[Fact] | ||
public async Task Transaction() | ||
{ | ||
var school1 = new School | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "school1" | ||
}; | ||
|
||
var school2 = new School | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "school2" | ||
}; | ||
|
||
using (var tran = _unitOfWork.BeginTransaction()) | ||
{ | ||
try | ||
{ | ||
await _unitOfWork.ExecuteAsync("insert school(id,name) values(@Id,@Name)", | ||
school1); | ||
await _unitOfWork.ExecuteAsync("insert school(id,name) values(@Id,@Name)", | ||
school2); | ||
throw new Exception(); | ||
tran.Commit(); | ||
} | ||
catch (Exception e) | ||
{ | ||
tran.Rollback(); | ||
} | ||
} | ||
var newSchool1 = await _unitOfWork.QueryAsync<School>("select * from school where id =@id", | ||
new { Id = school1.Id }); | ||
var newSchool2 = await _unitOfWork.QueryAsync<School>("select * from school where id =@id", | ||
new { Id = school2.Id }); | ||
Assert.False(newSchool1.Any() || newSchool2.Any()); | ||
|
||
} | ||
|
||
[Fact] | ||
public async Task HybridTransaction() | ||
{ | ||
var school1 = new School | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "school1" | ||
}; | ||
|
||
var school2 = new School | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "school2" | ||
}; | ||
using (var tran = _unitOfWork.BeginTransaction()) | ||
{ | ||
try | ||
{ | ||
await _schoolRepository.InsertAsync(school1); | ||
await _unitOfWork.SaveChangesAsync(); | ||
|
||
await _unitOfWork.ExecuteAsync("insert school(id,name) values(@Id,@Name)", | ||
school2); | ||
throw new Exception(); | ||
tran.Commit(); | ||
} | ||
catch (Exception e) | ||
{ | ||
tran.Rollback(); | ||
} | ||
} | ||
var newSchool1 = await _unitOfWork.QueryAsync<School>("select * from school where id =@id", | ||
new { Id = school1.Id }); | ||
var newSchool2 = await _unitOfWork.QueryAsync<School>("select * from school where id =@id", | ||
new { Id = school2.Id }); | ||
Assert.False(newSchool1.Any() || newSchool2.Any()); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.