From 2b378dc80a02a72c6297d0c9beb5a06263d0fbe9 Mon Sep 17 00:00:00 2001 From: Sam Claassens Date: Sun, 27 Mar 2022 09:09:38 -0500 Subject: [PATCH 1/2] Statuses and code+test cleanup --- src/NavAbilitySDK.jl | 8 +- src/navability/entities/Client.jl | 17 ++ src/navability/entities/StatusMessage.jl | 25 +++ src/navability/graphql/Status.jl | 31 ++++ src/navability/services/Factor.jl | 2 - src/navability/services/Solve.jl | 1 - src/navability/services/Status.jl | 67 ++++++++ src/navability/services/Utils.jl | 34 ++++ src/navability/services/Variable.jl | 2 - test/integration/fixtures.jl | 173 ++++++++++++++++++++ test/integration/runtests.jl | 25 +-- test/integration/testFactor.jl | 195 ++++------------------- test/integration/testSolve.jl | 78 ++------- test/integration/testVariable.jl | 101 +++--------- 14 files changed, 433 insertions(+), 326 deletions(-) create mode 100644 src/navability/entities/StatusMessage.jl create mode 100644 src/navability/graphql/Status.jl create mode 100644 src/navability/services/Status.jl create mode 100644 src/navability/services/Utils.jl create mode 100644 test/integration/fixtures.jl diff --git a/src/NavAbilitySDK.jl b/src/NavAbilitySDK.jl index 7a97863..91f5f30 100644 --- a/src/NavAbilitySDK.jl +++ b/src/NavAbilitySDK.jl @@ -3,12 +3,12 @@ module NavAbilitySDK # Global imports using DocStringExtensions using LinearAlgebra +using JSON # Graphql include("./navability/graphql/Factor.jl") +include("./navability/graphql/Status.jl") include("./navability/graphql/Variable.jl") -export GQL_FRAGMENT_FACTORS, GQL_GETFACTOR, GQL_GETFACTORS, GQL_GETFACTORSFILTERED -export GQL_FRAGMENT_VARIABLES, GQL_GETVARIABLE, GQL_GETVARIABLES, GQL_GETVARIABLESFILTERED include("./navability/graphql/QueriesDeprecated.jl") export QUERY_VARIABLE_LABELS @@ -40,10 +40,14 @@ export FactorType, Factor include("./navability/services/Variable.jl") include("./navability/services/Factor.jl") include("./navability/services/Solve.jl") +include("./navability/services/Status.jl") +include("./navability/services/Utils.jl") export getVariable, getVariables, listVariables, ls export addVariable, addPackedVariable export getFactor, getFactors, listFactors, lsf export addFactor, addPackedFactor export solveSession, solveFederated +export getStatusMessages, getStatusLatest, getStatusesLatest +export waitForCompletion end \ No newline at end of file diff --git a/src/navability/entities/Client.jl b/src/navability/entities/Client.jl index 980c067..3bebfcf 100644 --- a/src/navability/entities/Client.jl +++ b/src/navability/entities/Client.jl @@ -1,9 +1,26 @@ +import Base: show + +""" +$(TYPEDEF) +The context for a session, made from a user, robot, and session. +Users can have multiple robots and robots can have multiple sessions. +So this indicates a unique session. +""" struct Client userId::String robotId::String sessionId::String end +function show(io::IO, c::Client) + print(io, "Client: User=$(c.userId), Robot=$(c.robotId), Session=$(c.sessionId)") +end + +""" +$(TYPEDEF) +Some calls interact across multiple users, robots, and sessions. +A scope allows you to specify these more complex contexts. +""" struct Scope environmentIds::Vector{String} userIds::Vector{String} diff --git a/src/navability/entities/StatusMessage.jl b/src/navability/entities/StatusMessage.jl new file mode 100644 index 0000000..4c2a7eb --- /dev/null +++ b/src/navability/entities/StatusMessage.jl @@ -0,0 +1,25 @@ +import Base: show + +""" +$(TYPEDEF) +NOTE: Not used yet. +A status message from the server indicating the progress of +a request, e.g. addVariable or solveSession. +""" +struct StatusMessage + requestId::String + action::String + state::String + timestamp::String + client::Client + +function show(io::IO, s::StatusMessage) + print(io, """StatusMessage: + client: $(self.client) + requestId: $(s.requestId) + action: $(self.action) + state: $(self.state) + timestamp: $(self.timestamp) + """ + ) +end \ No newline at end of file diff --git a/src/navability/graphql/Status.jl b/src/navability/graphql/Status.jl new file mode 100644 index 0000000..687ed67 --- /dev/null +++ b/src/navability/graphql/Status.jl @@ -0,0 +1,31 @@ +GQL_GETSTATUSMESSAGES = """ +query sdk_ls_statusmessages(\$id: ID!) { + statusMessages(id: \$id) { + requestId, + action, + state, + timestamp, + client { + userId, + robotId, + sessionId + } + } +} +""" + +GQL_GETSTATUSLATEST = """ +query sdk_get_statuslatest(\$id: ID!) { + statusLatest(id: \$id) { + requestId, + action, + state, + timestamp, + client { + userId, + robotId, + sessionId + } + } +} +""" diff --git a/src/navability/services/Factor.jl b/src/navability/services/Factor.jl index 6febef9..a8be6ed 100644 --- a/src/navability/services/Factor.jl +++ b/src/navability/services/Factor.jl @@ -1,5 +1,3 @@ -using ..NavAbilitySDK -using JSON function addPackedFactor(navAbilityClient::NavAbilityClient, client::Client, factor)::String response = navAbilityClient.mutate(MutationOptions( diff --git a/src/navability/services/Solve.jl b/src/navability/services/Solve.jl index 0009a75..7ad15ff 100644 --- a/src/navability/services/Solve.jl +++ b/src/navability/services/Solve.jl @@ -1,4 +1,3 @@ -using ..NavAbilitySDK function solveSession(navAbilityClient::NavAbilityClient, client::Client)::String response = navAbilityClient.mutate(MutationOptions( diff --git a/src/navability/services/Status.jl b/src/navability/services/Status.jl new file mode 100644 index 0000000..ffe295b --- /dev/null +++ b/src/navability/services/Status.jl @@ -0,0 +1,67 @@ + +""" +$(SIGNATURES) +Get all the statuses for a request. + +Args: + navAbilityClient (NavAbilityClient): The NavAbility client. + id (String): The ID of the request that you want the statuses on. +""" +function getStatusMessages(navAbilityClient::NavAbilityClient, id::String) + response = navAbilityClient.mutate(MutationOptions( + "sdk_ls_statusmessages", + GQL_GETSTATUSMESSAGES, + Dict( + "id" => id + ) + )) + rootData = JSON.parse(response.Data) + if haskey(rootData, "errors") + throw("Error: $(data["errors"])") + end + data = get(rootData,"data",nothing) + if data === nothing return "Error" end + statusMessages = get(data,"statusMessages","Error") + # TODO: What about marshalling? + return statusMessages +end + +""" +$(SIGNATURES) +Get the latest status message for a request. + +Args: + navAbilityClient (NavAbilityClient): The NavAbility client. + id (String): The ID of the request that you want the latest status on. +""" +function getStatusLatest(navAbilityClient::NavAbilityClient, id::String) + response = navAbilityClient.mutate(MutationOptions( + "sdk_get_statuslatest", + GQL_GETSTATUSLATEST, + Dict( + # "client" => client, + "id" => id + ) + )) + rootData = JSON.parse(response.Data) + if haskey(rootData, "errors") + throw("Error: $(data["errors"])") + end + data = get(rootData,"data",nothing) + if data === nothing return "Error" end + statusMessage = get(data,"statusLatest","Error") + # TODO: What about marshalling? + return statusMessage +end + +""" +$(SIGNATURES) +Helper function to get a dictionary of all latest statues for a list of results. + +Args: + navAbilityClient (NavAbilityClient): The NavAbility client. + ids (Vector{String}): A list of the IDS that you want statuses on. +""" +function getStatusesLatest(navAbilityClient::NavAbilityClient, ids::Vector{String}) + return Dict(r=>getStatusLatest(navAbilityClient, r) for r in ids) +end \ No newline at end of file diff --git a/src/navability/services/Utils.jl b/src/navability/services/Utils.jl new file mode 100644 index 0000000..ef806f9 --- /dev/null +++ b/src/navability/services/Utils.jl @@ -0,0 +1,34 @@ +""" +$(SIGNATURES) +Wait for the requests to complete, poll until done. + +Args: + requestIds (List[str]): The request IDs that should be polled. + maxSeconds (int, optional): Maximum wait time. Defaults to 60. + expectedStatus (str, optional): Expected status message per request. + Defaults to "Complete". +""" +function waitForCompletion( + navAbilityClient::NavAbilityClient, + requestIds::Vector{String}; + maxSeconds::Int = 60, + expectedStatuses::Vector{String} = Nothing, + exceptionMessage::String = "Requests did not complete in time") +# + if expectedStatuses == Nothing + expectedStatuses = ["Complete", "Failed"] + end + wait_time = maxSeconds + tasksComplete = false + while !tasksComplete + statuses = values(getStatusesLatest(navAbilityClient, requestIds)) + tasksComplete = all(s["state"] in expectedStatuses for s in statuses) + if tasksComplete + return + else + sleep(2) + wait_time -= 2 + wait_time <= 0 && throw(error(exceptionMessage)) + end + end +end \ No newline at end of file diff --git a/src/navability/services/Variable.jl b/src/navability/services/Variable.jl index a389b72..3a0bca2 100644 --- a/src/navability/services/Variable.jl +++ b/src/navability/services/Variable.jl @@ -1,5 +1,3 @@ -using ..NavAbilitySDK -using JSON function addPackedVariable(navAbilityClient::NavAbilityClient, client::Client, variable)::String response = navAbilityClient.mutate(MutationOptions( diff --git a/test/integration/fixtures.jl b/test/integration/fixtures.jl new file mode 100644 index 0000000..14ef461 --- /dev/null +++ b/test/integration/fixtures.jl @@ -0,0 +1,173 @@ + +function createClients(apiUrl, userId, robotId, sessionId) + client = NavAbilityHttpsClient(apiUrl) + context = Client(userId,robotId,sessionId) + return client, context +end + +# function example1DGraph(client, context) +# variables = [ +# Variable("x0", :ContinuousScalar), +# Variable("x1", :ContinuousScalar), +# Variable("x2", :ContinuousScalar), +# Variable("x3", :ContinuousScalar), +# ] +# factors = [ +# Factor("x0f1", "Prior", ["x0"], FactorData(fnc=Prior(Normal(0, 1)).dump())), +# Factor( +# "x0x1f1", +# "LinearRelative", +# ["x0", "x1"], +# FactorData(fnc=LinearRelative(Normal(10, 0.1)).dump()), +# ), +# Factor( +# "x1x2f1", +# "Mixture", +# ["x1", "x2"], +# FactorData( +# fnc=Mixture( +# LinearRelative, +# OrderedDict([("hypo1", Normal(0, 2)), ("hypo2", Uniform(30, 55))]), +# [0.4, 0.6], +# 2, +# ).dump() +# ), +# ), +# Factor( +# "x2x3f1", +# "LinearRelative", +# ["x2", "x3"], +# FactorData(fnc=LinearRelative(Normal(-50, 1)).dump()), +# ), +# Factor( +# "x3x0f1", +# "LinearRelative", +# ["x3", "x0"], +# FactorData(fnc=LinearRelative(Normal(40, 1)).dump()), +# ), +# ] +# # Variables +# result_ids = [ +# await addVariable(navability_https_client, client_1d, v) for v in variables +# ] + [await addFactor(navability_https_client, client_1d, f) for f in factors] + +# logging.info(f"[Fixture] Adding variables and factors, waiting for completion") + +# # Await for only Complete messages, otherwise fail. +# await waitForCompletion( +# navability_https_client, +# result_ids, +# expectedStatuses=["Complete"], +# maxSeconds=120, +# ) + +# return (navability_https_client, client_1d, variables, factors) + + +# @pytest.fixture(scope="module") +# async def example_1d_graph_solved(example_1d_graph): +# """Get the graph after it has been solved. +# NOTE this changes the graph, so tests need to be defensive. +# """ +# navability_https_client, client, variables, factors = example_1d_graph +# logging.info(f"[Fixture] Solving graph, client = {client.dumps()}") +# requestId = await solveSession(navability_https_client, client) +# await waitForCompletion(navability_https_client, [requestId], maxSeconds=180) +# return (navability_https_client, client, variables, factors) + + +# @pytest.fixture(scope="module") +# async def example_2d_graph( +# navability_https_client: NavAbilityClient, client_2d: Client +# ): +# variables = [ +# Variable("x0", VariableType.Pose2.value), +# Variable("x1", VariableType.Pose2.value), +# Variable("x2", VariableType.Pose2.value), +# Variable("l0", VariableType.Point2.value), +# ] +# factors = [ +# Factor( +# "x0f1", +# "PriorPose2", +# ["x0"], +# FactorData( +# fnc=PriorPose2( +# FullNormal(np.zeros(3), np.diag([0.1, 0.1, 0.1])) +# ).dump() # This is a generator for a PriorPose2 +# ), +# ), +# Factor( +# "x0x1f1", +# "Pose2Pose2", +# ["x0", "x1"], +# FactorData( +# fnc=Pose2Pose2( +# FullNormal([1, 1, np.pi / 3], np.diag([0.1, 0.1, 0.1])) +# ).dump() # This is a generator for a PriorPose2 +# ), +# ), +# Factor( +# "x1x2f1", +# "Pose2Pose2", +# ["x1", "x2"], +# FactorData( +# fnc=Pose2Pose2( +# FullNormal([1, 1, np.pi / 3], np.diag([0.1, 0.1, 0.1])) +# ).dump() # This is a generator for a PriorPose2 +# ), +# ), +# # TODO: Improve problem setup in future. +# Factor( +# "l0f1", +# "PriorPoint2", +# ["l0"], +# FactorData( +# fnc=PriorPoint2(FullNormal(np.asarray([5, 0]), np.diag([2, 2]))).dump() +# ), +# ), +# Factor( +# "x0l0f1", +# "Point2Point2Range", +# ["x0", "l0"], +# FactorData(fnc=Point2Point2Range(Normal(5, 0.1)).dump()), # Range +# ), +# Factor( +# "x0l0f2", +# "Pose2Point2BearingRange", +# ["x0", "l0"], +# FactorData( +# fnc=Pose2Point2BearingRange( +# Normal(0, 0.3), Normal(5, 0.1) # Bearing, range +# ).dump() +# ), +# ), +# ] +# # Variables +# result_ids = [ +# await addVariable(navability_https_client, client_2d, v) for v in variables +# ] + [await addFactor(navability_https_client, client_2d, f) for f in factors] + +# logging.info(f"[Fixture] Adding variables and factors, waiting for completion") + +# # Await for only Complete messages, otherwise fail. +# await waitForCompletion( +# navability_https_client, +# result_ids, +# expectedStatuses=["Complete"], +# maxSeconds=120, +# ) + +# return (navability_https_client, client_2d, variables, factors) + + +# @pytest.fixture(scope="module") +# async def example_2d_graph_solved(example_2d_graph): +# """Get the graph after it has been solved. +# NOTE this changes the graph, so tests need to be defensive. +# """ +# navability_https_client, client, variables, factors = example_2d_graph +# logging.info(f"[Fixture] Solving graph, client = {client.dumps()}") +# requestId = await solveSession(navability_https_client, client) +# await waitForCompletion(navability_https_client, [requestId], maxSeconds=180) +# return (navability_https_client, client, variables, factors) diff --git a/test/integration/runtests.jl b/test/integration/runtests.jl index 55223c4..22cd642 100644 --- a/test/integration/runtests.jl +++ b/test/integration/runtests.jl @@ -1,21 +1,28 @@ +using Test +using Random + +include("./fixtures.jl") include("./testVariable.jl") include("./testFactor.jl") include("./testSolve.jl") -using Test -using Random -using .TestVariable -using .TestFactor -using .TestSolve - apiUrl = get(ENV,"API_URL","https://api.d1.navability.io") userId = get(ENV,"USER_ID","Guest") robotId = get(ENV,"ROBOT_ID","IntegrationRobot") sessionId = get(ENV,"SESSION_ID","TestSession"*randstring(7)) +# sessionId1d = get(ENV,"SESSION_ID","TestSession1D"*randstring(7)) +sessionId2d = get(ENV,"SESSION_ID","TestSession2D"*randstring(7)) @testset "nva-sdk-integration-testset" begin + # Creating one client and two contexts + # client, navabilityClient1D = createClients(apiUrl, userId, robotId, sessionId1d) + client, navabilityClient2D = createClients(apiUrl, userId, robotId, sessionId2d) + @info "Running nva-sdk-integration-testset..." - TestVariable.RunTests(apiUrl, userId, robotId, sessionId) - TestFactor.RunTests(apiUrl, userId, robotId, sessionId) - TestSolve.RunTests(apiUrl, userId, robotId, sessionId) + + # Note - Tests incrementally build on each other because this is an + # integration test. + runVariableTests(client, navabilityClient2D) + runFactorTests(client, navabilityClient2D) + # runSolverTests(client, navabilityClient2D) end diff --git a/test/integration/testFactor.jl b/test/integration/testFactor.jl index 9ef9589..d6bd020 100644 --- a/test/integration/testFactor.jl +++ b/test/integration/testFactor.jl @@ -1,179 +1,52 @@ -module TestFactor -using Test - -include("../../src/NavAbilitySDK.jl") -using .NavAbilitySDK - -MAX_POLLING_TRIES = 150 - - -function testAddFactor(apiUrl, userId, robotId, sessionId) - client = NavAbilityHttpsClient(apiUrl) - context = Client(userId,robotId,sessionId) - testVariableLabels = ["x0", "x1"] - testVariableTypes = ["RoME.Pose2","RoME.Pose2"] - addVariable(client,context,Variable(testVariableLabels[1], testVariableTypes[1])) - addVariable(client,context,Variable(testVariableLabels[2], testVariableTypes[2])) - testFactorLabels = ["x0x1f1","x0f1"] - testFactorTypes = ["Pose2Pose2","PriorPose2"] - addFactor(client,context,Factor(testFactorLabels[1], testFactorTypes[1], testVariableLabels, Pose2Pose2Data())) - addFactor(client,context,Factor(testFactorLabels[2], testFactorTypes[2], [testVariableLabels[1]], PriorPose2Data())) - return true -end - -function testLsf(apiUrl, userId, robotId, sessionId) - client = NavAbilityHttpsClient(apiUrl) - context = Client(userId,robotId,sessionId) - testVariableLabels = ["x0", "x1"] - testVariableTypes = ["RoME.Pose2","RoME.Pose2"] - addVariable(client,context,Variable(testVariableLabels[1], testVariableTypes[1])) - addVariable(client,context,Variable(testVariableLabels[2], testVariableTypes[2])) - variableAddSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualVariableLabels = ls(client,context) - if setdiff(testVariableLabels,actualVariableLabels) == [] - variableAddSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(testVariableLabels,actualVariableLabels))" - end +function testAddFactor(client, context, factorLabels, factorTypes, factorVariables, factorData) + resultIds = String[] + for (index, label) in enumerate(factorLabels) + resultId = addFactor(client,context,Factor(label, factorTypes[index], factorVariables[index], factorData[index])) + @test resultId != "Error" + push!(resultIds, resultId) end - if !variableAddSucceeded return false end - testFactorLabels = ["x0x1f1","x0f1"] - testFactorTypes = ["Pose2Pose2","PriorPose2"] - addFactor(client,context,Factor(testFactorLabels[1], testFactorTypes[1], testVariableLabels, Pose2Pose2Data())) - addFactor(client,context,Factor(testFactorLabels[2], testFactorTypes[2], [testVariableLabels[1]], PriorPose2Data())) - factorAddSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualFactorLabels = lsf(client,context) - if setdiff(testFactorLabels,actualFactorLabels) == [] - factorAddSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(testFactorLabels,actualFactorLabels))" - end - end - if !factorAddSucceeded return false end + + waitForCompletion(client, resultIds, expectedStatuses=["Complete"]) + return resultIds +end - return true +function testLsf(client, context, factorLabels, factorTypes) + @show setdiff(factorLabels, lsf(client, context)) + @test setdiff(factorLabels, lsf(client, context)) == [] end -function testGetFactor(apiUrl, userId, robotId, sessionId) - client = NavAbilityHttpsClient(apiUrl) - context = Client(userId,robotId,sessionId) - testVariableLabels = ["x0", "x1"] - testVariableTypes = ["RoME.Pose2","RoME.Pose2"] - addVariable(client,context,Variable(testVariableLabels[1], testVariableTypes[1])) - addVariable(client,context,Variable(testVariableLabels[2], testVariableTypes[2])) - variableAddSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualVariableLabels = ls(client,context) - if setdiff(testVariableLabels,actualVariableLabels) == [] - variableAddSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(testVariableLabels,actualVariableLabels))" - end +function testGetFactor(client, context, factorLabels, factorTypes) + for i in 1:length(factorLabels) + actualFactor = getFactor(client,context,factorLabels[i]) + @test actualFactor["label"] == factorLabels[i] + @test actualFactor["fnctype"] == factorTypes[i] end - if !variableAddSucceeded return false end - testFactorLabels = ["x0x1f1","x0f1"] - testFactorTypes = ["Pose2Pose2","PriorPose2"] - addFactor(client,context,Factor(testFactorLabels[1], testFactorTypes[1], testVariableLabels, Pose2Pose2Data())) - addFactor(client,context,Factor(testFactorLabels[2], testFactorTypes[2], [testVariableLabels[1]], PriorPose2Data())) - factorAddSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualFactorLabels = lsf(client,context) - if setdiff(testFactorLabels,actualFactorLabels) == [] - factorAddSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(testFactorLabels,actualFactorLabels))" - end - end - if !factorAddSucceeded return false end - for i in 1:size(testFactorLabels)[1] - actualFactor = getFactor(client,context,testFactorLabels[i]) - if !(actualFactor["label"] == testFactorLabels[i]) - return false - end - if !(actualFactor["fnctype"] == testFactorTypes[i]) - return false - end - end - return true end -function testGetFactors(apiUrl, userId, robotId, sessionId) - client = NavAbilityHttpsClient(apiUrl) - context = Client(userId,robotId,sessionId) - testVariableLabels = ["x0", "x1"] - testVariableTypes = ["RoME.Pose2","RoME.Pose2"] - addVariable(client,context,Variable(testVariableLabels[1], testVariableTypes[1])) - addVariable(client,context,Variable(testVariableLabels[2], testVariableTypes[2])) - variableAddSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualVariableLabels = ls(client,context) - if setdiff(testVariableLabels,actualVariableLabels) == [] - variableAddSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(testVariableLabels,actualVariableLabels))" - end - end - if !variableAddSucceeded return false end - testFactorLabels = ["x0x1f1","x0f1"] - testFactorTypes = ["Pose2Pose2","PriorPose2"] - addFactor(client,context,Factor(testFactorLabels[1], testFactorTypes[1], testVariableLabels, Pose2Pose2Data())) - addFactor(client,context,Factor(testFactorLabels[2], testFactorTypes[2], [testVariableLabels[1]], PriorPose2Data())) - factorAddSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualFactorLabels = lsf(client,context) - if setdiff(testFactorLabels,actualFactorLabels) == [] - factorAddSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(testFactorLabels,actualFactorLabels))" - end - end - if !factorAddSucceeded return false end +function testGetFactors(client, context, factorLabels, factorTypes) + # Make a quick dictionary of the expected variable Types + factorIdType = Dict(factorLabels .=> factorTypes) - factors = getFactors(client,context;detail=FULL) + factors = getFactors(client, context; detail=SUMMARY) for f in factors - if !haskey(f,"label") || !haskey(f,"fnctype") - return false - end - if f["label"] == "x0x1f1" && !(f["fnctype"] == "Pose2Pose2") - return false - end - if f["label"] == "x0f1" && !(f["fnctype"] == "PriorPose2") - return false - end + @test f["fnctype"] == factorIdType[f["label"]] end - return true end -function RunTests(apiUrl, userId, robotId, sessionId) +function runFactorTests(client, context) @testset "factor-testset" begin @info "Running factor-testset" - @test testAddFactor(apiUrl, userId, robotId, sessionId*"_testAddFactor") - @test testLsf(apiUrl, userId, robotId, sessionId*"_testLsf") - @test testGetFactor(apiUrl, userId, robotId, sessionId*"_testGetFactor") - @test testGetFactors(apiUrl, userId, robotId, sessionId*"_testGetFactors") - end -end + # TODO: Refactor as a dictionary, or just do most of this in addFactor. + factorLabels = ["x0x1f1","x0f1"] + factorTypes = ["Pose2Pose2","PriorPose2"] + factorVariables = [["x0", "x1"], ["x0"]] + factorData = [Pose2Pose2Data(), PriorPose2Data()] + @test testAddFactor(client, context, factorLabels, factorTypes, factorVariables, factorData) + @test testLsf(client, context, factorLabels, factorTypes) + @test testGetFactor(client, context, factorLabels, factorTypes) + @test testGetFactors(client, context, factorLabels, factorTypes) + end end \ No newline at end of file diff --git a/test/integration/testSolve.jl b/test/integration/testSolve.jl index 92aee9a..6318de2 100644 --- a/test/integration/testSolve.jl +++ b/test/integration/testSolve.jl @@ -1,78 +1,22 @@ -module TestSolve +MAX_POLLING_TRIES = 150 -using Test +function testSolveSession(client, context) + testVariableLabels = ls(client, context) -include("../../src/NavAbilitySDK.jl") -using .NavAbilitySDK + resultId = solveSession(client,context) -MAX_POLLING_TRIES = 150 + # Wait for them to be done before proceeding. + waitForCompletion(client, [resultId], expectedStatuses=["Complete"]) -function testSolveSession(apiUrl, userId, robotId, sessionId) - client = NavAbilityHttpsClient(apiUrl) - context = Client(userId,robotId,sessionId) - testVariableLabels = ["x0", "x1"] - testVariableTypes = ["RoME.Pose2","RoME.Pose2"] - addVariable(client,context,Variable(testVariableLabels[1], testVariableTypes[1])) - addVariable(client,context,Variable(testVariableLabels[2], testVariableTypes[2])) - variableAddSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualVariableLabels = ls(client,context) - if setdiff(testVariableLabels,actualVariableLabels) == [] - variableAddSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(testVariableLabels,actualVariableLabels))" - end - end - if !variableAddSucceeded return false end - testFactorLabels = ["x0x1f1","x0f1"] - testFactorTypes = ["Pose2Pose2","PriorPose2"] - addFactor(client,context,Factor(testFactorLabels[1], testFactorTypes[1], testVariableLabels, Pose2Pose2Data())) - addFactor(client,context,Factor(testFactorLabels[2], testFactorTypes[2], [testVariableLabels[1]], PriorPose2Data())) - factorAddSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualFactorLabels = lsf(client,context) - if setdiff(testFactorLabels,actualFactorLabels) == [] - factorAddSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(testFactorLabels,actualFactorLabels))" - end - end - if !factorAddSucceeded return false end - function validatePPEs(variable)::Bool - ppes = get(variable, "ppes", nothing) - if ppes === nothing return false end - if size(ppes)[1] < 1 return false end - return true - end - solveSession(client,context) - successfullySolved = false - for i in 1:MAX_POLLING_TRIES - variables = getVariables(client,context;detail=SUMMARY) - if all([validatePPEs(v) for v in variables]) - successfullySolved = true - break - end - sleep(15) - if i % 10 == 0 - @info "Polling for ppes..." - end - end - if !successfullySolved throw("Failed to solve.") end - return true + # Get PPE's to confirm all is there. + # This'll blow up if not. + Dict((vId=>getVariable(client, context, vId; detail=SUMMARY)["ppes"]["default"]) for vId in testVariableLabels) end -function RunTests(apiUrl, userId, robotId, sessionId) +function runSolveTests(client, context) @testset "solve-testset" begin @info "Running solve-testset" - @test testSolveSession(apiUrl, userId, robotId, sessionId*"_testSolveSession") + @test testSolveSession(client, context) end end - -end \ No newline at end of file diff --git a/test/integration/testVariable.jl b/test/integration/testVariable.jl index 556f8fb..40bd5d7 100644 --- a/test/integration/testVariable.jl +++ b/test/integration/testVariable.jl @@ -1,92 +1,31 @@ -module TestVariable - -using Test - -include("../../src/NavAbilitySDK.jl") -using .NavAbilitySDK - -MAX_POLLING_TRIES = 150 - -function testAddVariable(apiUrl, userId, robotId, sessionId, variableLabels, variableTypes, variableTypeStrings) - client = NavAbilityHttpsClient(apiUrl) - context = Client(userId,robotId,sessionId) +function testAddVariable(client, context, variableLabels, variableTypes, variableTypeStrings) + resultIds = String[] for (index, label) in enumerate(variableLabels) - @test addVariable(client,context,Variable(label, variableTypes[index])) != "Error" + resultId = addVariable(client, context, Variable(label, variableTypes[index])) + @test resultId != "Error" + push!(resultIds, resultId) end -end -function testLs(apiUrl, userId, robotId, sessionId, variableLabels, variableTypes, variableTypeStrings) - client = NavAbilityHttpsClient(apiUrl) - context = Client(userId,robotId,sessionId) + # Wait for them to be done before proceeding. + waitForCompletion(client, resultIds, expectedStatuses=["Complete"]) - for (index, label) in enumerate(variableLabels) - @test addVariable(client,context,Variable(label, variableTypes[index])) != "Error" - end - - for i in 1:MAX_POLLING_TRIES - actualVariableLabels = ls(client,context) - if setdiff(variableLabels,actualVariableLabels) == [] - return - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(variableLabels,actualVariableLabels))" - end - end - error("Exceeded polling time") + return resultIds end -function testGetVariable(apiUrl, userId, robotId, sessionId, variableLabels, variableTypes, variableTypeStrings) - client = NavAbilityHttpsClient(apiUrl) - context = Client(userId,robotId,sessionId) - - for (index, label) in enumerate(variableLabels) - addVariable(client,context,Variable(label, variableTypes[index])) - end - - addSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualVariableLabels = ls(client,context) - if setdiff(variableLabels,actualVariableLabels) == [] - addSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(variableLabels,actualVariableLabels))" - end - end - !addSucceeded && error("Exceeded polling time") +function testLs(client, context, variableLabels, variableTypes, variableTypeStrings) + @test setdiff(variableLabels, ls(client, context)) == [] +end - for i in 1:size(variableLabels)[1] +function testGetVariable(client, context, variableLabels, variableTypes, variableTypeStrings) + for i in 1:length(variableLabels) actualVariable = getVariable(client,context,variableLabels[i]) @test actualVariable["label"] == variableLabels[i] @test actualVariable["variableType"] == variableTypeStrings[i] end end -function testGetVariables(apiUrl, userId, robotId, sessionId, variableLabels, variableTypes, variableTypeStrings) - client = NavAbilityHttpsClient(apiUrl) - context = Client(userId,robotId,sessionId) - - for (index, label) in enumerate(variableLabels) - addVariable(client,context,Variable(label, variableTypes[index])) - end - - addSucceeded = false - for i in 1:MAX_POLLING_TRIES - actualVariableLabels = ls(client,context) - if setdiff(variableLabels,actualVariableLabels) == [] - addSucceeded = true - break - end - sleep(2) - if i % 10 == 0 - @info "Polling for: $(setdiff(variableLabels,actualVariableLabels))" - end - end - !addSucceeded && error("Exceeded polling time") +function testGetVariables(client, context, variableLabels, variableTypes, variableTypeStrings) # Make a quick dictionary of the expected variable Types varIdType = Dict(variableLabels .=> variableTypeStrings) @@ -96,7 +35,7 @@ function testGetVariables(apiUrl, userId, robotId, sessionId, variableLabels, va end end -function RunTests(apiUrl, userId, robotId, sessionId) +function runVariableTests(client, context) @testset "Testing Variables" begin @info "Running variable-testset" @@ -104,11 +43,9 @@ function RunTests(apiUrl, userId, robotId, sessionId) variableTypes = [:Pose2,"RoME.Pose2", :Point2, :ContinuousScalar] variableTypeStrings = ["RoME.Pose2","RoME.Pose2", "RoME.Point2", "IncrementalInference.Position{1}"] - @testset "Adding" begin testAddVariable(apiUrl, userId, robotId, sessionId*"_testAddVariable", variableLabels, variableTypes, variableTypeStrings); end - @testset "Listing" begin testLs(apiUrl, userId, robotId, sessionId*"_testLs", variableLabels, variableTypes, variableTypeStrings); end - @testset "Getting" begin testGetVariable(apiUrl, userId, robotId, sessionId*"_testGetVariable", variableLabels, variableTypes, variableTypeStrings); end - @testset "Getting Lists" begin testGetVariables(apiUrl, userId, robotId, sessionId*"_testGetVariables", variableLabels, variableTypes, variableTypeStrings); end + @testset "Adding" begin testAddVariable(client, context, variableLabels, variableTypes, variableTypeStrings); end + @testset "Listing" begin testLs(client, context, variableLabels, variableTypes, variableTypeStrings); end + @testset "Getting" begin testGetVariable(client, context, variableLabels, variableTypes, variableTypeStrings); end + @testset "Getting Lists" begin testGetVariables(client, context, variableLabels, variableTypes, variableTypeStrings); end end -end - end \ No newline at end of file From ecde4cb35790aafb581f209c6f06ab5d14bcf0cb Mon Sep 17 00:00:00 2001 From: Sam Claassens Date: Mon, 28 Mar 2022 07:31:43 -0500 Subject: [PATCH 2/2] Refactored tests --- test/integration/runtests.jl | 2 +- test/integration/testFactor.jl | 13 ++++++------- test/integration/testSolve.jl | 19 ++++++++++++------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/test/integration/runtests.jl b/test/integration/runtests.jl index 22cd642..b67134a 100644 --- a/test/integration/runtests.jl +++ b/test/integration/runtests.jl @@ -24,5 +24,5 @@ sessionId2d = get(ENV,"SESSION_ID","TestSession2D"*randstring(7)) # integration test. runVariableTests(client, navabilityClient2D) runFactorTests(client, navabilityClient2D) - # runSolverTests(client, navabilityClient2D) + runSolveTests(client, navabilityClient2D) end diff --git a/test/integration/testFactor.jl b/test/integration/testFactor.jl index d6bd020..8697648 100644 --- a/test/integration/testFactor.jl +++ b/test/integration/testFactor.jl @@ -6,13 +6,12 @@ function testAddFactor(client, context, factorLabels, factorTypes, factorVariabl @test resultId != "Error" push!(resultIds, resultId) end - + waitForCompletion(client, resultIds, expectedStatuses=["Complete"]) return resultIds end function testLsf(client, context, factorLabels, factorTypes) - @show setdiff(factorLabels, lsf(client, context)) @test setdiff(factorLabels, lsf(client, context)) == [] end @@ -28,7 +27,7 @@ function testGetFactors(client, context, factorLabels, factorTypes) # Make a quick dictionary of the expected variable Types factorIdType = Dict(factorLabels .=> factorTypes) - factors = getFactors(client, context; detail=SUMMARY) + factors = getFactors(client, context, detail=FULL) for f in factors @test f["fnctype"] == factorIdType[f["label"]] end @@ -44,9 +43,9 @@ function runFactorTests(client, context) factorVariables = [["x0", "x1"], ["x0"]] factorData = [Pose2Pose2Data(), PriorPose2Data()] - @test testAddFactor(client, context, factorLabels, factorTypes, factorVariables, factorData) - @test testLsf(client, context, factorLabels, factorTypes) - @test testGetFactor(client, context, factorLabels, factorTypes) - @test testGetFactors(client, context, factorLabels, factorTypes) + @testset "Adding" begin testAddFactor(client, context, factorLabels, factorTypes, factorVariables, factorData) end + @testset "Listing" begin testLsf(client, context, factorLabels, factorTypes) end + @testset "Getting" begin testGetFactor(client, context, factorLabels, factorTypes) end + @testset "Getting Lists" begin testGetFactors(client, context, factorLabels, factorTypes) end end end \ No newline at end of file diff --git a/test/integration/testSolve.jl b/test/integration/testSolve.jl index 6318de2..50582d8 100644 --- a/test/integration/testSolve.jl +++ b/test/integration/testSolve.jl @@ -1,22 +1,27 @@ -MAX_POLLING_TRIES = 150 -function testSolveSession(client, context) - testVariableLabels = ls(client, context) +function testSolveSession(client, context, variableLabels) + # allVariableLabels = ls(client, context, variableLabels) resultId = solveSession(client,context) # Wait for them to be done before proceeding. waitForCompletion(client, [resultId], expectedStatuses=["Complete"]) - # Get PPE's to confirm all is there. - # This'll blow up if not. - Dict((vId=>getVariable(client, context, vId; detail=SUMMARY)["ppes"]["default"]) for vId in testVariableLabels) + # Get PPE's are there for the connected variables. + # TODO - complete the factor graph. + for v in variableLabels # getVariables(client, context, detail=SUMMARY) + # First solve key (only) is the default result + @test getVariable(client, context, v)["ppes"][1]["solveKey"] == "default" + end end function runSolveTests(client, context) @testset "solve-testset" begin @info "Running solve-testset" - @test testSolveSession(client, context) + # TODO: Test the rest of the variables. + variableLabels = ["x0", "x1"] + + testSolveSession(client, context, variableLabels) end end