Path: blob/master/src/python/cocalc-api/tests/README.md
5581 views
CoCalc API Tests
This directory contains pytest tests for the cocalc-api Python package.
Prerequisites
Required: Set the
COCALC_API_KEYenvironment variable with a valid CoCalc API key (tests will fail if not set)Recommended: Set
PGHOSTfor database cleanup (see Automatic Cleanup below)Optionally set
COCALC_HOSTto specify the CoCalc server URL (defaults tohttp://localhost:5000)
Running Tests
Test Structure
conftest.py- Pytest configuration and fixtures (includes resource tracking and cleanup)test_hub.py- Tests for Hub client functionality (projects, database queries, messages)test_project.py- Tests for Project client functionality (ping, exec commands, Jupyter kernel management)test_jupyter.py- Tests for Jupyter kernel installation and code executiontest_org.py- Tests for organization management (create, users, licenses)test_org_basic.py- Basic organization API tests
Jupyter Tests and CI Environments
The Jupyter-related tests are skipped in CI environments (e.g., GitHub Actions) due to resource constraints:
test_jupyter.py: All 12 Jupyter tests are skipped whenCI=truetest_project.py::TestProjectSystem::test_stop_jupyter_kernel: Skipped whenCI=true
This is because Jupyter kernel operations are resource-intensive and can fail in containerized CI environments. The tests run successfully in local development environments.
Test counts:
Local development (CI not set): 64 tests run
GitHub Actions CI (CI=true): 52 tests run (12 Jupyter tests skipped)
Environment Variables
Required
COCALC_API_KEY- Your CoCalc API key
Optional
COCALC_HOST- CoCalc server URL (default:http://localhost:5000)COCALC_TESTS_CLEANUP- Enable/disable automatic cleanup (default:true)CI- Set totrueto skip Jupyter tests in CI environments (e.g., GitHub Actions). When not set, all tests run locally.
For Database Cleanup (Recommended)
PGHOST- PostgreSQL host (socket path or hostname)PGUSER- PostgreSQL user (default:smc)PGDATABASE- PostgreSQL database (default:smc)PGPORT- PostgreSQL port for network connections (default:5432)PGPASSWORD- PostgreSQL password (only needed for network connections)
Resource Tracking and Cleanup
Tracked Resource System
The test suite uses a resource tracking system to automatically manage all created resources. When writing tests, use the provided helper functions to ensure proper cleanup:
Available Helper Functions:
create_tracked_project(hub, resource_tracker, **kwargs)- Create and track a projectcreate_tracked_user(hub, resource_tracker, org_name, **kwargs)- Create and track a user accountcreate_tracked_org(hub, resource_tracker, org_name)- Create and track an organization
All tracked resources are automatically cleaned up at the end of the test session.
Shared Fixtures
Session-scoped fixtures (created once, shared across all tests):
temporary_project- A single test project used by all tests in the sessionproject_client- A Project client instance connected to the temporary projecthub- A Hub client instance
These fixtures ensure efficient resource usage by reusing the same project across all tests.
Automatic Cleanup
At the end of each test session, the cleanup system automatically:
Stops all tracked projects (via API to gracefully shut them down)
Hard-deletes all tracked resources from the PostgreSQL database in order:
Projects (removed first)
Accounts (including all owned projects like "My First Project")
Organizations (removed last)
Cleanup Configuration
Socket Connection (Local Development - Recommended):
Network Connection:
Disable Cleanup (Not Recommended):
When cleanup is disabled, test resources will remain in the database and must be manually removed.
Why Direct Database Cleanup?
The test suite uses direct PostgreSQL deletion instead of API calls because:
API deletion only sets
deleted=true(soft delete), leaving data in the databaseTests create many resources (projects, accounts, orgs) that need complete removal
Direct SQL ensures thorough cleanup including auto-created projects (e.g., "My First Project")
Prevents database bloat from repeated test runs
The cleanup process is safe and only removes resources that were explicitly tracked during test execution.