mongodb.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import pymongo
  2. import traceback
  3. from colorama import Fore
  4. from toolset.utils.output_helper import log
  5. class Database:
  6. @staticmethod
  7. def get_current_world_table(config):
  8. '''
  9. Return a JSON object containing all 10,000 World items as they currently
  10. exist in the database. This is used for verifying that entries in the
  11. database have actually changed during an Update verification test.
  12. '''
  13. results_json = []
  14. try:
  15. worlds_json = {}
  16. print("DATABASE_HOST: %s" % config.database_host)
  17. connection = pymongo.MongoClient(
  18. host=config.database_host)
  19. db = connection.hello_world
  20. for world in db.world.find():
  21. if "randomNumber" in world:
  22. if "id" in world:
  23. worlds_json[str(int(world["id"]))] = int(
  24. world["randomNumber"])
  25. elif "_id" in world:
  26. worlds_json[str(int(world["_id"]))] = int(
  27. world["randomNumber"])
  28. results_json.append(worlds_json)
  29. connection.close()
  30. except Exception:
  31. tb = traceback.format_exc()
  32. log("ERROR: Unable to load current MongoDB World table.",
  33. color=Fore.RED)
  34. log(tb)
  35. return results_json
  36. @staticmethod
  37. def test_connection(config):
  38. try:
  39. connection = pymongo.MongoClient(host=config.database_host)
  40. db = connection.hello_world
  41. db.world.find()
  42. db.close()
  43. return True
  44. except:
  45. return False