fafactory 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,68 @@
1
+ if Rails.env == "test"
2
+ class FafactoriesController < ApplicationController
3
+
4
+ # POST a new instance of the type of model requested
5
+ #
6
+ # ==== Parameters
7
+ # model<String>:: The model to create a new instance of
8
+ # data<Hash>:: A hash of the attributes for the model
9
+ def create_instance
10
+ obj = Module.const_get(params["hash"]["model"].to_sym).new
11
+
12
+ params["hash"]["data"].each do |key, value|
13
+ obj.send(:"#{key}=", value)
14
+ end
15
+
16
+ obj.save!
17
+
18
+ respond_to do |format|
19
+ format.xml do
20
+ render :xml => obj, :status => :created
21
+ end
22
+ end
23
+ end
24
+
25
+ # Purges the test database
26
+ def purge
27
+
28
+ # We have to disconnect the connection for active record because the
29
+ # connection doesn't make it through the fork, and the rake task will
30
+ # do it's own database load, etc anyway
31
+ dbconfig = ActiveRecord::Base.remove_connection
32
+ pid = fork do
33
+
34
+ require 'rake'
35
+ require 'rake/testtask'
36
+ require 'rake/rdoctask'
37
+
38
+ require 'tasks/rails'
39
+
40
+
41
+ Rake::Task['db:test:load'].invoke
42
+ end
43
+ ActiveRecord::Base.establish_connection(dbconfig)
44
+ Process.wait(pid)
45
+
46
+ respond_to do |format|
47
+ format.xml { head :ok }
48
+ end
49
+ end
50
+
51
+ # Find the instance of model identified by id and return it's xml.
52
+ #
53
+ # ==== Parameters
54
+ # id<Integer>:: The id of the instance to look up.
55
+ # model<String>:: The model to look up the instance for.
56
+ #
57
+ # ==== Returns
58
+ #
59
+ def find
60
+ obj = Module.const_get(params[:model].to_sym).find(params[:id])
61
+ respond_to do |format|
62
+ format.xml do
63
+ render :xml => obj
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
data/bin/fafactory ADDED
@@ -0,0 +1,35 @@
1
+ # Starts the rails apps configured in config/fafactory.yml in test mode on
2
+ # the given ports
3
+
4
+ def log(msg)
5
+ puts "* [Fafactory] #{msg}"
6
+ end
7
+
8
+ config = YAML.load_file('config/fafactory.yml')
9
+
10
+ config.each do |service, settings|
11
+ exec_args = []
12
+ if settings["port"]
13
+ exec_args << "--port=#{settings["port"]}"
14
+ end
15
+ exec_args << "--environment=test"
16
+
17
+ log "Starting application at #{settings["path"]}..."
18
+ fork { exec "#{settings["path"]}/script/server", *exec_args }
19
+ end
20
+
21
+ Signal.trap("TERM") do
22
+ log "Terminating..."
23
+ exit
24
+ end
25
+ Signal.trap("INT") do
26
+ log "Terminating..."
27
+ exit
28
+ end
29
+
30
+ begin
31
+ Process.waitall
32
+ rescue
33
+ end
34
+
35
+ log "All apps exited."
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ if Rails.env == "test"
3
+ map.resources :fafactories, :only => [:none],
4
+ :collection => { :create_instance => :post, :purge => :delete,
5
+ :find => :get }
6
+ end
7
+ end
data/lib/fafactory.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'active_resource'
2
+
3
+ class Fafactory < ActiveResource::Base
4
+
5
+ # Creates a new instance of a remote model using the data provided.
6
+ def self.create_instance(service, model, data)
7
+ Fafactory.configure_site(service)
8
+ result = Fafactory.post :create_instance, nil,
9
+ { :model => model, :data => data }.to_xml
10
+
11
+ return Hash.from_xml(result.body)
12
+ end
13
+
14
+ def self.purge(*services)
15
+ services.each do |service|
16
+ Fafactory.configure_site(service)
17
+ Fafactory.delete(:purge)
18
+ end
19
+
20
+ nil
21
+ end
22
+
23
+ # Loads an instance of a remote model based on the id given
24
+ def self.find(service, model, id)
25
+ Fafactory.configure_site(service)
26
+ Fafactory.get(:find, { :model => model, :id => id })
27
+ end
28
+
29
+ private
30
+ def self.configure_site(service)
31
+ @@fafactory_config ||= YAML.load_file('config/fafactory.yml')
32
+
33
+ self.site = "http://0.0.0.0:#{@@fafactory_config[service]["port"]}"
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fafactory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Gnoso, Inc.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-26 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activeresource
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.0"
24
+ version:
25
+ description: Fafactory (originally Far Away Factory) is a tool for remotely creating instances of ActiveRecord models within a service. This is useful when doing integration tests of services, because it allows you to set up the environment within the remote service from your test, rather than trying to keep an instance of the service in pristine shape.
26
+ email: alan@gnoso.com
27
+ executables:
28
+ - fafactory
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/fafactory.rb
35
+ - app/controllers/fafactories_controller.rb
36
+ - bin/fafactory
37
+ - config/routes.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/gnoso/fafactory
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.2
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Framework for creating objects in remote services.
66
+ test_files: []
67
+