test_csv.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. """Test v.in.ascii CSV capabilities
  2. :author: Vaclav Petras
  3. """
  4. import os
  5. from grass.gunittest.case import TestCase
  6. from grass.gunittest.main import test
  7. from grass.script.core import read_command
  8. INPUT_NOQUOTES = """Id,POINT_X,POINT_Y,Category,ED field estimate
  9. 100,437343.6704,4061363.41525,High Erosion,Low Deposition
  10. 101,453643.127906,4050070.29852,High Erosion,Low Erosion
  11. 102,454903.605427,4049480.80568,High Erosion,High Erosion
  12. 105,437734.838807,4060493.98315,High Erosion,Low Erosion
  13. 107,450833.019732,4048207.02664,High Erosion,Low Erosion
  14. """
  15. INPUT_DOUBLEQUOTES = """Id,POINT_X,POINT_Y,Category,"ED field estimate"
  16. 100,437343.6704,4061363.41525,"High Erosion","Low Deposition"
  17. 101,453643.127906,4050070.29852,"High Erosion","Low Erosion"
  18. 102,454903.605427,4049480.80568,"High Erosion","High Erosion"
  19. 105,437734.838807,4060493.98315,"High Erosion","Low Erosion"
  20. 107,450833.019732,4048207.02664,"High Erosion","Low Erosion"
  21. """
  22. INPUT_TSV = """Id\tPOINT_X\tPOINT_Y\tCategory\t"ED field estimate"
  23. 100\t437343.6704\t4061363.41525\t"High Erosion"\t"Low Deposition"
  24. 101\t453643.127906\t4050070.29852\t"High Erosion"\t"Low Erosion"
  25. 102\t454903.605427\t4049480.80568\t"High Erosion"\t"High Erosion"
  26. 105\t437734.838807\t4060493.98315\t"High Erosion"\t"Low Erosion"
  27. 107\t450833.019732\t4048207.02664\t"High Erosion"\t"Low Erosion"
  28. """
  29. INPUT_UNCOMMON = """Id@POINT_X@POINT_Y@Category@^ED field estimate^
  30. 100@437343.6704@4061363.41525@^High Erosion^@^Low Deposition^
  31. 101@453643.127906@4050070.29852@^High Erosion^@^Low Erosion^
  32. 102@454903.605427@4049480.80568@^High Erosion^@^High Erosion^
  33. 105@437734.838807@4060493.98315@^High Erosion^@^Low Erosion^
  34. 107@450833.019732@4048207.02664@^High Erosion^@^Low Erosion^
  35. """
  36. TABLE_1 = """cat|x|y|ed_cat|field_estimate
  37. 100|437343.6704|4061363.41525|High Erosion|Low Deposition
  38. 101|453643.127906|4050070.29852|High Erosion|Low Erosion
  39. 102|454903.605427|4049480.80568|High Erosion|High Erosion
  40. 105|437734.838807|4060493.98315|High Erosion|Low Erosion
  41. 107|450833.019732|4048207.02664|High Erosion|Low Erosion
  42. """
  43. class SimpleCsvTestCase(TestCase):
  44. xyvector = 'yxvetor_test'
  45. def tearDown(self):
  46. """Remove the vector map after each test method"""
  47. self.runModule('g.remove', flags='f', type='vector',
  48. name=self.xyvector)
  49. def test_no_text_delimeter(self):
  50. """Test type of resulting map"""
  51. self.assertModule(
  52. 'v.in.ascii', input='-', output=self.xyvector,
  53. separator='comma', skip=1, x=2, y=3, cat=1,
  54. columns="cat int, x double, y double,"
  55. " ed_cat varchar(20), field_estimate varchar(20)",
  56. stdin_=INPUT_NOQUOTES)
  57. category = read_command('v.db.select', map=self.xyvector,
  58. separator='pipe')
  59. self.assertEqual(first=TABLE_1.replace('\n', os.linesep),
  60. second=category,
  61. msg="Attribute table has wrong entries")
  62. def test_text_delimeter(self):
  63. """Test loading CSV with text delimiter
  64. Text delimiter added in r63581
  65. """
  66. self.assertModule(
  67. 'v.in.ascii', input='-', output=self.xyvector,
  68. separator='comma', text='doublequote',
  69. skip=1, x=2, y=3, cat=1,
  70. columns="cat int, x double, y double,"
  71. " ed_cat varchar(20), field_estimate varchar(20)",
  72. stdin_=INPUT_DOUBLEQUOTES)
  73. category = read_command('v.db.select', map=self.xyvector,
  74. separator='pipe')
  75. self.assertEqual(first=TABLE_1.replace('\n', os.linesep),
  76. second=category,
  77. msg="Attribute table has wrong entries")
  78. # TODO: a general method to compare attribute tables? (might need to solve because of floats)
  79. # TODO: standardize string strip? perhaps discourage, it messes up the diff
  80. # TODO: use replace solution for newlines in lib (compare to current one)
  81. def test_tsv(self):
  82. """Test loading TSV (CSV with tab as delim)
  83. Using double quote character for quote.
  84. """
  85. self.assertModule(
  86. 'v.in.ascii', input='-', output=self.xyvector,
  87. separator='tab', text='"',
  88. skip=1, x=2, y=3, cat=1,
  89. columns="cat int, x double, y double,"
  90. " ed_cat varchar(20), field_estimate varchar(20)",
  91. stdin_=INPUT_TSV)
  92. category = read_command('v.db.select', map=self.xyvector,
  93. separator='pipe')
  94. self.assertEqual(first=TABLE_1.replace('\n', os.linesep),
  95. second=category,
  96. msg="Attribute table has wrong entries")
  97. def test_uncommon_delims(self):
  98. """Test loading CSV with uncommon delimiters"""
  99. self.assertModule(
  100. 'v.in.ascii', input='-', output=self.xyvector,
  101. separator='@', text='^',
  102. skip=1, x=2, y=3, cat=1,
  103. columns="cat int, x double, y double,"
  104. " ed_cat varchar(20), field_estimate varchar(20)",
  105. stdin_=INPUT_UNCOMMON)
  106. category = read_command('v.db.select', map=self.xyvector,
  107. separator='pipe')
  108. self.assertEqual(first=TABLE_1.replace('\n', os.linesep),
  109. second=category,
  110. msg="Attribute table has wrong entries")
  111. INPUT_DELIM_IN_TEXT = """Id,POINT_X,POINT_Y,Category,"ED field estimate"
  112. 100,437343.6704,4061363.41525,"High Erosion, Low Canopy","Low Deposition, Low Canopy"
  113. 101,453643.127906,4050070.29852,"High Erosion, High Canopy","Low Erosion, Low Canopy"
  114. 102,454903.605427,4049480.80568,"High Erosion, High Canopy","High Erosion, Low Canopy"
  115. 105,437734.838807,4060493.98315,"High Erosion, Low Canopy","Low Erosion, High Canopy"
  116. 107,450833.019732,4048207.02664,"High Erosion, Low Canopy","Low Erosion, High Canopy"
  117. """
  118. TABLE_2 = """cat|x|y|ed_cat|field_estimate
  119. 100|437343.6704|4061363.41525|High Erosion, Low Canopy|Low Deposition, Low Canopy
  120. 101|453643.127906|4050070.29852|High Erosion, High Canopy|Low Erosion, Low Canopy
  121. 102|454903.605427|4049480.80568|High Erosion, High Canopy|High Erosion, Low Canopy
  122. 105|437734.838807|4060493.98315|High Erosion, Low Canopy|Low Erosion, High Canopy
  123. 107|450833.019732|4048207.02664|High Erosion, Low Canopy|Low Erosion, High Canopy
  124. """
  125. class AdvancedCsvTestCase(TestCase):
  126. xyvector = 'yxvetor_test'
  127. def tearDown(self):
  128. """Remove the vector map after each test method"""
  129. self.runModule('g.remove', flags='f', type='vector',
  130. name=self.xyvector)
  131. def test_delimeter_in_text(self):
  132. """Test loading CSV with delimiter in text
  133. Text delimiter added in r63581
  134. """
  135. self.assertModule(
  136. 'v.in.ascii', input='-', output=self.xyvector,
  137. separator='comma', text='doublequote',
  138. skip=1, x=2, y=3, cat=1,
  139. columns="cat int, x double, y double,"
  140. " ed_cat varchar(40), field_estimate varchar(40)",
  141. stdin_=INPUT_DELIM_IN_TEXT)
  142. category = read_command('v.db.select', map=self.xyvector,
  143. separator='pipe')
  144. self.assertEqual(first=TABLE_2.replace('\n', os.linesep),
  145. second=category,
  146. msg="Attribute table has wrong entries")
  147. if __name__ == '__main__':
  148. test()