暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os
  2. import logging
  3. import shutil
  4. from django.conf import settings
  5. from Database import sqlsetup
  6. _TESTDB_DEFAULT_DIR = '/tmp/'
  7. _TESTDB_DEFAULT_NAME = 'lodel2_test_db.sqlite'
  8. _TESTDB_COPY_SUFFIX = '_bck'
  9. ## Make a copy of an empty Db created by Database::SQLSetup::initDb()
  10. #
  11. # Usage example :
  12. # #Using 'default' as dbconfname
  13. # def setUpModule():
  14. # dbname = 'supertestdb'
  15. # initTestDb(dbname)
  16. # setDbConf(dbname)
  17. # ...
  18. #
  19. # #Using another name as dbconfname
  20. # def setUpModule():
  21. # dbname = 'superdbtest'
  22. # initTestDb(dbname)
  23. # setDbConf(dbname, 'mytest_dbconf')
  24. #
  25. # db_engine = sqlutils.getEngine('mytest_dbconf')
  26. # EmComponent.dbconf = 'mytest_dbconf'
  27. # #WARNING !!! This example as few chances to work due to the bad isolation of data sources in EmComponent... Will be solved with the EM object
  28. #
  29. def initTestDb(dbname = _TESTDB_DEFAULT_NAME, dbdir = _TESTDB_DEFAULT_DIR):
  30. dbname = os.path.join(dbdir, dbname)
  31. db_default = os.path.join(_TESTDB_DEFAULT_DIR, _TESTDB_DEFAULT_NAME)
  32. db_copy = os.path.join(_TESTDB_DEFAULT_DIR, _TESTDB_DEFAULT_NAME+_TESTDB_COPY_SUFFIX)
  33. if not os.path.isfile(db_copy):
  34. #The 'backup' copy didn't exists yet. Create it.
  35. settings.LODEL2SQLWRAPPER['db']['dbtest_default'] = {
  36. 'ENGINE': 'sqlite',
  37. 'NAME': db_default,
  38. }
  39. sqlsetup.init_db(dbconfname = 'dbtest_default')
  40. #Make the backup copy
  41. shutil.copyfile(db_default, db_copy)
  42. #Copy the Db at the wanted location
  43. shutil.copyfile(db_copy, dbname)
  44. return dbname
  45. def copyDb(dbname_src, dbname_dst, dbdir_src = _TESTDB_DEFAULT_DIR, dbdir_dst = _TESTDB_DEFAULT_DIR):
  46. dbname_src = os.path.join(dbdir_src, dbname_src)
  47. dbname_dst = os.path.join(dbdir_dst, dbname_dst)
  48. shutil.copyfile(dbname_src, dbname_dst)
  49. pass
  50. def saveDbState(dbname = _TESTDB_DEFAULT_NAME, dbdir = _TESTDB_DEFAULT_DIR):
  51. dbname = os.path.join(dbdir, dbname)
  52. shutil.copyfile(dbname, dbname+_TESTDB_COPY_SUFFIX)
  53. pass
  54. def restoreDbState(dbname = _TESTDB_DEFAULT_NAME, dbdir = _TESTDB_DEFAULT_DIR):
  55. dbname = os.path.join(dbdir, dbname)
  56. shutil.copyfile(dbname+_TESTDB_COPY_SUFFIX, dbname)
  57. pass
  58. def cleanDb(dbname = _TESTDB_DEFAULT_NAME, dbdir = _TESTDB_DEFAULT_DIR):
  59. dbname = os.path.join(dbdir, dbname)
  60. try: os.unlink(dbname)
  61. except:pass
  62. try: os.unlink(dbname+_TESTDB_COPY_SUFFIX)
  63. except:pass
  64. pass
  65. def setDbConf(dbname, dbconfname = 'default', dbdir = _TESTDB_DEFAULT_DIR):
  66. settings.LODEL2SQLWRAPPER['db'][dbconfname] = {
  67. 'ENGINE': 'sqlite',
  68. 'NAME': os.path.join(_TESTDB_DEFAULT_DIR, dbname)
  69. }
  70. pass