diff --git a/ChaosBot.UnitTests/DataBaseTests.cs b/ChaosBot.UnitTests/DataBaseTests.cs new file mode 100644 index 0000000..fb1397a --- /dev/null +++ b/ChaosBot.UnitTests/DataBaseTests.cs @@ -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 Query() + { + return BaseEntity.Query(); + } + + public override void SetFromRow(DataRow row) + { + id = (long)row["id"]; + } + } +} \ No newline at end of file