Skip to content

Commit

Permalink
Add more unit test cases;
Browse files Browse the repository at this point in the history
Add master-pr-ci.yml;
  • Loading branch information
agile.zhou committed Jan 7, 2024
1 parent 5e77ba9 commit 462654d
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/master-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- '**/*.md'
- '**/*.yml'
pull_request:
branches: [ master ]
branches: [ ]
workflow_dispatch:

jobs:
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/master-pr-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: master pr ci workflow

on:
pull_request:
branches: [ master ]
workflow_dispatch:

jobs:
build-reactapp:
runs-on: ubuntu-latest
defaults:
run:
working-directory: src/AgileConfig.Server.UI/react-ui-antd
strategy:
matrix:
node-version: [16.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- run: npm install
- run: npm run build
build-dotnet:
needs: build-reactapp
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 8.0.*
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
16 changes: 15 additions & 1 deletion AgileConfig.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.Data.Rep
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.Data.Repository.Mongodb", "src\AgileConfig.Server.Data.Repository.Mongodb\AgileConfig.Server.Data.Repository.Mongodb.csproj", "{C6B7A5A6-7287-4A20-9336-EBE82A5256F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AgileConfig.Server.Data.Repository.Selector", "src\AgileConfig.Server.Data.Repository.Selector\AgileConfig.Server.Data.Repository.Selector.csproj", "{15089E5A-12E4-4953-BA35-0CBB2B1189C0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.Data.Repository.Selector", "src\AgileConfig.Server.Data.Repository.Selector\AgileConfig.Server.Data.Repository.Selector.csproj", "{15089E5A-12E4-4953-BA35-0CBB2B1189C0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.Data.AbstractionTests", "test\AgileConfig.Server.Data.AbstractionTests\AgileConfig.Server.Data.AbstractionTests.csproj", "{964F5F7A-3BBD-47B3-8C28-EC16B1038A5A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.Data.FreesqlTests", "test\AgileConfig.Server.Data.FreesqlTests\AgileConfig.Server.Data.FreesqlTests.csproj", "{AC7E4D24-D5E8-4E30-BFB0-5DDC581CB0C2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -118,6 +122,14 @@ Global
{15089E5A-12E4-4953-BA35-0CBB2B1189C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15089E5A-12E4-4953-BA35-0CBB2B1189C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15089E5A-12E4-4953-BA35-0CBB2B1189C0}.Release|Any CPU.Build.0 = Release|Any CPU
{964F5F7A-3BBD-47B3-8C28-EC16B1038A5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{964F5F7A-3BBD-47B3-8C28-EC16B1038A5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{964F5F7A-3BBD-47B3-8C28-EC16B1038A5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{964F5F7A-3BBD-47B3-8C28-EC16B1038A5A}.Release|Any CPU.Build.0 = Release|Any CPU
{AC7E4D24-D5E8-4E30-BFB0-5DDC581CB0C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC7E4D24-D5E8-4E30-BFB0-5DDC581CB0C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC7E4D24-D5E8-4E30-BFB0-5DDC581CB0C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC7E4D24-D5E8-4E30-BFB0-5DDC581CB0C2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -139,6 +151,8 @@ Global
{955F64CC-9EAC-4563-804D-6E2CF9547357} = {1D2FD643-CB85-40F9-BC8A-CE4A39E9F43E}
{C6B7A5A6-7287-4A20-9336-EBE82A5256F1} = {1D2FD643-CB85-40F9-BC8A-CE4A39E9F43E}
{15089E5A-12E4-4953-BA35-0CBB2B1189C0} = {1D2FD643-CB85-40F9-BC8A-CE4A39E9F43E}
{964F5F7A-3BBD-47B3-8C28-EC16B1038A5A} = {F277EC27-8C0E-4490-9645-F5F3244F9246}
{AC7E4D24-D5E8-4E30-BFB0-5DDC581CB0C2} = {F277EC27-8C0E-4490-9645-F5F3244F9246}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7F10DB58-5B6F-4EAC-994F-14E8046B306F}
Expand Down
2 changes: 1 addition & 1 deletion src/AgileConfig.Server.Data.Freesql/FreeSqlUow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Task<bool> SaveChangesAsync()

public void Rollback()
{
_unitOfWork.Rollback();
_unitOfWork?.Rollback();
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AgileConfig.Server.Common\AgileConfig.Server.Common.csproj" />
<ProjectReference Include="..\..\src\AgileConfig.Server.Data.Abstraction\AgileConfig.Server.Data.Abstraction.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using AgileConfig.Server.Common;
using AgileConfig.Server.Data.Abstraction.DbProvider;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AgileConfig.Server.Data.AbstractionTests.DbConfig
{
[TestClass]
public class DbConfigInfoFactoryTests
{
[TestMethod]
public void TestGetConfigInfo()
{
var configMap = new Dictionary<string, string>() {
{"db:provider","sqlserver" },
{"db:conn","localhost" },
{"db:env:test:provider","sqlite" },
{"db:env:test:conn","Data Source=agile_config.db" },
};

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(configMap);
var configuration = configurationBuilder.Build();
Global.Config = configuration;

var configInfo = DbConfigInfoFactory.GetConfigInfo();
Assert.IsNotNull(configInfo);
Assert.AreEqual("sqlserver", configInfo.Provider);
Assert.AreEqual("localhost", configInfo.ConnectionString);

configInfo = DbConfigInfoFactory.GetConfigInfo("test");
Assert.IsNotNull(configInfo);
Assert.AreEqual("sqlite", configInfo.Provider);
Assert.AreEqual("Data Source=agile_config.db", configInfo.ConnectionString);

configInfo = DbConfigInfoFactory.GetConfigInfo("x");
Assert.IsNotNull(configInfo);
Assert.AreEqual("sqlserver", configInfo.Provider);
Assert.AreEqual("localhost", configInfo.ConnectionString);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AgileConfig.Server.Data.Freesql\AgileConfig.Server.Data.Freesql.csproj" />
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions test/AgileConfig.Server.Data.FreesqlTests/FreeSQLTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AgileConfig.Server.Data.Freesql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using AgileConfig.Server.Common;

namespace AgileConfig.Server.Data.Freesql.Tests
{
[TestClass()]
public class FreeSQLTests
{
[TestMethod()]
public void GetInstanceByEnvTest()
{
var configMap = new Dictionary<string, string>() {
{"db:provider","sqlite" },
{"db:conn","Data Source=agile_config.db" },
{"db:env:test:provider","sqlite" },
{"db:env:test:conn","Data Source=agile_config1.db" },
};
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(configMap);
var configuration = configurationBuilder.Build();
Global.Config = configuration;

var fsq = FreeSQL.GetInstanceByEnv("");
Assert.IsNotNull(fsq);
Assert.AreEqual(FreeSql.DataType.Sqlite, fsq.Ado.DataType);

var fsqtest = FreeSQL.GetInstanceByEnv("test");
Assert.IsNotNull(fsqtest);
Assert.AreEqual(FreeSql.DataType.Sqlite, fsqtest.Ado.DataType);

Assert.AreNotSame(fsq, fsqtest);
var fsqtest_ag = FreeSQL.GetInstanceByEnv("test");
Assert.AreSame(fsqtest, fsqtest_ag);


var fsq_none = FreeSQL.GetInstanceByEnv("x");
Assert.IsNotNull(fsq_none);
Assert.AreEqual(FreeSql.DataType.Sqlite, fsq_none.Ado.DataType);
Assert.AreSame(fsq, fsq_none);
}
}
}
141 changes: 141 additions & 0 deletions test/AgileConfig.Server.Data.FreesqlTests/FreeSqlUowTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AgileConfig.Server.Data.Freesql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using AgileConfig.Server.Common;
using AgileConfig.Server.Data.Entity;

namespace AgileConfig.Server.Data.Freesql.Tests
{
[TestClass()]
public class FreeSqlUowTests
{
[TestInitialize]
public void TestInitialize()
{
var configMap = new Dictionary<string, string>() {
{"db:provider","sqlite" },
{"db:conn","Data Source=agile_config.db" },
{"db:env:test:provider","sqlite" },
{"db:env:test:conn","Data Source=agile_config1.db" },
};
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(configMap);
var configuration = configurationBuilder.Build();
Global.Config = configuration;

var fsql = FreeSQL.GetInstanceByEnv("");
fsql.CodeFirst.SyncStructure<User_test>();
fsql.CodeFirst.SyncStructure<Address_test>();
fsql.Delete<User_test>(new User_test() { Id = 1 }).ExecuteAffrows();
fsql.Delete<Address_test>(new Address_test() { Id = 1 }).ExecuteAffrows();
}


[TestMethod()]
public async Task SaveChangesAsyncTest_success()
{
// arrange
var fsql = FreeSQL.GetInstanceByEnv("");
var user = new User_test()
{
Id = 1,
Name = "abc"
};
var address = new Address_test()
{
Id = 1,
Address = "Address"
};
// act
using var uow = new FreeSqlUow(fsql);

var userrepository = fsql.GetRepository<User_test>();
userrepository.UnitOfWork = uow.GetFreesqlUnitOfWork();
var addressrepository = fsql.GetRepository<Address_test>();
addressrepository.UnitOfWork = uow.GetFreesqlUnitOfWork();

uow.Begin();

userrepository.Insert(user);
user.Name = "test1";
userrepository.Update(user);
addressrepository.Insert(address);
address.Address = "test1";
addressrepository.Update(address);

await uow.SaveChangesAsync();

// assert
var username = fsql.GetRepository<User_test>().Where(x => x.Id == 1).ToOne().Name;
var add = fsql.GetRepository<Address_test>().Where(x => x.Id == 1).ToOne().Address;
Assert.AreEqual("test1", username);
Assert.AreEqual("test1", add);
}

[TestMethod()]
public async Task SaveChangesAsyncTest_rollback()
{
// arrange
var fsql = FreeSQL.GetInstanceByEnv("");
var user = new User_test()
{
Id = 2,
Name = "abc"
};
var address = new Address_test()
{
Id = 2,
Address = "Address"
};

// act
using var uow = new FreeSqlUow(fsql);
var userrepository = fsql.GetRepository<User_test>();
userrepository.UnitOfWork = uow.GetFreesqlUnitOfWork();
var addressrepository = fsql.GetRepository<Address_test>();
addressrepository.UnitOfWork = uow.GetFreesqlUnitOfWork();

uow.Begin();
try
{
userrepository.Insert(user);
user.Name = "test1";
userrepository.Update(user);
throw new Exception("test");
addressrepository.Insert(address);
address.Address = "test1";
addressrepository.Update(address);

await uow.SaveChangesAsync();
}
catch (Exception exc)
{
Assert.IsNotNull(exc);
}


// assert
var _user = fsql.GetRepository<User_test>().Where(x => x.Id == 2).ToOne();
var _address = fsql.GetRepository<Address_test>().Where(x => x.Id == 2).ToOne();

Assert.IsNull(_user);
Assert.IsNull(_address);
}
}

public class User_test
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Address_test
{
public int Id { get; set; }
public string Address { get; set; }
}
}

0 comments on commit 462654d

Please sign in to comment.