Skip to content

Commit

Permalink
Test projesi eklendi ve extention test edilebilir hale getirildi
Browse files Browse the repository at this point in the history
  • Loading branch information
enisgurkann committed Jan 4, 2022
1 parent d5d164e commit 6326d83
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 1 deletion.
23 changes: 23 additions & 0 deletions EnLock.Tesr/EnLock.Tesr.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions EnLock.Tesr/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Xunit;

namespace EnLock.Tesr;

public class UnitTest1
{
[Fact]
public void Test1()
{
}
}
28 changes: 28 additions & 0 deletions EnLock.Test/EnLock.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EnLock\EnLock.csproj" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions EnLock.Test/TestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore;

namespace EnLock.Test;

public class TestContext: DbContext
{
public TestContext() { }
public TestContext(DbContextOptions<TestContext> options): base(options) { }
public DbSet<TestModel> TestDbSet { get; set; }
}

8 changes: 8 additions & 0 deletions EnLock.Test/TestModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EnLock.Test;

public class TestModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
}
129 changes: 129 additions & 0 deletions EnLock.Test/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace EnLock.Test;

public class UnitTest1
{
private readonly TestContext _testContext;

public UnitTest1()
{
_testContext = new TestContext(CreateNewContextOptions());
}

private static DbContextOptions<TestContext> CreateNewContextOptions()
{
// Create a fresh service provider, and therefore a fresh
// InMemory database instance.
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();

// Create a new options instance telling the context to use an
// InMemory database and the new service provider.
var builder = new DbContextOptionsBuilder<TestContext>();
builder.UseInMemoryDatabase( "TestDbInMemory")
.UseInternalServiceProvider(serviceProvider);

return builder.Options;
}

public IEnumerable<TestModel> GetDbSet()
{
var list = new List<TestModel>();

list.Add(new TestModel() {Name = "Enis", Surname = "Gürkan"});
list.Add(new TestModel() {Name = "Enes", Surname = "Gürkan"});
list.Add(new TestModel() {Name = "Filiz", Surname = "Gürkan"});
return list;
}

[Fact]
public async Task AddTest()
{
using (var context = new TestContext(CreateNewContextOptions()))
{
await _testContext.TestDbSet.AddRangeAsync(GetDbSet());
await _testContext.SaveChangesAsync();

int count = await _testContext.TestDbSet.CountAsync();
Assert.Equal(count,3);
}
}

[Fact]
public async Task FirstOrDefault_Test()
{
using (var context = new TestContext(CreateNewContextOptions()))
{
await _testContext.TestDbSet.AddRangeAsync(GetDbSet());
await _testContext.SaveChangesAsync();


var model = await _testContext
.TestDbSet
.Where(s=> s.Name == "Enis")
.ToFirstOrDefaultWithNoLockAsync();

Assert.Equal(model.Name, "Enis");
}
}

[Fact]
public async Task List_Test()
{
using (var context = new TestContext(CreateNewContextOptions()))
{
await _testContext.TestDbSet.AddRangeAsync(GetDbSet());
await _testContext.SaveChangesAsync();


var list = await _testContext
.TestDbSet
.ToListWithNoLockAsync();

Assert.Equal(list.Count(), 3);
}
}

[Fact]
public async Task Any_Test()
{
using (var context = new TestContext(CreateNewContextOptions()))
{
await _testContext.TestDbSet.AddRangeAsync(GetDbSet());
await _testContext.SaveChangesAsync();


var status = await _testContext
.TestDbSet
.Where(s=> s.Name == "Enis")
.ToAnyWithNoLockAsync();

Assert.Equal(status, true);
}
}

[Fact]
public async Task RemoveTest()
{
using (var context = new TestContext(CreateNewContextOptions()))
{
await _testContext.TestDbSet.AddRangeAsync(GetDbSet());
await _testContext.SaveChangesAsync();

var model = await _testContext.TestDbSet.FirstOrDefaultAsync(s => s.Name == "Enis");
_testContext.TestDbSet.Remove(model);
await _testContext.SaveChangesAsync();
int count = await _testContext.TestDbSet.CountAsync();

Assert.Equal(count, 2);
}

}
}
6 changes: 6 additions & 0 deletions EnLock.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.0.31912.275
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnLock", "EnLock\EnLock.csproj", "{2AC7594B-6D50-447C-908E-AE74172E288C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnLock.Test", "EnLock.Test\EnLock.Test.csproj", "{1B37344E-B555-4AE9-9337-87ADC94E2C1F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{2AC7594B-6D50-447C-908E-AE74172E288C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AC7594B-6D50-447C-908E-AE74172E288C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2AC7594B-6D50-447C-908E-AE74172E288C}.Release|Any CPU.Build.0 = Release|Any CPU
{1B37344E-B555-4AE9-9337-87ADC94E2C1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B37344E-B555-4AE9-9337-87ADC94E2C1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B37344E-B555-4AE9-9337-87ADC94E2C1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B37344E-B555-4AE9-9337-87ADC94E2C1F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion EnLock/EnExtention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static async Task<T> ToFirstWithNoLockAsync<T>(this IQueryable<T> query,
},
TransactionScopeAsyncFlowOption.Enabled))
{
result = await query.FirstOrDefaultAsync(cancellationToken);
result = await query.FirstAsync(cancellationToken);
scope.Complete();
}
return result;
Expand Down

0 comments on commit 6326d83

Please sign in to comment.