Skip to content
Snippets Groups Projects
test.nu 3.59 KiB
Newer Older
Elliott Shugerman's avatar
Elliott Shugerman committed
# run-tests --threads=0

Elliott Shugerman's avatar
Elliott Shugerman committed
use assert
Elliott Shugerman's avatar
Elliott Shugerman committed
use std log
Elliott Shugerman's avatar
Elliott Shugerman committed

# def docker-compose-ps [] {
#     docker compose ps --format=json | split row "\n" | each { |l| $l | from json }
# }

# def docker-compose-all-up [] {
#     let results = docker-compose-ps
#     (($results | all { |row| $row.Status | str starts-with 'Up' }) and
#      (($results | length) > 0))
# }

const DEFAULT_DATABASE = 'postgres'
Elliott Shugerman's avatar
Elliott Shugerman committed
const base_env = {
    POSTGRES_CONTAINER_NAME: "postgres",
    BACKUP_SERVICE_CONTAINER_NAME: "backup-service",
    POSTGRES_USER: "postgres",
    POSTGRES_PASSWORD: "secret",
    SEED_DATABASE: "pagila",
}


def exec-sql [--database: string ] {
    (
        docker exec -i $env.POSTGRES_CONTAINER_NAME psql
            --csv
            --echo-errors
            --variable ON_ERROR_STOP=1
Elliott Shugerman's avatar
Elliott Shugerman committed
            --username $env.POSTGRES_USER
            --dbname (if $database != null { $database } else { $env.SEED_DATABASE })
    ) | from csv
}

Elliott Shugerman's avatar
Elliott Shugerman committed
def create-services [] {
    log info "Creating services"
    docker compose --progress=plain up --build --detach
}
def delete-services [] {
    log info "Deleting services"
    docker compose --progress=plain down
}

def create-test-db [] {
    log info "Creating empty test database"
    $'CREATE DATABASE ($env.SEED_DATABASE);' | exec-sql --database $DEFAULT_DATABASE
}
Elliott Shugerman's avatar
Elliott Shugerman committed
def drop-test-db [] {
    log info "Dropping test database"
    $'DROP DATABASE IF EXISTS ($env.SEED_DATABASE);' | exec-sql --database $DEFAULT_DATABASE
Elliott Shugerman's avatar
Elliott Shugerman committed
def populate-test-db [] {
    log info "Populating test database"
    open ./seed-data/pagila/pagila-schema.sql | exec-sql
    open ./seed-data/pagila/pagila-data.sql   | exec-sql
}

def backup [] {
Elliott Shugerman's avatar
Elliott Shugerman committed
    log info "Running backup"
    docker compose exec backup-service sh backup.sh
Elliott Shugerman's avatar
Elliott Shugerman committed
}

def restore [] {
Elliott Shugerman's avatar
Elliott Shugerman committed
    log info "Running restore"
Elliott Shugerman's avatar
Elliott Shugerman committed
    docker compose exec backup-service sh restore.sh
Elliott Shugerman's avatar
Elliott Shugerman committed
def assert-test-db-populated [] {
    let rows = 'SELECT count(1) FROM public.customer;' | exec-sql
    assert (not ($rows | is-empty)) 'Not populated: failed to select from table'
    assert ($rows.count.0 > 0) 'Not populated: table is empty'
}

def assert-test-db-dne [] {
Elliott Shugerman's avatar
Elliott Shugerman committed
    # TODO: make this do what the name says more directly
Elliott Shugerman's avatar
Elliott Shugerman committed
    let rows = 'SELECT count(1) FROM public.customer;' | exec-sql
    assert (($rows | is-empty) or ($rows.count.0 == 0))
}
Elliott Shugerman's avatar
Elliott Shugerman committed

const version_pairs = [
    { POSTGRES_VERSION: '11', ALPINE_VERSION: '3.10' },
    { POSTGRES_VERSION: '12', ALPINE_VERSION: '3.12' },
    { POSTGRES_VERSION: '13', ALPINE_VERSION: '3.14' },
    { POSTGRES_VERSION: '14', ALPINE_VERSION: '3.16' },
    { POSTGRES_VERSION: '15', ALPINE_VERSION: '3.17' },
    { POSTGRES_VERSION: '16', ALPINE_VERSION: '3.19' },
]

def full-test [postgres_version: string alpine_version: string] {
    let full_env = $base_env | merge { POSTGRES_VERSION: $postgres_version, ALPINE_VERSION: $alpine_version };
    with-env $full_env {
        timeit {
            delete-services
            create-services
            create-test-db
            populate-test-db
            assert-test-db-populated
            backup
            drop-test-db
            assert-test-db-dne
            create-test-db # restore needs it to already exist
            restore
            assert-test-db-populated # asserts there's actually data in the table
            delete-services
        }
Elliott Shugerman's avatar
Elliott Shugerman committed
    }
Elliott Shugerman's avatar
Elliott Shugerman committed

#[test]
def test-pg-11 [] {
    full-test '11' '3.10'
}

#[test]
def test-pg-12 [] {
    full-test '12' '3.12'
}

#[test]
def test-pg-13 [] {
    full-test '13' '3.14'
}

#[test]
def test-pg-14 [] {
    full-test '14' '3.16'
}

#[test]
def test-pg-15 [] {
full-test '15' '3.17' # TODO: try 18
}

#[test]
def test-pg-16 [] {
    full-test '16' '3.19'
}