Skip to content

Commit

Permalink
Added lua integration test
Browse files Browse the repository at this point in the history
Fixed several bugs
  • Loading branch information
FredyH committed Nov 9, 2021
1 parent 53e5f71 commit bc52bc0
Show file tree
Hide file tree
Showing 25 changed files with 1,065 additions and 38 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*.zip
cmake-build-debug
.idea
.vs
CMakeSettings.json
.vs
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (CMAKE_SIZEOF_VOID_P EQUAL 8)
endif ()
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
if (WIN32)
find_library(MARIADB_CLIENT_LIB mariadbclient HINTS "${PROJECT_SOURCE_DIR}MySQL/lib/windows")
find_library(MARIADB_CLIENT_LIB mariadbclient HINTS "${PROJECT_SOURCE_DIR}/MySQL/lib/windows")
else ()
find_library(MARIADB_CLIENT_LIB mariadbclient HINTS "${PROJECT_SOURCE_DIR}/MySQL/lib/linux")
find_library(CRYPTO_LIB crypto HINTS "${PROJECT_SOURCE_DIR}/MySQL/lib/linux")
Expand Down
27 changes: 27 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"configurations": [
{
"name": "x64-RelDebug",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
},
{
"name": "x86-RelDebug",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x86" ],
"variables": []
}
]
}
12 changes: 12 additions & 0 deletions IntegrationTest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# MySQLOO Lua Integration Tests

This folder contains integration tests for MySQLOO.

## Running

- Place this folder into the server's addons folder.
- Adjust the database settings in lua/autorun/server/init.lua
- ensure that the database used is **empty** as it will be filled with test data
- run `mysqloo_start_tests` in the server console

Each of the tests outputs its result to the console and at the end there is an overview of all tests printed.
10 changes: 10 additions & 0 deletions IntegrationTest/lua/autorun/server/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DatabaseSettings = {
Host = "localhost",
Port = 3306,
Username = "root",
Password = "",
Database = "test"
}

print("Loading MySQLOO Testing Framework")
include("mysqloo/init.lua")
8 changes: 8 additions & 0 deletions IntegrationTest/lua/mysqloo/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include("testframework.lua")
include("setup.lua")


local files = file.Find("mysqloo/tests/*.lua", "LUA")
for _,f in pairs(files) do
include("tests/" .. f)
end
18 changes: 18 additions & 0 deletions IntegrationTest/lua/mysqloo/setup.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require("mysqloo")

function TestFramework:ConnectToDatabase()
local db = mysqloo.connect(DatabaseSettings.Host, DatabaseSettings.Username, DatabaseSettings.Password, DatabaseSettings.Database, DatabaseSettings.Port)
db:connect()
db:wait()
return db
end

function TestFramework:RunQuery(db, queryStr)
local query = db:query(queryStr)
query:start()
function query:onError(err)
error(err)
end
query:wait()
return query:getData()
end
167 changes: 167 additions & 0 deletions IntegrationTest/lua/mysqloo/testframework.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@


TestFramework = TestFramework or {}
TestFramework.RegisteredTests = {}

local TestMT = {}
TestMT.__index = TestMT

function TestFramework:RegisterTest(name, f)
local tbl = setmetatable({}, {__index = TestMT})
tbl.TestFunction = f
tbl.Name = name
table.insert(TestFramework.RegisteredTests, tbl)
print("Registered test ", name)
end

function TestFramework:RunNextTest()
TestFramework.CurrentIndex = (TestFramework.CurrentIndex or 0) + 1
TestFramework.TestTimeout = CurTime() + 3
local test = TestFramework.RegisteredTests[TestFramework.CurrentIndex]
TestFramework.CurrentTest = test
if (!test) then
TestFramework:OnCompleted()
else
test:Run()
end
end

function TestFramework:CheckTimeout()
if (!TestFramework.CurrentTest) then return end
if (CurTime() > TestFramework.TestTimeout) then
TestFramework.CurrentTest:Fail("TIMEOUT")
end
end

hook.Add("Think", "TestFrameworkTimeoutCheck", function()
TestFramework:CheckTimeout()
end)

function TestFramework:ReportResult(success)
TestFramework.TestCount = (TestFramework.TestCount or 0) + 1
if (success) then
TestFramework.SuccessCount = (TestFramework.SuccessCount or 0) + 1
else
TestFramework.FailureCount = (TestFramework.FailureCount or 0) + 1
end
end

function TestFramework:OnCompleted()
print("[MySQLOO] Tests completed")
MsgC(Color(255, 255, 255), "Completed: ", Color(30, 230, 30), TestFramework.SuccessCount, Color(255, 255, 255), " Failures: ", Color(230, 30, 30), TestFramework.FailureCount, "\n")

for j = 0, 3 do
timer.Simple(j * 0.5, function()
for i = 1, 100 do
collectgarbage("collect")
end
end)
end
timer.Simple(2, function()
for i = 1, 100 do
collectgarbage("collect")
end
local diffBefore = TestFramework.AllocationCount - TestFramework.DeallocationCount
local diffAfter = mysqloo.allocationCount() - mysqloo.deallocationCount()
if (diffAfter > diffBefore) then
MsgC(Color(255, 255, 255), "Found potential memory leak with ", diffAfter - diffBefore, " new allocations that were not freed\n")
else
MsgC(Color(255, 255, 255), "All allocated objects were freed\n")
end
MsgC(Color(255, 255, 255), "Lua Heap Before: ", TestFramework.LuaMemory, " After: ", collectgarbage("count"), "\n")
end)
end

function TestFramework:Start()
for i = 1, 5 do
collectgarbage("collect")
end
TestFramework.CurrentIndex = 0
TestFramework.SuccessCount = 0
TestFramework.FailureCount = 0
TestFramework.AllocationCount = mysqloo.allocationCount()
TestFramework.DeallocationCount = mysqloo.deallocationCount()
TestFramework.LuaMemory = collectgarbage("count")
TestFramework:RunNextTest()
end

function TestMT:Fail(reason)
if (self.Completed) then return end
self.Completed = true
MsgC(Color(230, 30, 30), "FAILED\n")
MsgC(Color(230, 30, 30), "Error: ", reason, "\n")
TestFramework:ReportResult(false)
TestFramework:RunNextTest()
end

function TestMT:Complete()
if (self.Completed) then return end
self.Completed = true
MsgC(Color(30, 230, 30), "PASSED\n")
TestFramework:ReportResult(true)
TestFramework:RunNextTest()
end

function TestMT:Run()
MsgC("Test: ", self.Name, " ")
self.Completed = false
local status, err = pcall(function()
self.TestFunction(self)
end)
if (!status) then
self:Fail(err)
end
end

function TestMT:shouldBeNil(a)
if (a != nil) then
self:Fail(tostring(a) .. " was expected to be nil, but was not nil")
error("Assertion failed")
end
end

function TestMT:shouldBeGreaterThan(a, num)
if (num >= a) then
self:Fail(tostring(a) .. " was expected to be greater than " .. tostring(num))
error("Assertion failed")
end
end

function TestMT:shouldNotBeNil(a)
if (a == nil) then
self:Fail(tostring(a) .. " was expected to not be nil, but was nil")
error("Assertion failed")
end
end

function TestMT:shouldNotBeEqual(a, b)
if (a == b) then
self:Fail(tostring(a) .. " was equal to " .. tostring(b))
error("Assertion failed")
end
end

function TestMT:shouldBeEqual(a, b)
if (a != b) then
self:Fail(tostring(a) .. " was not equal to " .. tostring(b))
error("Assertion failed")
end
end

function TestMT:shouldHaveLength(tbl, exactLength)
if (#tbl != exactLength) then
self:Fail("Length of " .. tostring(tbl) .. " was not equal to " .. exactLength)
error("Assertion failed")
end
end

concommand.Add("mysqloo_start_tests", function(ply)
if (IsValid(ply)) then return end
print("Starting MySQLOO Tests")
if (#player.GetBots() == 0) then
RunConsoleCommand("bot")
end
timer.Simple(0.1, function()
TestFramework:Start()
end)
end)
14 changes: 14 additions & 0 deletions IntegrationTest/lua/mysqloo/tests/basic_mysql_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
TestFramework:RegisterTest("[Basic] selecting 1 should return 1", function(test)
local db = TestFramework:ConnectToDatabase()
local query = db:query("SELECT 3 as test")
function query:onSuccess(data)
test:shouldHaveLength(data, 1)
test:shouldBeEqual(data[1]["test"], 3)
test:Complete()
end
function query:onError(err)
test:Fail(err)
end
query:start()
query:wait()
end)
Loading

0 comments on commit bc52bc0

Please sign in to comment.