From a014509135348e2e9ae5334917303c2e94aff92b Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 15 Feb 2024 09:48:12 +0100 Subject: [PATCH] ajouts des tests --- tp1/DbContextLib/LibraryContext.cs | 13 +++++++- tp1/TestStub/Program.cs | 8 ++--- tp1/UnitTests/GlobalUsings.cs | 1 + tp1/UnitTests/UnitTest1.cs | 48 ++++++++++++++++++++++++++++++ tp1/UnitTests/UnitTests.csproj | 41 +++++++++++++++++++++++++ tp1/tp1.sln | 12 ++++++-- 6 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 tp1/UnitTests/GlobalUsings.cs create mode 100644 tp1/UnitTests/UnitTest1.cs create mode 100644 tp1/UnitTests/UnitTests.csproj diff --git a/tp1/DbContextLib/LibraryContext.cs b/tp1/DbContextLib/LibraryContext.cs index 883f51b..16fd76b 100644 --- a/tp1/DbContextLib/LibraryContext.cs +++ b/tp1/DbContextLib/LibraryContext.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Entities; +using Microsoft.Extensions.Options; namespace DbContextLib @@ -16,8 +17,18 @@ namespace DbContextLib protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseSqlite("Data Source=tp.Books.db"); + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseSqlite("Data Source=tp.Books.db"); + } } + public LibraryContext() + { } + public LibraryContext(DbContextOptions options ) + : base(options) + { } + + } } diff --git a/tp1/TestStub/Program.cs b/tp1/TestStub/Program.cs index b2de216..0174c20 100644 --- a/tp1/TestStub/Program.cs +++ b/tp1/TestStub/Program.cs @@ -9,13 +9,13 @@ Console.WriteLine("Hello, World!"); using (var context = new LibraryContext()) { BookEntity chewie = new BookEntity("B3", "test1", "test1"); - BookEntity yoda = new BookEntity ("B4", "test2", "test2"); - BookEntity ewok = new BookEntity ("mistake", "test3", "test3"); + BookEntity princeOfPersia = new BookEntity ("B4", "test2", "test2"); + BookEntity PercyJackson = new BookEntity ("mistake", "test3", "test3"); BookEntity the100 = new BookEntity("the100", "test4", "test4"); context.BooksSet.Add(chewie); - context.BooksSet.Add(yoda); - context.BooksSet.Add(ewok); + context.BooksSet.Add(princeOfPersia); + context.BooksSet.Add(PercyJackson); context.BooksSet.Add(the100); context.SaveChanges(); diff --git a/tp1/UnitTests/GlobalUsings.cs b/tp1/UnitTests/GlobalUsings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/tp1/UnitTests/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/tp1/UnitTests/UnitTest1.cs b/tp1/UnitTests/UnitTest1.cs new file mode 100644 index 0000000..af18152 --- /dev/null +++ b/tp1/UnitTests/UnitTest1.cs @@ -0,0 +1,48 @@ +using DbContextLib; +using Entities; +using Microsoft.Extensions.Options; +using Microsoft.EntityFrameworkCore; +using System.Linq; +using Xunit; +using Microsoft.Data.Sqlite; + +namespace UnitTests +{ + public class UnitTest1 + { + [Fact] + public void Add_Test() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder.UseSqlLite().options; + + //prepares the database with one instance of the context + using (var context = new LibraryContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + BookEntity the100 = new BookEntity( "the100", "", "" ); + BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" ); + BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" ); + context.BooksSet.Add(the100); + context.BooksSet.Add(princeOfPersia); + context.BooksSet.Add(PercyJackson); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new LibraryContext(options)) + { + context.Database.EnsureCreated(); + + Assert.Equal(3, context.BooksSet.Count()); + Assert.Equal("the100", context.BooksSet.First().Title); + } + } + + } +} \ No newline at end of file diff --git a/tp1/UnitTests/UnitTests.csproj b/tp1/UnitTests/UnitTests.csproj new file mode 100644 index 0000000..8afb1f5 --- /dev/null +++ b/tp1/UnitTests/UnitTests.csproj @@ -0,0 +1,41 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + diff --git a/tp1/tp1.sln b/tp1/tp1.sln index 0d2e984..4c31c62 100644 --- a/tp1/tp1.sln +++ b/tp1/tp1.sln @@ -5,11 +5,13 @@ VisualStudioVersion = 17.8.34330.188 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestStub", "TestStub\TestStub.csproj", "{8960D74C-259D-4779-80B6-789BA257D2B2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{A4130190-8883-42DE-89CA-3DF205DE710A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{A4130190-8883-42DE-89CA-3DF205DE710A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{F793C071-E178-48D3-BBE6-22F99D102C2B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{F793C071-E178-48D3-BBE6-22F99D102C2B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{5C673F26-2E43-4AA8-944E-1A3B309927CF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{5C673F26-2E43-4AA8-944E-1A3B309927CF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +35,10 @@ Global {5C673F26-2E43-4AA8-944E-1A3B309927CF}.Debug|Any CPU.Build.0 = Debug|Any CPU {5C673F26-2E43-4AA8-944E-1A3B309927CF}.Release|Any CPU.ActiveCfg = Release|Any CPU {5C673F26-2E43-4AA8-944E-1A3B309927CF}.Release|Any CPU.Build.0 = Release|Any CPU + {73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE