From a669e60735abda4584d516bac34511f705b1e1e7 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Thu, 16 Apr 2026 14:08:03 -0500 Subject: [PATCH] test: expand coverage on ionosphere + space_weather contexts 6 additional characterization tests for empty-list upsert paths, station filter isolation on latest_observation/1, multi-band xray coexistence, and newest-across-time/bands latest_* helpers. --- .../ionosphere/ionosphere_test.exs | 22 ++++++ .../space_weather/space_weather_test.exs | 71 +++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/test/microwaveprop/ionosphere/ionosphere_test.exs b/test/microwaveprop/ionosphere/ionosphere_test.exs index 599d3e3f..8e941a66 100644 --- a/test/microwaveprop/ionosphere/ionosphere_test.exs +++ b/test/microwaveprop/ionosphere/ionosphere_test.exs @@ -79,6 +79,28 @@ defmodule Microwaveprop.IonosphereTest do test "returns nil when no observations exist for the station" do assert Ionosphere.latest_observation("MHJ45") == nil end + + test "filters by station_code (another station's newer row does not leak through)" do + mhj_old = + @valid_attrs + |> Map.delete(:station_code) + |> Map.put(:valid_time, ~U[2026-04-15 18:00:00Z]) + |> Map.put(:fo_es_mhz, 4.0) + + al_newer = + @valid_attrs + |> Map.delete(:station_code) + |> Map.put(:valid_time, ~U[2026-04-15 23:00:00Z]) + |> Map.put(:fo_es_mhz, 11.0) + + {:ok, _} = Ionosphere.upsert_observations("MHJ45", [mhj_old]) + {:ok, _} = Ionosphere.upsert_observations("AL945", [al_newer]) + + row = Ionosphere.latest_observation("MHJ45") + assert row.station_code == "MHJ45" + assert row.valid_time == ~U[2026-04-15 18:00:00Z] + assert row.fo_es_mhz == 4.0 + end end describe "nearest_foes/3" do diff --git a/test/microwaveprop/space_weather/space_weather_test.exs b/test/microwaveprop/space_weather/space_weather_test.exs index 2faf4fa1..56b85795 100644 --- a/test/microwaveprop/space_weather/space_weather_test.exs +++ b/test/microwaveprop/space_weather/space_weather_test.exs @@ -54,6 +54,11 @@ defmodule Microwaveprop.SpaceWeatherTest do assert [stored] = Repo.all(SolarFluxObservation) assert stored.flux_sfu == 120.0 end + + test "empty list is a no-op returning {:ok, 0}" do + assert {:ok, 0} = SpaceWeather.upsert_solar_flux([]) + assert Repo.aggregate(SolarFluxObservation, :count, :id) == 0 + end end describe "upsert_xray/1" do @@ -79,6 +84,31 @@ defmodule Microwaveprop.SpaceWeatherTest do [stored] = Repo.all(SolarXrayObservation) assert stored.flux_wm2 == 4.0e-7 end + + test "rows with same valid_time but different energy_band coexist" do + rows = [ + %{ + valid_time: ~U[2026-04-14 19:49:00Z], + satellite: 18, + flux_wm2: 3.5e-7, + energy_band: "0.1-0.8nm" + }, + %{ + valid_time: ~U[2026-04-14 19:49:00Z], + satellite: 18, + flux_wm2: 1.2e-8, + energy_band: "0.05-0.4nm" + } + ] + + assert {:ok, 2} = SpaceWeather.upsert_xray(rows) + assert Repo.aggregate(SolarXrayObservation, :count, :id) == 2 + end + + test "empty list is a no-op returning {:ok, 0}" do + assert {:ok, 0} = SpaceWeather.upsert_xray([]) + assert Repo.aggregate(SolarXrayObservation, :count, :id) == 0 + end end describe "latest_kp/0" do @@ -120,5 +150,46 @@ defmodule Microwaveprop.SpaceWeatherTest do assert %SolarFluxObservation{flux_sfu: 112.0} = SpaceWeather.latest_f107() assert %SolarXrayObservation{flux_wm2: 1.0e-7} = SpaceWeather.latest_xray() end + + test "latest_f107/0 picks newest across multiple observations" do + {:ok, _} = + SpaceWeather.upsert_solar_flux([ + %{valid_time: ~U[2026-04-14 20:00:00Z], frequency_mhz: 2800, flux_sfu: 100.0}, + %{valid_time: ~U[2026-04-15 20:00:00Z], frequency_mhz: 2800, flux_sfu: 115.0}, + %{valid_time: ~U[2026-04-13 20:00:00Z], frequency_mhz: 2800, flux_sfu: 90.0} + ]) + + row = SpaceWeather.latest_f107() + assert row.valid_time == ~U[2026-04-15 20:00:00Z] + assert row.flux_sfu == 115.0 + end + + test "latest_xray/0 picks newest across multiple energy bands" do + {:ok, _} = + SpaceWeather.upsert_xray([ + %{ + valid_time: ~U[2026-04-15 17:00:00Z], + satellite: 18, + flux_wm2: 1.0e-7, + energy_band: "0.1-0.8nm" + }, + %{ + valid_time: ~U[2026-04-15 18:00:00Z], + satellite: 18, + flux_wm2: 2.0e-7, + energy_band: "0.05-0.4nm" + }, + %{ + valid_time: ~U[2026-04-15 17:30:00Z], + satellite: 18, + flux_wm2: 3.0e-7, + energy_band: "0.1-0.8nm" + } + ]) + + row = SpaceWeather.latest_xray() + assert row.valid_time == ~U[2026-04-15 18:00:00Z] + assert row.flux_wm2 == 2.0e-7 + end end end