在cypress的neo4j数据库上运行cypher查询的最佳方法

m4pnthwp  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(113)

我试图运行cypher查询对来自cypress测试的neo4j数据库。我在想最好的办法是什么。在用cy。task()为这个建议?这将有助于实现这一点。
我试着使用下面的方法运行一个基本的查询,但这似乎不像是Cypress的正确方式,即使查询确实执行:

function runCypher(query) {
  const driver = neo4j.driver(Cypress.env('NEO4J_QA'), neo4j.auth.basic(Cypress.env('NEO4J_USER'), Cypress.env('NEO4J_PASSWORD')));
  const session = driver.session();
  session.run(query);
}
hzbexzde

hzbexzde1#

查看neo4j-driver的注解,使用Promise API消费记录中的示例似乎很有用。

// the Promise way, where the complete result is collected before we act on it:
session
  .run('MERGE (james:Person {name : $nameParam}) RETURN james.name AS name', {
    nameParam: 'James'
  })                              // this is where your example gets to
  .then(result => {
    result.records.forEach(record => {
      console.log(record.get('name'))
    })
  })
  .catch(error => {
    console.log(error)
  })
  .then(() => session.close())

因此,您的函数只需要返回会话调用并通过.then()回调访问结果。

function runCypher(query) { 
  const {NEO4J_QA: url, NEO4J_USER: user, NEO4J_PASSWORD: password} = Cypress.env();
  const driver = neo4j.driver(url, neo4j.auth.basic(user, password)); 
  const session = driver.session(); 
  return session.run(query); 
}

it('tests neo4j query', () => {
  const myQuery = '...'
  runCypher(myQuery).then(results => {
    expect(results.records.length).to.eq(42)   // assertions here
  })
  .catch(error => {
    throw error                                // re-throw to fail the test
  })
  .then(() => session.close())                 // presume this is required for cleanup
})

你可以在浏览器中使用它,所以我认为你不需要任务。

相关问题