name: Run Integration Tests

on: [push, pull_request]

jobs:
  integration-test:
    name: Integration Tests
    strategy:
      matrix:
        os: [ubuntu-latest]
        goVer: ['1.19', '1.20']
    # Defines the platform for each test run.
    runs-on: ${{ matrix.os }}
    # define the container services used to run integration tests
    services:
      postgres:
        image: postgres:12
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
          POSTGRES_DB: users
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      mysql:
        image: mysql:8
        env:
          MYSQL_USER: dblab_user
          MYSQL_PASSWORD: dblab_pass
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: dblab_db
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      # Checks out our code locally so we can work with the files.
      - name: Checkout code
        uses: actions/checkout@v3

      # The steps that will be run through for each version and platform combination.
      - name: Set up Go ${{ matrix.goVer }}
        uses: actions/setup-go@v4
        with:
          go-version: ${{ matrix.goVer }}
          cache: true

      # Runs go test ./... against postgres container
      - name: Test Postgres
        run: |
          go run cmd/dbmigrate/main.go migrate up
          go run cmd/seeder/main.go seed
          go test -v ./...
        env:
          DB_USER: postgres
          DB_PASSWORD: password
          DB_HOST: localhost
          DB_PORT: 5432
          DB_NAME: users
          DB_DRIVER: postgres

      # Runs go test ./... against mysql container
      - name: Test MySQL
        run: |
          go run cmd/dbmigrate/main.go migrate up
          go run cmd/seeder/main.go seed
          go test -v ./...
        env:
          DB_USER: dblab_user
          DB_PASSWORD: dblab_pass
          DB_HOST: localhost
          DB_PORT: 3306
          DB_NAME: dblab_db
          DB_DRIVER: mysql
