Development of an internal social media platform with personalised dashboards for students
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.

test_mongodb.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from __future__ import absolute_import
  2. from kombu import Connection
  3. from kombu.tests.case import Case, SkipTest, Mock, skip_if_not_module
  4. class MockConnection(dict):
  5. def __setattr__(self, key, value):
  6. self[key] = value
  7. class test_mongodb(Case):
  8. def _get_connection(self, url, **kwargs):
  9. from kombu.transport import mongodb
  10. class _Channel(mongodb.Channel):
  11. def _create_client(self):
  12. self._client = Mock(name='client')
  13. class Transport(mongodb.Transport):
  14. Connection = MockConnection
  15. Channel = _Channel
  16. return Connection(url, transport=Transport, **kwargs).connect()
  17. @skip_if_not_module('pymongo')
  18. def test_defaults(self):
  19. url = 'mongodb://'
  20. c = self._get_connection(url)
  21. hostname, dbname, options = c.channels[0]._parse_uri()
  22. self.assertEqual(dbname, 'kombu_default')
  23. self.assertEqual(hostname, 'mongodb://127.0.0.1')
  24. @skip_if_not_module('pymongo')
  25. def test_custom_host(self):
  26. url = 'mongodb://localhost'
  27. c = self._get_connection(url)
  28. hostname, dbname, options = c.channels[0]._parse_uri()
  29. self.assertEqual(dbname, 'kombu_default')
  30. @skip_if_not_module('pymongo')
  31. def test_custom_database(self):
  32. url = 'mongodb://localhost/dbname'
  33. c = self._get_connection(url)
  34. hostname, dbname, options = c.channels[0]._parse_uri()
  35. self.assertEqual(dbname, 'dbname')
  36. @skip_if_not_module('pymongo')
  37. def test_custom_credentials(self):
  38. url = 'mongodb://localhost/dbname'
  39. c = self._get_connection(url, userid='foo', password='bar')
  40. hostname, dbname, options = c.channels[0]._parse_uri()
  41. self.assertEqual(hostname, 'mongodb://foo:bar@localhost/dbname')
  42. self.assertEqual(dbname, 'dbname')
  43. @skip_if_not_module('pymongo')
  44. def test_options(self):
  45. url = 'mongodb://localhost,localhost2:29017/dbname?fsync=true'
  46. c = self._get_connection(url)
  47. hostname, dbname, options = c.channels[0]._parse_uri()
  48. self.assertTrue(options['fsync'])
  49. @skip_if_not_module('pymongo')
  50. def test_real_connections(self):
  51. from pymongo.errors import ConfigurationError
  52. raise SkipTest(
  53. 'Test is functional: it actually connects to mongod')
  54. url = 'mongodb://localhost,localhost:29017/dbname'
  55. c = self._get_connection(url)
  56. client = c.channels[0].client
  57. nodes = client.connection.nodes
  58. # If there's just 1 node it is because we're connecting to a single
  59. # server instead of a repl / mongoss.
  60. if len(nodes) == 2:
  61. self.assertTrue(('localhost', 29017) in nodes)
  62. self.assertEqual(client.name, 'dbname')
  63. url = 'mongodb://localhost:27017,localhost2:29017/dbname'
  64. c = self._get_connection(url)
  65. client = c.channels[0].client
  66. # Login to admin db since there's no db specified
  67. url = 'mongodb://adminusername:adminpassword@localhost'
  68. c = self._get_connection()
  69. client = c.channels[0].client
  70. self.assertEqual(client.name, 'kombu_default')
  71. # Lets make sure that using admin db doesn't break anything
  72. # when no user is specified
  73. url = 'mongodb://localhost'
  74. c = self._get_connection(url)
  75. client = c.channels[0].client
  76. # Assuming there's user 'username' with password 'password'
  77. # configured in mongodb
  78. url = 'mongodb://username:password@localhost/dbname'
  79. c = self._get_connection(url)
  80. client = c.channels[0].client
  81. # Assuming there's no user 'nousername' with password 'nopassword'
  82. # configured in mongodb
  83. url = 'mongodb://nousername:nopassword@localhost/dbname'
  84. c = self._get_connection(url)
  85. with self.assertRaises(ConfigurationError):
  86. c.channels[0].client