在unittest的setUp()方法中,我设置了一些自变量,稍后在实际测试中引用.我还创建了一个装饰器来做一些日志记录.有没有办法可以从装饰器访问这些自变量?
为简单起见,我发布此代码:
def decorator(func):
def _decorator(*args, **kwargs):
# access a from TestSample
func(*args, **kwargs)
return _decorator
class TestSample(unittest.TestCase):
def setUp(self):
self.a = 10
def tearDown(self):
# tear down code
@decorator
def test_a(self):
# testing code goes here
Run Code Online (Sandbox Code Playgroud)
什么是访问的最好办法一个从装饰(()在设置中设定)?
我有一个Restful Web服务API,由不同的第三方使用.该API的一部分受到限制(您需要用户名/密码才能访问它).我想知道实现身份验证的最佳方法是什么?
我正在使用https,因此通信是加密的.我有两个想法:
我更接近于选择第一种方法(它是宁静的标准,比较容易实现,XML,JSON或HTML,可以在不改变任何使用),但我想看看你有什么看法?你推荐什么:第一种,第二种或第三种方法?
顺便说一下,我在服务器端使用Python.
我已经开始使用Django的测试框架,一切正常,直到我开始测试经过身份验证的页面.
为简单起见,我们假设这是一个测试:
class SimpleTest(TestCase):
def setUp(self):
user = User.objects.create_user('temporary', 'temporary@gmail.com', 'temporary')
def test_secure_page(self):
c = Client()
print c.login(username='temporary', password='temporary')
response = c.get('/users/secure/', follow=True)
user = User.objects.get(username='temporary')
self.assertEqual(response.context['email'], 'temporary@gmail.com')
Run Code Online (Sandbox Code Playgroud)
在我运行此测试后,它失败了,我看到login()的打印返回值返回True,但是response.content被重定向到登录页面(如果登录失败,则认证装饰器重定向到登录页面).我在装饰器中设置了一个断点来进行身份验证:
def authenticate(user):
if user.is_authenticated():
return True
return False
Run Code Online (Sandbox Code Playgroud)
它确实返回False.test_secure_page()中的第4行正确检索用户.
这是视图功能:
@user_passes_test(authenticate, login_url='/users/login')
def secure(request):
user = request.user
return render_to_response('secure.html', {'email': user.email})
Run Code Online (Sandbox Code Playgroud)
当然,如果我尝试通过应用程序登录(在测试之外),一切正常.
假设您想要模拟某些情况.公司可以有一个或多个分支机构.这些分支机构的员工可以在不同的公司(甚至同一公司的两个不同分支机构)工作.这当然只是一个例子.
我们还假设大多数搜索/查询都将在员工和公司集合上完成.
第一个(天真的)方法是嵌入所有东西(公司有一系列的分支机构和分支机构都有员工阵列):
{
name: "Company name",
// other company data
branches : [
{
name: "Branch name",
// other branch data
Employees: [
{
// employee1 data
},
{
// employee data
},
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是当人们有兴趣检索员工信息时,这将是非常低效的(人们必须检索公司,然后遍历每个分支以找到所需的员工).
另一方面,可以使用引用并模仿RDBMS(会有Company,Branch和Employee集合),但这意味着更多的查询.
第三个选项(我最接近),将Employee作为一个单独的集合,然后在Branches中有一个对它的引用数组.此外,为了允许更快的查询,例如:"具有特定名称的员工,适用于某个公司和某个分支",公司ObjectId可以存储在Employee集合中:
{
company_id: "some id",
first_name: "First name",
last_name: "Last name",
//
}
Run Code Online (Sandbox Code Playgroud)
因此,在这种情况下,要搜索具有某些公司和特定分支的某些名称的所有员工,就必须进行两次查询.第一个查询将返回满足"公司条件"(公司名称和分支名称)的公司,然后对Employee集合的第二个查询将返回所有具有指定名称且在第一个查询中返回其ID的公司中工作的员工.
你会以其他方式做到这一点吗?有没有其他"推荐"的方法来做到这一点?你会加一些改进吗?
更重要的是,当这两个查询返回具有小交集的结果集时,该怎么办?在这种情况下如何提高性能?
我在RESTful Web服务中使用Cherrypy,服务器返回XML(lxml用于创建XML).其中一些XML非常庞大.我注意到在处理了这样的请求(返回大型XML)之后,内存没有被释放.
所以,我已经隔离了一个问题并创建了一个非常简短的虚拟示例:
import cherrypy
from lxml import etree
class Server:
@cherrypy.expose
def index(self):
foo = etree.Element('foo')
for i in range(200000):
bar = etree.SubElement(foo, 'bar')
bar1 = etree.SubElement(bar, 'bar1')
bar1.text = "this is bar1 text ({0})".format(i)
bar2 = etree.SubElement(bar, 'bar2')
bar2.text = "this is bar2 text ({0})".format(i)
bar3 = etree.SubElement(bar, 'bar3')
bar3.text = "this is bar3 text ({0})".format(i)
bar4 = etree.SubElement(bar, 'bar4')
bar4.text = "this is bar4 text ({0})".format(i)
bar5 = etree.SubElement(bar, 'bar5')
bar5.text = "this is bar5 text ({0})".format(i) …
Run Code Online (Sandbox Code Playgroud) 在这个极简化的和最小化的例子Click事件处理程序已经为每个定义的一个标签(链接被点击后,显示简单的警告).单击该按钮后,第二个链接将添加到页面中.
但是,如果单击此新添加的链接,则不会调用事件处理程序.知道为什么吗?应该添加/更改什么,以便新添加的元素知道主页上定义的脚本?
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$("a").click(function() {
alert("link is clicked");
return false;
});
$("#btn").click(function() {
$("#second").html("<a href='#'>Second link</a>");
});
});
</script>
</head>
<body>
<div id="first">
<input id="btn" type="button" value="Get new content"/>
<a href="#">Original link</a>
</div>
<div id="second"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下,应该做什么,以便第二个链接知道脚本部分中定义的事件处理程序,以便在单击链接后显示警报?
您对解决这类情况的建议是什么?如何让页面上新添加的元素知道以前定义的Javascript代码?
python ×3
cherrypy ×1
consumption ×1
django ×1
jquery ×1
login ×1
lxml ×1
memory ×1
mongodb ×1
nosql ×1
rest ×1
scope ×1
testing ×1
unit-testing ×1
web-services ×1