From f4eabe47d687d1d128c2d17726243a1e7b646719 Mon Sep 17 00:00:00 2001 From: Henry Tang Date: Wed, 14 Mar 2018 05:25:50 +0000 Subject: [PATCH] Add Python 3 support --- src/MongoDBLibrary/__init__.py | 12 ++--- .../mongo_connection_manager.py | 22 +++++---- src/MongoDBLibrary/mongoquery.py | 48 ++++++++++--------- 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/MongoDBLibrary/__init__.py b/src/MongoDBLibrary/__init__.py index 369a2c5..0a6644a 100644 --- a/src/MongoDBLibrary/__init__.py +++ b/src/MongoDBLibrary/__init__.py @@ -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): @@ -10,9 +10,9 @@ 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} | @@ -20,6 +20,6 @@ class MongoDBLibrary(MongoConnectionManager, MongoQuery): | &{allResults} | Retrieve and Update One Mongodb Record | DBName | CollectionName | ${QueryJSON} | ${UpdateJSON} | | Log | ${allResults} | """ - + ROBOT_LIBRARY_SCOPE = 'GLOBAL' ROBOT_LIBRARY_VERSION = VERSION diff --git a/src/MongoDBLibrary/mongo_connection_manager.py b/src/MongoDBLibrary/mongo_connection_manager.py index ff5efcc..c6846ad 100644 --- a/src/MongoDBLibrary/mongo_connection_manager.py +++ b/src/MongoDBLibrary/mongo_connection_manager.py @@ -1,3 +1,5 @@ +from __future__ import print_function + from robot.libraries.BuiltIn import BuiltIn @@ -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:admin@foo.bar.org | ${27017} | - + """ dbapiModuleName = 'pymongo' db_api_2 = __import__(dbapiModuleName) - + dbPort = int(dbPort) #print "host is [ %s ]" % dbHost #print "port is [ %s ]" % dbPort @@ -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() diff --git a/src/MongoDBLibrary/mongoquery.py b/src/MongoDBLibrary/mongoquery.py index 33baed5..ba9d253 100644 --- a/src/MongoDBLibrary/mongoquery.py +++ b/src/MongoDBLibrary/mongoquery.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import json from bson.objectid import ObjectId from pymongo import ReturnDocument @@ -5,12 +7,12 @@ 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: @@ -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): @@ -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): @@ -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: @@ -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: @@ -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): @@ -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 | @@ -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): @@ -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): @@ -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, @@ -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, @@ -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): @@ -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