Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

105 rader
3.5 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. """
  23. Tests for modules:
  24. To execute webnotes/tests.py webnotes.tests.modules
  25. Uses Sandbox DocType for testing
  26. """
  27. import unittest
  28. import webnotes
  29. from webnotes.modules import Module
  30. class ModuleTest(unittest.TestCase):
  31. def setUp(self):
  32. webnotes.conn.begin()
  33. def update_timestamp(self, path):
  34. """
  35. Just open a file for write and save it so that the timetamp changes
  36. """
  37. import os
  38. path = os.path.join(Module('core').get_path(), path)
  39. c = file(path,'r').read()
  40. file(path,'w').write(c)
  41. def test_module_import(self):
  42. """
  43. Import a txt file into the database and check if a new column has been added
  44. """
  45. webnotes.conn.rollback()
  46. webnotes.conn.sql("alter table tabSandbox drop column `to_be_dropped`", ignore_ddl=1)
  47. webnotes.conn.begin()
  48. # delete from table
  49. webnotes.conn.sql("delete from tabDocField where parent='Sandbox' and fieldname='to_be_dropped'")
  50. self.update_timestamp('doctype/sandbox/sandbox.txt')
  51. # reload
  52. Module('core').reload('DocType','Sandbox')
  53. # commit re-adding
  54. webnotes.conn.commit()
  55. # check if column created
  56. self.assertTrue('to_be_dropped' in [r[0] for r in webnotes.conn.sql("desc tabSandbox")])
  57. # check if imported in table
  58. self.assertTrue(len(webnotes.conn.sql("select name from tabDocField where parent='Sandbox' and fieldname='to_be_dropped'"))==1)
  59. def test_read_js_code(self):
  60. """
  61. Read a js code file
  62. """
  63. data = Module('core').get_doc_file('DocType','Sandbox','.js').read()
  64. self.assertTrue('//test3456' in data)
  65. self.assertTrue('//import3456' in data)
  66. def test_sql_file(self):
  67. """
  68. Test an sql file
  69. """
  70. webnotes.conn.rollback()
  71. webnotes.conn.sql("drop trigger if exists sandbox_trigger")
  72. self.update_timestamp('doctype/sandbox/my_trigger.sql')
  73. Module('core').get_file('doctype','sandbox','my_trigger.sql').sync()
  74. self.assertTrue(webnotes.conn.sql("show triggers like 'tabSandbox'")[0][0]=='sandbox_trigger')
  75. def test_sync_all(self):
  76. """
  77. Test sync all (rerun the sql file test calling sync_all)
  78. """
  79. webnotes.conn.rollback()
  80. webnotes.conn.sql("drop trigger if exists sandbox_trigger")
  81. self.update_timestamp('doctype/sandbox/my_trigger.sql')
  82. Module('core').sync_all()
  83. self.assertTrue(webnotes.conn.sql("show triggers like 'tabSandbox'")[0][0]=='sandbox_trigger')
  84. def tearDown(self):
  85. if webnotes.conn.in_transaction:
  86. webnotes.conn.rollback()