Skip to content

Commit

Permalink
Add tests workflow (#71)
Browse files Browse the repository at this point in the history
* Add tests workflow

* Fix build release mode

* Fix docker sample env

* Del docker service

* Add clippy and fmt, create graph, format

* Fix working directory docker graph

* Fix migration script not found

* Reorg steps

* Fix split workflows and use debug mode

* Update pull request template checklist

* Redirect service output to log

* Fix change default log level

* Bump neo4j to pubky-docker version parity

* Fix docker down

* Fix cargo cache

* Rename workflows

* Fix workflow concurrency

* Test concurrency stop
  • Loading branch information
SHAcollision authored Aug 18, 2024
1 parent bd0e4a2 commit 5613a46
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .env-sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Logging
RUST_LOG=debug
RUST_LOG=info

# Reindex on start up. Will create all Redis index keys from the Neo4J graph
REINDEX=false
Expand Down
1 change: 0 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

> For tests to work you need a working neo4j instance with the example dataset in `docker/db-migration`
- [ ] **Code Quality**: Clippy has been run with no warnings, `cargo clippy`
- [ ] **Testing**: Implement and pass new tests for all new code, while maintaining existing test suite, `cargo test`.
- [ ] **Performance**: Ensure new code has relevant performance benchmarks, `cargo bench`
33 changes: 33 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Format

on:
workflow_dispatch:
workflow_call:
push:
branches: ["main"]
pull_request:
branches:
- main

jobs:
format:
runs-on: ubuntu-latest
timeout-minutes: 4
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: x86_64-unknown-linux-gnu
profile: minimal
components: rustfmt
override: true

- name: Run cargo fmt
run: cargo fmt -- --check # Fail if code is not formatted
49 changes: 49 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Lint

on:
workflow_dispatch:
workflow_call:
push:
branches: ["main"]
pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 10
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: x86_64-unknown-linux-gnu
profile: minimal
components: clippy
override: true

- name: Cache Cargo registry
uses: actions/cache@v3
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-registry-
- name: Cache Cargo clippy
uses: actions/cache@v3
with:
path: target
key: ${{ runner.os }}-cargo-clippy-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-clippy-
- name: Run clippy
run: cargo clippy -- -D warnings # Fail on clippy warnings
84 changes: 84 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Tests

on:
workflow_dispatch:
workflow_call:
push:
branches: ["main"]
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: x86_64-unknown-linux-gnu
profile: minimal
override: true

- name: Cache Cargo registry
uses: actions/cache@v3
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-registry-
- name: Cache Cargo build
uses: actions/cache@v3
with:
path: target
key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-build-
- name: Copy environment variables
run: cp .env-sample .env

- name: Set up Docker Compose
working-directory: docker
run: docker compose --env-file .env-sample up -d

- name: Wait for services to be ready
run: |
until nc -zv 127.0.0.1 6379; do
echo "Waiting for Redis to be ready..."
sleep 1
done
until curl -sS http://localhost:7474; do
echo "Waiting for Neo4j to be ready..."
sleep 1
done
- name: Create example graph
run: docker exec neo4j bash /db-migration/run-queries.sh

# - name: Install dependencies
# run: cargo build --release

- name: Run the service and redirect output to a log file
run: cargo run > service.log 2>&1 &

- name: Run integration tests
run: cargo test

- name: Show service logs if tests fail
if: failure()
run: cat service.log

- name: Tear down Docker Compose
if: always()
working-directory: docker
run: docker compose down --volumes
8 changes: 3 additions & 5 deletions benches/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ fn bench_stream_posts_total_engagement(c: &mut Criterion) {
});
}


fn bench_stream_most_followed(c: &mut Criterion) {
println!("***************************************");
println!("Benchmarking the user streams for most followed users.");
Expand All @@ -131,11 +130,10 @@ fn bench_stream_most_followed(c: &mut Criterion) {
.await
.unwrap();
criterion::black_box(user_stream);
});
},
);
});
});
}

fn bench_stream_user_posts(c: &mut Criterion) {
println!("***************************************");
println!("Benchmarking the post streams for a specific user.");
Expand Down
5 changes: 4 additions & 1 deletion benches/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ fn bench_get_post_tags(c: &mut Criterion) {
let rt = Runtime::new().unwrap();

c.bench_with_input(
BenchmarkId::new("bench_get_post_tags", format!("user_id: {}, post_id: {}", user_id, post_id)),
BenchmarkId::new(
"bench_get_post_tags",
format!("user_id: {}, post_id: {}", user_id, post_id),
),
&[user_id, post_id],
|b, &params| {
b.to_async(&rt).iter(|| async {
Expand Down
4 changes: 1 addition & 3 deletions benches/user.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use pubky_nexus::models::traits::Collection;
use pubky_nexus::models::user::{
Relationship, UserCounts, UserDetails, UserView,
};
use pubky_nexus::models::user::{Relationship, UserCounts, UserDetails, UserView};
use pubky_nexus::setup;
use pubky_nexus::Config;
use std::env;
Expand Down
4 changes: 0 additions & 4 deletions docker/.env-sample
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Pubky-social-app metadata
VERSION_TAG=0.1.0
INTERNAL_NETWORK_NAME=pubky-social-net

# Neo4J free does not support custom db_name and db_username
# the default ones are neo4j
NEO4J_DB_NAME=neo4j
Expand Down
22 changes: 19 additions & 3 deletions docker/db-migration/run-queries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
# Path to the queries file
QUERIES_FILE="/db-migration/indexer.cypher"

# Read queries from the file and execute each one
# time it
time cypher-shell -u neo4j -p 12345678 -f "$QUERIES_FILE"
echo "Starting Cypher query execution..."

# Check if the queries file exists
if [[ ! -f "$QUERIES_FILE" ]]; then
echo "Error: Queries file not found."
exit 1
fi

# Execute queries and time the execution
time cypher-shell -u neo4j -p 12345678 -f "$QUERIES_FILE"

if [[ $? -eq 0 ]]; then
echo "Queries executed successfully."
else
echo "Error: Query execution failed."
exit 1
fi

echo "Cypher query execution completed."
6 changes: 3 additions & 3 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
neo4j:
image: neo4j:5.21.0
image: neo4j:5.21.2-community
container_name: neo4j
restart: unless-stopped
ports:
Expand All @@ -12,8 +12,7 @@ services:
# Mount the data to container
- .database/neo4j/data:/data
- .database/neo4j/logs:/logs
# In case we want to import a database, add this volume. Once imported, comment
# - ./db-migration:/db-migration
- ./db-migration:/db-migration
environment:
NEO4J_initial_dbms_default__database: ${NEO4J_DB_NAME}
# IMPORTANT: If you change the auth params and you have already created the config files, will not take effect
Expand All @@ -27,6 +26,7 @@ services:
NEO4J_server_memory_heap_max__size: 2G
# NEO4J_apoc_uuid_enabled: false


redis:
image: redis/redis-stack:7.2.0-v11
container_name: redis
Expand Down
2 changes: 1 addition & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod info;
pub mod post;
pub mod tag;
pub mod traits;
pub mod user;
pub mod traits;
8 changes: 3 additions & 5 deletions src/models/user/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::models::traits::Collection;
use crate::{queries, RedisOps};
use axum::async_trait;
use neo4rs::Query;
use serde::{Deserialize, Serialize, Deserializer};
use utoipa::ToSchema;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json;
use utoipa::ToSchema;

/// Represents a user's single link with a title and URL.
#[derive(Serialize, Deserialize, ToSchema, Default, Clone, Debug)]
Expand Down Expand Up @@ -90,9 +90,7 @@ mod tests {
let config = Config::from_env();
setup(&config).await;

let user_details = UserDetails::get_by_ids(&USER_IDS)
.await
.unwrap();
let user_details = UserDetails::get_by_ids(&USER_IDS).await.unwrap();
assert_eq!(user_details.len(), USER_IDS.len());

for details in user_details[0..3].iter() {
Expand Down
2 changes: 1 addition & 1 deletion src/models/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tags;
mod view;

pub use counts::UserCounts;
pub use details::{UserLink, UserDetails};
pub use details::{UserDetails, UserLink};
pub use follows::{Followers, Following, Friends, UserFollows};
pub use relationship::Relationship;
pub use stream::{UserStream, UserStreamType};
Expand Down
2 changes: 1 addition & 1 deletion src/models/user/stream.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::error::Error;

use super::{Followers, Following, Friends, UserFollows, UserCounts, UserView};
use super::{Followers, Following, Friends, UserCounts, UserFollows, UserView};
use crate::{db::kv::index::sorted_sets::Sorting, RedisOps};
use serde::{Deserialize, Serialize};
use tokio::task::spawn;
Expand Down

0 comments on commit 5613a46

Please sign in to comment.