dm-fixtures 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "jeweler", "~> 1.8.4"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,23 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.8.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rdoc
10
+ json (1.7.5)
11
+ rake (0.9.2.2)
12
+ rcov (1.0.0)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ bundler
21
+ jeweler (~> 1.8.4)
22
+ rcov
23
+ rdoc (~> 3.12)
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012 Phill Baker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,40 @@
1
+ = dm-fixtures
2
+
3
+ A rip of some of ActiveRecord's fixtures helpers, ported to DataMapper. For the conservative/slow adopters of dm-sweatshop.
4
+
5
+ == Install & Run
6
+
7
+ # Gemfile
8
+ # Put dm-fixtures in dev + test to make its rake tasks available by default - only in :test is not available at the command line
9
+ group :development, :test do
10
+ gem 'dm-fixtures', :require => 'dm-fixtures'
11
+ end
12
+
13
+ # Rails project test/test_helper.rb
14
+ require 'dm-fixtures'
15
+ ...
16
+ fixtures :all
17
+
18
+ # This will load all the fixtures via test:prepare and run all tests
19
+ $ RAILS_ENV=test bundle exec rake test
20
+
21
+ # In addition, this will run only unit tests and show backtraces
22
+ $ RAILS_ENV=test BACKTRACE=yes bundle exec rake test:units
23
+
24
+ == Contributing to dm-fixtures
25
+
26
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
27
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
28
+ * Fork the project.
29
+ * Start a feature/bugfix branch.
30
+ * Commit and push until you are happy with your contribution.
31
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
32
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
33
+
34
+ == Support
35
+
36
+ This is open source, so feel free to branch. Admittedly, this does not fix the issue of updating the authoritative repo/official gem. So, I commit to maintaining this branch, and if I no longer am able to, or want to do that, I'll transfer responsibility - just shoot me an e-mail.
37
+
38
+ == Copyright
39
+
40
+ Copyright (c) 2012 Phill Baker. See LICENSE for further details (MIT).
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "dm-fixtures"
18
+ gem.homepage = "http://github.com/phillbaker/dm-fixtures"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Old school fixtures for DataMapper, specifically with dm-rails.}
21
+ gem.description = %Q{A rip of some of ActiveRecord's fixtures helpers, ported to DataMapper. For the conservative/slow adopters of dm-sweatshop.}
22
+ gem.email = "phillbaker@retrodict.com"
23
+ gem.authors = ["Phill Baker"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "dm-fixture #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "dm-fixtures"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Phill Baker"]
12
+ s.date = "2012-10-21"
13
+ s.description = "A rip of some of ActiveRecord's fixtures helpers, ported to DataMapper. For the conservative/slow adopters of dm-sweatshop."
14
+ s.email = "phillbaker@retrodict.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE",
24
+ "README",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "dm-fixtures.gemspec",
28
+ "lib/adapters.rb",
29
+ "lib/dm-fixtures.rb",
30
+ "lib/dm-fixtures/fixture_set/file.rb",
31
+ "lib/dm-fixtures/fixtures.rb",
32
+ "lib/dm-fixtures/railtie.rb",
33
+ "lib/dm-fixtures/railties/testing.rake",
34
+ "test/helper.rb",
35
+ "test/test_dm-fixture.rb"
36
+ ]
37
+ s.homepage = "http://github.com/phillbaker/dm-fixtures"
38
+ s.licenses = ["MIT"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = "1.8.24"
41
+ s.summary = "Old school fixtures for DataMapper, specifically with dm-rails."
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<bundler>, [">= 0"])
48
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
50
+ s.add_development_dependency(%q<rcov>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<bundler>, [">= 0"])
53
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
55
+ s.add_dependency(%q<rcov>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<bundler>, [">= 0"])
59
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,134 @@
1
+ # From https://github.com/bmabey/database_cleaner/blob/master/lib/database_cleaner/data_mapper/truncation.rb
2
+ module DataMapper
3
+ module Adapters
4
+
5
+ class DataObjectsAdapter
6
+ def storage_names(repository = :default)
7
+ raise NotImplementedError
8
+ end
9
+ end
10
+
11
+ class MysqlAdapter < DataObjectsAdapter
12
+ # taken from http://github.com/godfat/dm-mapping/tree/master
13
+ def storage_names(repository = :default)
14
+ select 'SHOW TABLES'
15
+ end
16
+
17
+ def truncate_table(table_name)
18
+ execute("TRUNCATE TABLE #{quote_name(table_name)};")
19
+ end
20
+
21
+ # copied from activerecord
22
+ def disable_referential_integrity
23
+ old = select("SELECT @@FOREIGN_KEY_CHECKS;")
24
+ begin
25
+ execute("SET FOREIGN_KEY_CHECKS = 0;")
26
+ yield
27
+ ensure
28
+ execute("SET FOREIGN_KEY_CHECKS = ?", *old)
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ class Sqlite3Adapter < DataObjectsAdapter
35
+ # taken from http://github.com/godfat/dm-mapping/tree/master
36
+ def storage_names(repository = :default)
37
+ # activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 177
38
+ sql = <<-SQL
39
+ SELECT name
40
+ FROM sqlite_master
41
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
42
+ SQL
43
+ # activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 181
44
+ select(sql)
45
+ end
46
+
47
+ def truncate_table(table_name)
48
+ execute("DELETE FROM #{quote_name(table_name)};")
49
+ execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
50
+ end
51
+
52
+ # this is a no-op copied from activerecord
53
+ # i didn't find out if/how this is possible
54
+ # activerecord also doesn't do more here
55
+ def disable_referential_integrity
56
+ yield
57
+ end
58
+ end
59
+
60
+ class SqliteAdapter < DataObjectsAdapter
61
+ # taken from http://github.com/godfat/dm-mapping/tree/master
62
+ def storage_names(repository = :default)
63
+ # activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 177
64
+ sql = <<-SQL
65
+ SELECT name
66
+ FROM sqlite_master
67
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
68
+ SQL
69
+ # activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 181
70
+ select(sql)
71
+ end
72
+
73
+ def truncate_table(table_name)
74
+ execute("DELETE FROM #{quote_name(table_name)};")
75
+ execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
76
+ end
77
+
78
+ # this is a no-op copied from activerecord
79
+ # since SQLite 3.6.19 the database engine has included the capability to enforce
80
+ # foreign key constraints but this functionality is disabled by default
81
+ def disable_referential_integrity
82
+ yield
83
+ end
84
+ end
85
+
86
+ # FIXME
87
+ # i don't know if this works
88
+ # i basically just copied activerecord code to get a rough idea what they do.
89
+ # i don't have postgres available, so i won't be the one to write this.
90
+ # maybe codes below gets some postgres/datamapper user going, though.
91
+ class PostgresAdapter < DataObjectsAdapter
92
+
93
+ # taken from http://github.com/godfat/dm-mapping/tree/master
94
+ def storage_names(repository = :default)
95
+ sql = <<-SQL
96
+ SELECT table_name FROM "information_schema"."tables"
97
+ WHERE table_schema = current_schema() and table_type = 'BASE TABLE'
98
+ SQL
99
+ select(sql)
100
+ end
101
+
102
+ def truncate_table(table_name)
103
+ execute("TRUNCATE TABLE #{quote_name(table_name)} RESTART IDENTITY CASCADE;")
104
+ end
105
+
106
+ # FIXME
107
+ # copied from activerecord
108
+ def supports_disable_referential_integrity?
109
+ version = select("SHOW server_version")[0][0].split('.')
110
+ (version[0].to_i >= 8 && version[1].to_i >= 1) ? true : false
111
+ rescue
112
+ return false
113
+ end
114
+
115
+ # FIXME
116
+ # copied unchanged from activerecord
117
+ def disable_referential_integrity(repository = :default)
118
+ if supports_disable_referential_integrity? then
119
+ execute(storage_names(repository).collect do |name|
120
+ "ALTER TABLE #{quote_name(name)} DISABLE TRIGGER ALL"
121
+ end.join(";"))
122
+ end
123
+ yield
124
+ ensure
125
+ if supports_disable_referential_integrity? then
126
+ execute(storage_names(repository).collect do |name|
127
+ "ALTER TABLE #{quote_name(name)} ENABLE TRIGGER ALL"
128
+ end.join(";"))
129
+ end
130
+ end
131
+ end
132
+
133
+ end
134
+ end
@@ -0,0 +1,27 @@
1
+ # A rip of some of ActiveRecord's fixtures helpers, ported to DataMapper. For the conservative/slow adopters of dm-sweatshop.
2
+ #
3
+ # Author:: Phill Baker
4
+ # Copyright:: Copyright (c) 2012 Phill Baker
5
+ # License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
6
+
7
+ require 'dm-core'
8
+ require 'adapters'
9
+ require 'dm-fixtures/fixtures'
10
+ require 'dm-fixtures/railtie'
11
+
12
+ if defined?(DataMapper)
13
+ # Follow ActiveRecord's model, put this in a pre-run rake task, just like starting the server
14
+ # p DataMapper::Model.descendants.entries #TODO below doesn't work because model classes aren't required? => they are in rails init...
15
+ # DataMapper.auto_migrate! # Get the database set up
16
+
17
+ class ActiveSupport::TestCase
18
+ include DataMapper::TestFixtures
19
+ self.fixture_path = "#{Rails.root}test/fixtures/"
20
+ end
21
+
22
+ ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
23
+
24
+ def create_fixtures(*fixture_set_names, &block)
25
+ FixtureSet.create_fixtures(ActiveSupport::TestCase.fixture_path, fixture_set_names, {}, &block)
26
+ end
27
+ end
@@ -0,0 +1,65 @@
1
+ begin
2
+ require 'psych'
3
+ rescue LoadError
4
+ end
5
+
6
+ require 'erb'
7
+ require 'yaml'
8
+
9
+ module DataMapper
10
+ class Fixtures
11
+ class File
12
+ include Enumerable
13
+
14
+ ##
15
+ # Open a fixture file named +file+. When called with a block, the block
16
+ # is called with the filehandle and the filehandle is automatically closed
17
+ # when the block finishes.
18
+ def self.open(file)
19
+ x = new file
20
+ block_given? ? yield(x) : x
21
+ end
22
+
23
+ def initialize(file)
24
+ @file = file
25
+ @rows = nil
26
+ end
27
+
28
+ def each(&block)
29
+ rows.each(&block)
30
+ end
31
+
32
+ RESCUE_ERRORS = [ ArgumentError ] # :nodoc:
33
+
34
+ private
35
+ if defined?(Psych) && defined?(Psych::SyntaxError)
36
+ RESCUE_ERRORS << Psych::SyntaxError
37
+ end
38
+
39
+ def rows
40
+ return @rows if @rows
41
+
42
+ begin
43
+ data = YAML.load(render(IO.read(@file)))
44
+ rescue *RESCUE_ERRORS => error
45
+ raise Fixture::FormatError, "a YAML error occurred parsing #{@file}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace
46
+ end
47
+ @rows = data ? validate(data).to_a : []
48
+ end
49
+
50
+ def render(content)
51
+ ERB.new(content).result
52
+ end
53
+
54
+ # Validate our unmarshalled data.
55
+ def validate(data)
56
+ unless Hash === data || YAML::Omap === data
57
+ raise Fixture::FormatError, 'fixture is not a hash'
58
+ end
59
+
60
+ raise Fixture::FormatError unless data.all? { |name, row| Hash === row }
61
+ data
62
+ end
63
+ end
64
+ end
65
+ end