flashdb_utest.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import flashdb
  2. import unittest
  3. import struct
  4. kvdb=None
  5. fdb=flashdb.FDB()
  6. DB_PATH = "test/out/fdb_kvdb"
  7. class FlashDBUnitTest(unittest.TestCase):
  8. def test_boot_count1(self):
  9. print('test boot_count increment 1')
  10. if fdb is None:
  11. fdb = flashdb.FDB()
  12. boot_count=0
  13. boot_count_blob = struct.pack('i', boot_count)
  14. boot_times = [0,1,2,3,0,0,0,0,0,0]
  15. boot_time_tuple = tuple(boot_times)
  16. boot_time_blob = struct.pack('@10Q', *boot_time_tuple)
  17. default_kv={
  18. 'username': 'armink', # string KV
  19. 'password': "123456", # string KV
  20. 'boot_count': boot_count_blob, # int type kv
  21. 'boot_time': boot_time_blob, # array type kv
  22. }
  23. #print(default_kv)
  24. kvdb = fdb.kvdb_init("env", DB_PATH, default_kv, None)
  25. res = fdb.kv_get_blob(kvdb, "boot_count", len(boot_count_blob))
  26. self.assertIsNotNone(res)
  27. boot_count = struct.unpack("i", res)[0]
  28. boot_count= boot_count+1
  29. boot_count_blob = struct.pack('i', boot_count)
  30. fdb.kv_set_blob(kvdb, "boot_count", boot_count_blob)
  31. res = fdb.kv_get_blob(kvdb, "boot_count", len(boot_count_blob))
  32. self.assertIsNotNone(res)
  33. new_boot_count = struct.unpack("i", res)[0]
  34. self.assertEqual(new_boot_count, boot_count)
  35. def xtest_boot_count2(self):
  36. print('test boot_count increment 2')
  37. if fdb is None:
  38. fdb = flashdb.FDB()
  39. boot_count=0
  40. boot_count_fmt='i'
  41. boot_count_blob = struct.pack(boot_count_fmt, boot_count)
  42. boot_count_size = len(boot_count_blob)
  43. boot_time = [0,1,2,3,0,0,0,0,0,0]
  44. boot_time_fmt='@10Q'
  45. boot_time_tuple = tuple(boot_time)
  46. boot_time_blob = struct.pack(boot_time_fmt, *boot_time_tuple)
  47. default_kv={
  48. 'username': 'armink', # string KV
  49. 'password': "123456", # string KV
  50. 'boot_count': boot_count_blob, # int type kv
  51. 'boot_time': boot_time_blob, # array type kv
  52. }
  53. #print(default_kv)
  54. kvdb = fdb.kvdb_init("env", DB_PATH, default_kv, None)
  55. boot_count = fdb.kv_get_by_fmt(kvdb, "boot_count", boot_count_size, boot_count_fmt)
  56. self.assertIsNotNone(boot_count)
  57. print("==================== kvdb_basic_sample ====================")
  58. print( "get the 'boot_count' value is %d" % boot_count)
  59. boot_count = boot_count +1
  60. res =fdb.kv_set_by_fmt(kvdb, "boot_count", boot_count, boot_count_fmt)
  61. new_boot_count = fdb.kv_get_by_fmt(kvdb, "boot_count", boot_count_size, boot_count_fmt)
  62. self.assertIsNotNone(new_boot_count)
  63. print( "get the 'boot_count' value is %d" % new_boot_count)
  64. print("===========================================================")
  65. self.assertEqual(new_boot_count, boot_count)
  66. def utest():
  67. suit = unittest.TestSuite("test1")
  68. suit.addTest(FlashDBUnitTest())
  69. runner = unittest.TextTestRunner()
  70. res = runner.run(suit)
  71. utest()