Skip to content

Commit bc52bc0

Browse files
committed
Added lua integration test
Fixed several bugs
1 parent 53e5f71 commit bc52bc0

25 files changed

+1065
-38
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111
*.zip
1212
cmake-build-debug
1313
.idea
14-
.vs
15-
CMakeSettings.json
14+
.vs

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if (CMAKE_SIZEOF_VOID_P EQUAL 8)
2222
endif ()
2323
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
2424
if (WIN32)
25-
find_library(MARIADB_CLIENT_LIB mariadbclient HINTS "${PROJECT_SOURCE_DIR}MySQL/lib/windows")
25+
find_library(MARIADB_CLIENT_LIB mariadbclient HINTS "${PROJECT_SOURCE_DIR}/MySQL/lib/windows")
2626
else ()
2727
find_library(MARIADB_CLIENT_LIB mariadbclient HINTS "${PROJECT_SOURCE_DIR}/MySQL/lib/linux")
2828
find_library(CRYPTO_LIB crypto HINTS "${PROJECT_SOURCE_DIR}/MySQL/lib/linux")

CMakeSettings.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "x64-RelDebug",
5+
"generator": "Ninja",
6+
"configurationType": "RelWithDebInfo",
7+
"inheritEnvironments": [ "msvc_x64_x64" ],
8+
"buildRoot": "${projectDir}\\out\\build\\${name}",
9+
"installRoot": "${projectDir}\\out\\install\\${name}",
10+
"cmakeCommandArgs": "",
11+
"buildCommandArgs": "",
12+
"ctestCommandArgs": ""
13+
},
14+
{
15+
"name": "x86-RelDebug",
16+
"generator": "Ninja",
17+
"configurationType": "RelWithDebInfo",
18+
"buildRoot": "${projectDir}\\out\\build\\${name}",
19+
"installRoot": "${projectDir}\\out\\install\\${name}",
20+
"cmakeCommandArgs": "",
21+
"buildCommandArgs": "",
22+
"ctestCommandArgs": "",
23+
"inheritEnvironments": [ "msvc_x86" ],
24+
"variables": []
25+
}
26+
]
27+
}

IntegrationTest/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# MySQLOO Lua Integration Tests
2+
3+
This folder contains integration tests for MySQLOO.
4+
5+
## Running
6+
7+
- Place this folder into the server's addons folder.
8+
- Adjust the database settings in lua/autorun/server/init.lua
9+
- ensure that the database used is **empty** as it will be filled with test data
10+
- run `mysqloo_start_tests` in the server console
11+
12+
Each of the tests outputs its result to the console and at the end there is an overview of all tests printed.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DatabaseSettings = {
2+
Host = "localhost",
3+
Port = 3306,
4+
Username = "root",
5+
Password = "",
6+
Database = "test"
7+
}
8+
9+
print("Loading MySQLOO Testing Framework")
10+
include("mysqloo/init.lua")

IntegrationTest/lua/mysqloo/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include("testframework.lua")
2+
include("setup.lua")
3+
4+
5+
local files = file.Find("mysqloo/tests/*.lua", "LUA")
6+
for _,f in pairs(files) do
7+
include("tests/" .. f)
8+
end

IntegrationTest/lua/mysqloo/setup.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require("mysqloo")
2+
3+
function TestFramework:ConnectToDatabase()
4+
local db = mysqloo.connect(DatabaseSettings.Host, DatabaseSettings.Username, DatabaseSettings.Password, DatabaseSettings.Database, DatabaseSettings.Port)
5+
db:connect()
6+
db:wait()
7+
return db
8+
end
9+
10+
function TestFramework:RunQuery(db, queryStr)
11+
local query = db:query(queryStr)
12+
query:start()
13+
function query:onError(err)
14+
error(err)
15+
end
16+
query:wait()
17+
return query:getData()
18+
end
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
2+
3+
TestFramework = TestFramework or {}
4+
TestFramework.RegisteredTests = {}
5+
6+
local TestMT = {}
7+
TestMT.__index = TestMT
8+
9+
function TestFramework:RegisterTest(name, f)
10+
local tbl = setmetatable({}, {__index = TestMT})
11+
tbl.TestFunction = f
12+
tbl.Name = name
13+
table.insert(TestFramework.RegisteredTests, tbl)
14+
print("Registered test ", name)
15+
end
16+
17+
function TestFramework:RunNextTest()
18+
TestFramework.CurrentIndex = (TestFramework.CurrentIndex or 0) + 1
19+
TestFramework.TestTimeout = CurTime() + 3
20+
local test = TestFramework.RegisteredTests[TestFramework.CurrentIndex]
21+
TestFramework.CurrentTest = test
22+
if (!test) then
23+
TestFramework:OnCompleted()
24+
else
25+
test:Run()
26+
end
27+
end
28+
29+
function TestFramework:CheckTimeout()
30+
if (!TestFramework.CurrentTest) then return end
31+
if (CurTime() > TestFramework.TestTimeout) then
32+
TestFramework.CurrentTest:Fail("TIMEOUT")
33+
end
34+
end
35+
36+
hook.Add("Think", "TestFrameworkTimeoutCheck", function()
37+
TestFramework:CheckTimeout()
38+
end)
39+
40+
function TestFramework:ReportResult(success)
41+
TestFramework.TestCount = (TestFramework.TestCount or 0) + 1
42+
if (success) then
43+
TestFramework.SuccessCount = (TestFramework.SuccessCount or 0) + 1
44+
else
45+
TestFramework.FailureCount = (TestFramework.FailureCount or 0) + 1
46+
end
47+
end
48+
49+
function TestFramework:OnCompleted()
50+
print("[MySQLOO] Tests completed")
51+
MsgC(Color(255, 255, 255), "Completed: ", Color(30, 230, 30), TestFramework.SuccessCount, Color(255, 255, 255), " Failures: ", Color(230, 30, 30), TestFramework.FailureCount, "\n")
52+
53+
for j = 0, 3 do
54+
timer.Simple(j * 0.5, function()
55+
for i = 1, 100 do
56+
collectgarbage("collect")
57+
end
58+
end)
59+
end
60+
timer.Simple(2, function()
61+
for i = 1, 100 do
62+
collectgarbage("collect")
63+
end
64+
local diffBefore = TestFramework.AllocationCount - TestFramework.DeallocationCount
65+
local diffAfter = mysqloo.allocationCount() - mysqloo.deallocationCount()
66+
if (diffAfter > diffBefore) then
67+
MsgC(Color(255, 255, 255), "Found potential memory leak with ", diffAfter - diffBefore, " new allocations that were not freed\n")
68+
else
69+
MsgC(Color(255, 255, 255), "All allocated objects were freed\n")
70+
end
71+
MsgC(Color(255, 255, 255), "Lua Heap Before: ", TestFramework.LuaMemory, " After: ", collectgarbage("count"), "\n")
72+
end)
73+
end
74+
75+
function TestFramework:Start()
76+
for i = 1, 5 do
77+
collectgarbage("collect")
78+
end
79+
TestFramework.CurrentIndex = 0
80+
TestFramework.SuccessCount = 0
81+
TestFramework.FailureCount = 0
82+
TestFramework.AllocationCount = mysqloo.allocationCount()
83+
TestFramework.DeallocationCount = mysqloo.deallocationCount()
84+
TestFramework.LuaMemory = collectgarbage("count")
85+
TestFramework:RunNextTest()
86+
end
87+
88+
function TestMT:Fail(reason)
89+
if (self.Completed) then return end
90+
self.Completed = true
91+
MsgC(Color(230, 30, 30), "FAILED\n")
92+
MsgC(Color(230, 30, 30), "Error: ", reason, "\n")
93+
TestFramework:ReportResult(false)
94+
TestFramework:RunNextTest()
95+
end
96+
97+
function TestMT:Complete()
98+
if (self.Completed) then return end
99+
self.Completed = true
100+
MsgC(Color(30, 230, 30), "PASSED\n")
101+
TestFramework:ReportResult(true)
102+
TestFramework:RunNextTest()
103+
end
104+
105+
function TestMT:Run()
106+
MsgC("Test: ", self.Name, " ")
107+
self.Completed = false
108+
local status, err = pcall(function()
109+
self.TestFunction(self)
110+
end)
111+
if (!status) then
112+
self:Fail(err)
113+
end
114+
end
115+
116+
function TestMT:shouldBeNil(a)
117+
if (a != nil) then
118+
self:Fail(tostring(a) .. " was expected to be nil, but was not nil")
119+
error("Assertion failed")
120+
end
121+
end
122+
123+
function TestMT:shouldBeGreaterThan(a, num)
124+
if (num >= a) then
125+
self:Fail(tostring(a) .. " was expected to be greater than " .. tostring(num))
126+
error("Assertion failed")
127+
end
128+
end
129+
130+
function TestMT:shouldNotBeNil(a)
131+
if (a == nil) then
132+
self:Fail(tostring(a) .. " was expected to not be nil, but was nil")
133+
error("Assertion failed")
134+
end
135+
end
136+
137+
function TestMT:shouldNotBeEqual(a, b)
138+
if (a == b) then
139+
self:Fail(tostring(a) .. " was equal to " .. tostring(b))
140+
error("Assertion failed")
141+
end
142+
end
143+
144+
function TestMT:shouldBeEqual(a, b)
145+
if (a != b) then
146+
self:Fail(tostring(a) .. " was not equal to " .. tostring(b))
147+
error("Assertion failed")
148+
end
149+
end
150+
151+
function TestMT:shouldHaveLength(tbl, exactLength)
152+
if (#tbl != exactLength) then
153+
self:Fail("Length of " .. tostring(tbl) .. " was not equal to " .. exactLength)
154+
error("Assertion failed")
155+
end
156+
end
157+
158+
concommand.Add("mysqloo_start_tests", function(ply)
159+
if (IsValid(ply)) then return end
160+
print("Starting MySQLOO Tests")
161+
if (#player.GetBots() == 0) then
162+
RunConsoleCommand("bot")
163+
end
164+
timer.Simple(0.1, function()
165+
TestFramework:Start()
166+
end)
167+
end)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
TestFramework:RegisterTest("[Basic] selecting 1 should return 1", function(test)
2+
local db = TestFramework:ConnectToDatabase()
3+
local query = db:query("SELECT 3 as test")
4+
function query:onSuccess(data)
5+
test:shouldHaveLength(data, 1)
6+
test:shouldBeEqual(data[1]["test"], 3)
7+
test:Complete()
8+
end
9+
function query:onError(err)
10+
test:Fail(err)
11+
end
12+
query:start()
13+
query:wait()
14+
end)

0 commit comments

Comments
 (0)