使用嵌入式数据库

时间:2017-10-20 03:42:07

标签: java unit-testing spring-data spring-data-neo4j graphaware

更新:我在neo4j timetree源中发现了一些类似的测试,但使用的是GraphAwareIntegrationTest,它扩展了ServerIntegrationTest。所以我尝试使用以下方法为我的测试创建一个GraphDatabaseService Bean,但仍然没有运气。我得到"没有为此数据库实例注册名称为ga.timetree.events.attach的过程。"这不可能吗?

@Bean
  public GraphDatabaseService graphDatabaseService() {
    GraphDatabaseService gds = new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().newGraphDatabase();
    Procedures procedures = (Procedures)((GraphDatabaseFacade) gds).getDependencyResolver().resolveDependency(Procedures.class);
    try {
        ClassPathProcedureUtils.registerAllProceduresAndFunctions(procedures);
    }catch (Exception ex) {
        log.error("error", ex);
    }
    return gds;
}

=====================

this issue类似,但我使用的是Spring Boot 2,SDN5和neo4j 3.2.5,graphaware和时间树。我有自动事件附件设置,我看到事件被保存到时间树,但我无法使用cypher使用过程调用进行查询。我收到错误:

Caused by: org.neo4j.ogm.exception.CypherException: Error executing Cypher; Code: Neo.ClientError.Procedure.ProcedureNotFound; Description: There is no procedure with the name `ga.timetree.range` registered for this database instance. Please ensure you've spelled the procedure name correctly and that the procedure is properly deployed.
at org.neo4j.ogm.drivers.embedded.request.EmbeddedRequest.executeRequest(EmbeddedRequest.java:176)

我没有在链接问题中看到TimeTreeProcedures类。嵌入式/单元测试是否仍支持此功能?

此外,如果支持,我想使用CustomRootTimeTree。任何帮助或指向cypher的指针,我都可以在过程调用中定义自定义根树id也非常感激。谢谢你的帮助!

测试:

    @Test
    public void testSingleTimeTree() {

        User user = new User("alper@alper.com", "alper", "alper");
        userRepository.save(user);
        Collection<User> found = userRepository.findByEmail("alper@alper.com");
        user = found.iterator().next();
        Workout workout = new Workout(new DateTime().plusMonths(2).getMillis());
        workoutRepository.save(workout);
        GraphUnit.printGraph(graphDb);
        Iterable<Workout> workouts = workoutRepository.findWorkouts();

        for(Workout workout1 : workouts) {
            log.info("workout: {}", workout1);
        }
    }

Repo(硬编码的开始/结束):

    public interface WorkoutRepository extends Neo4jRepository<Workout, Long> {

        @Query("CALL ga.timetree.range({start: 1506826887000, end: 1512097287, create: false})")
        Iterable<Workout> findWorkouts();

    }

的pom.xml:

  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.BUILD-SNAPSHOT</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>1.8</java.version>
      <spring-data-releasetrain.version>Kay-RELEASE</spring-data-releasetrain.version>
      <!--<neo4j-ogm.version>3.0.0</neo4j-ogm.version>-->
  </properties>

  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.springframework.data</groupId>
              <artifactId>spring-data-releasetrain</artifactId>
              <version>${spring-data-releasetrain.version}</version>
              <scope>import</scope>
              <type>pom</type>
          </dependency>
      </dependencies>
  </dependencyManagement>

  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-neo4j</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-rest</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
          <groupId>org.neo4j</groupId>
          <artifactId>neo4j-ogm-embedded-driver</artifactId>
          <version>${neo4j-ogm.version}</version>
      </dependency>
      <dependency>
          <groupId>org.neo4j</groupId>
          <artifactId>neo4j</artifactId>
          <version>3.2.5</version>
          <scope>runtime</scope>
      </dependency>

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>

      <!-- added by me -->
      <dependency>
          <groupId>org.neo4j</groupId>
          <artifactId>neo4j-kernel</artifactId>
          <version>3.2.5</version>
          <type>test-jar</type>
      </dependency>
      <dependency>
          <groupId>com.graphaware.neo4j</groupId>
          <artifactId>graphaware-framework-embedded</artifactId>
          <version>3.2.5.51</version>
          <type>pom</type>
      </dependency>
      <dependency>
          <groupId>com.graphaware.neo4j</groupId>
          <artifactId>timetree</artifactId>
          <version>3.2.1.51.27</version>
      </dependency>
      <dependency>
          <groupId>org.neo4j</groupId>
          <artifactId>neo4j-graphdb-api</artifactId>
          <version>3.2.5</version>
      </dependency>
      <dependency>
          <groupId>org.neo4j</groupId>
          <artifactId>neo4j-io</artifactId>
          <version>3.2.5</version>
          <type>test-jar</type>
      </dependency>
      <dependency>
          <groupId>com.graphaware.neo4j</groupId>
          <artifactId>tests</artifactId>
          <version>3.2.5.51</version>
      </dependency>
  </dependencies>

1 个答案:

答案 0 :(得分:1)

我终于可以通过调试到neo4j源来实现它。测试和使用过程的测试位于https://github.com/graphaware/neo4j-timetree,并且它使用ClassPathProcedureUtils类从目标/ classes目录加载类。它查找@Procedure注释并将处理这些类,并且程序变得可用。没有修改它从jar和目标/类加载类,我可以让它工作的唯一方法(虽然是一个hack)是将目标/类输出从neo4j-timetree项目复制到我的项目目标/类输出目录。

更新: 我也可以通过直接使用以下方式注册所需的程序来实现它:

@Bean
public GraphDatabaseService graphDatabaseService() {
    GraphDatabaseService gds = new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().newGraphDatabase();
    try {
        ClassPathProcedureUtils.registerAllProceduresAndFunctions(procedures);
        Procedures procedures = ((GraphDatabaseFacade)gds).getDependencyResolver().resolveDependency(Procedures.class);
        procedures.registerProcedure(TimedEventsProcedure.class);
        procedures.registerProcedure(TimeTreeProcedure.class);
        procedures.registerFunction(TimedEventsProcedure.class);
        procedures.registerFunction(TimeTreeProcedure.class);
      }catch (Exception ex) {
        log.error("error", ex);
      }
      return gds;
}