Write tests for database manipulation
This commit is contained in:
parent
5b552ac310
commit
a8be00abc9
84
ChaosBot.UnitTests/DataBaseTests.cs
Normal file
84
ChaosBot.UnitTests/DataBaseTests.cs
Normal file
@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using ChaosBot.Attribute;
|
||||
using ChaosBot.Database;
|
||||
using ChaosBot.Database.Entity;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ChaosBot.UnitTests
|
||||
{
|
||||
public class DataBaseTests
|
||||
{
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
Program.LoadConfiguration("./appsettings.json");
|
||||
Program.LoadLogger();
|
||||
AssemblyController.RegisterDBEntityType(typeof(TestEntity));
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
Controller.RawQuery("DROP TABLE TestTable;", readOutput: false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Insert_CanInsertDataToDBAndDeleteFromDB_True()
|
||||
{
|
||||
var testEntity = new TestEntity(1);
|
||||
|
||||
Assert.AreEqual(0, TestEntity.Query().Count());
|
||||
|
||||
TestEntity.Query().Insert(testEntity);
|
||||
|
||||
Assert.AreEqual(1, TestEntity.Query().Count());
|
||||
|
||||
TestEntity.Query().Delete();
|
||||
|
||||
Assert.AreEqual(0, TestEntity.Query().Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertUpdateDelete_CheckCanInsertUpdateAndDeleteFromDB_True()
|
||||
{
|
||||
Assert.AreEqual("", GetDBContent());
|
||||
TestEntity.Query().Insert(new TestEntity(5));
|
||||
Assert.AreEqual("5", GetDBContent());
|
||||
TestEntity.Query().Where("id", 5).Set("id", 6).Update();
|
||||
Assert.AreEqual("6", GetDBContent());
|
||||
TestEntity.Query().Where("id", 6).Delete();
|
||||
Assert.AreEqual("", GetDBContent());
|
||||
}
|
||||
|
||||
public string GetDBContent()
|
||||
{
|
||||
return string.Join(" ", TestEntity.Query().Distinct().Get().Select(o => o.id).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
[DBEntity("TestTable")]
|
||||
public class TestEntity : BaseEntity
|
||||
{
|
||||
public long id { get; private set; }
|
||||
|
||||
public TestEntity() {}
|
||||
|
||||
public TestEntity(int id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static QueryBuilder<TestEntity> Query()
|
||||
{
|
||||
return BaseEntity.Query<TestEntity>();
|
||||
}
|
||||
|
||||
public override void SetFromRow(DataRow row)
|
||||
{
|
||||
id = (long)row["id"];
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user