db.test.py 1.9 KB

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