Skip to content

Commit

Permalink
Remove Try/Finally since it is not necessary anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
serhatbolsu committed Jul 13, 2015
1 parent 0768df8 commit c52be34
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 92 deletions.
159 changes: 68 additions & 91 deletions src/MongoDBLibrary/mongoquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ def get_mongodb_databases(self):
| Log Many | @{allDBs} |
| Should Contain | ${allDBs} | DBName |
"""
try:
allDBs = self._dbconnection.database_names()
print "| @{allDBs} | Get Mongodb Databases |"
return allDBs
except:
pass
allDBs = self._dbconnection.database_names()
print "| @{allDBs} | Get Mongodb Databases |"
return allDBs


def get_mongodb_collections(self, dbName):
"""
Expand All @@ -33,14 +31,11 @@ def get_mongodb_collections(self, dbName):
| Log Many | @{allCollections} |
| Should Contain | ${allCollections} | CollName |
"""
try:
dbName = str(dbName)
db = self._dbconnection['%s' % (dbName,)]
allCollections = db.collection_names()
print "| @{allCollections} | Get MongoDB Collections | %s |" % (dbName)
return allCollections
except:
pass
dbName = str(dbName)
db = self._dbconnection['%s' % (dbName,)]
allCollections = db.collection_names()
print "| @{allCollections} | Get MongoDB Collections | %s |" % (dbName)
return allCollections


def drop_mongodb_database(self, dbDelName):
Expand All @@ -53,12 +48,10 @@ def drop_mongodb_database(self, dbDelName):
| @{allDBs} | Get MongoDB Collections | myDB |
| Should Not Contain | ${allDBs} | myDB |
"""
try:
dbDelName = str(dbDelName)
print "| Drop MongoDB Database | %s |" % (dbDelName)
self._dbconnection.drop_database('%s' % (dbDelName))
except:
pass
dbDelName = str(dbDelName)
print "| Drop MongoDB Database | %s |" % (dbDelName)
self._dbconnection.drop_database('%s' % (dbDelName))


def drop_mongodb_collection(self, dbName, dbCollName):
"""
Expand All @@ -70,13 +63,11 @@ def drop_mongodb_collection(self, dbName, dbCollName):
| @{allCollections} | Get MongoDB Collections | myDB |
| Should Not Contain | ${allCollections} | CollectionName |
"""
try:
dbName = str(dbName)
db = self._dbconnection['%s' % (dbName,)]
db.drop_collection('%s' % (dbCollName))
print "| Drop MongoDB Collection | %s | %s |" % (dbName,dbCollName)
except:
pass
dbName = str(dbName)
db = self._dbconnection['%s' % (dbName,)]
db.drop_collection('%s' % (dbCollName))
print "| Drop MongoDB Collection | %s | %s |" % (dbName,dbCollName)


def validate_mongodb_collection(self, dbName, dbCollName):
"""
Expand All @@ -87,15 +78,12 @@ def validate_mongodb_collection(self, dbName, dbCollName):
| ${allResults} | Validate MongoDB Collection | DBName | CollectionName |
| Log | ${allResults} |
"""
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
except:
pass
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

def get_mongodb_collection_count(self, dbName, dbCollName):
"""
Expand All @@ -105,16 +93,13 @@ def get_mongodb_collection_count(self, dbName, dbCollName):
| ${allResults} | Get MongoDB Collection Count | DBName | CollectionName |
| Log | ${allResults} |
"""
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
db = self._dbconnection['%s' % (dbName,)]
coll = db['%s' % (dbCollName)]
count = coll.count()
print "| ${allResults} | Get MongoDB Collection Count | %s | %s |" % (dbName,dbCollName)
return count
except:
pass
dbName = str(dbName)
dbCollName = str(dbCollName)
db = self._dbconnection['%s' % (dbName,)]
coll = db['%s' % (dbCollName)]
count = coll.count()
print "| ${allResults} | Get MongoDB Collection Count | %s | %s |" % (dbName,dbCollName)
return count

def save_mongodb_records(self, dbName, dbCollName, recordJSON):
"""
Expand All @@ -134,19 +119,16 @@ def save_mongodb_records(self, dbName, dbCollName, recordJSON):
| ${allResults} | Save MongoDB Records | foo | bar | {"timestamp":1, "msg":"Hello 1"} |
| Log | ${allResults} |
"""
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
recordJSON = dict(json.loads(recordJSON))
if recordJSON.has_key('_id'):
recordJSON['_id']=ObjectId(recordJSON['_id'])
db = self._dbconnection['%s' % (dbName,)]
coll = db['%s' % (dbCollName)]
allResults = coll.save(recordJSON)
print "| ${allResults} | Save MongoDB Records | %s | %s | %s |" % (dbName,dbCollName,recordJSON)
return allResults
except:
pass
dbName = str(dbName)
dbCollName = str(dbCollName)
recordJSON = dict(json.loads(recordJSON))
if recordJSON.has_key('_id'):
recordJSON['_id']=ObjectId(recordJSON['_id'])
db = self._dbconnection['%s' % (dbName,)]
coll = db['%s' % (dbCollName)]
allResults = coll.save(recordJSON)
print "| ${allResults} | Save MongoDB Records | %s | %s | %s |" % (dbName,dbCollName,recordJSON)
return allResults

def retrieve_all_mongodb_records(self, dbName, dbCollName, returnDocuments=False):
"""
Expand Down Expand Up @@ -247,25 +229,22 @@ 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):
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
criteria = dict(json.loads(recordJSON))
db = self._dbconnection['%s' % (dbName,)]
coll = db['%s' % (dbCollName)]
if fields:
results = coll.find(criteria, fields)
else:
results = coll.find(criteria)
if returnDocuments:
return list(results)
else:
response = ''
for d in results:
response = '%s%s' % (response, d.items())
return response
except:
pass
dbName = str(dbName)
dbCollName = str(dbCollName)
criteria = dict(json.loads(recordJSON))
db = self._dbconnection['%s' % (dbName,)]
coll = db['%s' % (dbCollName)]
if fields:
results = coll.find(criteria, fields)
else:
results = coll.find(criteria)
if returnDocuments:
return list(results)
else:
response = ''
for d in results:
response = '%s%s' % (response, d.items())
return response

def remove_mongodb_records(self, dbName, dbCollName, recordJSON):
"""
Expand All @@ -286,16 +265,14 @@ def remove_mongodb_records(self, dbName, dbCollName, recordJSON):
| ${output} | Retrieve All MongoDB Records | ${MDBDB} | ${MDBColl} |
| Should Not Contain | ${output} | 'timestamp', 1 |
"""
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
recordJSON = json.loads(recordJSON)
if recordJSON.has_key('_id'):
recordJSON['_id']=ObjectId(recordJSON['_id'])
db = self._dbconnection['%s' % (dbName,)]
coll = db['%s' % (dbCollName)]
allResults = coll.remove(recordJSON)
print "| ${allResults} | Remove MongoDB Records | %s | %s | %s |" % (dbName,dbCollName,recordJSON)
return allResults
except:
pass
dbName = str(dbName)
dbCollName = str(dbCollName)
recordJSON = json.loads(recordJSON)
if recordJSON.has_key('_id'):
recordJSON['_id']=ObjectId(recordJSON['_id'])
db = self._dbconnection['%s' % (dbName,)]
coll = db['%s' % (dbCollName)]
allResults = coll.remove(recordJSON)
print "| ${allResults} | Remove MongoDB Records | %s | %s | %s |" % (dbName,dbCollName,recordJSON)
return allResults

2 changes: 1 addition & 1 deletion src/MongoDBLibrary/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Update this before release
VERSION = "0.2.1"
VERSION = "0.2.2"

0 comments on commit c52be34

Please sign in to comment.