Passed
Pull Request — main (#81)
by Angeline
01:38
created

test_cmd_aacgmv2.TestCmdAACGMV2.test_main_help()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import subprocess
2
import numpy as np
3
import os
4
import pytest
5
6
import aacgmv2
7
8
9
class TestCmdAACGMV2(object):
10
    """Unit tests for the command line interface."""
11
    def setup_method(self):
12
        """Run before every method to create a clean testing setup."""
13
        self.test_dir = os.path.join(aacgmv2.__path__[0], 'tests', 'test_data')
14
        self.output = os.path.join(self.test_dir, "output.txt")
15
        self.convert = os.path.join(self.test_dir, "test_convert.txt")
16
        self.single = os.path.join(self.test_dir,
17
                                   'test_convert_single_line.txt')
18
        self.mlt = os.path.join(self.test_dir, 'test_convert_mlt.txt')
19
        self.mlt_single = os.path.join(self.test_dir,
20
                                       'test_convert_mlt_single_line.txt')
21
        self.rtol = 1.0e-4
22
23
    def teardown_method(self):
24
        """Run after every method to clean up previous testing."""
25
        if os.path.isfile(self.output):
26
            os.remove(self.output)
27
28
        del self.test_dir, self.output, self.convert, self.single, self.mlt
29
        del self.mlt_single, self.rtol
30
31
    @pytest.mark.parametrize('pin,ref',
32
                             [([], [[57.4810, 93.5290, 1.04566],
33
                                    [58.5380, 93.9324, 1.0456],
34
                                    [59.5900, 94.3614, 1.04556]]),
35
                              (['-v'], [[51.6616, -66.6338, 306.1783],
36
                                        [52.6792, -66.7291, 306.5470],
37
                                        [53.6980, -66.8286, 306.91265]]),
38
                              (['-g'], [[57.6746, 93.6036, 1.0471],
39
                                        [58.7271, 94.0102, 1.0471],
40
                                        [59.7743, 94.4425, 1.0471]]),
41
                              (['-t'], [[57.4785, 93.5398, 1.04566],
42
                                        [58.5354, 93.9438, 1.04561],
43
                                        [59.5873, 94.3731, 1.04556]]),
44
                              (['-t', '-v'], [[51.6524, -66.6180, 306.1750],
45
                                              [52.6738, -66.7167, 306.5451],
46
                                              [53.6964, -66.8202, 306.9121]])])
47
    def test_convert_command_line(self, pin, ref):
48
        """Test the output from the command line routine.
49
50
        Parameters
51
        ----------
52
        pin : list
53
           List of potential flags
54
        ref : list
55
           List of expected output values
56
57
        """
58
        p_commands = ['python', '-m', 'aacgmv2', 'convert', '-i',
59
                      self.convert, '-d', '20150224', '-o',
60
                      self.output]
61
        p_commands.extend(pin)
62
        pin = subprocess.Popen(p_commands)
63
        pin.communicate()
64
        pin.wait()
65
        assert os.path.isfile(self.output)
66
        data = np.loadtxt(self.output)
67
        np.testing.assert_allclose(data, ref, rtol=self.rtol)
68
69
    def test_convert_today(self):
70
        """Test the shape of the output for today's date."""
71
        pin = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
72
                                self.convert, '-o', self.output])
73
        pin.communicate()
74
        pin.wait()
75
        assert os.path.isfile(self.output)
76
        data = np.loadtxt(self.output)
77
        assert data.shape == (3, 3)
78
79
    def test_convert_single_line(self):
80
        """Test the command line with a single line as input."""
81
        pin = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
82
                                self.single, '-d', '20150224', '-o',
83
                                self.output])
84
        pin.communicate()
85
        pin.wait()
86
        assert os.path.isfile(self.output)
87
        data = np.loadtxt(self.output)
88
        np.testing.assert_allclose(data, [57.4810, 93.5290, 1.04566],
89
                                   rtol=self.rtol)
90
91
    def test_main_help(self):
92
        """Test the help output."""
93
        pin = subprocess.Popen('python -m aacgmv2 -h', shell=True,
94
                               stdout=subprocess.PIPE)
95
        stdout, _ = pin.communicate()
96
        pin.wait()
97
        assert b'usage' in stdout
98
99
    def test_convert_stdin_stdout(self):
100
        """Test the ability to pipe in inputs and pipe out the outputs."""
101
        pin = subprocess.Popen(
102
            'echo 60 15 300 | python -m aacgmv2 convert -d 20150224',
103
            shell=True, stdout=subprocess.PIPE)
104
        stdout, _ = pin.communicate()
105
        pin.wait()
106
        assert b'57.48099198 93.52895314' in stdout
107
108
    @pytest.mark.parametrize('pin,ref',
109
                             [([], [9.0912, 9.8246, 10.5579]),
110
                              (['-v'], [-120.3687, 44.6313, -150.3687])])
111
    def test_convert_mlt_command_line(self, pin, ref):
112
        """Test the command line MLT conversion options.
113
114
        Parameters
115
        ----------
116
        pin : list
117
            List of input flags
118
        ref : list
119
            List of expected outputs
120
121
        """
122
        p_command = ['python', '-m', 'aacgmv2', 'convert_mlt', '-i',
123
                     self.mlt, '20150224140015', '-o', self.output]
124
        p_command.extend(pin)
125
        pin = subprocess.Popen(p_command)
126
        pin.communicate()
127
        pin.wait()
128
        assert os.path.isfile(self.output)
129
        data = np.loadtxt(self.output)
130
        np.testing.assert_allclose(data, ref, rtol=self.rtol)
131
132
    def test_convert_mlt_single_line(self):
133
        """Test the CLI for converting MLT."""
134
        pin = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert_mlt', '-i',
135
                                self.mlt_single, '20150224140015', '-o',
136
                                self.output])
137
        pin.communicate()
138
        pin.wait()
139
        assert os.path.isfile(self.output)
140
        data = np.loadtxt(self.output)
141
        np.testing.assert_allclose(data, 9.0912, rtol=self.rtol)
142
143
    @pytest.mark.parametrize('echo_command', [
144
        ('echo 12 | python -m aacgmv2 convert_mlt -v 20150224140015'),
145
        ('echo 12 | python -m aacgmv2 convert_mlt 20150224140015 -v')])
146
    def test_convert_mlt_stdin_stdout(self, echo_command):
147
        """Test MLT conversion through piped input and output.
148
149
        Parameters
150
        ----------
151
        echo_command : str
152
            Command to run starting with an echo pipe
153
154
        """
155
        pin = subprocess.Popen(echo_command, shell=True, stdout=subprocess.PIPE)
156
        stdout, _ = pin.communicate()
157
        pin.wait()
158
        assert b'44.63126817' in stdout
159