EF #2

Merged
nathan.boileau merged 16 commits from EF into master 2 years ago

@ -1,15 +1,23 @@
using System; using System.Collections.ObjectModel;
using System.Collections.Generic; using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFLol namespace EFLol
{ {
public class ChampionEntity public class ChampionEntity
{ {
public int Id { get; set; } public int Id { get; set; }
[MaxLength(30, ErrorMessage = "Name cannot be longer than 30 characters.")]
public string Name { get; set; } public string Name { get; set; }
[MaxLength(256, ErrorMessage = "Bio cannot be longer than 256 characters.")]
public string Bio { get; set; } public string Bio { get; set; }
//public ChampionClass Class { get; set; }
//public string Icon { get; set; }
//public LargeImage Image { get; set; }
//public ReadOnlyDictionary<string, int> Characteristics { get; private set; }
//private HashSet<Skill> skills = new HashSet<Skill>();
public ReadOnlyCollection<SkinEntity> Skins { get; set; }
} }
} }

@ -7,14 +7,15 @@ using Microsoft.EntityFrameworkCore;
namespace EFLol namespace EFLol
{ {
public class ChampionContext : DbContext public class MyDbContext : DbContext
{ {
public DbSet<ChampionEntity> Champions { get; set; } public DbSet<ChampionEntity> Champions { get; set; }
public DbSet<SkinEntity> Skins { get; set; }
public ChampionContext() public MyDbContext()
{ } { }
public ChampionContext(DbContextOptions<ChampionContext> options) public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options) : base(options)
{ } { }
@ -22,9 +23,15 @@ namespace EFLol
{ {
if (!optionsBuilder.IsConfigured) if (!optionsBuilder.IsConfigured)
{ {
//optionsBuilder.UseSqlServer("Data Source=loldb.db");
optionsBuilder.UseSqlite("Data Source=loldb.db"); optionsBuilder.UseSqlite("Data Source=loldb.db");
} }
} }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ChampionEntity>().Property(c => c.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<SkinEntity>().Property(s => s.Id).ValueGeneratedOnAdd();
}
} }
} }

@ -1,37 +1,67 @@
using EFLol; using System.Collections.ObjectModel;
using EFLol;
/*ChampionEntity Zeus = new ChampionEntity
using (var context = new MyDbContext())
{
// Clean the DB before starting
Console.WriteLine("Clean the DB before starting");
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
SkinEntity black = new SkinEntity { Name = "Black", Description = "Black skin", Icon = "black.png", Price = 0.99f };
SkinEntity white = new SkinEntity { Name = "White", Description = "White skin", Icon = "white.png", Price = 150.99f };
SkinEntity green = new SkinEntity { Name = "Green", Description = "Green skin", Icon = "green.png", Price = 4.99f };
ChampionEntity Zeus = new ChampionEntity
{ {
Name = "Zeus", Name = "Zeus",
Bio = "Zeus is the king of the gods." Bio = "Zeus is the god of the sky",
Skins = new ReadOnlyCollection<SkinEntity>(new List<SkinEntity> { black, white })
};
ChampionEntity Hera = new ChampionEntity
{
Name = "Hera",
Bio = "Hera is the goddess of marriage",
Skins = new ReadOnlyCollection<SkinEntity>(new List<SkinEntity> { green })
}; };
ChampionEntity Poseidon = new ChampionEntity { Name = "Poseidon", Bio = "Poseidon is the god of the sea" };
using (var context = new ChampionContext())
using (var context = new MyDbContext())
{ {
Console.WriteLine("Adding Zeus to the database..."); // Crée des champions et les insère dans la base
context.Champions.Add(Zeus); Console.WriteLine("Creates and inserts new Champions");
context.Add(Zeus);
context.Add(Hera);
context.Add(Poseidon);
context.SaveChanges(); context.SaveChanges();
}*/ }
ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is the king of the gods." };
ChampionEntity yoda = new ChampionEntity { Name = "Yoda", Bio = "Zeus is the king of the gods." };
ChampionEntity ewok = new ChampionEntity { Name = "Ewok" , Bio = "Zeus is the king of the gods." };
using (var context = new ChampionContext()) using (var context = new MyDbContext())
{
foreach (var n in context.Champions)
{ {
// Crée des nounours et les insère dans la base // Use LINQ to display the skins for each champion
Console.WriteLine("Creates and inserts new Nounours"); var skins = from s in context.Skins
context.Add(chewie); where n.Id == s.Id
context.Add(yoda); select s;
context.Add(ewok);
Console.WriteLine($"Champion n°{n.Id} - {n.Name}");
}
context.SaveChanges(); context.SaveChanges();
} }
using (var context = new ChampionContext()) using (var context = new MyDbContext())
{ {
foreach (var n in context.Champions) foreach (var n in context.Skins)
{ {
Console.WriteLine($"{n.Id} - {n.Name}"); Console.WriteLine($"Skin n°{n.Id} - {n.Name}" + (n.Price != null ? $" - {n.Price}" : ""));
} }
context.SaveChanges(); context.SaveChanges();
} }

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFLol
{
public class SkinEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Icon { get; set; }
public float Price { get; set; }
}
}

@ -6,83 +6,107 @@ namespace TestUnitaire
{ {
public class TestEfLol public class TestEfLol
{ {
[Fact]
public async Task TestAddInMemory() [Theory]
[InlineData("Zeus", "Dieu de la foudre", true)]
[InlineData("Hades", "Dieu des enfers", true)]
[InlineData("Aphrodite", "Déesse de l'amour", true)]
[InlineData("AresAresAresAresAresAresAresAresAresAres",
"Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" +
"Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" +
"Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre", false)]
public async Task TestAddInMemory(String name, String bio, bool expected)
{ {
// Arrange
var connection = new SqliteConnection("DataSource=:memory:"); var connection = new SqliteConnection("DataSource=:memory:");
connection.Open(); connection.Open();
var options = new DbContextOptionsBuilder<ChampionContext>() var options = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection) .UseSqlite(connection)
.Options; .Options;
// Act using (var context = new MyDbContext(options))
using (var context = new ChampionContext(options))
{ {
await context.Database.EnsureCreatedAsync(); await context.Database.EnsureCreatedAsync();
var Zeus = new ChampionEntity var Dieu = new ChampionEntity
{ {
Name = "Zeus", Name = name,
Bio = "Zeus is the king of the gods." Bio = bio
};
var Poseidon = new ChampionEntity
{
Name = "Poseidon",
Bio = "Poseidon is the king of the sea."
}; };
ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus");
Assert.Null(found); Assert.Null(found);
await context.Champions.AddAsync(Zeus); await context.Champions.AddAsync(Dieu);
await context.Champions.AddAsync(Poseidon);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name);
Assert.NotNull(found); Assert.NotNull(found);
Assert.Equal(2, await context.Champions.CountAsync()); Assert.Equal(1, await context.Champions.CountAsync());
Assert.Equal("Zeus", found.Name); Assert.Equal(name, found.Name);
// Test if the max length of the name is respected (30) and the max length of the bio is respected (256)
if (expected)
{
Assert.True(found.Name.Length <= 30);
Assert.True(found.Bio.Length <= 256);
} }
else
{
Assert.False(found.Bio.Length <= 256);
Assert.False(found.Name.Length <= 30);
}
}
} }
[Fact] [Fact]
public void ModifyTestInMemory() public void ModifyTestInMemory()
{ {
var options = new DbContextOptionsBuilder<ChampionContext>() var connection = new SqliteConnection("DataSource=:memory:");
.UseInMemoryDatabase(databaseName: "Modify_Test_database") connection.Open();
var options = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options; .Options;
//prepares the database with one instance of the context //prepares the database with one instance of the context
using (var context = new MyDbContext(options))
using (var context = new ChampionContext(options))
{ {
//context.Database.OpenConnection();
context.Database.EnsureCreated();
ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is the king of the gods." }; ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is the king of the gods." };
ChampionEntity yoda = new ChampionEntity { Name = "Yoda", Bio = "Zeus is the king of the gods." }; ChampionEntity yoda = new ChampionEntity { Name = "Yoda", Bio = "Zeus is the king of the gods." };
ChampionEntity ewok = new ChampionEntity { Name = "Ewok", Bio = "Zeus is the king of the gods." }; ChampionEntity ewok = new ChampionEntity { Name = "Ewok", Bio = "Zeus is the king of the gods." };
context.Champions.Add(chewie); context.Champions.Add(chewie);
context.Champions.Add(yoda); context.Champions.Add(yoda);
context.Champions.Add(ewok); context.Champions.Add(ewok);
context.SaveChanges(); context.SaveChanges();
} }
//prepares the database with one instance of the context //uses another instance of the context to do the tests
using (var context = new ChampionContext(options)) using (var context = new MyDbContext(options))
{ {
context.Database.EnsureCreated();
string nameToFind = "ew"; string nameToFind = "ew";
Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "ewo"; nameToFind = "wo";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
var ewok = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First(); var ewok = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First();
ewok.Name = "Wicket"; ewok.Name = "Wicket";
context.SaveChanges(); context.SaveChanges();
} }
//prepares the database with one instance of the context //uses another instance of the context to do the tests
using (var context = new ChampionContext(options)) using (var context = new MyDbContext(options))
{ {
context.Database.EnsureCreated();
string nameToFind = "ew"; string nameToFind = "ew";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "wick"; nameToFind = "wick";
@ -91,7 +115,3 @@ namespace TestUnitaire
} }
} }
} }

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using System.Security.Cryptography;
using Microsoft.AspNetCore.Mvc;
using Model; using Model;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Loading…
Cancel
Save