Passed
Pull Request — develop (#47)
by Angeline
01:04
created

TestCmdAACGMV2.test_convert_single_line()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
from __future__ import division, absolute_import, unicode_literals, print_function
3
4
import subprocess
5
import numpy as np
6
import os
7
import pytest
8
from sys import version_info
9
10
import aacgmv2
11
12
class TestCmdAACGMV2:
13
    def setup(self):
14
        """Runs before every method to create a clean testing setup"""
15
        self.test_dir = os.path.join(aacgmv2.__path__[0], 'tests', 'test_data')
16
        self.output = os.path.join(self.test_dir, "output.txt")
17
        self.convert = os.path.join(self.test_dir, "test_convert.txt")
18
        self.single = os.path.join(self.test_dir,
19
                                   'test_convert_single_line.txt')
20
        self.mlt = os.path.join(self.test_dir, 'test_convert_mlt.txt')
21
        self.mlt_single = os.path.join(self.test_dir,
22
                                       'test_convert_mlt_single_line.txt')
23
        self.rtol = 1.0e-4
24
25
    def teardown(self):
26
        """Runs after every method to clean up previous testing"""
27
        if os.path.isfile(self.output):
28
            os.remove(self.output)
29
30
        del self.test_dir, self.output, self.convert, self.single, self.mlt
31
        del self.mlt_single, self.rtol
32
33
    @pytest.mark.parametrize('pin,ref',
34
                             [([], [[57.4810, 93.5290, 1.04566],
35
                                    [58.5380, 93.9324, 1.0456],
36
                                    [59.5900, 94.3614, 1.04556]]),
37
                              (['-v'], [[51.6616, -66.6338, 306.1783],
38
                                        [52.6792, -66.7291, 306.5470],
39
                                        [53.6980, -66.8286, 306.91265]]),
40
                              (['-g'], [[57.6746, 93.6036, 1.0471],
41
                                        [58.7271, 94.0102, 1.0471],
42
                                        [59.7743, 94.4425, 1.0471]]),
43
                              (['-t'], [[57.4785, 93.5398, 1.04566],
44
                                        [58.5354, 93.9438, 1.04561],
45
                                        [59.5873, 94.3731, 1.04556]]),
46
                              (['-t', '-v'], [[51.6524, -66.6180, 306.1750],
47
                                              [52.6738, -66.7167, 306.5451],
48
                                              [53.6964, -66.8202, 306.9121]])])
49
    def test_convert_command_line(self, pin, ref):
50
        """ Test the output from the command line routine"""
51
        p_commands = ['python', '-m', 'aacgmv2', 'convert', '-i',
52
                      self.convert, '-d', '20150224', '-o',
53
                      self.output]
54
        p_commands.extend(pin)
55
        p = subprocess.Popen(p_commands)
56
        p.communicate()
57
        p.wait()
58
        data = np.loadtxt(self.output)
59
        np.testing.assert_allclose(data, ref, rtol=self.rtol)
60
61
    @pytest.mark.skip(version_info.major == 3,
62
                      reason='only works locally for Python 3')
63
    def test_convert_today(self):
64
        """ Test the shape of the output for today's date """
65
        p = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
66
                              self.convert, '-o', self.output])
67
        p.communicate()
68
        p.wait()
69
        data = np.loadtxt(self.output)
70
        assert data.shape == (3,3)
71
        
72
    def test_convert_single_line(self):
73
        """ Test the command line with a single line as input """
74
        p = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
75
                              self.single, '-d', '20150224', '-o', self.output])
76
        p.communicate()
77
        p.wait()
78
        data = np.loadtxt(self.output)
79
        np.testing.assert_allclose(data, [57.4810, 93.5290, 1.04566],
80
                                   rtol=self.rtol)
81
82
    def test_convert_stdin_stdout(self):
83
        p = subprocess.Popen(
84
            'echo 60 15 300 | python -m aacgmv2 convert -d 20150224',
85
                             shell=True, stdout=subprocess.PIPE)
86
        stdout, _ = p.communicate()
87
        p.wait()
88
        assert b'57.48099198 93.52895314' in stdout
89
90
    @pytest.mark.parametrize('pin,ref',
91
                             [([], [9.0912, 9.8246, 10.5579]),
92
                              (['-v'], [-120.3687, 44.6313, -150.3687])
93
                             ])
94
    def test_convert_mlt_command_line(self, pin, ref):
95
        """ Test the command line MLT conversion options"""
96
        p_command = ['python', '-m', 'aacgmv2', 'convert_mlt', '-i',
97
                     self.mlt, '20150224140015', '-o', self.output]
98
        p_command.extend(pin)
99
        p = subprocess.Popen(p_command)
100
        p.communicate()
101
        p.wait()
102
        data = np.loadtxt(self.output)
103
        np.testing.assert_allclose(data, ref, rtol=self.rtol)
104
105
106
    def test_convert_mlt_single_line(self):
107
        p = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert_mlt', '-i',
108
                              self.mlt_single, '20150224140015', '-o',
109
                              self.output])
110
        p.communicate()
111
        p.wait()
112
        data = np.loadtxt(self.output)
113
        np.testing.assert_allclose(data, 9.0912, rtol=self.rtol)
114
115
    @pytest.mark.parametrize('echo_command',
116
                             [('echo 12 | python -m aacgmv2 convert_mlt -v 20150224140015'),
117
                              ('echo 12 | python -m aacgmv2 convert_mlt 20150224140015 -v')])
118
    def test_convert_mlt_stdin_stdout(self, echo_command):
119
        p = subprocess.Popen(echo_command, shell=True, stdout=subprocess.PIPE)
120
        stdout, _ = p.communicate()
121
        p.wait()
122
        assert b'44.63126817' in stdout
123