avancer
continuous-integration/drone/push Build is passing Details

master
PATRICK 1 year ago
parent 7bc5aea9da
commit e08cc0df05

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
@ -20,6 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\ModeleToEntities\ModeleToEntities.csproj" />
<ProjectReference Include="..\StubbedContext\StubbedContextLib.csproj" />
</ItemGroup>

@ -1,5 +1,6 @@
using Entities;
using Microsoft.AspNetCore.Mvc;
using ModeleToEntities;
using StubbedContextLib;
namespace API.Controllers
@ -21,7 +22,7 @@ namespace API.Controllers
public async Task<ActionResult<IEnumerable<GroupEntity>>> GetGroups()
{
_logger.LogInformation("Getting groups ");
var groups = await _groupService.GetGroups();
var groups = await _groupService.Gets();
return Ok(groups);
}
@ -29,7 +30,7 @@ namespace API.Controllers
public async Task<ActionResult<GroupEntity>> GetGroup(int id)
{
_logger.LogInformation("Getting a group with id {id}",id);
var group = await _groupService.GetGroup(id);
var group = await _groupService.GetById(id);
return Ok(group);
}
@ -45,7 +46,7 @@ namespace API.Controllers
public async Task<ActionResult<GroupEntity>> AddGroup([FromQuery]GroupEntity group)
{
_logger.LogInformation("Adding a group with id : {id}",group.Id);
group.Id = _groupService.GetGroups().Result.Count() + 1;
group.Id = _groupService.Gets().Result.Count() + 1;
var newGroup = await _groupService.AddGroup(group);
return Ok(newGroup);
}

@ -1,10 +1,12 @@
using Entities;
using Microsoft.Extensions.Configuration;
using StubbedContextLib;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ModeleToEntities;
using Entities;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.

@ -1,7 +1,167 @@
namespace ModeleToEntities
using Entities;
using Modele;
namespace ModeleToEntities
{
public class Extensions
public static class Extensions
{
public static GroupEntity ToEntity(this Group group)
{
return new GroupEntity
{
Id = group.Id,
Num = group.Num,
year = group.year,
sector = group.sector,
VocabularyList = group.VocabularyList.Select(v => v.ToEntity()).ToList()
};
}
public static Group ToModel(this GroupEntity group)
{
return new Group
{
Id = group.Id,
Num = group.Num,
year = group.year,
sector = group.sector,
VocabularyList = group.VocabularyList.Select(v => v.ToModel()).ToList()
};
}
public static VocabularyListEntity ToEntity(this VocabularyList vocabularyList)
{
return new VocabularyListEntity
{
Id = vocabularyList.Id,
Name = vocabularyList.Name,
Image = vocabularyList.Image,
UserId = vocabularyList.UserId,
translation = vocabularyList.translation.Select(t => t.ToEntity()).ToList(),
Groups = vocabularyList.Groups.Select(g => g.ToEntity()).ToList()
};
}
public static VocabularyList ToModel(this VocabularyListEntity vocabularyList)
{
return new VocabularyList
{
Id = vocabularyList.Id,
Name = vocabularyList.Name,
Image = vocabularyList.Image,
UserId = vocabularyList.UserId,
translation = vocabularyList.translation.Select(t => t.ToModel()).ToList(),
Groups = vocabularyList.Groups.Select(g => g.ToModel()).ToList()
};
}
public static TranslateEntity ToEntity(this Translate translate)
{
return new TranslateEntity
{
Id = translate.Id,
WordsId = translate.WordsId,
Words = translate.Words.Select(w => w.ToEntity()).ToList(),
VocabularyListVocId = translate.VocabularyListVocId,
VocabularyListVoc = translate.VocabularyListVoc.ToEntity()
};
}
public static Translate ToModel(this TranslateEntity translate)
{
return new Translate
{
Id = translate.Id,
WordsId = translate.WordsId,
Words = translate.Words.Select(w => w.ToModel()).ToList(),
VocabularyListVocId = translate.VocabularyListVocId,
VocabularyListVoc = translate.VocabularyListVoc.ToModel()
};
}
public static VocabularyEntity ToEntity(this Vocabulary vocabulary)
{
return new VocabularyEntity
{
word = vocabulary.word,
LangueName = vocabulary.LangueName
};
}
public static Vocabulary ToModel(this VocabularyEntity vocabulary)
{
return new Vocabulary
{
word = vocabulary.word,
LangueName = vocabulary.LangueName
};
}
public static UserEntity ToEntity(this User user)
{
return new UserEntity
{
Id = user.Id,
Name = user.Name,
UserName = user.UserName,
NickName = user.NickName,
ExtraTime = user.ExtraTime,
GroupId = user.GroupId,
Password = user.Password,
Email = user.Email
};
}
public static User ToModel(this UserEntity user)
{
return new User
{
Id = user.Id,
Name = user.Name,
UserName = user.UserName,
NickName = user.NickName,
ExtraTime = user.ExtraTime,
GroupId = user.GroupId,
Password = user.Password,
Email = user.Email
};
}
public static RoleEntity ToEntity(this Role role)
{
return new RoleEntity
{
Id = role.Id,
Name = role.Name
};
}
public static Role ToModel(this RoleEntity role)
{
return new Role
{
Id = role.Id,
Name = role.Name
};
}
public static LangueEntity ToEntity(this Langue langue)
{
return new LangueEntity
{
name = langue.name
};
}
public static Langue ToModel(this LangueEntity langue)
{
return new Langue
{
name = langue.name
};
}
}
}

@ -1,14 +1,14 @@
using DbContextLib;
using Entities;
using Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using StubbedContextLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StubbedContextLib
namespace ModeleToEntities
{
public class GroupService : IService<GroupEntity>
{
@ -37,7 +37,7 @@ namespace StubbedContextLib
return group;
}
public async Task<GroupEntity> GetGroup(int id)
public async Task<GroupEntity> GetById(int id)
{
var group = await _context.Groups.FindAsync(id);
if (group == null)
@ -47,7 +47,7 @@ namespace StubbedContextLib
return group;
}
public async Task<IEnumerable<GroupEntity>> GetGroups()
public async Task<IEnumerable<GroupEntity>> Gets()
{
var groups = await _context.Groups.ToListAsync();
return groups;

@ -5,12 +5,12 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StubbedContextLib
namespace ModeleToEntities
{
public interface IService<T>
{
Task<IEnumerable<T>> GetGroups();
Task<T> GetGroup(int id);
Task<IEnumerable<T>> Gets();
Task<T> GetById(int id);
Task<T> AddGroup(T group);
Task<T> DeleteGroup(int id);
Task<T> UpdateGroup(T group);

@ -6,4 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\Modele\Modele.csproj" />
<ProjectReference Include="..\StubbedContext\StubbedContextLib.csproj" />
</ItemGroup>
</Project>

@ -90,16 +90,16 @@ namespace StubbedContextLib
modelBuilder.Entity<GroupEntity>().HasData(
new GroupEntity
{
Id= 1,
Id = 1,
Num = 1,
sector = "informatics",
year = 1,
VocabularyList = null
});
}
}
public StubbedContext() { }
public StubbedContext() { }
public StubbedContext(DbContextOptions<LibraryContext> options) : base(options)
{

@ -9,6 +9,48 @@ namespace TU
[TestClass]
public class GroupTU
{
[TestMethod]
public async Task TestGetGroups()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var groups = await context.Groups.ToListAsync();
Assert.IsNotNull(groups);
Assert.AreEqual(1, groups.Count);
Assert.AreEqual("informatics", groups[0].sector);
}
}
[TestMethod]
public async Task TestGetGroup() {
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var newBook = new GroupEntity { Id = 2, year = 2, sector = "medecin" };
await context.Groups.AddAsync(newBook);
await context.SaveChangesAsync();
var group1 = await context.Groups.FirstOrDefaultAsync(b => b.sector == "medecin");
Assert.IsNotNull(group1);
Assert.AreEqual("medecin", group1.sector);
Assert.AreEqual(2, group1.year);
Assert.AreEqual(2, group1.Id);
}
}
[TestMethod]
public async Task TestAddGroup()
{

@ -0,0 +1,140 @@
using DbContextLib;
using Entities;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using StubbedContextLib;
namespace TU
{
[TestClass]
public class LangueTU
{
[TestMethod]
public async Task TestAddLangue()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var newLangue = new LangueEntity { name = "français" , vocabularys=null };
await context.Langues.AddAsync(newLangue);
await context.SaveChangesAsync();
var langue = await context.Langues.FirstOrDefaultAsync(b => b.name == "français");
Assert.IsNotNull(langue);
Assert.AreEqual("français", langue.name);
Assert.IsNull(langue.vocabularys);
}
}
[TestMethod]
public async Task TestDeleteLangue()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var vocab = new VocabularyEntity { word = "test", Langue = null };
var newLangue = new LangueEntity { name = "français", vocabularys=[vocab] };
vocab.Langue = newLangue;
await context.Langues.AddAsync(newLangue);
await context.SaveChangesAsync();
var langue = await context.Langues.FirstOrDefaultAsync(b => b.name == "français");
Assert.IsNotNull(langue);
Assert.AreEqual("français", langue.name);
Assert.AreEqual(vocab, langue.vocabularys.First());
context.Langues.Remove(newLangue);
await context.SaveChangesAsync();
var langue2 = await context.Langues.FirstOrDefaultAsync(b => b.name == "français");
Assert.IsNull(langue2);
}
}
[TestMethod]
public async Task TestUpdateLangue()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var newBook = new GroupEntity { Id = 2, year = 2, sector = "medecin" };
await context.Groups.AddAsync(newBook);
await context.SaveChangesAsync();
var group1 = await context.Groups.FirstOrDefaultAsync(b => b.sector == "medecin");
Assert.IsNotNull(group1);
Assert.AreEqual("medecin", group1.sector);
Assert.AreEqual(2, group1.year);
Assert.AreEqual(2, group1.Id);
group1.sector = "informatique";
group1.year = 3;
context.Groups.Update(group1);
await context.SaveChangesAsync();
var group2 = await context.Groups.FirstOrDefaultAsync(b => b.sector == "informatique");
Assert.IsNotNull(group2);
Assert.AreEqual("informatique", group2.sector);
Assert.AreEqual(3, group2.year);
Assert.AreEqual(2, group2.Id);
}
}
[TestMethod]
public async Task TestGetLangues()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var langues = await context.Langues.ToListAsync();
Assert.IsNotNull(langues);
Assert.AreEqual(2, langues.Count);
Assert.AreEqual("English", langues[0].name);
Assert.AreEqual("French", langues[1].name);
}
}
[TestMethod]
public async Task TestGetLangue()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var newLangue = new LangueEntity { name = "francais" };
await context.Langues.AddAsync(newLangue);
await context.SaveChangesAsync();
var langue = await context.Langues.FirstOrDefaultAsync(b => b.name == "francais");
Assert.IsNotNull(langue);
Assert.AreEqual("francais", langue.name);
}
}
}
}

@ -0,0 +1,124 @@
using DbContextLib;
using Entities;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using StubbedContextLib;
namespace TU
{
[TestClass]
public class RoleTU
{
[TestMethod]
public async Task TestGetRoles()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var roles = await context.Roles.ToListAsync();
Assert.IsNotNull(roles);
Assert.AreEqual(3, roles.Count);
Assert.AreEqual("Admin", roles[0].Name);
}
}
[TestMethod]
public async Task TestGetRole()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var newRole = new RoleEntity { Id = 4, Name = "user" };
await context.Roles.AddAsync(newRole);
await context.SaveChangesAsync();
var role1 = await context.Roles.FirstOrDefaultAsync(b => b.Name == "user");
Assert.IsNotNull(role1);
Assert.AreEqual("user", role1.Name);
Assert.AreEqual(4, role1.Id);
}
}
[TestMethod]
public async Task TestAddRole()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var newRole = new RoleEntity { Name = "user" };
await context.Roles.AddAsync(newRole);
await context.SaveChangesAsync();
var role = await context.Roles.FirstOrDefaultAsync(b => b.Name == "user");
Assert.IsNotNull(role);
Assert.AreEqual("user", role.Name);
}
}
[TestMethod]
public async Task TestUpdateRole()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var newRole = new RoleEntity { Name = "user" };
await context.Roles.AddAsync(newRole);
await context.SaveChangesAsync();
var role = await context.Roles.FirstOrDefaultAsync(b => b.Name == "user");
role.Name = "admin";
context.Roles.Update(role);
await context.SaveChangesAsync();
var role1 = await context.Roles.FirstOrDefaultAsync(b => b. Name == "admin");
Assert.IsNotNull(role1);
Assert.AreEqual("admin", role1.Name);
}
}
[TestMethod]
public async Task TestDeleteRole()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
var newRole = new RoleEntity { Name = "user" };
await context.Roles.AddAsync(newRole);
await context.SaveChangesAsync();
var role = await context.Roles.FirstOrDefaultAsync(b => b.Name == "user");
context.Roles.Remove(role);
await context.SaveChangesAsync();
var role1 = await context.Roles.FirstOrDefaultAsync(b => b.Name == "user");
Assert.IsNull(role1);
}
}
}
}
Loading…
Cancel
Save