对于我的单元测试,我使用一个基于Jetty的简单测试服务器:
package eu.kostia.textanalysis.webservices.jetty;
import java.awt.Desktop;
import java.net.URI;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class TestServer {
static private final String CONTEXT_PATH = "/webservice";
static private final String PROJECT_HOME = System.getenv("MY_WORKSPACE_HOME") + "/WebServices";
static public final int PORT = 8080;
private Server server;
private Exception startException;
private static class SingletonHolder {
private static final TestServer INSTANCE = new TestServer();
}
/**
* Returns the singleton instance of the test server.
*
* @return the singleton instance of the test server.
*/
public …Run Code Online (Sandbox Code Playgroud) 我的模型由父母和孩子组成,具有一对一的关系:
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
name = Column(String)
child = relationship("Child", backref="parent", uselist=False, lazy='joined')
class Child(Base):
__tablename__ = 'child'
child_id = Column(Integer, ForeignKey(Parent.id), primary_key=True)
value = Column(Integer)
Run Code Online (Sandbox Code Playgroud)
我的测试数据如下:
q = s.query(Parent)
pd.read_sql(q.statement,s.bind)
id name child_id value
1 a 1 10
2 b 2 20
3 c 3 30
Run Code Online (Sandbox Code Playgroud)
现在我想使用此查询只获得child.value> 20的父母:
q = s.query(Parent).filter(Parent.child.value > 20)
Run Code Online (Sandbox Code Playgroud)
但是会发生以下错误:
AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object
associated with Parent.child has an attribute 'value'
Run Code Online (Sandbox Code Playgroud)
当然我可以直接在Child类上查询,但我的目标是检索一个Parent对象.