test_srv.py 2.87 KB
Newer Older
Maximilien Naveau's avatar
Maximilien Naveau committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
"""
license BSD 3-clause
Copyright (c) 2020, CNRS

Unit-tests for the python API of the DynamicGraphManager
"""

import os
import unittest
import rostest
import rospy

from dynamic_graph_bridge_msgs.srv import (
    RunPythonCommand,
    RunCommand,
    RunPythonFile,
    RunPythonCommandRequest,
    RunCommandRequest,
    RunPythonFileRequest,
)


class TestServices(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_run_python_command(self):
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
40
        rospy.wait_for_service("run_python_command")
Maximilien Naveau's avatar
Maximilien Naveau committed
41
        try:
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
42
            client = rospy.ServiceProxy("run_python_command", RunPythonCommand)
Maximilien Naveau's avatar
Maximilien Naveau committed
43
44
45
46
            request = RunPythonCommandRequest()
            request.input = "1+1"
            response = client(request)
        except rospy.ServiceException as e:
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
47
            print("Service call failed: %s" % e)
Maximilien Naveau's avatar
Maximilien Naveau committed
48
            self.assertTrue(False)
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
49

Maximilien Naveau's avatar
Maximilien Naveau committed
50
51
52
53
54
        self.assertEqual(response.result, "1+1_result_python_cmd")
        self.assertEqual(response.standardoutput, "standardoutput")
        self.assertEqual(response.standarderror, "standarderror")

    def test_run_command(self):
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
55
        rospy.wait_for_service("run_command")
Maximilien Naveau's avatar
Maximilien Naveau committed
56
        try:
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
57
            client = rospy.ServiceProxy("run_command", RunCommand)
Maximilien Naveau's avatar
Maximilien Naveau committed
58
59
60
61
            request = RunCommandRequest()
            request.input = "1+1"
            response = client(request)
        except rospy.ServiceException as e:
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
62
            print("Service call failed: %s" % e)
Maximilien Naveau's avatar
Maximilien Naveau committed
63
            self.assertTrue(False)
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
64

Maximilien Naveau's avatar
Maximilien Naveau committed
65
66
67
68
69
        self.assertEqual(response.result, "1+1_result_cmd")
        self.assertEqual(response.standardoutput, "standardoutput")
        self.assertEqual(response.standarderror, "standarderror")

    def test_run_python_file_good(self):
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
70
        rospy.wait_for_service("run_python_file")
Maximilien Naveau's avatar
Maximilien Naveau committed
71
        try:
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
72
            client = rospy.ServiceProxy("run_python_file", RunPythonFile)
Maximilien Naveau's avatar
Maximilien Naveau committed
73
74
75
76
            request = RunPythonFileRequest()
            request.input = os.path.abspath(__file__)
            response = client(request)
        except rospy.ServiceException as e:
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
77
            print("Service call failed: %s" % e)
Maximilien Naveau's avatar
Maximilien Naveau committed
78
79
80
            self.assertTrue(False)

        self.assertEqual(response.result, "True")
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
81

Maximilien Naveau's avatar
Maximilien Naveau committed
82
    def test_run_python_file_bad(self):
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
83
        rospy.wait_for_service("run_python_file")
Maximilien Naveau's avatar
Maximilien Naveau committed
84
        try:
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
85
            client = rospy.ServiceProxy("run_python_file", RunPythonFile)
Maximilien Naveau's avatar
Maximilien Naveau committed
86
87
88
89
            request = RunPythonFileRequest()
            request.input = "hthre21@#$%@)#_#%*+($^&$i;gnvj;bae"
            response = client(request)
        except rospy.ServiceException as e:
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
90
            print("Service call failed: %s" % e)
Maximilien Naveau's avatar
Maximilien Naveau committed
91
92
93
94
95
            self.assertTrue(False)

        self.assertEqual(response.result, "False")


Guilhem Saurel's avatar
format    
Guilhem Saurel committed
96
97
if __name__ == "__main__":
    rostest.rosrun("dynamic_graph_bridge_msgs", "test_srv", TestServices)