簡體   English   中英

如何使用csv加載機制創建pytest會話級neo4j實例?

[英]How to create pytest session level neo4j instance with csv loading mechanism?

在pytest期間創建一個neo4j會話級夾具的最佳方法是什么,同樣在neo4j中,必須在該會話中向數據庫加載節點csv和關系csv文件,以便測試可以在完整數據庫(而不是空白數據庫)中執行。

我嘗試了以下方法,如果您有更好的解決方案,請回答,謝謝

默認配置是http端口7474和螺栓7687,假設一個neo4j正在使用http端口7474運行,現在我們無法使用7474運行另一個neo4j實例,我們必須更改neo4j.conf中的端口,然后我們可以啟動另一個實例,解決這個問題

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 0))
sock.listen(1)
port = sock.getsockname()[1]

它將為您提供免費端口,並且需要在運行時在neo4j.conf中進行更改,現在讓我們看看這個pytest固定裝置

@pytest.fixture(scope="session")
def neo4j_graph_instance(csv_node_file, csv_relation_file):
    instancesDirectory = os.path.join(PATH_TEST_DIR, "neo4j_instance_acceptance")
if not os.path.exists(instancesDirectory):
    os.makedirs(instancesDirectory)

archive_file = "neo4j-community-3.2.1-unix.tar.gz"
filename = instancesDirectory + "/" + archive_file

if (os.path.isfile(filename)) == True:
    try:
with TarFile.open(filename, "r:gz") as archive:
            archive.extractall(instancesDirectory)
            print ("successfully extracted")

    except IOError:
        print "file is not there"
else:
    try:
        uri = "http://dist.neo4j.org/neo4j-community-3.2.1-unix.tar.gz"
        urlretrieve(uri, filename)
        try:
            with TarFile.open(filename, "r:gz") as archive:
                archive.extractall(instancesDirectory)
        except IOError:
            print "file is not there"
    except IOError:
        print "Could not connect to internet to download the file"

neo4j_home = os.path.join(instancesDirectory, "neo4j-community-3.2.1")
neo4j_import_dir = os.path.join(neo4j_home, "import")
neo4j_inst = Neo4jInstance(home=neo4j_home)

neo4j_inst.set_config("dbms.connector.http.listen_address", ":%s" % get_open_port())
neo4j_inst.set_config("dbms.connector.bolt.listen_address", ":%s" % get_open_port())
neo4j_inst.set_config("dbms.security.auth_enabled", "false")
neo4j_inst.set_config("dbms.connector.https.enabled", "false")

# db loading mechanism #
# Rajib: I am getting csv files fixture and copying them in neo4j import dir  in run time.
# Then getting bolt uri for that active instance  , and executing the command 'load_db_script'.
# At the end I am returning one Graph instance with fully loaded with csv files.


neo4j_inst.start_neo4j_instance()
time.sleep(15)
#:todo need to avoid sleep , instead check socket connection to desired host and port
print "copying csv files to neo4j import direcotry"

copy_script = "cp %s %s %s" % (
    csv_node_file, csv_relation_file,  neo4j_import_dir)
call(copy_script, shell=True)
print "successfully copied to neo4j import directory "
neo4j_inst_bolt_uri = "bolt://localhost:%d" % neo4j_inst.get_bolt_port()
load_cypher_file = os.path.join(DMS_PATH, "load_csv.cypher")
load_db_script = "cat %s | %s -a %s --format verbose --debug" % (
    load_cypher_file, neo4j_inst.cypher_shell_script(), neo4j_inst_bolt_uri)
call(load_db_script, shell=True)
neo4j_inst_http_port = neo4j_inst.get_http_port()

yield Graph(host='localhost', http_port=neo4j_inst_http_port, bolt=False)

print "running teardown methods"

neo4j_inst.stop_neo4j_instance()
neo4j_inst.delete_store()

因此,每次它將提供帶有neo4j實例的燈具,其中將在運行時為滿載數據庫分配端口。

會有更好的方法,如果您做得比這更好,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM