annotate tests/test_storage.nim @ 1139:114f395b9144

did: finish refactoring and updated all tests accordingly
author sam <sam@basx.dev>
date Sat, 08 Jun 2024 14:58:25 +0700
parents 71315636ba82
children
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
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
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
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
13 Store(storage, KEY, TEST_VALUE)
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
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}"
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
19 Store(storage, key, i)
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
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() =
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
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
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
27 UserStorage.Purge()
986
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
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
34 SystemStorage.Purge()
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
35 UserStorage.Purge()
986
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()