You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modules.py 2.3 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. Tests for modules:
  3. To execute webnotes/tests.py webnotes.tests.modules
  4. Uses Sandbox DocType for testing
  5. """
  6. import unittest
  7. import webnotes
  8. from webnotes.modules import Module
  9. class ModuleTest(unittest.TestCase):
  10. def setUp(self):
  11. webnotes.conn.begin()
  12. def update_timestamp(self, path):
  13. """
  14. Just open a file for write and save it so that the timetamp changes
  15. """
  16. import os
  17. path = os.path.join(Module('core').get_path(), path)
  18. c = file(path,'r').read()
  19. file(path,'w').write(c)
  20. def test_module_import(self):
  21. """
  22. Import a txt file into the database and check if a new column has been added
  23. """
  24. webnotes.conn.rollback()
  25. webnotes.conn.sql("alter table tabSandbox drop column `to_be_dropped`", ignore_ddl=1)
  26. webnotes.conn.begin()
  27. # delete from table
  28. webnotes.conn.sql("delete from tabDocField where parent='Sandbox' and fieldname='to_be_dropped'")
  29. self.update_timestamp('doctype/sandbox/sandbox.txt')
  30. # reload
  31. Module('core').reload('DocType','Sandbox')
  32. # commit re-adding
  33. webnotes.conn.commit()
  34. # check if column created
  35. self.assertTrue('to_be_dropped' in [r[0] for r in webnotes.conn.sql("desc tabSandbox")])
  36. # check if imported in table
  37. self.assertTrue(len(webnotes.conn.sql("select name from tabDocField where parent='Sandbox' and fieldname='to_be_dropped'"))==1)
  38. def test_read_js_code(self):
  39. """
  40. Read a js code file
  41. """
  42. data = Module('core').get_doc_file('DocType','Sandbox','.js').read()
  43. self.assertTrue('//test3456' in data)
  44. self.assertTrue('//import3456' in data)
  45. def test_sql_file(self):
  46. """
  47. Test an sql file
  48. """
  49. webnotes.conn.rollback()
  50. webnotes.conn.sql("drop trigger if exists sandbox_trigger")
  51. self.update_timestamp('doctype/sandbox/my_trigger.sql')
  52. Module('core').get_file('doctype','sandbox','my_trigger.sql').sync()
  53. self.assertTrue(webnotes.conn.sql("show triggers like 'tabSandbox'")[0][0]=='sandbox_trigger')
  54. def test_sync_all(self):
  55. """
  56. Test sync all (rerun the sql file test calling sync_all)
  57. """
  58. webnotes.conn.rollback()
  59. webnotes.conn.sql("drop trigger if exists sandbox_trigger")
  60. self.update_timestamp('doctype/sandbox/my_trigger.sql')
  61. Module('core').sync_all()
  62. self.assertTrue(webnotes.conn.sql("show triggers like 'tabSandbox'")[0][0]=='sandbox_trigger')
  63. def tearDown(self):
  64. if webnotes.conn.in_transaction:
  65. webnotes.conn.rollback()