Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* fixed the rack_datamapper naming problem

* added integration test

* added monkey patch to let generated views spec work with datamapper
  • Loading branch information...
commit 4f1e648fe3f259cf3125784cb4b65afb76449e02 1 parent 4957a55
Christian Meier authored October 07, 2009
1  .gitignore
... ...
@@ -1,2 +1,3 @@
1 1
 pkg
  2
+tmp
2 3
 *~
3  Manifest.txt
@@ -25,13 +25,14 @@ lib/datamapper4rails.rb
25 25
 lib/datamapper4rails/adapters/base_adapter.rb
26 26
 lib/datamapper4rails/adapters/restful_adapter.rb
27 27
 lib/datamapper4rails/datamapper_store.rb
  28
+lib/datamapper4rails/integration_test.rb
28 29
 lib/datamapper4rails/identity_maps.rb
29 30
 lib/datamapper4rails/overlay.rb
30 31
 lib/datamapper4rails/preload_models.rb
31 32
 lib/datamapper4rails/restful_transactions.rb
32 33
 lib/datamapper4rails/rspec_default_values.rb
  34
+lib/datamapper4rails/rspec.rb
33 35
 lib/datamapper4rails/version.rb
34 36
 spec/datamapper_store_spec.rb
35  
-spec/restful_adapter_spec.rb
36 37
 spec/spec.opts
37 38
 spec/spec_helper.rb
13  Rakefile
@@ -12,7 +12,7 @@ require './lib/datamapper4rails/version.rb'
12 12
 Hoe.new('datamapper4rails', Datamapper4rails::VERSION) do |p|
13 13
   p.rubyforge_name = 'datamapper4rail'
14 14
   p.developer('mkristian', 'm.kristian@web.de')
15  
-  p.extra_deps = ['rack_datamapper', 'rails_datamapper']
  15
+  p.extra_deps = [['rack-datamapper', '~>0.2'], 'rails_datamapper']
16 16
   p.remote_rdoc_dir = '' # Release to root
17 17
 end
18 18
 
@@ -30,6 +30,17 @@ Spec::Rake::SpecTask.new(:spec) do |t|
30 30
   t.spec_files = Pathname.glob('./spec/**/*_spec.rb')
31 31
 end
32 32
 
  33
+desc 'generate rails using all generators and run the specs'
  34
+task :integration_tests => [:install] do
  35
+  require 'datamapper4rails/integration_test'
  36
+  Datamapper4Rails::IntegrationTest.new do |t|
  37
+    t.generate "datamapper_model role name:string"
  38
+    t.generate "datamapper_scaffold domain name:string"
  39
+    t.generate "datamapper_rspec_model user name:string"
  40
+    t.generate "datamapper_rspec_scaffold group name:string"
  41
+  end
  42
+end
  43
+
33 44
 require 'yard'
34 45
 
35 46
 YARD::Rake::YardocTask.new
52  datamapper_rails_templates.rb
@@ -23,7 +23,7 @@
23 23
 rake "gems:install"
24 24
 
25 25
 # install specs rake tasks
26  
-generate("rspec")
  26
+generate("rspec", "-f")
27 27
 
28 28
 # install datamapper rake tasks
29 29
 generate("datamapper_install")
@@ -35,6 +35,10 @@
35 35
 gsub_file 'spec/spec_helper.rb', /^\s*config[.]/, '  #\0'
36 36
 gsub_file 'test/test_helper.rb', /^[^#]*fixtures/, '  #\0'
37 37
 
  38
+file 'spec/support/datamapper.rb', <<-CODE
  39
+require 'datamapper4rails/rspec'
  40
+CODE
  41
+
38 42
 # add middleware
39 43
 def middleware(name)
40 44
   log "middleware", name
@@ -60,4 +64,50 @@ def middleware(name)
60 64
 }
61 65
 CODE
62 66
 
  67
+# gzip fix for jruby
  68
+initializer 'monkey_patches.rb', <<-CODE
  69
+if RUBY_PLATFORM =~ /java/
  70
+  require 'zlib'
  71
+  class Zlib::GzipWriter
  72
+    def <<(arg)
  73
+      write(arg)
  74
+    end
  75
+  end
  76
+end
  77
+CODE
  78
+
  79
+file 'prepare_jruby.sh', <<-CODE
  80
+#!/bin/bash
  81
+
  82
+echo
  83
+echo "shall freeze rails and fix a bug which prevents rails to use certain"
  84
+echo "java gems like the dataobjects drivers !!"
  85
+echo
  86
+
  87
+mvn --version
  88
+if [ $? -ne 0 ] ; then
  89
+
  90
+        echo "please install maven >= 2.0.9 from maven.apache.org"
  91
+        exit -1
  92
+fi
  93
+
  94
+mvn de.saumya.mojo:rails-maven-plugin:rails-freeze-gems de.saumya.mojo:rails-maven-plugin:gems-install
  95
+
  96
+echo
  97
+echo "you can run rails with (no need to install jruby !!)"
  98
+echo
  99
+echo "\tmvn de.saumya.mojo:rails-maven-plugin:server"
  100
+echo
  101
+echo "more info on"
  102
+echo "\tgithub.org/mkristian/rails-maven-plugin"
  103
+echo
  104
+CODE
  105
+
63 106
 rake 'db:sessions:create'
  107
+
  108
+logger.info 
  109
+logger.info 
  110
+logger.info "info mavenized rails application"
  111
+logger.info "\tgithub.org/mkristian/rails-maven-plugin"
  112
+logger.info 
  113
+logger.info 
1  lib/datamapper4rails.rb
... ...
@@ -1,4 +1,5 @@
1 1
 require 'rails_datamapper'
  2
+require 'rack-datamapper'
2 3
 require 'datamapper4rails/datamapper_store'
3 4
 
4 5
 # keep this here until rails_datamapper has it included
45  lib/datamapper4rails/integration_test.rb
... ...
@@ -0,0 +1,45 @@
  1
+module Datamapper4Rails
  2
+  class IntegrationTest
  3
+    
  4
+    attr_accessor :generator_args
  5
+
  6
+    attr_accessor :rails_template
  7
+
  8
+    attr_accessor :directory
  9
+    
  10
+    def initialize
  11
+      @ruby = RUBY_PLATFORM =~ /java/ ? 'jruby' : 'ruby'
  12
+      @directory = 'tmp'
  13
+      @generator_args = []
  14
+      @rails_template = 'datamapper_rails_templates.rb'
  15
+      yield self if block_given?
  16
+      execute
  17
+    end
  18
+
  19
+    def generate(*args)
  20
+      @generator_args << args.map(&:to_s).join(" ")
  21
+    end
  22
+
  23
+    def execute
  24
+      FileUtils.rm_rf(@directory)
  25
+      run("-S rails -fm #{rails_template} #{directory}")  
  26
+      FileUtils.cd(@directory) do
  27
+        @generator_args.each do |arg|
  28
+          run("script/generate #{arg}")  
  29
+        end
  30
+        run("-S rake spec")
  31
+        #run("-S rake test:units")
  32
+      end
  33
+
  34
+    end
  35
+
  36
+    def run(command) 
  37
+      unless system("#{@ruby} #{command}")
  38
+        puts
  39
+        puts "error in: #{@ruby} #{command}"
  40
+        exit 1
  41
+      end
  42
+    end
  43
+    
  44
+  end
  45
+end
14  lib/datamapper4rails/overlay.rb
@@ -13,9 +13,11 @@ def a.lookup(clazz)
13 13
       @clazz = clazz
14 14
     end
15 15
     a.lookup(self.class)
16  
-    def a.add_generator(generator_name, prepend = false)
17  
-      path = File.join(@clazz.lookup(generator_name).path, 'templates')
18  
-      push(path) unless member? path
  16
+    def a.add_generator(generator_name)
  17
+      unless frozen?
  18
+        path = File.join(@clazz.lookup(generator_name).path, 'templates')
  19
+        push(path) unless member? path
  20
+      end
19 21
     end
20 22
     a
21 23
   end
@@ -40,8 +42,10 @@ def source_path(relative_source)
40 42
     # first check if the template can be found with in any of the overlay directories
41 43
     if dirs = options[:overlay_dirs]
42 44
       generator_name = self.class.name.underscore.sub(/_generator/, '')
43  
-      dirs.insert(0, File.join(self.class.lookup(generator_name).path, 'templates'))
44  
-      
  45
+      unless dirs.frozen?
  46
+        dirs.insert(0, File.join(self.class.lookup(generator_name).path, 'templates'))
  47
+        dirs.freeze
  48
+      end
45 49
       file = path.nil? ? name : path
46 50
       dirs.each do |dir|
47 51
         if (f = File.join(dir, file)) and File.exists?(f)
8  lib/datamapper4rails/rspec.rb
... ...
@@ -0,0 +1,8 @@
  1
+module DataMapper
  2
+  module Resource
  3
+  
  4
+    def has_attribute?(name)
  5
+      properties[name] != nil
  6
+    end
  7
+  end
  8
+end
2  lib/datamapper4rails/version.rb
... ...
@@ -1,3 +1,3 @@
1 1
 class Datamapper4rails
2  
-  VERSION = '0.2.0'
  2
+  VERSION = '0.2.0'.freeze
3 3
 end
204  spec/restful_adapter_spec.rb
... ...
@@ -1,204 +0,0 @@
1  
-$LOAD_PATH << File.dirname(__FILE__)
2  
-require 'spec_helper'
3  
-require 'datamapper4rails/adapters/restful_adapter'
4  
-require 'slf4r/ruby_logger'
5  
-
6  
-class Item
7  
-  include DataMapper::Resource
8  
-
9  
-  property :id, Serial
10  
-  property :name, String
11  
-
12  
-  belongs_to :group
13  
-end
14  
-class User
15  
-  include DataMapper::Resource
16  
-
17  
-  property :id, Serial
18  
-
19  
-  belongs_to :container
20  
-  has n, :groups, :through => Resource
21  
-end
22  
-class Group
23  
-  include DataMapper::Resource
24  
-
25  
-  property :id, Serial
26  
-
27  
-  has n, :users, :through => Resource
28  
-  has n, :items
29  
-end
30  
-
31  
-class Container
32  
-  include DataMapper::Resource
33  
-
34  
-  property :id, Serial
35  
-
36  
-  has 1, :user
37  
-
38  
-  def items
39  
-    @items ||= []
40  
-  end
41  
-end
42  
-
43  
-def mock_attribute(name)
44  
-  attr = Object.new
45  
-  def attr.name
46  
-    @name
47  
-  end
48  
-  def attr.name=(name)
49  
-    @name = name
50  
-  end
51  
-  attr.name= name
52  
-  attr
53  
-end
54  
-
55  
-def mock_item(stubs={})
56  
-  @mock_item ||= mock(Item, stubs)
57  
-end
58  
-
59  
-def mock_query(stubs={})
60  
-  @mock_query ||= mock(DataMapper::Query, stubs)
61  
-end
62  
-
63  
-describe DataMapper::Adapters::RestfulAdapter do
64  
-
65  
-  before :each do
66  
-    @adapter = DataMapper::Adapters::RestfulAdapter.new(:name, "uri://")
67  
-
68  
-    def @adapter.key_value_from_query(query)
69  
-      432
70  
-    end
71  
-    def @adapter.resource_name_from_query(query)
72  
-      "item"
73  
-    end
74  
-    def @adapter.http_put(uri, data)
75  
-      @uri = uri
76  
-      @data = data
77  
-    end
78  
-    def @adapter.http_delete(uri)
79  
-      @uri = uri
80  
-    end
81  
-    def @adapter.data
82  
-      @data
83  
-    end
84  
-    def @adapter.uri
85  
-      @uri
86  
-    end
87  
-    def @adapter.body=(b)
88  
-      @body = b
89  
-    end
90  
-    def @adapter.send_request
91  
-      res = Object.new
92  
-      def res.body=(b)
93  
-        @body = b
94  
-      end
95  
-      def res.body
96  
-        @body
97  
-      end
98  
-      def res.kind_of?(clazz)
99  
-        true
100  
-      end
101  
-      res.body= @body
102  
-      res
103  
-    end
104  
-    
105  
-  end
106  
-
107  
-  it 'should create an Item' do
108  
-    @adapter.body = "<item><id>123</id><name>zappa</name></item>"
109  
-
110  
-    item = Item.new
111  
-    @adapter.create_resource(item).should == item
112  
-    item.id.should == 123
113  
-    item.name.should == 'zappa'
114  
-  end
115  
-
116  
-  it 'should update an Item' do
117  
-    mock_item.should_receive(:to_query)
118  
-    mock_item.should_receive(:name).and_return("item")
119  
-    mock_item.should_receive(:to_xml)
120  
-    
121  
-    @adapter.update_resource(mock_item, mock_attribute(:name) => "frank zappa")
122  
-
123  
-    @adapter.data.should == "<item><name>frank zappa</name></item>"
124  
-  end
125  
-
126  
-  it 'should update Items' do    
127  
-    mock_query.should_receive(:limit).and_return(1)
128  
-    @adapter.update({mock_attribute(:name) => "frank zappa"}, mock_query)
129  
-
130  
-    @adapter.data.should == "<item><name>frank zappa</name></item>"
131  
-  end
132  
-
133  
-  it 'should delete an Item' do
134  
-    mock_item.should_receive(:name).and_return("item")
135  
-    mock_item.should_receive(:to_query)
136  
-    @adapter.delete_resource(mock_item)
137  
-    @adapter.uri.should == "/items/432.xml"
138  
-  end
139  
-
140  
-  it 'should delete Items' do    
141  
-    mock_query.should_receive(:limit).and_return(1)
142  
-    @adapter.delete(mock_query)
143  
-
144  
-    @adapter.uri.should == "/items/432.xml"
145  
-  end
146  
-
147  
-  it 'should read an Item' do
148  
-    @adapter.body = "<item><id>123</id><name>zappa</name></item>"
149  
-
150  
-    query = DataMapper::Query.new(Item.new.repository, Item)
151  
-    item = @adapter.read_resource(query)
152  
-    item.id.should == 123
153  
-    item.name.should == 'zappa'
154  
-  end
155  
-
156  
-  it 'should read Items' do
157  
-    @adapter.body = "<items type='array'><item><id>123</id><name>zappa</name></item></items>"
158  
-
159  
-    query = DataMapper::Query.new(Item.new.repository, Item)
160  
-    items = @adapter.read_resources(query)
161  
-    items.size.should == 1
162  
-    items[0].id.should == 123
163  
-    items[0].name.should == 'zappa'
164  
-  end
165  
-#end
166  
-
167  
-
168  
-#describe 'associations of ' + DataMapper::Adapters::RestfulAdapter.to_s do
169  
-  it 'should read nested resource (belongs_to)' do
170  
-    @adapter.body = "<item><id>123</id><name>zappa</name>" +
171  
-      "<group><id>342</id>" + #<items tpye='array'><item><id>1234</id><name>frank zappa</name></item></items>" + 
172  
-      "</group>" +
173  
-      "</item>"
174  
-
175  
-    query = DataMapper::Query.new(Item.new.repository, Item)
176  
-    item = @adapter.read_resource(query)
177  
-    item.id.should == 123
178  
-    item.name.should == 'zappa'
179  
-    item.group.id.should == 342
180  
-  end
181  
-
182  
-  it 'should read nested resource manual "has n"' do
183  
-    @adapter.body = "<container><id>342</id><items type='array'>" +
184  
-      "<item><id>543</id><name>hmm</name></item>" +
185  
-      "</items></container>"
186  
-
187  
-    query = DataMapper::Query.new(Container.new.repository, Container)
188  
-    c = @adapter.read_resource(query)
189  
-    c.id.should == 342
190  
-    c.items.size.should == 1
191  
-    c.items[0].id.should == 543
192  
-    c.items[0].name.should == 'hmm'
193  
-  end
194  
-
195  
-#   it 'should read nested resource (has 1)' do
196  
-#     @adapter.body = "<container><id>342</id>" +
197  
-#       "<user><id>543</id></user>" +
198  
-#       "</container>"
199  
-
200  
-#     query = DataMapper::Query.new(Container.new.repository, Container)
201  
-#     c = @adapter.read_resource(query)
202  
-#     c.id.should == 342
203  
-#   end
204  
-end

0 notes on commit 4f1e648

Please sign in to comment.
Something went wrong with that request. Please try again.