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

Added tests for generators and option --parent #385

Merged
merged 5 commits into from Sep 21, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/rails/generators/mongo_mapper/config/config_generator.rb
Expand Up @@ -3,22 +3,35 @@
module MongoMapper
module Generators
class ConfigGenerator < Rails::Generators::Base
desc "creates the MongoMapper configuration at config/mongo.yml"
desc 'creates the MongoMapper configuration at config/mongo.yml'

argument :database_name, :type => :string, :optional => true

def self.source_root
@source_root ||= File.expand_path("../templates", __FILE__)
end

def app_name
Rails::Application.subclasses.first.parent.to_s.underscore
@source_root ||= File.expand_path('../templates', __FILE__)
end

def create_config_file
template 'mongo.yml', File.join('config', "mongo.yml")
template 'mongo.yml', File.join('config', 'mongo.yml')
end

protected

def app_name
@app_name ||= defined_app_const_base? ? defined_app_name : File.basename(destination_root)
end

def defined_app_name
defined_app_const_base.underscore
end

def defined_app_const_base
Rails.respond_to?(:application) && defined?(Rails::Application) &&
Rails.application.is_a?(Rails::Application) && Rails.application.class.name.sub(/::Application$/, '')
end

alias :defined_app_const_base? :defined_app_const_base

end
end
end
19 changes: 18 additions & 1 deletion lib/rails/generators/mongo_mapper/model/model_generator.rb
Expand Up @@ -4,11 +4,15 @@ module MongoMapper
module Generators
class ModelGenerator < Rails::Generators::NamedBase
desc 'Creates a mongomapper model'

argument :name, :type => :string
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
class_option :timestamps, :type => :boolean

check_class_collision

class_option :timestamps, :type => :boolean
class_option :parent, :type => :string, :desc => "The parent class for the generated model"

def self.source_root
@source_root ||= File.expand_path("../templates", __FILE__)
end
Expand All @@ -18,6 +22,19 @@ def create_model_file
end

hook_for :test_framework

protected

def parent_class_name
options[:parent]
end

# Rails 3.0.X compatibility
unless methods.include?(:module_namespacing)
def module_namespacing(&block)
yield if block
end
end
end
end
end
14 changes: 9 additions & 5 deletions lib/rails/generators/mongo_mapper/model/templates/model.rb
@@ -1,13 +1,17 @@
<% module_namespacing do -%>
<%- module_namespacing do -%>
<%- if parent_class_name.present? -%>
class <%= class_name %> < <%= parent_class_name.classify %>
<%- else -%>
class <%= class_name %>
include MongoMapper::Document
<%- end -%>

<% attributes.each do |attribute| -%>
<%- attributes.each do |attribute| -%>
key :<%= attribute.name %>, <%= attribute.type.to_s.camelcase %>
<% end -%>
<% if options[:timestamps] %>
<%- if options[:timestamps] -%>
timestamps!
<% end -%>
<%- end -%>

end
<% end -%>
<%- end -%>
23 changes: 23 additions & 0 deletions test/test_generators_helper.rb
@@ -0,0 +1,23 @@
require 'rails/generators'
require 'rails/generators/test_case'

require 'rails/generators/mongo_mapper/config/config_generator'
require 'rails/generators/mongo_mapper/model/model_generator'

module TestGeneratorsHelper
def self.included(base)
base.class_eval do
destination File.expand_path('../tmp', File.dirname(__FILE__))

setup :prepare_destination
teardown :cleanup_destination_root

base.tests MongoMapper::Generators.const_get(base.name.sub(/Test$/, ''))
end
end

def cleanup_destination_root
rm_rf(destination_root)
end

end
14 changes: 14 additions & 0 deletions test/unit/test_config_generator.rb
@@ -0,0 +1,14 @@
class ConfigGeneratorTest < Rails::Generators::TestCase
include TestGeneratorsHelper

test 'mongo.yml are properly created' do
run_generator
assert_file 'config/mongo.yml', /#{File.basename(destination_root)}/
end

test 'mongo.yml are properly created with defined database_name' do
run_generator %w{dummy}
assert_file 'config/mongo.yml', /dummy/
end

end
35 changes: 35 additions & 0 deletions test/unit/test_model_generator.rb
@@ -0,0 +1,35 @@
class ModelGeneratorTest < Rails::Generators::TestCase
include TestGeneratorsHelper

test 'help shows MongoMapper options' do
content = run_generator ['--help']
assert_match(/rails generate mongo_mapper:model/, content)
end

test 'model are properly created' do
run_generator ['Color']
assert_file 'app/models/color.rb', /class Color/
assert_file 'app/models/color.rb', /include MongoMapper::Document/
end

test 'model are properly created with attributes' do
run_generator ['Color', 'name:string', 'saturation:integer']
assert_file 'app/models/color.rb', /class Color/
assert_file 'app/models/color.rb', /include MongoMapper::Document/
assert_file 'app/models/color.rb', /key :name, String/
assert_file 'app/models/color.rb', /key :saturation, Integer/
end

test 'model are properly created with timestamps option' do
run_generator ['Color', '--timestamps']
assert_file 'app/models/color.rb', /class Color/
assert_file 'app/models/color.rb', /include MongoMapper::Document/
assert_file 'app/models/color.rb', /timestamps/
end

test 'model are properly created with parent option' do
run_generator ['Green', '--parent', 'Color']
assert_file 'app/models/green.rb', /class Green < Color/
end

end