Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. """
  2. This script demonstrates how pytest-runner facilitates
  3. the simple execution of complex tasks with their
  4. dependencies.
  5.  
  6. Save this file and invoke it
  7. under Python (i.e. ``python test-mongodb-records.py``).
  8.  
  9. It creates a MongoDB instance, and then runs some
  10. assertions against it.
  11.  
  12. MongoDB must be installed to a typical location or
  13. available on PATH.
  14.  
  15. As it uses jaraco.mongodb, set MONGODB_HOME to
  16. specify the MongoDB version to use for the ephemeral
  17. instance.
  18.  
  19. Running this script will download the necessary
  20. requirements to ./.eggs.
  21. """
  22.  
  23. setup_params = dict(
  24. setup_requires=[
  25. 'pytest_runner',
  26. ],
  27. tests_require=[
  28. 'pytest',
  29. 'jaraco.mongodb>=3.10',
  30. ],
  31. )
  32.  
  33. if __name__ == '__main__':
  34. import sys
  35. # note that sys.argv[0] is the script name
  36. opts = ' '.join(sys.argv)
  37. sys.argv[1:] = ['pytest', '--addopts=' + opts]
  38. __import__('setuptools').setup(**setup_params)
  39. raise SystemExit(0)
  40.  
  41.  
  42. import random
  43. import itertools
  44.  
  45. import pytest
  46. from jaraco.mongodb.testing import assert_covered
  47.  
  48.  
  49. @pytest.fixture
  50. def docs_in_db(mongodb_instance):
  51. """
  52. Install 100 records with random numbers
  53. """
  54. conn = mongodb_instance.get_connection()
  55. coll = conn.test_db.test_coll
  56. coll.drop()
  57. coll.create_index('number')
  58. n_records = 100
  59. for n in itertools.islice(itertools.count(), n_records):
  60. doc = dict(
  61. number=random.randint(0, 2**32-1),
  62. value='some value',
  63. )
  64. conn.test_db.test_coll.insert(doc)
  65. return coll
  66.  
  67.  
  68. def test_records(docs_in_db, mongodb_instance):
  69. "Test 100 records are present and query is covered"
  70. # load all the numbers to ensure the index is in RAM
  71. _hint = 'number_1'
  72. _filter = {'number': {'$gt': 0}}
  73. _projection = {'number': True, '_id': False}
  74. cur = docs_in_db.find(filter=_filter, projection=_projection).hint(_hint)
  75. assert_covered(cur)
  76.  
  77. # consume the cursor for good measure
  78. docs = list(cur)
  79. assert len(docs) == 100
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement