annotate tests/test_storage.nim @ 1136:71315636ba82

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