pvoutput-client 0.0.3

Client for the PVOutput.org API
Documentation
use jiff::civil::Date;
use proptest::property_test;
use rand::{prelude::*, rng};

use crate::{
	Client, ClientError,
	types::{
		error::AddOutputError,
		response::{AggregateDate, Shade},
	},
};

mod parse;

#[test]
#[ignore = "makes a network request"]
fn invalid_credentials() {
	let client = Client::new("invalid", 1, None, None).unwrap();
	let result = tokio::runtime::Builder::new_current_thread()
		.enable_all()
		.build()
		.unwrap()
		.block_on(async { client.get_ladder(None).await });

	assert!(matches!(result, Err(ClientError::InvalidApiKey)));
}

/// WARNING: This test may make a network request.
/// It's not *supposed* to, but if there's a bug in the implementation of [`Client::add_outputs`], it could.
#[test]
fn missing_data() {
	let client = Client::new("invalid", 0, None, None).unwrap();
	let result = tokio::runtime::Builder::new_current_thread()
		.build()
		.unwrap()
		.block_on(async { client.add_outputs(vec![]).await });

	assert!(matches!(result, Err(ClientError::EndpointSpecific(AddOutputError::NoData))));
}

#[test]
fn sort_shade_levels() {
	let sorted = vec![Shade::No, Shade::Low, Shade::Medium, Shade::High];
	let mut levels = sorted.clone();
	levels.shuffle(&mut rng());
	levels.sort();
	assert_eq!(levels, sorted);
}

#[property_test]
#[test]
#[allow(clippy::cast_possible_wrap)]
fn aggregate_date(#[strategy = 0..=9999u16] year: u16, #[strategy = 1..=12u8] month: u8) {
	let date = AggregateDate::Year(year);
	assert_eq!(date.year(), year);
	assert_eq!(date.as_arbitrary_date().unwrap(), Date::new(year as i16, 1, 1).unwrap());

	let date = AggregateDate::YearAndMonth(year, month);
	assert_eq!(date.year(), year);
	assert_eq!(date.as_arbitrary_date().unwrap(), Date::new(year as i16, month as i8, 1).unwrap());
}