test_read   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 123
Duplicated Lines 13.82 %

Importance

Changes 0
Metric Value
eloc 80
dl 17
loc 123
rs 10
c 0
b 0
f 0
wmc 15

8 Functions

Rating   Name   Duplication   Size   Complexity  
A test_3hourly_index() 17 17 1
A _assert_age() 0 4 1
A df_3h() 0 3 1
A test_update() 0 11 2
A test_auto_update() 0 11 2
A test_not_avail() 0 13 5
A test_age() 0 8 2
A test_daily() 0 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
# vim:fileencoding=utf-8
3
#
4
# Copyright (c) 2020 Stefan Bender
5
#
6
# This module is part of pyspaceweather.
7
# pyspaceweather is free software: you can redistribute it or modify
8
# it under the terms of the GNU General Public License as published
9
# by the Free Software Foundation, version 2.
10
# See accompanying COPYING.GPLv2 file or http://www.gnu.org/licenses/gpl-2.0.html.
11
"""Space weather index read tests
12
"""
13
import os
14
import shutil
15
16
import numpy as np
17
import pandas as pd
18
import requests
19
20
import pytest
21
22
from spaceweather import (
23
	ap_kp_3h, sw_daily, get_file_age, update_data,
24
	SW_PATH_ALL, SW_PATH_5Y,
25
)
26
from spaceweather.celestrak import DL_URL_5Y
27
28
29
@pytest.fixture(scope="module")
30
def df_3h():
31
	return ap_kp_3h()
32
33
34
def test_age():
35
	now = pd.Timestamp.utcnow()
36
	for p in [SW_PATH_ALL, SW_PATH_5Y]:
37
		assert os.path.exists(p)
38
		fage0 = get_file_age(p)
39
		fage1 = now - get_file_age(p, relative=False)
40
		assert (fage0 > pd.Timedelta("3h")) == (fage1 > pd.Timedelta("3h"))
41
		assert (fage0 > pd.Timedelta("1d")) == (fage1 > pd.Timedelta("1d"))
42
43
44
def _assert_age(p, age):
45
	assert os.path.exists(p)
46
	fage = get_file_age(p)
47
	assert fage < pd.Timedelta(age)
48
49
50
@pytest.mark.online
51
def test_update(tmpdir):
52
	# test with temporary files
53
	tmpdir = str(tmpdir)
54
	tmp_all = os.path.join(tmpdir, "tmp_all.dat")
55
	tmp_5y = os.path.join(tmpdir, "tmp_5y.dat")
56
	shutil.copy(SW_PATH_ALL, tmp_all)
57
	shutil.copy(SW_PATH_5Y, tmp_5y)
58
	update_data(swpath_all=tmp_all, swpath_5y=tmp_5y, min_age="100d")
59
	for (p, age) in zip([tmp_all, tmp_5y], ["1460d", "100d"]):
60
		_assert_age(p, age)
61
62
63
def test_auto_update(mocker, tmpdir):
64
	# test with non-existent file
65
	mocker.patch("requests.get")
66
	tmpdir = str(tmpdir)
67
	update_data(swpath_5y=os.path.join(tmpdir, "foo.dat"))
68
	requests.get.assert_called_with(DL_URL_5Y, stream=True)
69
	# Should update the last-5-year data
70
	sw_daily(update=True, update_interval="1d")
71
	requests.get.assert_called_with(DL_URL_5Y, stream=True)
72
	with pytest.warns(UserWarning):
73
		sw_daily(update=False, update_interval="0h")
74
75
76
def test_not_avail(mocker, tmpdir):
77
	# test with non-existent file
78
	mocker.patch("requests.get")
79
	tmpdir = str(tmpdir)
80
	tmpfile = os.path.join(tmpdir, "foo.dat")
81
	# daily
82
	with pytest.raises(IOError):
83
		with pytest.warns(UserWarning):
84
			sw_daily(update=False, update_interval="0h", swpath_5y=tmpfile)
85
	# 3h data
86
	with pytest.raises(IOError):
87
		with pytest.warns(UserWarning):
88
			ap_kp_3h(update=False, update_interval="0h", swpath_5y=tmpfile)
89
90
91
def test_daily():
92
	df = sw_daily()
93
	np.testing.assert_allclose(
94
		df.loc["2000-01-01"].values,
95
		np.array([
96
			2.000e+03, 1.000e+00, 1.000e+00, 2.272e+03, 7.000e+00, 5.300e+00, 4.700e+00,
97
			4.000e+00, 3.300e+00, 4.300e+00, 3.000e+00, 4.300e+00, 3.700e+00, 3.270e+01,
98
			5.600e+01, 3.900e+01, 2.700e+01, 1.800e+01, 3.200e+01, 1.500e+01, 3.200e+01,
99
			2.200e+01, 3.000e+01, 1.300e+00, 6.000e+00, 7.100e+01, 1.256e+02, 0.000e+00,
100
			1.611e+02, 1.750e+02, 1.299e+02, 1.662e+02, 1.790e+02,
101
		]),
102
		rtol=1e-12,
103
	)
104
105
106 View Code Duplication
@pytest.mark.parametrize(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
107
	"name, result",
108
	[
109
		("Ap", np.array([56, 39, 27, 18, 32, 15, 32, 22])),
110
		("Kp", np.array([5.3, 4.7, 4.0, 3.3, 4.3, 3.0, 4.3, 3.7])),
111
	]
112
)
113
def test_3hourly_index(name, result, df_3h):
114
	df = df_3h
115
	np.testing.assert_allclose(
116
		df.loc[
117
			pd.date_range(
118
				"2000-01-01 01:30", "2000-01-01 23:30", freq="3h"
119
			)
120
		][name].values,
121
		result,
122
		rtol=1e-12,
123
	)
124