Implement DatabaseTests and SlotTests

This commit is contained in:
jvyden 2021-10-19 22:19:37 -04:00
parent 15e3870620
commit bbeb7d980e
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
3 changed files with 71 additions and 2 deletions

View file

@ -0,0 +1,15 @@
using System;
using System.Threading.Tasks;
namespace ProjectLighthouse.Tests {
public class DatabaseTests : LighthouseTest {
[DatabaseFact]
public async Task CanCreateUserTwice() {
await using Database database = new();
int rand = new Random().Next();
await database.CreateUser("createUserTwiceTest" + rand);
await database.CreateUser("createUserTwiceTest" + rand);
}
}
}

View file

@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using ProjectLighthouse.Types;
using Xunit;
namespace ProjectLighthouse.Tests {
public class SlotTests : LighthouseTest {
[DatabaseFact]
public async Task ShouldOnlyShowUsersLevels() {
await using Database database = new();
User userA = await database.CreateUser("unitTestUser0");
User userB = await database.CreateUser("unitTestUser1");
Location l = new();
database.Locations.Add(l);
await database.SaveChangesAsync();
Slot slotA = new() {
Creator = userA,
Name = "slotA",
Location = l,
LocationId = l.Id,
};
Slot slotB = new() {
Creator = userB,
Name = "slotB",
Location = l,
LocationId = l.Id,
};
database.Slots.Add(slotA);
database.Slots.Add(slotB);
await database.SaveChangesAsync();
// XmlSerializer serializer = new(typeof(Slot));
// Slot slot = (Slot)serializer.Deserialize(new StringReader(bodyString));
string respA = await this.Client.GetStringAsync("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser0");
string respB = await this.Client.GetStringAsync("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser1");
Assert.NotEqual(respA, respB);
Assert.DoesNotContain(respA, "slotB");
Assert.DoesNotContain(respB, "slotA");
}
}
}

View file

@ -1,3 +1,4 @@
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -22,11 +23,15 @@ namespace ProjectLighthouse {
); );
public async Task<User> CreateUser(string username) { public async Task<User> CreateUser(string username) {
User user;
if((user = await Users.Where(u => u.Username == username).FirstOrDefaultAsync()) != null)
return user;
Location l = new(); // store to get id after submitting Location l = new(); // store to get id after submitting
this.Locations.Add(l); // add to table this.Locations.Add(l); // add to table
await this.SaveChangesAsync(); // saving to the database returns the id and sets it on this entity await this.SaveChangesAsync(); // saving to the database returns the id and sets it on this entity
User user = new() { user = new() {
Username = username, Username = username,
LocationId = l.Id, LocationId = l.Id,
Biography = username + " hasn't introduced themselves yet.", Biography = username + " hasn't introduced themselves yet.",
@ -36,7 +41,6 @@ namespace ProjectLighthouse {
await this.SaveChangesAsync(); await this.SaveChangesAsync();
return user; return user;
} }
#nullable enable #nullable enable