Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add basic github actions PR checks #14

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Test PR

on:
pull_request

jobs:
test:
name: Run Tests
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Rust
run: |
rustup update stable
rustup default stable
rustup component add rustfmt clippy

- name: Print Rust version
run: rustc --version

- name: Build
run: cargo build --verbose

- name: Unit test
run: cargo test --verbose

- name: E2E test
run: cargo test --features e2e-test --no-default-features

- name: Check formatting
run: cargo fmt -- --check

- name: Clippy
run: cargo clippy -- -D warnings
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.iml
.idea/
Cargo.lock
.env
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ serde = { version = "1.0.125", features = ["derive"] }
chrono = {version = "0.4.19", features = ["serde"] }
serde_json = "1.0.64"
semver = "1.0.24"

[dev-dependencies]
dotenv = "0.15.0"
ctor = "0.1.26"

[features]
e2e-test = []
43 changes: 39 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use chrono::NaiveDateTime;
use reqwest::blocking::Client as HttpClient;
use reqwest::header::CONTENT_TYPE;
use semver::Version;
use serde::Serialize;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::time::Duration;
use semver::Version;

extern crate serde_json;

Expand Down Expand Up @@ -173,19 +173,54 @@ impl Event {
}
}

#[cfg(test)]
mod test_setup {
use ctor::ctor;
use dotenv::dotenv;

#[ctor]
fn load_dotenv() {
dotenv().ok(); // Load the .env file
println!("Loaded .env for tests");
}
}

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

// see https://us.posthog.com/project/115809/ for the e2e project

#[test]
fn inner_event_adds_lib_properties_correctly() {
// Arrange
let mut event = Event::new("unit test event", "1234");
event.insert_prop("key1", "value1").unwrap();
let api_key = "test_api_key".to_string();

// Act
let inner_event = InnerEvent::new(event, api_key);

// Assert
let props = &inner_event.properties.props;
assert_eq!(
props.get("$lib_name"),
Some(&serde_json::Value::String("posthog-rs".to_string()))
);
}

#[cfg(feature = "e2e-test")]
#[test]
fn get_client() {
let client = crate::client(env!("POSTHOG_API_KEY"));
use std::collections::HashMap;

let api_key = std::env::var("POSTHOG_RS_E2E_TEST_API_KEY").unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an .env.example file to make it clear that we need this to run tests locally? You could also add a section to the README.md explaining that one needs to grab an API key from their instance to be able to run tests

let client = crate::client(api_key.as_str());

let mut child_map = HashMap::new();
child_map.insert("child_key1", "child_value1");

let mut event = Event::new("test", "1234");
let mut event = Event::new("e2e test event", "1234");
event.insert_prop("key1", "value1").unwrap();
event.insert_prop("key2", vec!["a", "b"]).unwrap();
event.insert_prop("key3", child_map).unwrap();
Expand Down
Loading