Skip to content

Commit

Permalink
Refactor tests + add generator tests (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
jensljungblad committed Dec 12, 2017
1 parent f16c720 commit 335e18b
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -23,6 +23,7 @@ config/initializers/secret_token.rb
/test/dummy/log/*
/test/dummy/tmp/*
/test/dummy/db/*.sqlite3
/test/tmp/*
Gemfile.lock
.tags
gemfiles/.bundle
Expand Down
4 changes: 0 additions & 4 deletions lib/tasks/godmin_tasks.rake

This file was deleted.

2 changes: 1 addition & 1 deletion test/fakes/article.rb
Expand Up @@ -5,7 +5,7 @@ def self.table_name
end

def self.column_names
["id", "title"]
%w[id title]
end
end
end
6 changes: 3 additions & 3 deletions test/fakes/article_service.rb
Expand Up @@ -13,8 +13,8 @@ class ArticleService
scope :published

filter :title
filter :country, as: :select, collection: %w(Sweden Canada)
filter :tags, as: :multiselect, collection: %w(Apple Banana)
filter :country, as: :select, collection: %w[Sweden Canada]
filter :tags, as: :multiselect, collection: %w[Apple Banana]

batch_action :unpublish
batch_action :publish, confirm: true, only: [:unpublished], except: [:published]
Expand All @@ -25,7 +25,7 @@ def initialize(*)
end

def resources_relation
[:foo, :bar, :baz]
%i[foo bar baz]
end

def order_by_foobar(resources, direction)
Expand Down
77 changes: 77 additions & 0 deletions test/generators/resource_generator_test.rb
@@ -0,0 +1,77 @@
require "test_helper"
require "generators/godmin/resource/resource_generator"

module Godmin
class ResourceGeneratorTest < ::Rails::Generators::TestCase
tests ResourceGenerator
destination File.expand_path("../../tmp", __FILE__)
setup :prepare_destination

def test_resource_generator_in_standalone_install
system "cd #{destination_root} && rails new . --skip-test --skip-spring --skip-bundle --skip-git --quiet"
system "cd #{destination_root} && bin/rails generate godmin:install --quiet"
system "cd #{destination_root} && bin/rails generate godmin:resource foo bar --quiet"

assert_file "config/routes.rb", /resources :foos/
assert_file "app/views/shared/_navigation.html.erb", /<%= navbar_item Foo %>/

assert_file "app/controllers/foos_controller.rb" do |content|
expected_content = <<-CONTENT.strip_heredoc
class FoosController < ApplicationController
include Godmin::Resources::ResourceController
end
CONTENT
assert_match expected_content, content
end

assert_file "app/services/foo_service.rb" do |content|
expected_content = <<-CONTENT.strip_heredoc
class FooService
include Godmin::Resources::ResourceService
attrs_for_index :bar
attrs_for_show :bar
attrs_for_form :bar
end
CONTENT
assert_match expected_content, content
end
end

def test_resource_generator_in_engine_install
system "cd #{destination_root} && rails new . --skip-test --skip-spring --skip-bundle --skip-git --quiet"
system "cd #{destination_root} && bin/rails plugin new fakemin --mountable --quiet"
system "cd #{destination_root} && fakemin/bin/rails generate godmin:install --quiet"
system "cd #{destination_root} && fakemin/bin/rails generate godmin:resource foo bar --quiet"

assert_file "fakemin/config/routes.rb", /resources :foos/
assert_file "fakemin/app/views/fakemin/shared/_navigation.html.erb", /<%= navbar_item Foo %>/

assert_file "fakemin/app/controllers/fakemin/foos_controller.rb" do |content|
expected_content = <<-CONTENT.strip_heredoc
module Fakemin
class FoosController < ApplicationController
include Godmin::Resources::ResourceController
end
end
CONTENT
assert_match expected_content, content
end

assert_file "fakemin/app/services/fakemin/foo_service.rb" do |content|
expected_content = <<-CONTENT.strip_heredoc
module Fakemin
class FooService
include Godmin::Resources::ResourceService
attrs_for_index :bar
attrs_for_show :bar
attrs_for_form :bar
end
end
CONTENT
assert_match expected_content, content
end
end
end
end
55 changes: 55 additions & 0 deletions test/unit/authorization/policy_finder_test.rb
@@ -0,0 +1,55 @@
require "test_helper"

module Godmin
module Authorization
class PolicyFinderTest < ActiveSupport::TestCase
class Article; end
class ArticlePolicy; end

def test_find_by_model
klass = Class.new do
extend ActiveModel::Naming

def self.name
"Article"
end
end

policy = PolicyFinder.find(klass, Godmin::Authorization::PolicyFinderTest)
assert_equal ArticlePolicy, policy
end

def test_find_by_class
policy = PolicyFinder.find(Article)
assert_equal ArticlePolicy, policy
end

def test_find_by_symbol
policy = PolicyFinder.find(:article, Godmin::Authorization::PolicyFinderTest)
assert_equal ArticlePolicy, policy
end

def test_override_policy_class_on_class
klass = Class.new do
def self.policy_class
ArticlePolicy
end
end

policy = PolicyFinder.find(klass)
assert_equal ArticlePolicy, policy
end

def test_override_policy_class_on_instance
klass = Class.new do
def policy_class
ArticlePolicy
end
end

policy = PolicyFinder.find(klass.new)
assert_equal ArticlePolicy, policy
end
end
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 335e18b

Please sign in to comment.