Repository navigation
Expand file tree
/
Copy pathQuick.Core.Mvc.Session.Tests.pas
More file actions
184 lines (148 loc) · 6.99 KB
/
Copy pathQuick.Core.Mvc.Session.Tests.pas
File metadata and controls
184 lines (148 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
unit Quick.Core.Mvc.Session.Tests;
{ Tests: T-SS-01..12
T-SS-01 CreateSession returns a non-nil ISession
T-SS-02 New session Id is non-empty
T-SS-03 New session IsAvailable = True
T-SS-04 SetString / GetString round-trip
T-SS-05 SetInteger / GetInteger round-trip
T-SS-06 SetFloat / GetFloat round-trip (exact equality via Int cast)
T-SS-07 GetString for unknown key returns empty string
T-SS-08 GetInteger for unknown key returns 0
T-SS-09 Remove deletes a stored key
T-SS-10 Clear empties all keys in the session
T-SS-11 Overwriting a key stores the latest value
T-SS-12 SessionExists returns True after session is created
}
interface
uses
DUnitX.TestFramework,
System.SysUtils,
Quick.Core.Mvc.Session;
type
[TestFixture]
TSessionTests = class
private
fStore : TInMemorySessionStore;
fSession : ISession;
public
[Setup]
procedure SetUp;
[TearDown]
procedure TearDown;
[Test]
procedure T_SS_01_CreateSession_ReturnsNonNil;
[Test]
procedure T_SS_02_NewSession_IdIsNonEmpty;
[Test]
procedure T_SS_03_NewSession_IsAvailableTrue;
[Test]
procedure T_SS_04_SetString_GetString_RoundTrip;
[Test]
procedure T_SS_05_SetInteger_GetInteger_RoundTrip;
[Test]
procedure T_SS_06_SetFloat_GetFloat_RoundTrip;
[Test]
procedure T_SS_07_GetString_UnknownKey_ReturnsEmpty;
[Test]
procedure T_SS_08_GetInteger_UnknownKey_ReturnsZero;
[Test]
procedure T_SS_09_Remove_DeletesKey;
[Test]
procedure T_SS_10_Clear_EmptiesAllKeys;
[Test]
procedure T_SS_11_Overwrite_ReturnsLatestValue;
[Test]
procedure T_SS_12_SessionExists_ReturnsTrueAfterCreate;
end;
implementation
{ TSessionTests }
procedure TSessionTests.SetUp;
begin
fStore := TInMemorySessionStore.Create(300); // 5-min timeout
fSession := fStore.CreateSession('test-session-id', 300, True);
end;
procedure TSessionTests.TearDown;
begin
fSession := nil;
fStore.Free;
end;
// ─── T-SS-01 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_01_CreateSession_ReturnsNonNil;
begin
Assert.IsNotNull(fSession, 'CreateSession should return a non-nil ISession');
end;
// ─── T-SS-02 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_02_NewSession_IdIsNonEmpty;
begin
Assert.IsNotEmpty(fSession.Id, 'Session Id should not be empty');
end;
// ─── T-SS-03 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_03_NewSession_IsAvailableTrue;
begin
Assert.IsTrue(fSession.IsAvailable, 'Newly created session should be available');
end;
// ─── T-SS-04 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_04_SetString_GetString_RoundTrip;
begin
fSession.SetString('user', 'Alice');
Assert.AreEqual('Alice', fSession.GetString('user'));
end;
// ─── T-SS-05 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_05_SetInteger_GetInteger_RoundTrip;
begin
fSession.SetInteger('count', 42);
Assert.AreEqual(Int64(42), fSession.GetInteger('count'));
end;
// ─── T-SS-06 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_06_SetFloat_GetFloat_RoundTrip;
begin
fSession.SetFloat('score', 9.5);
// Float round-trip via string may differ slightly; compare truncated integer part
Assert.AreEqual(Int64(9), Trunc(fSession.GetFloat('score')));
end;
// ─── T-SS-07 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_07_GetString_UnknownKey_ReturnsEmpty;
begin
Assert.AreEqual('', fSession.GetString('__no_such_key__'),
'GetString for unknown key should return empty string');
end;
// ─── T-SS-08 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_08_GetInteger_UnknownKey_ReturnsZero;
begin
Assert.AreEqual(Int64(0), fSession.GetInteger('__no_such_key__'),
'GetInteger for unknown key should return 0');
end;
// ─── T-SS-09 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_09_Remove_DeletesKey;
begin
fSession.SetString('temp', 'value');
fSession.Remove('temp');
Assert.AreEqual('', fSession.GetString('temp'),
'Key should be empty after Remove');
end;
// ─── T-SS-10 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_10_Clear_EmptiesAllKeys;
begin
fSession.SetString('a', '1');
fSession.SetString('b', '2');
fSession.Clear;
Assert.AreEqual('', fSession.GetString('a'), 'After Clear, key ''a'' should be gone');
Assert.AreEqual('', fSession.GetString('b'), 'After Clear, key ''b'' should be gone');
end;
// ─── T-SS-11 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_11_Overwrite_ReturnsLatestValue;
begin
fSession.SetString('name', 'Alice');
fSession.SetString('name', 'Bob');
Assert.AreEqual('Bob', fSession.GetString('name'),
'Second SetString should overwrite the first');
end;
// ─── T-SS-12 ─────────────────────────────────────────────────────────────────
procedure TSessionTests.T_SS_12_SessionExists_ReturnsTrueAfterCreate;
begin
Assert.IsTrue(fStore.SessionExists('test-session-id'),
'SessionExists should return True for a session just created');
end;
initialization
TDUnitX.RegisterTestFixture(TSessionTests);
end.