Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zonque committed Mar 24, 2024
1 parent b7bc7c5 commit 8af8b3d
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rubyonrails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ jobs:
bundler-cache: true
# Add or replace database setup steps here
- name: Set up database schema
run: bin/rails db:schema:load
run: bundle exec rails db:schema:load
# Add or replace test runners here
- name: Run tests
run: bin/rails test
run: RAILS_ENV=test bundle exec rails test

lint:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"
gem 'simplecov', require: false
gem 'simplecov'
end

group :production do
Expand Down
25 changes: 25 additions & 0 deletions app/helpers/cleanup_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module CleanupHelper
def cleanup
r = Event.where("end_date < ?", DateTime.now-1.day).destroy_all
r.each do |event|
Rails.logger.info "Deleted outdated event #{event.id} (#{event.name}) with #{event.entries.count} entries"
end

r = Entry.where("date < ?", DateTime.now-3.hours).destroy_all
r.each do |entry|
Rails.logger.info "Deleted outdated entry #{entry.id} (#{entry.event.name})"
end

r = Event.unconfirmed.where("created_at < ?", DateTime.now-1.day).destroy_all
r.each do |event|
Rails.logger.info "Deleted unconfirmed event #{event.id} (#{event.name})"
end

r = Entry.unconfirmed.where("created_at < ?", DateTime.now-1.day).destroy_all
r.each do |entry|
Rails.logger.info "Deleted unconfirmed entry #{entry.id} (#{entry.event.name})"
end

AltchaSolution.cleanup
end
end
7 changes: 7 additions & 0 deletions app/models/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Entry < ActiveRecord::Base
validates_numericality_of :seats

validates_presence_of :date
validate :date_in_future

validates_presence_of :location

validates_presence_of :longitude
Expand Down Expand Up @@ -88,4 +90,9 @@ def create_token
self.token = SecureRandom.hex(6)
end

def date_in_future
if self.date and self.date < Time.now
self.errors.add(:date, "should not be in the past")
end
end
end
12 changes: 6 additions & 6 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ def offers_way_back
entries.confirmed.in_future.offer.way_back
end

def end_date_in_future
if self.end_date and self.end_date < Time.now
self.errors.add(:end_date, "should not be in the past")
end
end

before_create :create_admin_token
before_create :create_id

Expand Down Expand Up @@ -88,4 +82,10 @@ def create_id
def create_admin_token
self.admin_token = SecureRandom.hex(8)
end

def end_date_in_future
if self.end_date and self.end_date < Time.now
self.errors.add(:end_date, "should not be in the past")
end
end
end
17 changes: 17 additions & 0 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
#!/usr/bin/env ruby

if ENV["RAILS_ENV"] == "test"
require 'simplecov'

SimpleCov.start :rails do
add_filter "app/admin"
add_filter "app/channels"
add_filter "app/jobs"
add_filter "app/models/concerns/"
add_filter "config"
add_filter "db"
add_filter "lib/tasks"
add_filter "test"
add_filter "vendor"
end
end

APP_PATH = File.expand_path("../config/application", __dir__)
require_relative "../config/boot"
require "rails/commands"
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# this is usually not necessary, and can slow down your test suite. However, it's
# recommended that you enable it in continuous integration systems to ensure eager
# loading is working properly before deploying your code.
config.eager_load = ENV["CI"].present?
config.eager_load = true

# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
Expand Down
40 changes: 39 additions & 1 deletion test/helpers/cleanup_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
require "test_helper"

class CleanupTest < ActionDispatch::IntegrationTest
test "should run cleanup without errors" do
test "should remove outdated events" do
event = events(:one)
event.end_date = DateTime.now-2.days
event.save(validate: false)

ApplicationController.helpers.cleanup

assert_nil Event.find_by(id: event.id)
end

test "should remove outdated entries" do
entry = entries(:rwt1)
entry.date = DateTime.now-4.hours
entry.save(validate: false)

ApplicationController.helpers.cleanup

assert_nil Entry.find_by(id: entry.id)
end

test "should remove unconfirmed events" do
event = events(:one)
event.created_at = DateTime.now-1.days
event.confirmed_at = nil
event.save

ApplicationController.helpers.cleanup

assert_nil Event.find_by(id: event.id)
end

test "should remove unconfirmed entries" do
entry = entries(:rwt1)
entry.created_at = DateTime.now-1.days
entry.confirmed_at = nil
entry.save

ApplicationController.helpers.cleanup

assert_nil Entry.find_by(id: entry.id)
end
end
18 changes: 0 additions & 18 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
require 'simplecov'

# SimpleCov.enable_for_subprocesses true

# SimpleCov.at_fork do |pid|
# SimpleCov.minimum_coverage 0
# SimpleCov.start
# end

SimpleCov.start :rails do
add_filter "/app/admin"
add_filter "/app/channels"
add_filter "/app/jobs"
add_filter "/config"
add_filter "/test"
add_filter "/vendor"
end

ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"

Expand Down

0 comments on commit 8af8b3d

Please sign in to comment.