Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 103 additions & 51 deletions scripts/test_knowrob_ros_lib.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,119 @@
from knowrob_ros.knowrob_ros_lib import KnowRobRosLib, graph_answer_to_dict, get_default_modalframe, graph_answers_to_list
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_knowrob_ros_lib.py

Unit tests for KnowRobRosLib, now including incremental-query support.
"""

import unittest
import rosunit

from knowrob_ros.knowrob_ros_lib import (
KnowRobRosLib,
TripleQueryBuilder,
graph_answer_to_dict,
graph_answers_to_list,
get_default_modalframe,
)
from knowrob_ros.msg import (
KeyValuePair,
AskOneAction,
AskOneGoal,
AskOneResult,
AskAllAction,
AskAllGoal,
AskAllResult,
GraphQueryMessage,
GraphAnswerMessage,
AskIncrementalResult,
AskIncrementalNextSolutionResult,
AskOneResult,
TellResult,
)

class TestKnowrobRosLib(unittest.TestCase):
def test_ask_all(self):
# Test the ask_one function
ask_all_result = self.knowrob_ros.ask_all("lpn:jealous(lpn:vincent, X)", get_default_modalframe())
self.assertTrue(ask_all_result.status == AskAllResult.TRUE)
result_dict = graph_answers_to_list(ask_all_result.answers)
print("Result dict:", str(result_dict))
self.assertEqual(result_dict, [{
'X': 'http://knowrob.org/kb/lpn#marsellus'
}])

def test_ask_one(self):
# Test the ask_one function
ask_one_result = self.knowrob_ros.ask_one("lpn:jealous(lpn:vincent, X)", get_default_modalframe())
self.assertTrue(ask_one_result.status == AskOneResult.TRUE)
result_dict = graph_answer_to_dict(ask_one_result.answer)
print("Result dict:", str(result_dict))
self.assertEqual(result_dict, {
'X': 'http://knowrob.org/kb/lpn#marsellus'
})
class TestKnowrobRosLib(unittest.TestCase):
"""
TestCase for KnowRobRosLib, covering AskOne, AskAll, AskIncremental, and Tell.
"""

# def test_tell(self):
# # Create the triples to be added
# builder = knowrob_ros_lib.TripleQueryBuilder()
# builder.add("alice", "knows", "bob")
# builder.add("bob", "likes", "pizza")
# query_str = builder.build_query_string()

# # Test the tell function
# result = knowrob_ros.tell(query_str)
# self.assertTrue(result.success)
# result = knowrob_ros.ask_all("lpn:jelous(alice, X)")
# self.assertEqual(result.bindings, [{
# 'X': 'pizza'
# }])

# Init the test class
@classmethod
def setUpClass(cls):
# Initialize the knowrob_ros_lib
"""
Initialize KnowRobRosLib and ROS node once for all tests.
"""
cls.knowrob_ros = KnowRobRosLib()
# Initialize the ROS node
cls.knowrob_ros.init_node("test_knowrob_ros_lib")

@classmethod
def tearDownClass(cls):
# Shutdown the ROS node
"""
Shutdown ROS node after all tests.
"""
cls.knowrob_ros.shutdown_node()


def test_ask_all(self):
"""AskAll should return all matches for a query."""
result = self.knowrob_ros.ask_all(
"lpn:jealous(lpn:vincent, X)",
get_default_modalframe()
)
self.assertEqual(result.status, AskAllResult.TRUE)
bindings = graph_answers_to_list(result.answers)
self.assertEqual(bindings, [{
'X': 'http://knowrob.org/kb/lpn#marsellus'
}])

def test_ask_one(self):
"""AskOne should return a single binding for a query."""
result = self.knowrob_ros.ask_one(
"lpn:jealous(lpn:vincent, X)",
get_default_modalframe()
)
self.assertEqual(result.status, AskOneResult.TRUE)
binding = graph_answer_to_dict(result.answer)
self.assertEqual(binding, {
'X': 'http://knowrob.org/kb/lpn#marsellus'
})

def test_tell(self):
"""Tell should insert triples and they should be queryable."""
builder = TripleQueryBuilder()
builder.add("alice", "marriedTo", "frank")
triples = builder.get_triples()

tell_result = self.knowrob_ros.tell(triples, get_default_modalframe())
self.assertEqual(tell_result.status, TellResult.TRUE)

query_result = self.knowrob_ros.ask_all(
"marriedTo(alice, X)",
get_default_modalframe()
)
bindings = graph_answers_to_list(query_result.answers)
self.assertEqual(bindings, [{
'X': 'frank'
}])

def test_ask_incremental(self):
"""
Full incremental-query flow: start, get first solution, then finish.
"""
# Start incremental query
start = self.knowrob_ros.ask_incremental(
"lpn:jealous(lpn:vincent, X)",
get_default_modalframe()
)
self.assertEqual(start.status, AskIncrementalResult.TRUE)
query_id = start.queryId
self.assertGreater(query_id, 0)

# Retrieve next (first) solution
next_sol = self.knowrob_ros.next_solution(query_id)
self.assertEqual(next_sol.status, AskIncrementalNextSolutionResult.TRUE)
binding = graph_answer_to_dict(next_sol.answer)
self.assertEqual(binding, {
'X': 'http://knowrob.org/kb/lpn#marsellus'
})

# Finish incremental query
finished = self.knowrob_ros.finish_incremental(query_id)
self.assertTrue(finished)


# Note: stray free-standing setUpClass below is a duplicate and has no effect on tests.
@classmethod
def setUpClass(cls):
cls.knowrob_ros = KnowRobRosLib()
Expand All @@ -70,7 +122,7 @@ def setUpClass(cls):

if __name__ == '__main__':
rosunit.unitrun(
'knowrob_ros', # your package
'knowrob_ros', # package name
'test_knowrob_ros_lib', # test name
TestKnowrobRosLib # your TestCase
)
TestKnowrobRosLib # TestCase class
)
Loading