Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#567 add test suite #573

Merged
merged 7 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: CI
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


on:
pull_request:
push:
branches: [ main ]

jobs:
# lint:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will like to add rubocop to the project later, so just stubbing this out for now

# runs-on: ubuntu-latest
# steps:
# - name: Checkout code
# uses: actions/checkout@v4

# - name: Set up Ruby
# uses: ruby/setup-ruby@v1
# with:
# ruby-version: .ruby-version
# bundler-cache: true

# - name: Lint code for consistent style
# run: bin/rubocop -f github

test:
runs-on: ubuntu-latest

services:
mysql:
image: mariadb:10.11
env:
MARIADB_ROOT_PASSWORD: mysecretpassword
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Install packages
run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable curl libvips libjemalloc2 default-mysql-client default-libmysqlclient-dev

- name: Checkout code
uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
bundler-cache: true

- uses: actions/setup-node@v4
with:
node-version: 16

- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v4
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

- name: Install Node modules
run: yarn install

- name: Run tests
env:
RAILS_ENV: test
DATABASE_URL: mysql2://root:[email protected]:3306
run: bin/rails db:setup test
52 changes: 47 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
tmp/
.DS_Store
Copy link
Collaborator Author

@murny murny May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We weren't gitignoring log/test.log, so instead of just adding this, I brought in the .gitignore from a fresh rails app and refactored this a bit (our "custom" ones are at the bottom with some comments).

log/development.log
config/initializers/comfortable_mexican_sofa.rb
storage/
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore all environment files (except templates).
/.env*
!/.env*.erb

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep

# Ignore storage (uploaded files in development and any SQLite databases).
/storage/*
!/storage/.keep
/tmp/storage/*
!/tmp/storage/
!/tmp/storage/.keep

/public/assets

# Ignore master key for decrypting credentials and more.
/config/master.key

/app/assets/builds/*
!/app/assets/builds/.keep

# Ignore for Build
/vendor/bundle

# Webpacker stuff
/public/packs
/public/packs-test
/node_modules
/yarn-error.log
yarn-debug.log*
.yarn-integrity

# Our custom ignores
.DS_Store
# This is ignored due to Ansible I assume:
config/initializers/comfortable_mexican_sofa.rb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume too.

1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
match '/500', to: 'errors#internal_server_error', via: :all
# Ensure that this route is defined last
comfy_route :cms, path: "/"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
27 changes: 27 additions & 0 deletions test/integration/cms_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require "test_helper"

class CmsTest < ActionDispatch::IntegrationTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done later, but it would be good to cover the features noted in the readme:

  • home page
  • staff (view, new and edit)
  • admin

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that sounds like a good idea, especially Staff as this is custom (not part of the CMS) 👍. So will add a note to add more tests for this here #567

test "homepage" do
get comfy_cms_render_page_path(cms_path: "")

assert_response :success
assert_select "h2", "Search the Library"
end

test "hours and locations page" do
get comfy_cms_render_page_path(cms_path: 'hours-locations')

assert_response :success
assert_select "h1", /Hours & Locations/
Copy link
Collaborator Author

@murny murny May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The H1 tag on these pages has a lot of extra information in it, such as links and other things.

So for example for the "About us" page, you can see in this photo the links on the right is actually inside the <H1>:
image

So using a regex to just extract and assert the actual header content here.

This could probably be improved by moving the extra info outside of the header tag.

end

test "about us page" do
get comfy_cms_render_page_path(cms_path: 'about-us')

assert_response :success
assert_select "h1", /About Us/
assert_select "h2", "A Message from our Chief Librarian"
end
end
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
require_relative '../config/environment'
require 'rails/test_help'

# Seed the database with our CMS seed data, if it's not already there
Copy link
Collaborator Author

@murny murny May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some notes here:

This will seed the test DB with our CMS data BEFORE the tests run, so it's technically outside the transactions that normally happen in the test suite. This is good because, every time we run tests, we don't need to reseed the DB (as this seeding is expensive, and takes a good ~30 seconds as we have many, many pages). It's bad in the case of what happens when the seed data changes? These changes won't be available right away, and will likely require someone to manually drop the test DB. But I assume changing the seed data is a rare occurrence.

I also tried to move this to a rake task but was having issues with Comfy not being available.

Typically, we should be using fixtures here and following Comfy best practice here, which is to have a minimal version of our website for testing:
https://github.com/comfy/comfortable-mexican-sofa/wiki/HowTo:-Minimal-Fixtures-For-Testing

Or we can parse down the amount of CMS pages we have? As it appears, we have a ton of redundant/old pages or multiple versions (sandbox/french) of our CMS.

The less we have, the faster the seeding can take place, which means we could in theory just use fixtures and seed the CMS every time before the tests run, which means we could get the best of both worlds.

if Rake::Task.tasks.empty? && Comfy::Cms::Site.count.zero?
HomeCms::Application.load_tasks

Comfy::Cms::Site.create!(identifier: 'ualberta-libraries', hostname: 'localhost')
Rake::Task['comfy:cms_seeds:import'].invoke('library-cms-seeds', 'ualberta-libraries')
end

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
Expand Down
Loading