2017-02-02 27 views
4

私はOGM(グラフのORM)としてネオモデルとDjangoとNeo4jを併用しています。それはうまく動作しますが、テストに関しては、Neomodelはリレーショナルデータベースで通常のDjangoの動作をサポートしていません。つまり、テストの開始時に作成され、終了すると破棄される一時的なNeo4jインスタンスは作成されません。Neo4jとDjangoのテスト

私はいくつかの研究を行ってきたと私は2つの解決策発見しました:

  • 最初に、カスタムを作成しているあなたは、開発データベースを停止ランナーを発見し、その後、別のテスト・データベースを起動しますテストを実行し、最後にテストインスタンスを停止し、開発インスタンスを再度開始します。この解決策はスレッドDjango testing of neo4j databaseで提案されています。次のコードは、3.1.1のNeo4jのバージョンのために適応されています:

    from time import sleep 
    from subprocess import call 
    
    from django.test.runner import DiscoverRunner 
    from py2neo import authenticate 
    
    class DiscoverRunner(DiscoverRunner): 
    def setup_databases(self, *args, **kwargs): 
        # Stop your development instance 
        call("sudo neo4j stop", shell=True) 
        # Sleep to ensure the service has completely stopped 
        sleep(1) 
        # Start your test instance (see section below for more details) 
        success = call("neo4j_test/neo4j-community-3.1.1/bin/neo4j" 
            " start", shell=True) 
        # Need to sleep to wait for the test instance to completely come up 
        sleep(10) 
        if success != 0: 
         return False 
    
        # These lines have been commented because I've set the configuration 
        # dbms.security.auth_enabled=false 
        #try: 
        # # For neo4j 2.2.x you'll need to set a password or deactivate auth 
        # # Nigel Small's py2neo gives us an easy way to accomplish this 
        # # call("source /path/to/virtualenv/bin/activate && " 
        # #  "/path/to/virtualenv/bin/neoauth " 
        # #  "neo4j neo4j my-p4ssword") 
    
        # authenticate("localhost:7474", "neo4j", "my-password") 
    
        #except OSError: 
        # pass 
        # Don't import neomodel until we get here because we need to wait 
        # for the new db to be spawned 
        from neomodel import db 
        # Delete all previous entries in the db prior to running tests 
        query = "match (n)-[r]-() delete n,r" 
        db.cypher_query(query) 
        super(DiscoverRunner, self).__init__(*args, **kwargs) 
    
    def teardown_databases(self, old_config, **kwargs): 
        from neomodel import db 
        # Delete all previous entries in the db after running tests 
        query = "match (n)-[r]-() delete n,r" 
        db.cypher_query(query) 
        sleep(1) 
        # Shut down test neo4j instance 
        success = call("neo4j_test/neo4j-community-3.1.1/bin/neo4j" 
            " stop", shell=True) 
        if success != 0: 
         return False 
        sleep(1) 
        # start back up development instance 
        call("sudo neo4j start", shell=True) 
    

    それが暗号化接続を使用してのために'~/.neo4/known_hosts'に見上げているため、それは、テスト・データベースに接続し、クエリを作成しようとするまで、それは正常に動作します接続キーが、それは次のエラーで開発データベースとクラッシュの一つと競合:

    neo4j.v1.exceptions.ProtocolError: Server certificate does not match known certificate for 'localhost'; check details in file '~/.neo4j/known_hosts' 
    

    ので、この認証チェックを回避する方法はありますか?

  • 第2の解決策は、同じ発見ランナーを作成し、Neo4jで使用可能なImpermanentDatabaseをセットアップすることです。問題は、私が見つけたすべての情報がJava Exceotのthis、Neo4j ImpermanentDatabase in python unittestsにあり、これをPythonでどのように実装するかを明確にしていないことです。誰かが(それはPythonのドライバでさえ直接neomodel、py2neoとだかどうかは関係ありません)事前に非常に

感謝をpythonでそれを使用する方法のいくつかの概念を持ってい。

答えて

1

いくつか調査した後、私は最初のアプローチを使って解決策を考え出しました。

ドライバに接続するときのNeo4jの公式Pythonのドライバが設定可能なパラメータ(暗号化)を持つ:

GraphDatabase.driver('bolt://' + hostname, auth=basic_auth(username, password), encrypted=True) 

暗号化されたパラメータがFalseに設定されている場合、'~/.neo4/known_hosts'が無視され、エラーは存在しません。残念ながら、ネオモデルのフレームワークはまだそのパラメータのチューニングをサポートしていませんが、私はgithubでリポジトリをフォークし、ENCRYPTED_CONNECTIONというグローバル設定の変数を追加しました。

プルリクエストが受け入れられるのを待っていますが、私はどちらかが受け入れられるかどうかを通知します。

ところで、誰かが私に2番目のアプローチのいくつかのリグを与えることができれば、それはすばらしい質問にコメントしました。

ありがとうございました。