assert_equal 测试MongoDB函数时未定义名称错误'db'



我的assert_equal有问题,这表明没有定义名称"db"。

该函数正在测试我编写的函数,该函数返回评级值为5的企业的百分比。当我按照编写的方式运行函数时,函数会返回期望值和百分比,这很好,但我也需要assert_equal来通过。

我试过使用

数据库。[collection].count-无效语法collection.count评级值为0的企业总数,即未计算正确的数字db.collection.count与上述相同

所以我自己的功能在上失败了

如有任何建议,我们将不胜感激。

我对python和MongoDB非常陌生。

Avril

出于安全考虑,我已删除了密码和客户端信息。

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'(打印(Percentage_ratingvalue_five(

我设法解决了我的问题。

我通过传递不带引号的集合名称来更改名称,解决了这个问题,如下所示:

def get_rating_value_percentage(集合(:"quot"返回RateValue为5的金额的0到1之间的浮动"quot"密码=客户端=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(打印(Percentage_ratingvalue_five(

然后是后来的

assert_equal(get_rating_value_percentage(db.uk((完成测试正常。

BWAvril

最新更新