Permalink
Browse files

PyMongo 3.x Support

PEP 8 fixes
Remove unnecassary commits
  • Loading branch information...
1 parent f914f47 commit 0768df8633e66e0afc1f876b0b6d6af4446b74ab @serhatbolsu serhatbolsu committed Jul 13, 2015
@@ -2,6 +2,7 @@
from mongoquery import MongoQuery
from version import VERSION
+
class MongoDBLibrary(MongoConnectionManager, MongoQuery):
"""
MongoDB Library contains utilities meant for Robot Framework's usage.
@@ -24,4 +25,3 @@ class MongoDBLibrary(MongoConnectionManager, MongoQuery):
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
ROBOT_LIBRARY_VERSION = VERSION
-
@@ -1,5 +1,4 @@
-import ConfigParser
-from robot.api import logger
+
class MongoConnectionManager(object):
"""
@@ -12,7 +11,8 @@ def __init__(self):
"""
self._dbconnection = None
- def connect_to_mongodb(self, dbHost='localhost', dbPort=27017, dbMaxPoolSize=10, dbNetworkTimeout=None, dbDocClass=dict, dbTZAware=False):
+ def connect_to_mongodb(self, dbHost='localhost', dbPort=27017, dbMaxPoolSize=10, dbNetworkTimeout=None,
+ dbDocClass=dict, dbTZAware=False):
"""
Loads pymongo and connects to the MongoDB host using parameters submitted.
@@ -24,7 +24,7 @@ def connect_to_mongodb(self, dbHost='localhost', dbPort=27017, dbMaxPoolSize=10,
"""
dbapiModuleName = 'pymongo'
- db_api_2 = __import__(dbapiModuleName);
+ db_api_2 = __import__(dbapiModuleName)
dbPort = int(dbPort)
#print "host is [ %s ]" % dbHost
@@ -35,9 +35,12 @@ def connect_to_mongodb(self, dbHost='localhost', dbPort=27017, dbMaxPoolSize=10,
#print "document_class is [ %s ]" % dbDocClass
#print "tz_aware is [ %s ]" % dbTZAware
print "| Connect To MondoDB | dbHost | dbPort | dbMaxPoolSize | dbNetworktimeout | dbDocClass | dbTZAware |"
- print "| Connect To MondoDB | %s | %s | %s | %s | %s | %s |" % (dbHost,dbPort,dbMaxPoolSize,dbNetworkTimeout,dbDocClass,dbTZAware)
+ print "| Connect To MondoDB | %s | %s | %s | %s | %s | %s |" % (dbHost, dbPort, dbMaxPoolSize, dbNetworkTimeout,
+ dbDocClass, dbTZAware)
- self._dbconnection = db_api_2.connection.Connection (host=dbHost, port=dbPort, max_pool_size=dbMaxPoolSize, network_timeout=dbNetworkTimeout, document_class=dbDocClass, tz_aware=dbTZAware);
+ self._dbconnection = db_api_2.MongoClient(host=dbHost, port=dbPort, socketTimeoutMS=dbNetworkTimeout,
+ document_class=dbDocClass, tz_aware=dbTZAware,
+ maxPoolSize=dbMaxPoolSize)
def disconnect_from_mongodb(self):
"""
@@ -47,5 +50,4 @@ def disconnect_from_mongodb(self):
| Disconnect From MongoDB | # disconnects from current connection to the MongoDB server |
"""
print "| Disconnect From MongoDB |"
- self._dbconnection.disconnect()
-
+ self._dbconnection.close()
@@ -16,14 +16,12 @@ def get_mongodb_databases(self):
| Log Many | @{allDBs} |
| Should Contain | ${allDBs} | DBName |
"""
- cur = None
try:
allDBs = self._dbconnection.database_names()
print "| @{allDBs} | Get Mongodb Databases |"
return allDBs
- finally :
- if cur :
- self._dbconnection.end_request()
+ except:
+ pass
def get_mongodb_collections(self, dbName):
"""
@@ -35,16 +33,15 @@ def get_mongodb_collections(self, dbName):
| Log Many | @{allCollections} |
| Should Contain | ${allCollections} | CollName |
"""
- db = None
try:
dbName = str(dbName)
db = self._dbconnection['%s' % (dbName,)]
allCollections = db.collection_names()
print "| @{allCollections} | Get MongoDB Collections | %s |" % (dbName)
return allCollections
- finally :
- if db :
- self._dbconnection.end_request()
+ except:
+ pass
+
def drop_mongodb_database(self, dbDelName):
"""
@@ -56,14 +53,12 @@ def drop_mongodb_database(self, dbDelName):
| @{allDBs} | Get MongoDB Collections | myDB |
| Should Not Contain | ${allDBs} | myDB |
"""
- cur = None
try:
dbDelName = str(dbDelName)
print "| Drop MongoDB Database | %s |" % (dbDelName)
self._dbconnection.drop_database('%s' % (dbDelName))
- finally :
- if cur :
- self._dbconnection.end_request()
+ except:
+ pass
def drop_mongodb_collection(self, dbName, dbCollName):
"""
@@ -75,15 +70,13 @@ def drop_mongodb_collection(self, dbName, dbCollName):
| @{allCollections} | Get MongoDB Collections | myDB |
| Should Not Contain | ${allCollections} | CollectionName |
"""
- db = None
try:
dbName = str(dbName)
db = self._dbconnection['%s' % (dbName,)]
db.drop_collection('%s' % (dbCollName))
print "| Drop MongoDB Collection | %s | %s |" % (dbName,dbCollName)
- finally :
- if db :
- self._dbconnection.end_request()
+ except:
+ pass
def validate_mongodb_collection(self, dbName, dbCollName):
"""
@@ -94,17 +87,15 @@ def validate_mongodb_collection(self, dbName, dbCollName):
| ${allResults} | Validate MongoDB Collection | DBName | CollectionName |
| Log | ${allResults} |
"""
- db = None
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
db = self._dbconnection['%s' % (dbName,)]
allResults = db.validate_collection('%s' % dbCollName)
print "| ${allResults} | Validate MongoDB Collection | %s | %s |" % (dbName,dbCollName)
return allResults
- finally :
- if db :
- self._dbconnection.end_request()
+ except:
+ pass
def get_mongodb_collection_count(self, dbName, dbCollName):
"""
@@ -114,7 +105,6 @@ def get_mongodb_collection_count(self, dbName, dbCollName):
| ${allResults} | Get MongoDB Collection Count | DBName | CollectionName |
| Log | ${allResults} |
"""
- db = None
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
@@ -123,9 +113,8 @@ def get_mongodb_collection_count(self, dbName, dbCollName):
count = coll.count()
print "| ${allResults} | Get MongoDB Collection Count | %s | %s |" % (dbName,dbCollName)
return count
- finally :
- if db :
- self._dbconnection.end_request()
+ except:
+ pass
def save_mongodb_records(self, dbName, dbCollName, recordJSON):
"""
@@ -145,7 +134,6 @@ def save_mongodb_records(self, dbName, dbCollName, recordJSON):
| ${allResults} | Save MongoDB Records | foo | bar | {"timestamp":1, "msg":"Hello 1"} |
| Log | ${allResults} |
"""
- db = None
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
@@ -157,9 +145,8 @@ def save_mongodb_records(self, dbName, dbCollName, recordJSON):
allResults = coll.save(recordJSON)
print "| ${allResults} | Save MongoDB Records | %s | %s | %s |" % (dbName,dbCollName,recordJSON)
return allResults
- finally :
- if db :
- self._dbconnection.end_request()
+ except:
+ pass
def retrieve_all_mongodb_records(self, dbName, dbCollName, returnDocuments=False):
"""
@@ -260,7 +247,6 @@ def retrieve_mongodb_records_with_desired_fields(self, dbName, dbCollName, recor
return self._retrieve_mongodb_records(dbName, dbCollName, recordJSON, data, returnDocuments)
def _retrieve_mongodb_records(self, dbName, dbCollName, recordJSON, fields=[], returnDocuments=False):
- db = None
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
@@ -278,9 +264,8 @@ def _retrieve_mongodb_records(self, dbName, dbCollName, recordJSON, fields=[], r
for d in results:
response = '%s%s' % (response, d.items())
return response
- finally :
- if db :
- self._dbconnection.end_request()
+ except:
+ pass
def remove_mongodb_records(self, dbName, dbCollName, recordJSON):
"""
@@ -301,7 +286,6 @@ def remove_mongodb_records(self, dbName, dbCollName, recordJSON):
| ${output} | Retrieve All MongoDB Records | ${MDBDB} | ${MDBColl} |
| Should Not Contain | ${output} | 'timestamp', 1 |
"""
- db = None
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
@@ -313,7 +297,5 @@ def remove_mongodb_records(self, dbName, dbCollName, recordJSON):
allResults = coll.remove(recordJSON)
print "| ${allResults} | Remove MongoDB Records | %s | %s | %s |" % (dbName,dbCollName,recordJSON)
return allResults
- finally :
- if db :
- self._dbconnection.end_request()
-
+ except:
+ pass
@@ -1,5 +1,4 @@
import time
-import atexit
import shutil
import tempfile
import subprocess
@@ -33,7 +32,7 @@ def start_mongodb(self):
time.sleep(0.1)
try:
print i
- self._conn = pymongo.Connection('localhost', self._mongodb_port)
+ self._conn = pymongo.MongoClient('localhost', self._mongodb_port)
except pymongo.errors.ConnectionFailure:
continue
else:

0 comments on commit 0768df8

Please sign in to comment.