Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python 3 support #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/MongoDBLibrary/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from mongo_connection_manager import MongoConnectionManager
from mongoquery import MongoQuery
from version import VERSION
from .mongo_connection_manager import MongoConnectionManager
from .mongoquery import MongoQuery
from .version import VERSION


class MongoDBLibrary(MongoConnectionManager, MongoQuery):
Expand All @@ -10,16 +10,16 @@ class MongoDBLibrary(MongoConnectionManager, MongoQuery):
This can allow you to query your Mongo database after an action has been made to verify the results.

References:

+ PyMongo 3.0.3 Documentation - http://api.mongodb.org/python/3.0.3/

Example Usage:
| Connect To MongoDB | foo.bar.org | ${27017} |
| ${QueryJSON} | Set Variable | {"name" : "username" ,"in_use": false} |
| ${UpdateJSON} | Set Variable | {"$set": {"in_use" : true}} |
| &{allResults} | Retrieve and Update One Mongodb Record | DBName | CollectionName | ${QueryJSON} | ${UpdateJSON} |
| Log | ${allResults} |
"""

ROBOT_LIBRARY_SCOPE = 'GLOBAL'
ROBOT_LIBRARY_VERSION = VERSION
22 changes: 12 additions & 10 deletions src/MongoDBLibrary/mongo_connection_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function

from robot.libraries.BuiltIn import BuiltIn


Expand All @@ -18,17 +20,17 @@ def connect_to_mongodb(self, dbHost='localhost', dbPort=27017, dbMaxPoolSize=10,
dbDocClass=dict, dbTZAware=False):
"""
Loads pymongo and connects to the MongoDB host using parameters submitted.

Example usage:
| # To connect to foo.bar.org's MongoDB service on port 27017 |
| Connect To MongoDB | foo.bar.org | ${27017} |
| # Or for an authenticated connection, note addtion of "mongodb://" to host uri |
| Connect To MongoDB | mongodb://admin:[email protected] | ${27017} |

"""
dbapiModuleName = 'pymongo'
db_api_2 = __import__(dbapiModuleName)

dbPort = int(dbPort)
#print "host is [ %s ]" % dbHost
#print "port is [ %s ]" % dbPort
Expand All @@ -37,20 +39,20 @@ def connect_to_mongodb(self, dbHost='localhost', dbPort=27017, dbMaxPoolSize=10,
#print "slave_okay is [ %s ]" % dbSlaveOkay
#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 | 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.MongoClient(host=dbHost, port=dbPort, socketTimeoutMS=dbNetworkTimeout,
document_class=dbDocClass, tz_aware=dbTZAware,
maxPoolSize=dbMaxPoolSize)

def disconnect_from_mongodb(self):
"""
Disconnects from the MongoDB server.

For example:
| Disconnect From MongoDB | # disconnects from current connection to the MongoDB server |
| Disconnect From MongoDB | # disconnects from current connection to the MongoDB server |
"""
print "| Disconnect From MongoDB |"
print("| Disconnect From MongoDB |")
self._dbconnection.close()
48 changes: 25 additions & 23 deletions src/MongoDBLibrary/mongoquery.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from __future__ import print_function

import json
from bson.objectid import ObjectId
from pymongo import ReturnDocument


class MongoQuery(object):
"""
Query handles all the querying done by the MongoDB Library.
Query handles all the querying done by the MongoDB Library.
"""

def get_mongodb_databases(self):
"""
Returns a list of all of the databases currently on the MongoDB
Returns a list of all of the databases currently on the MongoDB
server you are connected to.

Usage is:
Expand All @@ -19,7 +21,7 @@ def get_mongodb_databases(self):
| Should Contain | ${allDBs} | DBName |
"""
allDBs = self._dbconnection.database_names()
print "| @{allDBs} | Get Mongodb Databases |"
print("| @{allDBs} | Get Mongodb Databases |")
return allDBs

def get_mongodb_collections(self, dbName):
Expand All @@ -38,7 +40,7 @@ def get_mongodb_collections(self, dbName):
except TypeError:
self._builtin.fail("Connection failed, please make sure you have run 'Connect To Mongodb' first.")
allCollections = db.collection_names()
print "| @{allCollections} | Get MongoDB Collections | %s |" % dbName
print("| @{allCollections} | Get MongoDB Collections | %s |" % dbName)
return allCollections

def drop_mongodb_database(self, dbDelName):
Expand All @@ -52,7 +54,7 @@ def drop_mongodb_database(self, dbDelName):
| Should Not Contain | ${allDBs} | myDB |
"""
dbDelName = str(dbDelName)
print "| Drop MongoDB Database | %s |" % dbDelName
print("| Drop MongoDB Database | %s |" % dbDelName)
try:
self._dbconnection.drop_database('%s' % dbDelName)
except TypeError:
Expand All @@ -74,11 +76,11 @@ def drop_mongodb_collection(self, dbName, dbCollName):
except TypeError:
self._builtin.fail("Connection failed, please make sure you have run 'Connect To Mongodb' first.")
db.drop_collection('%s' % dbCollName)
print "| Drop MongoDB Collection | %s | %s |" % (dbName, dbCollName)
print("| Drop MongoDB Collection | %s | %s |" % (dbName, dbCollName))

def validate_mongodb_collection(self, dbName, dbCollName):
"""
Returns a string of validation info. Raises CollectionInvalid if
Returns a string of validation info. Raises CollectionInvalid if
validation fails.

Usage is:
Expand All @@ -92,7 +94,7 @@ def validate_mongodb_collection(self, dbName, dbCollName):
except TypeError:
self._builtin.fail("Connection failed, please make sure you have run 'Connect To Mongodb' first.")
allResults = db.validate_collection('%s' % dbCollName)
print "| ${allResults} | Validate MongoDB Collection | %s | %s |" % (dbName, dbCollName)
print("| ${allResults} | Validate MongoDB Collection | %s | %s |" % (dbName, dbCollName))
return allResults

def get_mongodb_collection_count(self, dbName, dbCollName):
Expand All @@ -111,15 +113,15 @@ def get_mongodb_collection_count(self, dbName, dbCollName):
self._builtin.fail("Connection failed, please make sure you have run 'Connect To Mongodb' first.")
coll = db['%s' % dbCollName]
count = coll.count()
print "| ${allResults} | Get MongoDB Collection Count | %s | %s |" % (dbName, dbCollName)
print("| ${allResults} | Get MongoDB Collection Count | %s | %s |" % (dbName, dbCollName))
return count

def save_mongodb_records(self, dbName, dbCollName, recordJSON):
"""
If to_save already has an "_id" then an update() (upsert) operation is
performed and any existing document with that "_id" is overwritten.
Otherwise an insert() operation is performed. In this case if manipulate
is True an "_id" will be added to to_save and this method returns the
If to_save already has an "_id" then an update() (upsert) operation is
performed and any existing document with that "_id" is overwritten.
Otherwise an insert() operation is performed. In this case if manipulate
is True an "_id" will be added to to_save and this method returns the
"_id" of the saved document.

| ${allResults} | Save MongoDB Records | DBName | CollectionName | JSON |
Expand All @@ -143,7 +145,7 @@ def save_mongodb_records(self, dbName, dbCollName, recordJSON):
self._builtin.fail("Connection failed, please make sure you have run 'Connect To Mongodb' first.")
coll = db['%s' % dbCollName]
allResults = coll.save(recordJSON)
print "| ${allResults} | Save MongoDB Records | %s | %s | %s |" % (dbName, dbCollName, recordJSON)
print("| ${allResults} | Save MongoDB Records | %s | %s | %s |" % (dbName, dbCollName, recordJSON))
return allResults

def update_many_mongodb_records(self, dbName, dbCollName, queryJSON, updateJSON, upsert=False):
Expand All @@ -169,9 +171,9 @@ def update_many_mongodb_records(self, dbName, dbCollName, queryJSON, updateJSON,
self._builtin.fail("Connection failed, please make sure you have run 'Connect To Mongodb' first.")
coll = db['%s' % collection_name]
allResults = coll.update_many(query_json, update_json, upsert=upsert)
print "Matched: %i documents" % allResults.matched_count
print "| ${allResults} | Update Many MongoDB Records | %s | %s | %s | %s |" % (
dbName, dbCollName, query_json, update_json)
print("Matched: %i documents" % allResults.matched_count)
print("| ${allResults} | Update Many MongoDB Records | %s | %s | %s | %s |" % (
dbName, dbCollName, query_json, update_json))
return allResults.modified_count

def retrieve_all_mongodb_records(self, dbName, dbCollName, returnDocuments=False):
Expand Down Expand Up @@ -199,7 +201,7 @@ def retrieve_some_mongodb_records(self, dbName, dbCollName, recordJSON, returnDo
| Log | ${allResults} |
| Should Contain X Times | ${allResults} | '${recordNo1}' | 1 |
"""
print "| ${allResults} | Retrieve Some MongoDB Records | %s | %s | %s |" % (dbName, dbCollName, recordJSON)
print("| ${allResults} | Retrieve Some MongoDB Records | %s | %s | %s |" % (dbName, dbCollName, recordJSON))
return self._retrieve_mongodb_records(dbName, dbCollName, recordJSON, returnDocuments=returnDocuments)

def retrieve_and_update_one_mongodb_record(self, dbName, dbCollName, queryJSON, updateJSON,
Expand Down Expand Up @@ -228,12 +230,12 @@ def retrieve_and_update_one_mongodb_record(self, dbName, dbCollName, queryJSON,
self._builtin.fail("Connection failed, please make sure you have run 'Connect To Mongodb' first.")
coll = db['%s' % dbcollname]
all_results = coll.find_one_and_update(record_json, update_json, return_document=document_to_return)
print "| ${allResults} | Retrieve And Update One Mongodb Record | %s | %s | %s | %s | %s" % (
print("| ${allResults} | Retrieve And Update One Mongodb Record | %s | %s | %s | %s | %s" % (
dbname,
dbcollname,
queryJSON,
updateJSON,
returnBeforeDocument)
returnBeforeDocument))
return all_results

def retrieve_mongodb_records_with_desired_fields(self, dbName, dbCollName, recordJSON, fields, return__id=True,
Expand Down Expand Up @@ -304,8 +306,8 @@ def retrieve_mongodb_records_with_desired_fields(self, dbName, dbCollName, recor
else:
data = []

print "| ${allResults} | retreive_mongodb_records_with_desired_fields | %s | %s | %s | %s | %s |" % (
dbName, dbCollName, recordJSON, fields, return__id)
print("| ${allResults} | retreive_mongodb_records_with_desired_fields | %s | %s | %s | %s | %s |" % (
dbName, dbCollName, recordJSON, fields, return__id))
return self._retrieve_mongodb_records(dbName, dbCollName, recordJSON, data, returnDocuments)

def _retrieve_mongodb_records(self, dbName, dbCollName, recordJSON, fields=[], returnDocuments=False):
Expand Down Expand Up @@ -359,5 +361,5 @@ def remove_mongodb_records(self, dbName, dbCollName, recordJSON):
self._builtin.fail("Connection failed, please make sure you have run 'Connect To Mongodb' first.")
coll = db['%s' % dbCollName]
allResults = coll.remove(recordJSON)
print "| ${allResults} | Remove MongoDB Records | %s | %s | %s |" % (dbName, dbCollName, recordJSON)
print("| ${allResults} | Remove MongoDB Records | %s | %s | %s |" % (dbName, dbCollName, recordJSON))
return allResults