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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
import pytest
from astropy import time, units as u
from poliastro.bodies import Earth, Mars, Sun
from poliastro.ephem import Ephem
from poliastro.examples import churi, iss
from poliastro.plotting import OrbitPlotter2D, OrbitPlotter3D
@pytest.mark.parametrize("plotter_class", [OrbitPlotter2D, OrbitPlotter3D])
def test_get_figure_has_expected_properties(plotter_class):
frame = plotter_class()
figure = frame.show()
assert figure.data == ()
assert figure.layout.autosize is True
assert "xaxis" in figure.layout
assert "yaxis" in figure.layout
def test_get_3d_figure_has_expected_properties():
frame = OrbitPlotter3D()
figure = frame.show()
assert figure.data == ()
assert figure.layout.autosize is True
assert "xaxis" in figure.layout.scene
assert "yaxis" in figure.layout.scene
assert "zaxis" in figure.layout.scene
assert "aspectmode" in figure.layout.scene
@pytest.mark.parametrize("plotter_class", [OrbitPlotter2D, OrbitPlotter3D])
def test_set_different_attractor_raises_error(plotter_class):
body1 = Earth
body2 = Mars
frame = plotter_class()
frame.set_attractor(body1)
with pytest.raises(NotImplementedError) as excinfo:
frame.set_attractor(body2)
assert "Attractor has already been set to Earth" in excinfo.exconly()
@pytest.mark.parametrize("plotter_class", [OrbitPlotter2D, OrbitPlotter3D])
def test_plot_sets_attractor(plotter_class):
frame = plotter_class()
assert frame._attractor is None
frame.plot(iss)
assert frame._attractor == iss.attractor
@pytest.mark.parametrize("plotter_class", [OrbitPlotter2D, OrbitPlotter3D])
def test_plot_appends_data(plotter_class):
frame = plotter_class()
assert len(frame.trajectories) == 0
frame.plot(iss)
assert len(frame.trajectories) == 1
@pytest.mark.parametrize("plotter_class", [OrbitPlotter2D, OrbitPlotter3D])
def test_plot_trajectory_without_attractor_raises_error(plotter_class):
frame = plotter_class()
with pytest.raises(ValueError) as excinfo:
frame._frame = 1 # Set it to something not None to skip frame check
frame.plot_trajectory({})
assert (
"An attractor must be set up first, please use "
"set_attractor(Major_Body) or plot(orbit)" in excinfo.exconly()
)
def test_plot_2d_trajectory_without_frame_raises_error():
frame = OrbitPlotter2D()
with pytest.raises(ValueError) as excinfo:
frame.set_attractor(Sun)
frame.plot_trajectory({})
assert (
"A frame must be set up first, please use "
"set_orbit_frame(orbit) or plot(orbit)" in excinfo.exconly()
)
def test_ephem_without_frame_raises_error():
epochs = time.Time("2020-04-29 10:43", scale="tdb")
earth = Ephem.from_body(Earth, epochs)
plotter = OrbitPlotter2D()
with pytest.raises(ValueError) as excinfo:
plotter.set_attractor(Sun)
plotter.plot_ephem(earth)
assert (
"A frame must be set up first, please use "
"set_orbit_frame(orbit) or plot(orbit)" in excinfo.exconly()
)
def test_plot_3d_trajectory_plots_a_trajectory():
frame = OrbitPlotter3D()
assert len(frame.trajectories) == 0
trajectory = churi.sample()
frame.set_attractor(Sun)
frame.plot_trajectory(trajectory)
assert len(frame.trajectories) == 1
assert frame._attractor == Sun
def test_plot_2d_trajectory_plots_a_trajectory():
frame = OrbitPlotter2D()
assert len(frame.trajectories) == 0
trajectory = churi.sample()
frame.set_attractor(Sun)
frame.set_orbit_frame(churi)
frame.plot_trajectory(trajectory)
assert len(frame.trajectories) == 1
assert frame._attractor == Sun
def test_set_view():
frame = OrbitPlotter3D()
frame.set_view(0 * u.deg, 0 * u.deg, 1000 * u.m)
figure = frame.show()
eye = figure["layout"]["scene"]["camera"]["eye"]
assert eye["x"] == 1
assert eye["y"] == 0
assert eye["z"] == 0
def test_dark_theme():
frame = OrbitPlotter3D(dark=True)
assert frame._layout.template.layout.plot_bgcolor == "rgb(17,17,17)"
|