diff --git a/test/Project.toml b/test/Project.toml index 0df42424c4ce05b41abef67ed1a8759ef45f5373..6ae1986a51ca4e0d43522b3a798dcbd3c50b9211 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -2,3 +2,4 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" diff --git a/test/runtests.jl b/test/runtests.jl index 66c86a7b0762ae906ec01d6889870ece14b3612e..5f4ecb66a3920db9155ce496c332e41dd5bb2f9e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -23,6 +23,7 @@ using Test using Logging +using UUIDs # first try and load a local CaosDB installation; if that fails, # set-up CaosDB.jl with repo and branch specified in # `CaosDBIntegrationTests`. The latter is mainly useful for running @@ -137,4 +138,135 @@ end @test_throws CaosDB.Exceptions.GenericCaosDBException retrieve(["20", "21", "22"]) end + @testset "Test entity insertion, update and deletion" begin + # 1. Insert + ent_with_name = CaosDB.Entity.create_recordtype("TestEnt") + insert_rt_transaction = create_transaction() + add_insert_entity(insert_rt_transaction, ent_with_name) + execute(insert_rt_transaction) + results = get_results(insert_rt_transaction) + + @test length(results) == 1 + @test has_errors(results[1]) == false + @test get_id(results[1]) != "" + + # 2. update + ent_with_name = execute_query("FIND TestEnt") + prop = CaosDB.Entity.create_property_entity( + name = "TestProperty", + datatype = "TEXT", + unit = "cm", + is_reference = false, + is_list = false, + ) + + insert_prop_transaction = create_transaction() + add_insert_entity(insert_prop_transaction, prop) + execute(insert_prop_transaction) + results = get_results(insert_prop_transaction) + + + retrieve_prop_transaction = create_transaction() + add_retrieve_by_id(retrieve_prop_transaction, get_id(results[1])) + execute(retrieve_prop_transaction) + results = get_results(retrieve_prop_transaction) + + @test length(results) == 1 + @test has_errors(results[1]) == false + @test get_name(results[1]) == "TestProperty" + #@test get_unit(results[1]) == "cm" + @test get_datatype(results[1])[1] == "TEXT" + + prop2 = CaosDB.Entity.create_property(id = get_id(results[1])) + CaosDB.Entity.append_property(ent_with_name[1], prop2) + + update_transaction = create_transaction() + add_update_entity(update_transaction, ent_with_name[1]) + execute(update_transaction) + results = get_results(update_transaction) + @test length(results) == 1 + + + retrieve_transaction = create_transaction() + add_retrieve_by_id(retrieve_transaction, get_id(results[1])) + execute(retrieve_transaction) + results = get_results(retrieve_transaction) + + @test length(results) == 1 + @test has_errors(results[1]) == false + @test length(CaosDB.Entity.get_properties(results[1])) == 1 + prop = CaosDB.Entity.get_property(results[1], 1) + @test get_name(prop) == "TestProperty" + #@test get_unit(prop) == "cm" + + # 3. delete + ent_with_name = execute_query("FIND TestEnt") + delete_transaction = create_transaction() + add_delete_by_id(delete_transaction, get_id(ent_with_name[1])) + execute(delete_transaction) + results = get_results(delete_transaction) + + @test length(results) == 1 + @test has_errors(results[1]) == false + @test get_id(results[1]) != "" + + # 4. Cleanup + ent_with_name = execute_query("FIND TestProperty") + cleanup_transaction = create_transaction() + add_delete_by_id(cleanup_transaction, get_id(ent_with_name[1])) + execute(cleanup_transaction) + + end + + @testset "Test upload and download of a File" begin + # 1. Setup + fname = string(uuid4()) # file name + touch(fname) + write(fname, "Some Content") # cannot be empty + upload_path = string(pwd(), "/", fname) + download_path = string(pwd(), "/", string(uuid4())) + + # 2. Upload Entity + ent_with_name = CaosDB.Entity.create_entity("TestFile") + set_role(ent_with_name, "FILE") + set_local_path(ent_with_name, upload_path) + set_remote_path(ent_with_name, string("/Inttests/", fname)) + + single_insert_transaction = create_transaction() + + add_insert_entity(single_insert_transaction, ent_with_name) + execute(single_insert_transaction) + results = get_results(single_insert_transaction) + + @test length(results) == 1 + @test has_errors(results[1]) == false + @test get_id(results[1]) != "" + + # 3. Download entity + ent_with_name = execute_query("FIND TestFile") + single_insert_transaction = create_transaction() + add_retrieve_and_download_file_by_id( + single_insert_transaction, + get_id(ent_with_name[1]), + download_path, + ) + execute(single_insert_transaction) + results = get_results(single_insert_transaction) + + @test length(results) == 1 + @test has_errors(results[1]) == false + @test get_id(results[1]) != "" + @test isfile(download_path) == true + + # 4. Cleanup + ent_with_name = execute_query("FIND TestFile") + single_insert_transaction = create_transaction() + add_delete_by_id(single_insert_transaction, get_id(ent_with_name[1])) + execute(single_insert_transaction) + + rm(upload_path) + rm(download_path) + + end + end