Passed
Pull Request — develop (#77)
by Angeline
02:05
created

test_cmd_aacgmv2.TestCmdAACGMV2.setup_method()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 11
rs 9.9
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
@pytest.mark.xfail
10
class TestCmdAACGMV2(object):
11
    """Unit tests for the command line interface."""
12
    def setup_method(self):
13
        """Run before every method to create a clean testing setup."""
14
        self.test_dir = os.path.join(aacgmv2.__path__[0], 'tests', 'test_data')
15
        self.output = os.path.join(self.test_dir, "output.txt")
16
        self.convert = os.path.join(self.test_dir, "test_convert.txt")
17
        self.single = os.path.join(self.test_dir,
18
                                   'test_convert_single_line.txt')
19
        self.mlt = os.path.join(self.test_dir, 'test_convert_mlt.txt')
20
        self.mlt_single = os.path.join(self.test_dir,
21
                                       'test_convert_mlt_single_line.txt')
22
        self.rtol = 1.0e-4
23
24
    def teardown_method(self):
25
        """Run after every method to clean up previous testing."""
26
        if os.path.isfile(self.output):
27
            os.remove(self.output)
28
29
        del self.test_dir, self.output, self.convert, self.single, self.mlt
30
        del self.mlt_single, self.rtol
31
32
    @pytest.mark.parametrize('pin,ref',
33
                             [([], [[57.4810, 93.5290, 1.04566],
34
                                    [58.5380, 93.9324, 1.0456],
35
                                    [59.5900, 94.3614, 1.04556]]),
36
                              (['-v'], [[51.6616, -66.6338, 306.1783],
37
                                        [52.6792, -66.7291, 306.5470],
38
                                        [53.6980, -66.8286, 306.91265]]),
39
                              (['-g'], [[57.6746, 93.6036, 1.0471],
40
                                        [58.7271, 94.0102, 1.0471],
41
                                        [59.7743, 94.4425, 1.0471]]),
42
                              (['-t'], [[57.4785, 93.5398, 1.04566],
43
                                        [58.5354, 93.9438, 1.04561],
44
                                        [59.5873, 94.3731, 1.04556]]),
45
                              (['-t', '-v'], [[51.6524, -66.6180, 306.1750],
46
                                              [52.6738, -66.7167, 306.5451],
47
                                              [53.6964, -66.8202, 306.9121]])])
48
    def test_convert_command_line(self, pin, ref):
49
        """Test the output from the command line routine.
50
51
        Parameters
52
        ----------
53
        pin : list
54
           List of potential flags
55
        ref : list
56
           List of expected output values
57
58
        """
59
        p_commands = ['python', '-m', 'aacgmv2', 'convert', '-i',
60
                      self.convert, '-d', '20150224', '-o',
61
                      self.output]
62
        p_commands.extend(pin)
63
        p = subprocess.Popen(p_commands)
64
        p.communicate()
65
        p.wait()
66
        assert os.path.isfile(self.output)
67
        data = np.loadtxt(self.output)
68
        np.testing.assert_allclose(data, ref, rtol=self.rtol)
69
70
    def test_convert_today(self):
71
        """Test the shape of the output for today's date."""
72
        p = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
73
                              self.convert, '-o', self.output])
74
        p.communicate()
75
        p.wait()
76
        assert os.path.isfile(self.output)
77
        data = np.loadtxt(self.output)
78
        assert data.shape == (3, 3)
79
80
    def test_convert_single_line(self):
81
        """Test the command line with a single line as input."""
82
        p = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
83
                              self.single, '-d', '20150224', '-o', self.output])
84
        p.communicate()
85
        p.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
        p = subprocess.Popen('python -m aacgmv2 -h', shell=True,
94
                             stdout=subprocess.PIPE)
95
        stdout, _ = p.communicate()
96
        p.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
        p = subprocess.Popen(
102
            'echo 60 15 300 | python -m aacgmv2 convert -d 20150224',
103
            shell=True, stdout=subprocess.PIPE)
104
        stdout, _ = p.communicate()
105
        p.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
        p = subprocess.Popen(p_command)
126
        p.communicate()
127
        p.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
        p = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert_mlt', '-i',
135
                              self.mlt_single, '20150224140015', '-o',
136
                              self.output])
137
        p.communicate()
138
        p.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
        p = subprocess.Popen(echo_command, shell=True, stdout=subprocess.PIPE)
156
        stdout, _ = p.communicate()
157
        p.wait()
158
        assert b'44.63126817' in stdout
159