annotate tests/test_storage.nim @ 990:1705e005cdee

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