Skip to content

Commit

Permalink
Desktop (#6)
Browse files Browse the repository at this point in the history
* clippy cleanup
polished integration test setup

* reduce chatter
  • Loading branch information
grtwje authored Apr 22, 2022
1 parent f19b207 commit 41e4376
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 60 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
on: [push]
on:
pull_request:
branches:
- main
name: build
jobs:
build:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/security-audit.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
name: Security audit
on:
schedule:
- cron: "0 8 * * *"
push:
paths:
- "**/Cargo.*"
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ path = "src/se_ms_api.rs"
chrono = "0.4"
reqwest = { version = "0.11", features = ["json", "blocking", "cookies"] }
serde = { version = "1", features = ["derive"] }

[dev-dependencies]
lazy_static = "1.4"
22 changes: 14 additions & 8 deletions src/se_ms_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
//! let site_id = "my_site_id";
//! let api_key = "my_api_key";
//!
//! let solar_edge = SolaredgeCredentials::new(&site_id, &api_key); // (1)
//! let req = SiteDetailsReq::new(); // (2)
//! let resp = req.send(&solar_edge); // (3)
//! let cred = SolaredgeCredentials::create(&site_id, &api_key); // (1)
//! let req = SiteDetailsReq::new(); // (2)
//! let resp = req.send(&cred); // (3)
//!
//! match resp { // (4)
//! match resp { // (4)
//! Ok(r) => {
//! println!("My site's status is {}.", r.details.status);
//! }
Expand All @@ -37,7 +37,7 @@
//! * [SiteEnergyDetailedReq] / [SiteEnergyDetailedResp]
//!
#![deny(unused_crate_dependencies)]
//#![warn(unused_crate_dependencies)]
#![deny(unused_extern_crates)]
#![warn(missing_docs)]

Expand Down Expand Up @@ -73,7 +73,7 @@ impl SolaredgeCredentials {
const MONITORING_API_URL: &'static str = "https://monitoringapi.solaredge.com/";

/// Create a Solaredge destination for the requests from the given site id and api_key.
pub fn new(site_id: &str, api_key: &str) -> Self {
pub fn create(site_id: &str, api_key: &str) -> Self {
let url_start = SolaredgeCredentials::MONITORING_API_URL.to_string();
let site_id = site_id.to_string();
let url_end = format!("api_key={}", api_key);
Expand All @@ -84,17 +84,23 @@ impl SolaredgeCredentials {
url_end,
}
}

/// See the site ID bing used in the credentials.
pub fn site_id(&self) -> &str {
&self.site_id
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn solaredge_new_unit_test() {
let se = SolaredgeCredentials::new("id", "key");
fn solaredge_credentials_unit_test() {
let se = SolaredgeCredentials::create("id", "key");
assert_eq!(se.url_start, SolaredgeCredentials::MONITORING_API_URL);
assert_eq!(se.site_id, "id");
assert_eq!(se.site_id(), "id");
assert_eq!(se.url_end, "api_key=key");
}
}
50 changes: 24 additions & 26 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
use se_ms_api::SolaredgeCredentials;
use std::env;
use std::fs;
use std::sync::Once;

pub const TIME_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";
pub const TIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S";

const TEST_CREDENTIALS_FILE: &'static str = "test_credentials.txt";
const TEST_CREDENTIALS_FILE: &str = "test_credentials.txt";

static mut SITE_ID: String = String::new();
static mut API_KEY: String = String::new();
static INIT: Once = Once::new();
lazy_static! {
pub static ref TEST_CREDENTIALS: SolaredgeCredentials = {
let mut site_id = String::new();
let mut api_key = String::new();

pub fn get_site_id_and_api_key() -> (&'static str, &'static str) {
unsafe {
INIT.call_once(|| {
let path = env::current_dir().unwrap();
let path = path.join("tests").join(TEST_CREDENTIALS_FILE);
let path = env::current_dir().unwrap();
let path = path.join("tests").join(TEST_CREDENTIALS_FILE);

let contents = fs::read_to_string(path)
.expect(&format!("Unable to read {}.", TEST_CREDENTIALS_FILE));
let mut lines = contents.lines();
if let Some(s) = lines.next() {
SITE_ID = s.to_string();
}
if let Some(s) = lines.next() {
API_KEY = s.to_string();
}
if SITE_ID.len() == 0 || API_KEY.len() == 0 {
panic!("Ill formed credentials file.");
}
});
(&SITE_ID, &API_KEY)
}
let contents = fs::read_to_string(path)
.unwrap_or_else(|_| panic!("Unable to read {}.", TEST_CREDENTIALS_FILE));
let mut lines = contents.lines();
if let Some(s) = lines.next() {
site_id = s.to_string();
}
if let Some(s) = lines.next() {
api_key = s.to_string();
}
if site_id.is_empty() || api_key.is_empty() {
panic!("Ill formed credentials file.");
}

SolaredgeCredentials::create(&site_id, &api_key)
};
}
32 changes: 9 additions & 23 deletions tests/integration_reqs_test.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use chrono::NaiveDateTime;

#[macro_use]
extern crate lazy_static;

mod common;

use se_ms_api::{
CurrentVersionReq, MeterType, SiteDetailsReq, SiteEnergyDetailedReq, SolaredgeCredentials,
SupportedVersionsReq,
CurrentVersionReq, MeterType, SiteDetailsReq, SiteEnergyDetailedReq, SupportedVersionsReq,
};

#[test]
fn site_energy_detailed_integration_test() {
let (site_id, api_key) = common::get_site_id_and_api_key();

let solar_edge = SolaredgeCredentials::new(&site_id, &api_key);

let start_ndt = match NaiveDateTime::parse_from_str("2022-01-01 00:00:00", common::TIME_FORMAT)
{
Ok(dt) => dt,
Expand All @@ -31,7 +29,7 @@ fn site_energy_detailed_integration_test() {
Some(vec![MeterType::SelfConsumption]),
);

let resp = req.send(&solar_edge);
let resp = req.send(&common::TEST_CREDENTIALS);

match resp {
Ok(r) => {
Expand All @@ -56,12 +54,8 @@ fn site_energy_detailed_integration_test() {

#[test]
fn current_version_integration_test() {
let (site_id, api_key) = common::get_site_id_and_api_key();

let solar_edge = SolaredgeCredentials::new(&site_id, &api_key);

let req = CurrentVersionReq::new();
let resp = req.send(&solar_edge);
let resp = req.send(&common::TEST_CREDENTIALS);

match resp {
Ok(r) => {
Expand All @@ -75,12 +69,8 @@ fn current_version_integration_test() {

#[test]
fn supported_versions_integration_test() {
let (site_id, api_key) = common::get_site_id_and_api_key();

let solar_edge = SolaredgeCredentials::new(&site_id, &api_key);

let req = SupportedVersionsReq::new();
let resp = req.send(&solar_edge);
let resp = req.send(&common::TEST_CREDENTIALS);

match resp {
Ok(r) => {
Expand All @@ -94,16 +84,12 @@ fn supported_versions_integration_test() {

#[test]
fn site_details_integration_test() {
let (site_id, api_key) = common::get_site_id_and_api_key();

let solar_edge = SolaredgeCredentials::new(&site_id, &api_key);

let req = SiteDetailsReq::new();
let resp = req.send(&solar_edge);
let resp = req.send(&common::TEST_CREDENTIALS);

match resp {
Ok(r) => {
assert_eq!(r.details.id.to_string(), site_id);
assert_eq!(r.details.id.to_string(), common::TEST_CREDENTIALS.site_id());
assert_eq!(r.details.status, "Active");
assert_eq!(r.details.location.countryCode, "US");
assert_eq!(r.details.primaryModule.manufacturerName, "LG");
Expand Down

0 comments on commit 41e4376

Please sign in to comment.