Skip to content

Commit

Permalink
feat(ops): add move-tests github action and improve test coverage (#310)
Browse files Browse the repository at this point in the history
- new github action: downloads the sui binary, runs the tests and also
checks if there is enough test coverage.
- new tests in the contract: increases test coverage in order to reach
at least 80%.
  • Loading branch information
Tzal3x authored Dec 5, 2024
1 parent a330e8a commit 4d1e19e
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
45 changes: 45 additions & 0 deletions .github/workflows/move-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Run Move tests

on:
push:
branches:
- main
paths:
- move/**
pull_request:
paths:
- move/**

jobs:
run-tests:
name: Run Tests
runs-on: ubuntu-latest

defaults:
run:
working-directory: move/walrus_site

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

- name: Download Sui
run: |
VERSION=$(curl -s https://api.github.com/repos/MystenLabs/sui/releases/latest | jq -r '.tag_name')
curl -L "https://github.com/MystenLabs/sui/releases/download/$VERSION/sui-$VERSION-ubuntu-x86_64.tgz" -o sui.tgz
sudo tar -xvzf sui.tgz -C /usr/local/bin
# Run Tests with Coverage
- name: Run tests
run: sui-debug move test --coverage

- name: Check Move Coverage
run: |
COVERAGE_OUTPUT=$(sui-debug move coverage summary)
echo "$COVERAGE_OUTPUT"
COVERAGE_PERCENT=$(echo "$COVERAGE_OUTPUT" | grep "% Move Coverage:" | sed -E 's/.*: ([0-9]+(\.[0-9]+)?).*/\1/')
if (( $(echo "$COVERAGE_PERCENT < 80" | bc -l) )); then
echo "Coverage is below 80%. Build failed."
exit 1
fi
echo "Coverage is $COVERAGE_PERCENT%. Build passed."
104 changes: 103 additions & 1 deletion move/walrus_site/tests/site_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
module walrus_site::site_tests {
use walrus_site::site::{
ERangeStartGreaterThanRangeEnd,
EStartAndEndRangeAreNone
EStartAndEndRangeAreNone,
Site, Range
};

#[test]
#[expected_failure(abort_code = EStartAndEndRangeAreNone)]
fun test_new_range_no_bounds_defined() {
Expand Down Expand Up @@ -48,4 +50,104 @@ module walrus_site::site_tests {
option::some(1)
);
}

/// This test runs a typical process
/// checking many of the contract's functions.
#[test]
fun test_site_flow_with_resources_and_routes() {
use sui::test_scenario;
let owner = @0xCAFE;
let mut scenario = test_scenario::begin(owner);
// Create a site.
{
let site = walrus_site::site::new_site(
b"Example".to_string(), scenario.ctx()
);
transfer::public_transfer(site, owner)
};

// Rename site and add a resource with headers to the site.
scenario.next_tx(owner);
{
let mut site = scenario.take_from_sender<Site>();
// Update the site name.
walrus_site::site::update_name(&mut site, b"Fancy Example".to_string());
// Create a resource.
let mut resource = walrus_site::site::new_resource(
b"index.html".to_string(),
601749199,
124794210,
option::none<Range>()
);
// Add a header to the resource.
walrus_site::site::add_header(
&mut resource,
b"Content-Type".to_string(),
b"text/html; charset=utf-8".to_string(),
);
// Add the resource to the site.
walrus_site::site::add_resource(&mut site, resource);
// Move the resource to a different path.
walrus_site::site::move_resource(
&mut site,
b"index.html".to_string(),
b"styles.css".to_string(),
);
// Delete the resource.
walrus_site::site::remove_resource(
&mut site, b"styles.css".to_string()
);
scenario.return_to_sender<Site>(site);
};

// Create a route, add it to the site.
scenario.next_tx(owner);
{
let mut site = scenario.take_from_sender<Site>();
// Create the routes DF.
walrus_site::site::create_routes(&mut site);

// Create a resource and add it to the site.
// This is needed for the insert_route to work,
// since objects from previous transactions are lost.
let resource = walrus_site::site::new_resource(
b"index.html".to_string(),
601749199,
124794210,
option::none<Range>()
);
walrus_site::site::add_resource(&mut site, resource);

// Add some rerouting pairs.
walrus_site::site::insert_route(
&mut site,
b"/path1".to_string(),
b"index.html".to_string()
);
walrus_site::site::insert_route(
&mut site,
b"/path2".to_string(),
b"index.html".to_string()
);
// Delete the last route.
walrus_site::site::remove_route(
&mut site,
&b"/path2".to_string(),
);

// Remove all routes.
walrus_site::site::remove_all_routes_if_exist(&mut site);
scenario.return_to_sender<Site>(site);
};

// Burn the site.
scenario.next_tx(owner);
{
let site = scenario.take_from_sender<Site>();
walrus_site::site::burn(site);
};
scenario.end();
}


}

0 comments on commit 4d1e19e

Please sign in to comment.