cockroach 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.rvmrc +1 -0
  2. data/Gemfile +28 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +124 -0
  5. data/Rakefile +37 -0
  6. data/VERSION +1 -0
  7. data/cockroach.gemspec +96 -0
  8. data/lib/cockroach.rb +43 -0
  9. data/lib/cockroach/base/load_nodes.rb +38 -0
  10. data/lib/cockroach/base/node.rb +192 -0
  11. data/lib/cockroach/base/node_structure.rb +11 -0
  12. data/lib/cockroach/config.rb +54 -0
  13. data/lib/cockroach/config/loader.rb +35 -0
  14. data/lib/cockroach/fixtures/factory_girl.rb +4 -0
  15. data/lib/cockroach/fixtures/factory_girl/loader.rb +43 -0
  16. data/lib/cockroach/fixtures/factory_girl/node.rb +51 -0
  17. data/lib/cockroach/fixtures/factory_girl/profiler.rb +34 -0
  18. data/lib/cockroach/railtie.rb +7 -0
  19. data/lib/cockroach/source.rb +40 -0
  20. data/lib/cockroach/source/model.rb +33 -0
  21. data/lib/cockroach/source/node.rb +27 -0
  22. data/lib/cockroach/version.rb +3 -0
  23. data/lib/generators/cockroach/install_generator.rb +20 -0
  24. data/lib/generators/cockroach/templates/cockroach.rb +8 -0
  25. data/lib/generators/cockroach/templates/faker.yml +16 -0
  26. data/lib/tasks/faker.rake +35 -0
  27. data/test/config/config_test.rb +129 -0
  28. data/test/config/loader_test.rb +42 -0
  29. data/test/fixturers/factory_girl/aliasing_test.rb +151 -0
  30. data/test/fixturers/factory_girl/loading_test.rb +45 -0
  31. data/test/fixturers/factory_girl/node_test.rb +386 -0
  32. data/test/fixturers/factory_girl/profiler_test.rb +124 -0
  33. data/test/fixturers/source_test.rb +153 -0
  34. data/test/generators/install_generator_test.rb +14 -0
  35. data/test/support/active_record.rb +11 -0
  36. data/test/support/data/correct_with_option.yml +13 -0
  37. data/test/support/data/correct_without_option.yml +10 -0
  38. data/test/support/data/dummy_structure/config/faker.yml +13 -0
  39. data/test/support/data/dummy_structure/config/user_only.yml +1 -0
  40. data/test/support/data/dummy_structure/config/witness.yml +6 -0
  41. data/test/support/data/dummy_structure/test/factories/user_factory.rb +16 -0
  42. data/test/support/database_cleaner.rb +13 -0
  43. data/test/support/factory_girl_mocked.rb +32 -0
  44. data/test/support/models/user.rb +23 -0
  45. data/test/test_helper.rb +26 -0
  46. metadata +151 -0
@@ -0,0 +1,124 @@
1
+ require "test_helper"
2
+ require "user"
3
+
4
+ module Cockroach
5
+ class FactoryGirlProfilerTest < Test::Unit::TestCase
6
+ context "Simple profiler" do
7
+ setup do
8
+ before_setup
9
+ mock_factory_girl
10
+ Cockroach.setup do |c|
11
+ c.root = File.expand_path("../../../support/data/dummy_structure", __FILE__)
12
+ c.config_path = "./config/user_only.yml"
13
+ end
14
+ end
15
+
16
+ teardown do
17
+ after_teardown
18
+ end
19
+
20
+ should "inherit default config" do
21
+ profiler = nil
22
+
23
+ assert_nothing_thrown do
24
+ profiler = Cockroach::FactoryGirl::Profiler.new
25
+ end
26
+
27
+ assert_equal Cockroach.config.profile, profiler.instance_variable_get(:@source)
28
+ end
29
+
30
+ should "load! should preload nodes" do
31
+ profiler = Cockroach::FactoryGirl::Profiler.new
32
+
33
+ profiler.instance_variable_set(:@nodes, {})
34
+
35
+ profiler.expects(:load)
36
+
37
+ profiler.load!
38
+ end
39
+
40
+ context "Subnodes" do
41
+
42
+ context "Access" do
43
+ should "be from top level" do
44
+ profiler = Cockroach::FactoryGirl::Profiler.new
45
+ assert_equal @users_node, profiler['person']
46
+ end
47
+ end
48
+
49
+ should "loading" do
50
+ profiler = Cockroach::FactoryGirl::Profiler.new
51
+
52
+ assert_nil profiler.instance_variable_get(:@loaded)
53
+
54
+ fake_node = mock()
55
+ fake_node.stubs(:node_name).returns('name')
56
+ Cockroach::FactoryGirl::Node.stubs(:new).with(any_parameters).returns(fake_node)
57
+ Cockroach::FactoryGirl::Node.expects(:new).with("users_amount",1000).returns(fake_node)
58
+
59
+ assert profiler.load
60
+ assert profiler.instance_variable_get(:@loaded)
61
+ end
62
+
63
+ should "loading!" do
64
+ profiler = Cockroach::FactoryGirl::Profiler.new
65
+
66
+ profiler.load
67
+
68
+ profiler.nodes.each_value do |node|
69
+ node.stubs(:load!)
70
+ node.expects(:load!)
71
+ end
72
+
73
+ assert profiler.load!
74
+ end
75
+ end
76
+ end
77
+
78
+ context "Profiling" do
79
+ setup do
80
+ before_setup
81
+ mock_factory_girl
82
+ ActiveSupport::Inflector.inflections do |inflect|
83
+ inflect.singular /(ss)$/, '\1'
84
+ end
85
+ Cockroach.setup do |c|
86
+ c.root = File.expand_path("../../../support/data/dummy_structure", __FILE__)
87
+ c.config_path = "./config/witness.yml"
88
+ end
89
+ @profile = Cockroach::FactoryGirl::Profiler.new
90
+ end
91
+
92
+ should "load without error" do
93
+ ::FactoryGirl.stubs(:factory_by_name).with(any_parameters)
94
+
95
+ assert_nothing_thrown do
96
+ @profile.load
97
+ end
98
+ end
99
+
100
+ should "load! without error" do
101
+ ::FactoryGirl.stubs(:factory_by_name).with(any_parameters)
102
+
103
+ mocks = ((1..6).to_a.collect {|i| stub('witness', :id => i) })
104
+ message_mock = stub('message', :id => 0)
105
+
106
+ ::FactoryGirl.expects(:create).with("witness").times(6).returns( *mocks )
107
+
108
+ ::FactoryGirl.expects(:create).with("message", {"author" => mocks[0]}).at_least(1).at_most(6).returns(message_mock)
109
+ ::FactoryGirl.expects(:create).with("message", {"author" => mocks[1]}).at_least(1).at_most(6).returns(message_mock)
110
+ ::FactoryGirl.expects(:create).with("message", {"author" => mocks[2]}).at_least(1).at_most(6).returns(message_mock)
111
+ ::FactoryGirl.expects(:create).with("message", {"author" => mocks[3]}).at_least(1).at_most(6).returns(message_mock)
112
+ ::FactoryGirl.expects(:create).with("message", {"author" => mocks[4]}).at_least(1).at_most(6).returns(message_mock)
113
+ ::FactoryGirl.expects(:create).with("message", {"author" => mocks[5]}).at_least(1).at_most(6).returns(message_mock)
114
+
115
+ Cockroach::FactoryGirl::Node.any_instance.stubs(:allowed_options).returns(['author'])
116
+
117
+ @profile.load
118
+ assert_nothing_thrown do
119
+ @profile.load!
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,153 @@
1
+ require "test_helper"
2
+ require "user"
3
+
4
+ module Cockroach
5
+ class FactoryGirlSourceTest < Test::Unit::TestCase
6
+ setup do
7
+ before_setup
8
+ mock_factory_girl
9
+ end
10
+
11
+ teardown do
12
+ after_teardown
13
+ end
14
+
15
+ context "Getting source" do
16
+ context "Model" do
17
+ setup do
18
+ @old_const = Object.const_get(:Place) if Object.const_defined?(:Place)
19
+ @place = stub('Place')
20
+ silence_warnings { Object.const_set(:Place, @place) }
21
+ end
22
+
23
+ teardown do
24
+ if @old_const
25
+ silence_warnings { Object.const_set('Place', @old_const) }
26
+ end
27
+ end
28
+
29
+ should "get model with conditions option" do
30
+ Cockroach::Source::Model.expects(:new).with(Place, {"conditions" => "column = 'test'"})
31
+
32
+ Cockroach::Source.get_source({"model" => "Place", "conditions" => "column = 'test'"})
33
+ end
34
+
35
+ should "get model with id option" do
36
+ Cockroach::Source::Model.expects(:new).with(Place, {"id" => "3"})
37
+
38
+ Cockroach::Source.get_source({"id" => "3","model" => "Place"})
39
+ end
40
+
41
+ should "get model without options" do
42
+ Cockroach::Source::Model.expects(:new).with(Place, {})
43
+
44
+ Cockroach::Source.get_source({"model" => "Place"})
45
+ end
46
+ end
47
+ end
48
+
49
+ context "Node" do
50
+ setup do
51
+ @lands_node = Cockroach::FactoryGirl::Node.new(
52
+ 'places' => {
53
+ 'as' => 'lands',
54
+ 'amount' => '10'
55
+ })
56
+ @source = Cockroach::Source::Node.new @lands_node
57
+ end
58
+
59
+ should "find return nil if no record created yet" do
60
+ assert_nil @source.send(:find, 1)
61
+ end
62
+
63
+ should "random return nil if no record created yet" do
64
+ assert_nil @source.sample
65
+ end
66
+
67
+ should "return record with certain id" do
68
+ places = ((1..10).to_a.collect {|i| stub('place', :id => i) })
69
+ ::FactoryGirl.stubs("create").with("place").returns( *places )
70
+ @lands_node.__send__(:load!)
71
+
72
+ place_class = stub('place_clase')
73
+ place_class.stubs(:find).with(6).returns(p = places[5])
74
+ factory = @lands_node.instance_variable_get(:@factory)
75
+ factory.stubs(:send).with(:class_name).returns(place_class)
76
+
77
+ assert_equal p, @source.send(:find, 6)
78
+ end
79
+
80
+ should "return random record" do
81
+ places = ((1..10).to_a.collect {|i| stub('place', :id => i) })
82
+ ::FactoryGirl.stubs("create").with("place").returns( *places )
83
+ @lands_node.__send__(:load!)
84
+
85
+ place_class = stub('place_clase')
86
+ place_class.stubs(:find).with(any_parameters).returns(p = places.sample)
87
+ factory = @lands_node.instance_variable_get(:@factory)
88
+ factory.stubs(:send).with(:class_name).returns(place_class)
89
+
90
+ assert_equal p, @source.sample
91
+ end
92
+ end
93
+
94
+ context "Model" do
95
+ setup do
96
+ @old_const = Object.const_get(:Place) if Object.const_defined?(:Place)
97
+ @place = stub('Place')
98
+ silence_warnings { Object.const_set(:Place, @place) }
99
+
100
+ @source = Cockroach::Source::Model.new Place
101
+ end
102
+
103
+ teardown do
104
+ if @old_const
105
+ silence_warnings { Object.const_set('Place', @old_const) }
106
+ end
107
+ end
108
+
109
+ should "return record with certain id" do
110
+ places = ((1..10).to_a.collect {|i| stub('place', :id => i) })
111
+ @place.stubs(:find).with(6).returns(p = places[5])
112
+
113
+ assert_equal p, @source.send(:find, 6)
114
+ end
115
+
116
+ should "return random record" do
117
+ places = ((1..10).to_a.collect {|i| stub('place', :id => i) })
118
+ @place.stubs(:order).with("RAND()").returns(@place)
119
+ @place.stubs(:first).returns(p = places.sample)
120
+
121
+ assert_equal p, @source.sample
122
+ end
123
+
124
+ context "Specific instance" do
125
+ setup do
126
+ @source = Cockroach::Source::Model.new Place, {"id" => "3"}
127
+ end
128
+
129
+ should "return specific record only" do
130
+ places = ((1..10).to_a.collect {|i| stub('place', :id => i) })
131
+ @place.expects(:find).with("3").returns(p = places[2])
132
+
133
+ assert_equal p, @source.sample
134
+ end
135
+ end
136
+
137
+ context "Where condition" do
138
+ setup do
139
+ @source = Cockroach::Source::Model.new Place, {"conditions" => "exists = TRUE"}
140
+ end
141
+
142
+ should "return specific record only" do
143
+ places = ((1..10).to_a.collect {|i| stub('place', :id => i) })
144
+ @place.stubs(:order).with("RAND()").returns(@place)
145
+ @place.stubs(:where).with("exists = TRUE").returns(@place)
146
+ @place.stubs(:first).returns(p = places.sample)
147
+
148
+ assert_equal p, @source.sample
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,14 @@
1
+ require "test_helper"
2
+ require "generators/cockroach/install_generator"
3
+
4
+ class InstallGeneratorTest < Rails::Generators::TestCase
5
+ tests Cockroach::Generators::InstallGenerator
6
+ destination File.expand_path("../../../tmp", __FILE__)
7
+ setup :prepare_destination
8
+
9
+ test "Assert all files are properly created" do
10
+ run_generator
11
+ assert_file "config/faker.yml"
12
+ assert_file "config/initializers/cockroach.rb"
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ require 'logger'
2
+ require "active_record"
3
+
4
+ ActiveRecord::Base.establish_connection(
5
+ :adapter => "sqlite3",
6
+ :database => ':memory:'
7
+ )
8
+
9
+ ActiveRecord::Migration.verbose = false
10
+ #ActiveRecord::Migration.verbose = true
11
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
@@ -0,0 +1,13 @@
1
+ fixture_gem: :factory_girl
2
+ fixtures_path: test/fixtures
3
+
4
+ users_amount: 1000
5
+ feeds_ratio: 1000
6
+ ships:
7
+ amount: 200
8
+ boats:
9
+ ratio: 2000
10
+ corsair:
11
+ ratio:
12
+ upper_limit: 200
13
+ lower_limit: 100
@@ -0,0 +1,10 @@
1
+ users_amount: 1000
2
+ feeds_ratio: 1000
3
+ ships:
4
+ amount: 200
5
+ boats:
6
+ ratio: 2000
7
+ corsair:
8
+ ratio:
9
+ upper_limit: 200
10
+ lower_limit: 100
@@ -0,0 +1,13 @@
1
+ fixture_gem: :factory_girl
2
+ fixtures_path: test/fixtures
3
+
4
+ users_amount: 1000
5
+ feeds_ratio: 1000
6
+ ships:
7
+ amount: 200
8
+ boats:
9
+ ratio: 2000
10
+ corsair:
11
+ ratio:
12
+ upper_limit: 200
13
+ lower_limit: 100
@@ -0,0 +1 @@
1
+ users_amount: 1000
@@ -0,0 +1,6 @@
1
+ witness:
2
+ amount: 6
3
+ message:
4
+ ratio: 3
5
+ author_as: witness
6
+
@@ -0,0 +1,16 @@
1
+ FactoryGirl.define do
2
+ factory(:user) do
3
+ sequence(:email) { |n| "user_number#{n}@test.com" }
4
+ sequence(:first_name) { |n| "user_number#{n}" }
5
+ sequence(:last_name) { |n| "user_number#{n}" }
6
+
7
+ factory :user_admin do
8
+ role :admin
9
+ end
10
+
11
+ factory :user_other do
12
+ role :other
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,13 @@
1
+ require 'database_cleaner'
2
+
3
+ DatabaseCleaner.strategy = :truncation
4
+
5
+ module OrmSetup
6
+ def before_setup
7
+ DatabaseCleaner.start
8
+ end
9
+
10
+ def after_teardown
11
+ DatabaseCleaner.clean
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ #Hack to mock factory girl after it was loaded
2
+ require 'factory_girl'
3
+ Object.__send__(:remove_const, :FactoryGirl)
4
+ module FactoryGirl
5
+ class << self
6
+ attr_accessor :definition_file_paths
7
+ end
8
+
9
+ self.definition_file_paths = %w(factories test/factories spec/factories)
10
+
11
+ def self.define
12
+ end
13
+
14
+ module Mock
15
+ def sequence_number_for(factory)
16
+ if (@sequences ||= {})[factory].nil?
17
+ @sequences[factory] ||= 0
18
+ else
19
+ @sequences[factory] += 1
20
+ end
21
+ end
22
+
23
+ def mock_factory_girl
24
+ ::FactoryGirl.stubs(:factories)
25
+ ::FactoryGirl.stubs(:find_definitions)
26
+ ::FactoryGirl.stubs(:factory_by_name).with("user").returns(stub('user'))
27
+ ::FactoryGirl.stubs(:factory_by_name).with("place").returns(stub('place'))
28
+ ::FactoryGirl.stubs(:factory_by_name).with("bird").returns(stub('bird'))
29
+ ::FactoryGirl.stubs(:factory_by_name).with("test").raises(ArgumentError.new("Factory not registered: test"))
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_record'
2
+
3
+ class CreateUsersTables < ActiveRecord::Migration
4
+ def change
5
+ create_table(:users) do |t|
6
+ t.string :email
7
+ t.string :first_name
8
+ t.string :last_name
9
+ t.string :role, :default => 'user'
10
+
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
15
+
16
+ CreateUsersTables.migrate(:up)
17
+
18
+ class User < ActiveRecord::Base
19
+ validates :first_name, :last_name, presence: true, length: {:within => 2..100}
20
+ validates :email, presence: true, uniqueness: true, length: {:maximum => 100}
21
+
22
+ attr_accessible :email, :first_name, :last_name
23
+ end