Add Unit tests for Article entity 📝📝
continuous-integration/drone/push Build is failing Details

testTony
Louis LABORIE 1 year ago
parent 6fccf58b65
commit 9bf771b6ef

@ -1,35 +0,0 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TestsUnitaires")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TestsUnitaires")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("FF8AFA55-12FE-4856-A0A1-21344D366E12")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

@ -1,14 +0,0 @@
using System;
using Xunit;
namespace TestsUnitaires
{
public class Tests
{
[Fact]
public void Test1()
{
Assert.True(true);
}
}
}

@ -0,0 +1,103 @@
using DbContextLib;
using Entities;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace Tests;
public class ArticleDB_Tests
{
[Fact]
public void Add_Test()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new LibraryContext(options))
{
context.Database.EnsureCreated();
ArticleEntity a1 = new ArticleEntity { Id = 1, Title = "Breaking News Elisabeth 2 Died", Description = "The queen of England died today at the age of 95", DatePublished = "2022-02-06", LectureTime = 2, Author = "Tom Smith" };
ArticleEntity a2 = new ArticleEntity { Id = 2, Title = "The new iPhone 15", Description = "The new iPhone 15 is out and it's the best phone ever", DatePublished = "2022-02-06", LectureTime = 3, Author = "Tom Smith" };
ArticleEntity a3 = new ArticleEntity { Id = 3, Title = "M&M's new recipe", Description = "M&M's new recipe is out and it's the best chocolate ever", DatePublished = "2022-02-06", LectureTime = 1, Author = "M&M's Red" };
context.Add(a1);
context.Add(a2);
context.Add(a3);
context.SaveChanges();
Assert.Equal(3, context.ArticleSet.Count());
Assert.Equal("Tom Smith", context.ArticleSet.First().Author);
connection.Close();
}
}
[Fact]
public void Modify_Test()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new LibraryContext(options))
{
context.Database.EnsureCreated();
ArticleEntity a1 = new ArticleEntity { Id = 1, Title = "Breaking News Elisabeth 2 Died", Description = "The queen of England died today at the age of 95", DatePublished = "2022-02-06", LectureTime = 2, Author = "Tom Smith" };
ArticleEntity a2 = new ArticleEntity { Id = 2, Title = "The new iPhone 15", Description = "The new iPhone 15 is out and it's the best phone ever", DatePublished = "2022-02-06", LectureTime = 3, Author = "Tom Smith" };
ArticleEntity a3 = new ArticleEntity { Id = 3, Title = "M&M's new recipe", Description = "M&M's new recipe is out and it's the best chocolate ever", DatePublished = "2022-02-06", LectureTime = 1, Author = "M&M's Red" };
context.Add(a1);
context.Add(a2);
context.Add(a3);
context.SaveChanges();
var article = context.ArticleSet.First(a => a.Title.Equals("Breaking News Elisabeth 2 Died"));
article.Author = "Louis Laborie";
context.SaveChanges();
string persRemove = "Tom Smith";
string persNew = "Louis Laborie";
Assert.Equal(1, context.ArticleSet.Count(a => a.Author.Equals(persRemove)));
Assert.Equal(1, context.ArticleSet.Count(a => a.Author.Equals(persNew)));
connection.Close();
}
}
[Fact]
public void Remove_Test()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new LibraryContext(options))
{
context.Database.EnsureCreated();
ArticleEntity a1 = new ArticleEntity { Id = 1, Title = "Breaking News Elisabeth 2 Died", Description = "The queen of England died today at the age of 95", DatePublished = "2022-02-06", LectureTime = 2, Author = "Tom Smith" };
ArticleEntity a2 = new ArticleEntity { Id = 2, Title = "The new iPhone 15", Description = "The new iPhone 15 is out and it's the best phone ever", DatePublished = "2022-02-06", LectureTime = 3, Author = "Tom Smith" };
ArticleEntity a3 = new ArticleEntity { Id = 3, Title = "M&M's new recipe", Description = "M&M's new recipe is out and it's the best chocolate ever", DatePublished = "2022-02-06", LectureTime = 1, Author = "M&M's Red" };
context.Add(a1);
context.Add(a2);
context.Add(a3);
context.SaveChanges();
var article = context.ArticleSet.First(a => a.Title.Equals("Breaking News Elisabeth 2 Died"));
context.Remove(article);
context.SaveChanges();
Assert.Equal(2, context.ArticleSet.Count());
Assert.Equal(0, context.ArticleSet.Count(a => a.Title.Equals("Breaking News Elisabeth 2 Died")));
connection.Close();
}
}
}

@ -1,66 +1,34 @@
<?xml version="1.0" encoding="utf-8"?> <Project Sdk="Microsoft.NET.Sdk">
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" <PropertyGroup>
Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/> <TargetFramework>net8.0</TargetFramework>
<PropertyGroup> <ImplicitUsings>enable</ImplicitUsings>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Nullable>enable</Nullable>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{FF8AFA55-12FE-4856-A0A1-21344D366E12}</ProjectGuid> <IsPackable>false</IsPackable>
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <IsTestProject>true</IsTestProject>
<OutputType>Library</OutputType> </PropertyGroup>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestsUnitaires</RootNamespace> <ItemGroup>
<AssemblyName>TestsUnitaires</AssemblyName> <PackageReference Include="coverlet.collector" Version="6.0.0" />
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<FileAlignment>512</FileAlignment> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" />
</PropertyGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PlatformTarget>AnyCPU</PlatformTarget> <PackageReference Include="xunit" Version="2.7.0" />
<DebugSymbols>true</DebugSymbols> <PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<DebugType>full</DebugType> <PrivateAssets>all</PrivateAssets>
<Optimize>false</Optimize> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<OutputPath>bin\Debug\</OutputPath> </PackageReference>
<DefineConstants>DEBUG;TRACE</DefineConstants> </ItemGroup>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <ItemGroup>
</PropertyGroup> <ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <ProjectReference Include="..\Entities\Entities.csproj" />
<PlatformTarget>AnyCPU</PlatformTarget> </ItemGroup>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize> <ItemGroup>
<OutputPath>bin\Release\</OutputPath> <Using Include="Xunit" />
<DefineConstants>TRACE</DefineConstants> </ItemGroup>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Data"/>
<Reference Include="System.Xml"/>
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c">
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
</Reference>
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c">
<HintPath>..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath>
</Reference>
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c">
<HintPath>..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c">
<HintPath>..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Tests.cs"/>
<Compile Include="Properties\AssemblyInfo.cs"/>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project> </Project>

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xunit" version="2.1.0" targetFramework="net45" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.1.0" targetFramework="net45" />
<package id="xunit.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net45" />
</packages>

@ -1,24 +1,29 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test_Console_EF", "Test_Console_EF\Test_Console_EF.csproj", "{E27E2617-28A1-4675-B12B-89430582C05E}" # Visual Studio Version 17
VisualStudioVersion = 17.8.34525.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test_Console_EF", "Test_Console_EF\Test_Console_EF.csproj", "{E27E2617-28A1-4675-B12B-89430582C05E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{40BD34D7-3F07-410A-BC04-2A5B09E758C0}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{40BD34D7-3F07-410A-BC04-2A5B09E758C0}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{F94BEE1F-302F-4654-8D85-AD41E0BACD03}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{F94BEE1F-302F-4654-8D85-AD41E0BACD03}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{F1B4BCE5-8DE7-4EFB-8BC1-D7E04EA75302}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{F1B4BCE5-8DE7-4EFB-8BC1-D7E04EA75302}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_DbDataManager", "API_DbDataManager\API_DbDataManager.csproj", "{FBA0CF18-7488-4088-A7C5-5D2071D19CCB}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API_DbDataManager", "API_DbDataManager\API_DbDataManager.csproj", "{FBA0CF18-7488-4088-A7C5-5D2071D19CCB}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_Services", "API_Services\API_Services.csproj", "{4FB7D286-B583-44BC-BB79-4AE3058AFEDE}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API_Services", "API_Services\API_Services.csproj", "{4FB7D286-B583-44BC-BB79-4AE3058AFEDE}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_Model", "API_Model\API_Model.csproj", "{F09B566E-8D25-4D70-B9F0-99E6969D4D1F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API_Model", "API_Model\API_Model.csproj", "{F09B566E-8D25-4D70-B9F0-99E6969D4D1F}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_Mapping", "API_Mapping\API_Mapping.csproj", "{BECFAD2C-E442-4300-8121-5AE6610B92DF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API_Mapping", "API_Mapping\API_Mapping.csproj", "{BECFAD2C-E442-4300-8121-5AE6610B92DF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{96FDB5DE-6707-4856-94CD-EFAF0C7CEB4B}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{96FDB5DE-6707-4856-94CD-EFAF0C7CEB4B}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{6AACD337-70A0-429B-979C-1B1E004BF2E0}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API", "API\API.csproj", "{6AACD337-70A0-429B-979C-1B1E004BF2E0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsUnitaires", "TestsUnitaires\TestsUnitaires.csproj", "{C1982C7C-AE09-4E96-B1A9-B6ADE4097452}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -66,5 +71,12 @@ Global
{6AACD337-70A0-429B-979C-1B1E004BF2E0}.Debug|Any CPU.Build.0 = Debug|Any CPU {6AACD337-70A0-429B-979C-1B1E004BF2E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AACD337-70A0-429B-979C-1B1E004BF2E0}.Release|Any CPU.ActiveCfg = Release|Any CPU {6AACD337-70A0-429B-979C-1B1E004BF2E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AACD337-70A0-429B-979C-1B1E004BF2E0}.Release|Any CPU.Build.0 = Release|Any CPU {6AACD337-70A0-429B-979C-1B1E004BF2E0}.Release|Any CPU.Build.0 = Release|Any CPU
{C1982C7C-AE09-4E96-B1A9-B6ADE4097452}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C1982C7C-AE09-4E96-B1A9-B6ADE4097452}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1982C7C-AE09-4E96-B1A9-B6ADE4097452}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1982C7C-AE09-4E96-B1A9-B6ADE4097452}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

Loading…
Cancel
Save