test_sqlite3.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.sqlite3 as sqlite3
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase
  12. class MockSqlite3(object):
  13. """
  14. Mock sqlite3 class
  15. """
  16. version = "2.6.0"
  17. sqlite_version = "3.8.2"
  18. def __init__(self):
  19. self.dbase = None
  20. self.isolation_level = None
  21. def connect(self, dbase, isolation_level=None):
  22. """
  23. Mock connect method
  24. """
  25. self.dbase = dbase
  26. self.isolation_level = isolation_level
  27. return MockSqlite3()
  28. @staticmethod
  29. def cursor():
  30. """
  31. Mock connect method
  32. """
  33. return MockSqlite3()
  34. @staticmethod
  35. def execute(sql):
  36. """
  37. Mock connect method
  38. """
  39. return sql
  40. @staticmethod
  41. def fetchall():
  42. """
  43. Mock connect method
  44. """
  45. return True
  46. class Sqlite3TestCase(TestCase, LoaderModuleMockMixin):
  47. """
  48. TestCase for salt.modules.sqlite3
  49. """
  50. def setup_loader_modules(self):
  51. return {sqlite3: {"sqlite3": MockSqlite3()}}
  52. # 'version' function tests: 1
  53. def test_version(self):
  54. """
  55. Tests if it return version of pysqlite.
  56. """
  57. self.assertEqual(sqlite3.version(), "2.6.0")
  58. # 'sqlite_version' function tests: 1
  59. def test_sqlite_version(self):
  60. """
  61. Tests if it return version of sqlite.
  62. """
  63. self.assertEqual(sqlite3.sqlite_version(), "3.8.2")
  64. # 'modify' function tests: 1
  65. def test_modify(self):
  66. """
  67. Tests if it issue an SQL query to sqlite3 (with no return data).
  68. """
  69. self.assertFalse(sqlite3.modify())
  70. self.assertTrue(
  71. sqlite3.modify("/root/test.db", "CREATE TABLE test(id INT, testdata TEXT);")
  72. )
  73. # 'fetch' function tests: 1
  74. def test_fetch(self):
  75. """
  76. Tests if it retrieve data from an sqlite3 db
  77. (returns all rows, be careful!)
  78. """
  79. self.assertFalse(sqlite3.fetch())
  80. self.assertTrue(
  81. sqlite3.fetch("/root/test.db", "CREATE TABLE test(id INT, testdata TEXT);")
  82. )
  83. # 'tables' function tests: 1
  84. def test_tables(self):
  85. """
  86. Tests if it show all tables in the database.
  87. """
  88. self.assertFalse(sqlite3.tables())
  89. self.assertTrue(sqlite3.tables("/root/test.db"))
  90. # 'indices' function tests: 1
  91. def test_indices(self):
  92. """
  93. Tests if it show all indices in the database.
  94. """
  95. self.assertFalse(sqlite3.indices())
  96. self.assertTrue(sqlite3.indices("/root/test.db"))
  97. # 'indexes' function tests: 1
  98. def test_indexes(self):
  99. """
  100. Tests if it show all indices in the database,
  101. for people with poor spelling skills
  102. """
  103. self.assertTrue(sqlite3.indexes("/root/test.db"))