annotate tests/test_storage.nim @ 1121:21690a5aa280

del: unused sqlite library
author sam <sam@basx.dev>
date Sat, 27 Apr 2024 17:21:47 +0700
parents 1205e7757732
children 71315636ba82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
1 import std/os
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
2 import std/strformat
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
3
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
4 import semicongine
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
5
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
6 proc testSimple(storage: StorageType) =
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
7 const TEST_VALUE = 42
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
8 const KEY = "test"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
9
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
10 # get default
1099
1205e7757732 add: enforce adding default value for storage loads
sam <sam@basx.dev>
parents: 1098
diff changeset
11 assert storage.load(KEY, 0) == default(type(TEST_VALUE))
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
12
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
13 # save and load custom
1098
45cf94a6535c did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents: 1097
diff changeset
14 store(storage, KEY, TEST_VALUE)
1099
1205e7757732 add: enforce adding default value for storage loads
sam <sam@basx.dev>
parents: 1098
diff changeset
15 assert storage.load(KEY, 0) == TEST_VALUE
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
16
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
17 proc stressTest(storage: StorageType) =
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
18 for i in 1 .. 10000:
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
19 let key = &"key-{i}"
1098
45cf94a6535c did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents: 1097
diff changeset
20 store(storage, key, i)
1099
1205e7757732 add: enforce adding default value for storage loads
sam <sam@basx.dev>
parents: 1098
diff changeset
21 assert storage.load(key, 0) == i
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
22
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
23 proc main() =
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
24 SystemStorage.purge()
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
25 echo "SystemStorage: Testing simple store/load"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
26 SystemStorage.testSimple()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
27
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
28 UserStorage.purge()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
29 echo "UserStorage: Testing simple store/load"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
30 UserStorage.testSimple()
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
31
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
32 echo "Stress test with 10'000 saves/loads"
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
33 SystemStorage.stressTest()
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
34
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
35 SystemStorage.purge()
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
36 UserStorage.purge()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
37
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
38
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
39 when isMainModule:
1098
45cf94a6535c did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents: 1097
diff changeset
40 main()