assert_equal NameError 'db' is not defined when testing MongoDB function

Viewed 16

I have a problem with the assert_equal which is indicating that name 'db' is not defined.

The function is testing my function that I have written that returns the percentage of businesses that have a rating value of 5. When I run the function as written, the function returns expected values and percentages which is great but I need the assert_equal to pass too.

I have tried using

db.[collection].count - invalid syntax collection.count Total number of business with a rating value - 0 ie not calculating the correct number db.collection.count same as above

So my own function then fails

Any suggestions are gratefully received.

I am very new to python and MongoDB.

Avril

I have removed the password and client information for security reasons.

def get_rating_value_percentage(collection):
"""
Return a float between 0 and 1 of the amount with a RatingValue of 5
"""

password = 
client = 
db = some_data

i=0.00

# testing print (type(i)) checking to see that is set up as a float
# testing print(type(collection))

total_business_s_ratingvalue = db[collection].count('RatingValue')
print ('Total number of businesses with a rating value - %s' %total_business_s_ratingvalue)
total_business_s_ratingvaluefive = db[collection].count({'RatingValue' :5})
print ('Total number of business with a rating value of 5 - %s'%total_business_s_ratingvaluefive )
i = total_business_s_ratingvaluefive / total_business_s_ratingvalue
print ('Percentage of business in collection %s with a rating value of five' %i )
return str(i) # this is for the tests lower down - it is asking for a string so converted i to a string 

Percentage_ratingvalue_five=get_rating_value_percentage('uk') print (Percentage_ratingvalue_five)

1 Answers

I managed to resolve my issue.

I resolved this issue by changing the name by passing the collection name without quotes as follows:

def get_rating_value_percentage(collection): """ Return a float between 0 and 1 of the amount with a RatingValue of 5 """ password = client = db = some_data

# print(type(db))
i=0.00

total_business_s_ratingvalue = collection.count('RatingValue')
print ('Total number of businesses with a rating value - %s' %total_business_s_ratingvalue)
total_business_s_ratingvaluefive = collection.count({'RatingValue' :5})
print ('Total number of business with a rating value of 5 - %s'%total_business_s_ratingvaluefive )
i = total_business_s_ratingvaluefive / total_business_s_ratingvalue
print ('Percentage of business in collection %s with a rating value of five' %i )
return (i) # 

Percentage_ratingvalue_five=get_rating_value_percentage(db.uk) print (Percentage_ratingvalue_five)

then the later

assert_equal(get_rating_value_percentage(db.uk) completed the test ok.

BW Avril

Related