db.test.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 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. #% keywords: database
  18. #% keywords: 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. def main():
  32. test_file = options['test']
  33. expected = grass.tempfile()
  34. result = grass.tempfile()
  35. dbconn = grassdb.db_connection()
  36. grass.message(_("Using DB driver: %s") % dbconn['driver'])
  37. infile = os.path.join(os.environ['GISBASE'], 'etc', 'db.test', test_file)
  38. inf = file(infile)
  39. while True:
  40. type = inf.readline()
  41. if not type:
  42. break
  43. type = type.rstrip('\r\n')
  44. sql = inf.readline().rstrip('\r\n')
  45. sys.stdout.write(sql + '\n')
  46. # Copy expected result to temp file
  47. if type == 'X':
  48. r = grass.write_command('db.execute', input = '-', stdin = sql + '\n')
  49. else:
  50. resf = file(result, 'w')
  51. r = grass.write_command('db.select', input = '-', flags = 'c', stdin = sql + '\n', stdout = resf)
  52. resf.close()
  53. if r != 0:
  54. grass.error("EXECUTE: ******** ERROR ********")
  55. else:
  56. grass.message(_("EXECUTE: OK"))
  57. expf = file(expected, 'w')
  58. while True:
  59. res = inf.readline().rstrip('\r\n')
  60. if not res:
  61. break
  62. expf.write(res + '\n')
  63. expf.close()
  64. if type == 'S':
  65. if grass.call(['diff', result, expected]) != 0:
  66. grass.error("RESULT: ******** ERROR ********")
  67. else:
  68. grass.message(_("RESULT: OK"))
  69. if __name__ == "__main__":
  70. options, flags = grass.parser()
  71. main()