db.test.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: db.test
  5. # AUTHOR(S): Radim Blazek
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: Test database driver
  8. # COPYRIGHT: (C) 2004-2014 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (version 2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. #%module
  16. #% description: Test database driver, database must exist and set by db.connect.
  17. #% keyword: database
  18. #% keyword: attribute table
  19. #%end
  20. #%option
  21. #% key: test
  22. #% type: string
  23. #% description: Test name
  24. #% required: yes
  25. #% options: test1
  26. #%end
  27. import sys
  28. import os
  29. from grass.script import core as grass
  30. from grass.script import db as grassdb
  31. from grass.exceptions import CalledModuleError
  32. def main():
  33. test_file = options['test']
  34. expected = grass.tempfile()
  35. result = grass.tempfile()
  36. dbconn = grassdb.db_connection()
  37. grass.message(_("Using DB driver: %s") % dbconn['driver'])
  38. infile = os.path.join(os.environ['GISBASE'], 'etc', 'db.test', test_file)
  39. inf = file(infile)
  40. while True:
  41. type = inf.readline()
  42. if not type:
  43. break
  44. type = type.rstrip('\r\n')
  45. sql = inf.readline().rstrip('\r\n')
  46. sys.stdout.write(sql + '\n')
  47. # Copy expected result to temp file
  48. try:
  49. if type == 'X':
  50. grass.write_command('db.execute', input = '-', stdin = sql + '\n')
  51. else:
  52. resf = file(result, 'w')
  53. grass.write_command('db.select', input = '-', flags = 'c', stdin = sql + '\n', stdout = resf)
  54. resf.close()
  55. except CalledModuleError:
  56. grass.error("EXECUTE: ******** ERROR ********")
  57. else:
  58. grass.message(_("EXECUTE: OK"))
  59. expf = file(expected, 'w')
  60. while True:
  61. res = inf.readline().rstrip('\r\n')
  62. if not res:
  63. break
  64. expf.write(res + '\n')
  65. expf.close()
  66. if type == 'S':
  67. if grass.call(['diff', result, expected]) != 0:
  68. grass.error("RESULT: ******** ERROR ********")
  69. else:
  70. grass.message(_("RESULT: OK"))
  71. if __name__ == "__main__":
  72. options, flags = grass.parser()
  73. main()