cutest-cj 1.7.1 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/cutest +5 -3
- data/lib/cutest.rb +1 -1
- data/lib/cutest/database.rb +5 -78
- data/lib/cutest/database/activerecord_driver.rb +79 -0
- data/lib/cutest/database/sequel_driver.rb +33 -0
- data/lib/cutest/database/sequel_factories.rb +128 -0
- data/test/run.rb +2 -0
- metadata +28 -16
- checksums.yaml +0 -7
data/bin/cutest
CHANGED
@@ -34,9 +34,11 @@ if files.any?
|
|
34
34
|
env = '.env'
|
35
35
|
rack_env = "#{env}.#{ENV['RACK_ENV']}"
|
36
36
|
|
37
|
-
File.
|
38
|
-
|
39
|
-
|
37
|
+
if File.file? env
|
38
|
+
File.foreach env do |line|
|
39
|
+
key, value = line.split "=", 2
|
40
|
+
ENV[key] = value.gsub('\n', '').strip
|
41
|
+
end
|
40
42
|
end
|
41
43
|
|
42
44
|
if File.file? rack_env
|
data/lib/cutest.rb
CHANGED
data/lib/cutest/database.rb
CHANGED
@@ -1,79 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
require 'active_record'
|
7
|
-
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
|
10
|
-
private
|
11
|
-
|
12
|
-
def factory *attrs
|
13
|
-
attrs.each do |key, value|
|
14
|
-
if key.is_a? Hash
|
15
|
-
key.each do |k, v|
|
16
|
-
send("#{k}=", v) unless send("#{k}").present?
|
17
|
-
end
|
18
|
-
else
|
19
|
-
send("#{key}=", value) unless send("#{key}").present?
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
if respond_to?(:creator_id) and not creator_id
|
24
|
-
self.creator_id = Cutest.config.creator_id
|
25
|
-
end
|
26
|
-
|
27
|
-
if respond_to?(:updater_id) and not updater_id
|
28
|
-
self.updater_id = Cutest.config.updater_id
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def reset database = false, options = {}
|
34
|
-
connect database
|
35
|
-
|
36
|
-
c = config.merge options
|
37
|
-
|
38
|
-
ignore_tables = %w(schema_migrations)
|
39
|
-
|
40
|
-
if c.key? :ignore_tables
|
41
|
-
ignore_tables.concat c[:ignore_tables]
|
42
|
-
end
|
43
|
-
|
44
|
-
conn = ActiveRecord::Base.connection
|
45
|
-
tables = conn.execute("show tables").map { |r| r[0] }
|
46
|
-
|
47
|
-
tables.each do |t|
|
48
|
-
unless ignore_tables.include? t
|
49
|
-
conn.execute("SET FOREIGN_KEY_CHECKS = 0")
|
50
|
-
conn.execute("TRUNCATE #{t}")
|
51
|
-
conn.execute("SET FOREIGN_KEY_CHECKS = 1")
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def connect database = false
|
57
|
-
ActiveRecord::Base.default_timezone = Time.zone
|
58
|
-
|
59
|
-
db = URI.parse database || config[:url]
|
60
|
-
|
61
|
-
ActiveRecord::Base.establish_connection(
|
62
|
-
adapter: db.scheme == 'postgres' ? 'postgresql' : db.scheme,
|
63
|
-
encoding: 'utf8',
|
64
|
-
reconnect: true,
|
65
|
-
database: db.path[1..-1],
|
66
|
-
host: db.host,
|
67
|
-
port: db.port,
|
68
|
-
username: db.user,
|
69
|
-
password: db.password,
|
70
|
-
)
|
71
|
-
end
|
72
|
-
|
73
|
-
def config
|
74
|
-
Cutest.config.database.clone
|
75
|
-
end
|
76
|
-
end
|
1
|
+
if defined? Sequel
|
2
|
+
require_relative 'database/sequel_driver'
|
3
|
+
require_relative 'database/sequel_factories'
|
4
|
+
elsif defined? ActiveRecord
|
5
|
+
require_relative 'database/activerecord_driver'
|
77
6
|
end
|
78
|
-
|
79
|
-
ActiveRecord::Base.send :include, Cutest::Database::Helper
|
@@ -0,0 +1,79 @@
|
|
1
|
+
class Cutest
|
2
|
+
module Database
|
3
|
+
extend self
|
4
|
+
|
5
|
+
module Helper
|
6
|
+
require 'active_record'
|
7
|
+
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def factory *attrs
|
13
|
+
attrs.each do |key, value|
|
14
|
+
if key.is_a? Hash
|
15
|
+
key.each do |k, v|
|
16
|
+
send("#{k}=", v) unless send("#{k}").present?
|
17
|
+
end
|
18
|
+
else
|
19
|
+
send("#{key}=", value) unless send("#{key}").present?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
if respond_to?(:creator_id) and not creator_id
|
24
|
+
self.creator_id = Cutest.config.creator_id
|
25
|
+
end
|
26
|
+
|
27
|
+
if respond_to?(:updater_id) and not updater_id
|
28
|
+
self.updater_id = Cutest.config.updater_id
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def reset database = false, options = {}
|
34
|
+
connect database
|
35
|
+
|
36
|
+
c = config.merge options
|
37
|
+
|
38
|
+
ignore_tables = %w(schema_migrations)
|
39
|
+
|
40
|
+
if c.key? :ignore_tables
|
41
|
+
ignore_tables.concat c[:ignore_tables]
|
42
|
+
end
|
43
|
+
|
44
|
+
conn = ActiveRecord::Base.connection
|
45
|
+
tables = conn.execute("show tables").map { |r| r[0] }
|
46
|
+
|
47
|
+
tables.each do |t|
|
48
|
+
unless ignore_tables.include? t
|
49
|
+
conn.execute("SET FOREIGN_KEY_CHECKS = 0")
|
50
|
+
conn.execute("TRUNCATE #{t}")
|
51
|
+
conn.execute("SET FOREIGN_KEY_CHECKS = 1")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def connect database = false
|
57
|
+
ActiveRecord::Base.default_timezone = Time.zone
|
58
|
+
|
59
|
+
db = URI.parse database || config[:url]
|
60
|
+
|
61
|
+
ActiveRecord::Base.establish_connection(
|
62
|
+
adapter: db.scheme == 'postgres' ? 'postgresql' : db.scheme,
|
63
|
+
encoding: 'utf8',
|
64
|
+
reconnect: true,
|
65
|
+
database: db.path[1..-1],
|
66
|
+
host: db.host,
|
67
|
+
port: db.port,
|
68
|
+
username: db.user,
|
69
|
+
password: db.password,
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
73
|
+
def config
|
74
|
+
Cutest.config.database.clone
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
ActiveRecord::Base.send :include, Cutest::Database::Helper
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Cutest
|
2
|
+
module Database
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def reset database = false, options = {}
|
6
|
+
db = connect database
|
7
|
+
|
8
|
+
c = config.merge options
|
9
|
+
|
10
|
+
ignore_tables = %w(schema_migrations)
|
11
|
+
|
12
|
+
if c.key? :ignore_tables
|
13
|
+
ignore_tables.concat c[:ignore_tables]
|
14
|
+
end
|
15
|
+
|
16
|
+
db.tables.each do |t|
|
17
|
+
unless ignore_tables.include? t
|
18
|
+
db.run("SET FOREIGN_KEY_CHECKS = 0")
|
19
|
+
db.run("TRUNCATE #{t}")
|
20
|
+
db.run("SET FOREIGN_KEY_CHECKS = 1")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def connect database = false
|
26
|
+
@db ||= Sequel.connect database || config[:url]
|
27
|
+
end
|
28
|
+
|
29
|
+
def config
|
30
|
+
Cutest.config.database.clone
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
class Factory
|
5
|
+
def initialize(values_proc)
|
6
|
+
@values_proc = values_proc
|
7
|
+
end
|
8
|
+
|
9
|
+
# The Proc this factory uses to generate a new set of values.
|
10
|
+
attr_reader :values_proc
|
11
|
+
|
12
|
+
# Merges all key/value pairs generated by this factory into the
|
13
|
+
# given +values+ hash.
|
14
|
+
def apply_values(values={})
|
15
|
+
@values = values
|
16
|
+
instance_eval(&values_proc)
|
17
|
+
@values
|
18
|
+
end
|
19
|
+
|
20
|
+
# Merges all key/value pairs that are generated by the given +factory+ into
|
21
|
+
# the values currently being generated by this factory. Should be called
|
22
|
+
# inside a values Proc to include values from some higher-level factory.
|
23
|
+
#
|
24
|
+
# User.factory do
|
25
|
+
# name Randgen.name
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# User.factory(:with_url) do
|
29
|
+
# include_factory User.factory
|
30
|
+
# url "http://www.example.com"
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# User.make # Has only a name property
|
34
|
+
# User.make(:with_url) # Has both name *and* url properties
|
35
|
+
def include_factory(factory)
|
36
|
+
factory.apply_values(@values)
|
37
|
+
end
|
38
|
+
|
39
|
+
alias_method :include, :include_factory
|
40
|
+
|
41
|
+
# Gets/sets the value of the given +key+. If any +args+ are provided, the
|
42
|
+
# value is set to the first one. Otherwise, if a block is given it will be
|
43
|
+
# called with a number unique to the given +key+ (like a sequence) and the
|
44
|
+
# return value of the block is used as the value.
|
45
|
+
def method_missing(key, *args)
|
46
|
+
if args.any?
|
47
|
+
@values[key] = args.first
|
48
|
+
elsif block_given?
|
49
|
+
@counts ||= {}
|
50
|
+
@counts[key] ||= 0
|
51
|
+
@counts[key] += 1
|
52
|
+
@values[key] = yield(@counts[key])
|
53
|
+
end
|
54
|
+
|
55
|
+
@values[key]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Model
|
60
|
+
class << self
|
61
|
+
attr_writer :factory_method
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns the name of the Sequel::Model class method that this factory uses
|
65
|
+
# to make new instances. Defaults to +:create+. Other useful methods are
|
66
|
+
# +:new+ (to prevent saving to the database) and +:find_or_create+ (to avoid
|
67
|
+
# uniqueness constraints in the database).
|
68
|
+
def self.factory_method
|
69
|
+
return @factory_method unless @factory_method.nil?
|
70
|
+
return superclass.factory_method if superclass.respond_to?(:factory_method)
|
71
|
+
:create
|
72
|
+
end
|
73
|
+
|
74
|
+
# A Hash of factories for this model, keyed by factory name.
|
75
|
+
def self.factories
|
76
|
+
@factories ||= {}
|
77
|
+
end
|
78
|
+
|
79
|
+
# Gets/sets the factory with the given +name+. If a block is given, uses that
|
80
|
+
# block to create a new factory.
|
81
|
+
def self.factory(name=:default)
|
82
|
+
factories[name] = Factory.new(Proc.new) if block_given?
|
83
|
+
factories[name]
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.has_factory?(name)
|
87
|
+
not factory(name).nil?
|
88
|
+
end
|
89
|
+
|
90
|
+
# Makes an instance of this model using the factories with the given
|
91
|
+
# +factory_names+. Any +values+ given to this method will override
|
92
|
+
# factory-produced values with the same name after all factory values have
|
93
|
+
# been generated. The default factory is used if no factory names are given.
|
94
|
+
#
|
95
|
+
# Note: If +values+ is not a Hash, it will be used as a factory name.
|
96
|
+
def self.make(values={}, *factory_names)
|
97
|
+
unless Hash === values
|
98
|
+
factory_names.unshift(values)
|
99
|
+
values = {}
|
100
|
+
end
|
101
|
+
|
102
|
+
factory_names << :default if factory_names.empty?
|
103
|
+
|
104
|
+
factory_values = factory_names.inject({}) do |memo, name|
|
105
|
+
fac = factory(name) or raise "Unknown #{self} factory: #{name}"
|
106
|
+
fac.apply_values(memo)
|
107
|
+
end
|
108
|
+
|
109
|
+
send factory_method, factory_values.merge(values)
|
110
|
+
end
|
111
|
+
|
112
|
+
# Forces the #factory_method to be the given +method+ temporarily while an
|
113
|
+
# instance is made, then reverts to the old factory method.
|
114
|
+
def self.make_with(method, *args)
|
115
|
+
tmp = @factory_method
|
116
|
+
@factory_method = method
|
117
|
+
obj = make(*args)
|
118
|
+
@factory_method = tmp
|
119
|
+
obj
|
120
|
+
end
|
121
|
+
|
122
|
+
# Sugar for +make_with(:new, *args)+. Useful when the #factory_method is
|
123
|
+
# something other than +:new+ but you still want to use it.
|
124
|
+
def self.build(*args)
|
125
|
+
make_with(:new, *args)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
data/test/run.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cutest-cj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Damian Janowski
|
@@ -10,62 +11,70 @@ authors:
|
|
10
11
|
autorequire:
|
11
12
|
bindir: bin
|
12
13
|
cert_chain: []
|
13
|
-
date: 2014-
|
14
|
+
date: 2014-09-10 00:00:00.000000000 Z
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: pry
|
17
18
|
requirement: !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
18
20
|
requirements:
|
19
|
-
- - '>='
|
21
|
+
- - ! '>='
|
20
22
|
- !ruby/object:Gem::Version
|
21
23
|
version: '0'
|
22
24
|
type: :runtime
|
23
25
|
prerelease: false
|
24
26
|
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
25
28
|
requirements:
|
26
|
-
- - '>='
|
29
|
+
- - ! '>='
|
27
30
|
- !ruby/object:Gem::Version
|
28
31
|
version: '0'
|
29
32
|
- !ruby/object:Gem::Dependency
|
30
33
|
name: awesome_print
|
31
34
|
requirement: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
32
36
|
requirements:
|
33
|
-
- - '>='
|
37
|
+
- - ! '>='
|
34
38
|
- !ruby/object:Gem::Version
|
35
39
|
version: '0'
|
36
40
|
type: :runtime
|
37
41
|
prerelease: false
|
38
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
39
44
|
requirements:
|
40
|
-
- - '>='
|
45
|
+
- - ! '>='
|
41
46
|
- !ruby/object:Gem::Version
|
42
47
|
version: '0'
|
43
48
|
- !ruby/object:Gem::Dependency
|
44
49
|
name: clap
|
45
50
|
requirement: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
46
52
|
requirements:
|
47
|
-
- - '>='
|
53
|
+
- - ! '>='
|
48
54
|
- !ruby/object:Gem::Version
|
49
55
|
version: '0'
|
50
56
|
type: :runtime
|
51
57
|
prerelease: false
|
52
58
|
version_requirements: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
53
60
|
requirements:
|
54
|
-
- - '>='
|
61
|
+
- - ! '>='
|
55
62
|
- !ruby/object:Gem::Version
|
56
63
|
version: '0'
|
57
64
|
- !ruby/object:Gem::Dependency
|
58
65
|
name: mocoso
|
59
66
|
requirement: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
60
68
|
requirements:
|
61
|
-
- - '>='
|
69
|
+
- - ! '>='
|
62
70
|
- !ruby/object:Gem::Version
|
63
71
|
version: '0'
|
64
72
|
type: :runtime
|
65
73
|
prerelease: false
|
66
74
|
version_requirements: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
67
76
|
requirements:
|
68
|
-
- - '>='
|
77
|
+
- - ! '>='
|
69
78
|
- !ruby/object:Gem::Version
|
70
79
|
version: '0'
|
71
80
|
description: Run tests in separate processes to avoid shared state.
|
@@ -87,6 +96,9 @@ files:
|
|
87
96
|
- cutest.gemspec
|
88
97
|
- lib/cutest.rb
|
89
98
|
- lib/cutest/database.rb
|
99
|
+
- lib/cutest/database/activerecord_driver.rb
|
100
|
+
- lib/cutest/database/sequel_driver.rb
|
101
|
+
- lib/cutest/database/sequel_factories.rb
|
90
102
|
- test/assert.rb
|
91
103
|
- test/assert_equal.rb
|
92
104
|
- test/assert_raise.rb
|
@@ -104,26 +116,26 @@ files:
|
|
104
116
|
homepage: https://github.com/djanowski/cutest
|
105
117
|
licenses:
|
106
118
|
- MIT
|
107
|
-
metadata: {}
|
108
119
|
post_install_message:
|
109
120
|
rdoc_options: []
|
110
121
|
require_paths:
|
111
122
|
- lib
|
112
123
|
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
113
125
|
requirements:
|
114
|
-
- - '>='
|
126
|
+
- - ! '>='
|
115
127
|
- !ruby/object:Gem::Version
|
116
128
|
version: '0'
|
117
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
118
131
|
requirements:
|
119
|
-
- - '>='
|
132
|
+
- - ! '>='
|
120
133
|
- !ruby/object:Gem::Version
|
121
134
|
version: '0'
|
122
135
|
requirements: []
|
123
136
|
rubyforge_project:
|
124
|
-
rubygems_version:
|
137
|
+
rubygems_version: 1.8.23
|
125
138
|
signing_key:
|
126
|
-
specification_version:
|
139
|
+
specification_version: 3
|
127
140
|
summary: Forking tests.
|
128
141
|
test_files: []
|
129
|
-
has_rdoc:
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 224cb7bb171191f5c0e9ceeea0299171871c0d8f
|
4
|
-
data.tar.gz: f8d25e5a5ca28c3fa07d00c3562da48ecb233193
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 09b987489d3e419b21a31d386bfe7fd6a06b67970d8168d6d0914a69816bb83c48fdc14dac566db619c0b552bd0793f618f5abc8e6824d8d4cab14f16ba6ed81
|
7
|
-
data.tar.gz: 3a439c1113fde62e73b28e297a3d9e2562237dbcc87ec6b4126f53efc267735be90f2a6d4772c8b73ec38e38e7e1812be41ccbf1d41ab50dbb3b46fddb6e173a
|