active_mocker 1.4.2 → 1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -0
- data/README.md +35 -29
- data/lib/active_mocker.rb +3 -1
- data/lib/active_mocker/active_record.rb +27 -5
- data/lib/active_mocker/active_record/scope.rb +5 -5
- data/lib/active_mocker/active_record/unknown_module.rb +4 -4
- data/lib/active_mocker/generate.rb +36 -15
- data/lib/active_mocker/loaded_mocks.rb +60 -54
- data/lib/active_mocker/mock.rb +24 -0
- data/lib/active_mocker/mock/association.rb +6 -0
- data/lib/{active_mock → active_mocker/mock}/base.rb +43 -89
- data/lib/{active_mock → active_mocker/mock}/collection.rb +14 -7
- data/lib/{active_mock → active_mocker/mock}/do_nothing_active_record_methods.rb +7 -1
- data/lib/active_mocker/mock/exceptions.rb +25 -0
- data/lib/active_mocker/mock/has_and_belongs_to_many.rb +6 -0
- data/lib/active_mocker/mock/has_many.rb +60 -0
- data/lib/active_mocker/mock/hash_process.rb +19 -0
- data/lib/active_mocker/mock/mock_abilities.rb +98 -0
- data/lib/{active_mock → active_mocker/mock}/next_id.rb +7 -2
- data/lib/{active_mock → active_mocker/mock}/object_inspect.rb +3 -1
- data/lib/{active_mock → active_mocker/mock}/queries.rb +32 -29
- data/lib/active_mocker/mock/records.rb +62 -0
- data/lib/{active_mock → active_mocker/mock}/relation.rb +4 -3
- data/lib/active_mocker/mock/template_methods.rb +39 -0
- data/lib/active_mocker/mock_template.erb +45 -31
- data/lib/active_mocker/model_reader.rb +29 -0
- data/lib/active_mocker/model_schema.rb +23 -16
- data/lib/active_mocker/model_schema/generate.rb +19 -8
- data/lib/active_mocker/railtie.rb +17 -0
- data/lib/active_mocker/reparameterize.rb +2 -4
- data/lib/active_mocker/rspec_helper.rb +18 -0
- data/lib/active_mocker/task.rake +15 -0
- data/lib/active_mocker/version.rb +1 -1
- metadata +80 -206
- data/.gitignore +0 -19
- data/.hound.yml +0 -0
- data/.travis.yml +0 -5
- data/Gemfile +0 -4
- data/Rakefile +0 -26
- data/active_mocker.gemspec +0 -40
- data/lib/active_mock/association.rb +0 -7
- data/lib/active_mock/creators.rb +0 -25
- data/lib/active_mock/has_and_belongs_to_many.rb +0 -7
- data/lib/active_mock/has_many.rb +0 -54
- data/lib/active_mock/records.rb +0 -83
- data/lib/active_mocker/active_mock.rb +0 -26
- data/lib/active_mocker/active_record/const_missing.rb +0 -5
- data/lib/active_mocker/class_exists.rb +0 -12
- data/lib/active_mocker/const_sets.rb +0 -24
- data/lib/active_mocker/mock_task.rb +0 -12
- data/sample_app_rails_4/.idea/.generators +0 -8
- data/sample_app_rails_4/.idea/.name +0 -1
- data/sample_app_rails_4/.idea/.rakeTasks +0 -7
- data/sample_app_rails_4/.idea/dataSources.ids +0 -141
- data/sample_app_rails_4/.idea/dataSources.xml +0 -36
- data/sample_app_rails_4/.idea/dictionaries/zeisler.xml +0 -3
- data/sample_app_rails_4/.idea/encodings.xml +0 -5
- data/sample_app_rails_4/.idea/inspectionProfiles/Project_Default.xml +0 -20
- data/sample_app_rails_4/.idea/inspectionProfiles/profiles_settings.xml +0 -7
- data/sample_app_rails_4/.idea/misc.xml +0 -5
- data/sample_app_rails_4/.idea/modules.xml +0 -10
- data/sample_app_rails_4/.idea/runConfigurations/Development__sample_app_rails_4.xml +0 -28
- data/sample_app_rails_4/.idea/runConfigurations/Production__sample_app_rails_4.xml +0 -28
- data/sample_app_rails_4/.idea/runConfigurations/spec__sample_app_rails_4.xml +0 -26
- data/sample_app_rails_4/.idea/runConfigurations/test__sample_app_rails_4.xml +0 -28
- data/sample_app_rails_4/.idea/sample_app_rails_4.iml +0 -262
- data/sample_app_rails_4/.idea/scopes/scope_settings.xml +0 -5
- data/sample_app_rails_4/.idea/vcs.xml +0 -7
- data/sample_app_rails_4/.idea/workspace.xml +0 -619
- data/sample_app_rails_4/.rspec +0 -1
- data/sample_app_rails_4/.secret +0 -1
- data/sample_app_rails_4/Gemfile +0 -13
- data/sample_app_rails_4/Guardfile +0 -53
- data/sample_app_rails_4/LICENSE +0 -21
- data/sample_app_rails_4/README.md +0 -25
- data/sample_app_rails_4/README.nitrous.md +0 -20
- data/sample_app_rails_4/Rakefile +0 -6
- data/sample_app_rails_4/app/assets/images/rails.png +0 -0
- data/sample_app_rails_4/app/assets/javascripts/application.js +0 -17
- data/sample_app_rails_4/app/assets/javascripts/sessions.js.coffee +0 -3
- data/sample_app_rails_4/app/assets/javascripts/static_pages.js.coffee +0 -3
- data/sample_app_rails_4/app/assets/javascripts/users.js.coffee +0 -3
- data/sample_app_rails_4/app/assets/stylesheets/application.css +0 -13
- data/sample_app_rails_4/app/assets/stylesheets/custom.css.scss +0 -246
- data/sample_app_rails_4/app/assets/stylesheets/sessions.css.scss +0 -3
- data/sample_app_rails_4/app/assets/stylesheets/static_pages.css.scss +0 -3
- data/sample_app_rails_4/app/assets/stylesheets/users.css.scss +0 -3
- data/sample_app_rails_4/app/controllers/application_controller.rb +0 -6
- data/sample_app_rails_4/app/controllers/concerns/.keep +0 -0
- data/sample_app_rails_4/app/controllers/microposts_controller.rb +0 -31
- data/sample_app_rails_4/app/controllers/relationships_controller.rb +0 -21
- data/sample_app_rails_4/app/controllers/sessions_controller.rb +0 -21
- data/sample_app_rails_4/app/controllers/static_pages_controller.rb +0 -18
- data/sample_app_rails_4/app/controllers/users_controller.rb +0 -80
- data/sample_app_rails_4/app/helpers/application_helper.rb +0 -12
- data/sample_app_rails_4/app/helpers/sessions_helper.rb +0 -49
- data/sample_app_rails_4/app/helpers/static_pages_helper.rb +0 -2
- data/sample_app_rails_4/app/helpers/users_helper.rb +0 -10
- data/sample_app_rails_4/app/mailers/.keep +0 -0
- data/sample_app_rails_4/app/models/.keep +0 -0
- data/sample_app_rails_4/app/models/concerns/.keep +0 -0
- data/sample_app_rails_4/app/models/micropost.rb +0 -24
- data/sample_app_rails_4/app/models/relationship.rb +0 -6
- data/sample_app_rails_4/app/models/user.rb +0 -39
- data/sample_app_rails_4/app/views/layouts/_footer.html.erb +0 -13
- data/sample_app_rails_4/app/views/layouts/_header.html.erb +0 -31
- data/sample_app_rails_4/app/views/layouts/_shim.html.erb +0 -3
- data/sample_app_rails_4/app/views/layouts/application.html.erb +0 -22
- data/sample_app_rails_4/app/views/microposts/_micropost.html.erb +0 -11
- data/sample_app_rails_4/app/views/relationships/create.js.erb +0 -2
- data/sample_app_rails_4/app/views/relationships/destroy.js.erb +0 -2
- data/sample_app_rails_4/app/views/sessions/new.html.erb +0 -19
- data/sample_app_rails_4/app/views/shared/_error_messages.html.erb +0 -12
- data/sample_app_rails_4/app/views/shared/_feed.html.erb +0 -6
- data/sample_app_rails_4/app/views/shared/_feed_item.html.erb +0 -15
- data/sample_app_rails_4/app/views/shared/_micropost_form.html.erb +0 -7
- data/sample_app_rails_4/app/views/shared/_stats.html.erb +0 -15
- data/sample_app_rails_4/app/views/shared/_user_info.html.erb +0 -12
- data/sample_app_rails_4/app/views/static_pages/about.html.erb +0 -8
- data/sample_app_rails_4/app/views/static_pages/contact.html.erb +0 -6
- data/sample_app_rails_4/app/views/static_pages/help.html.erb +0 -8
- data/sample_app_rails_4/app/views/static_pages/home.html.erb +0 -34
- data/sample_app_rails_4/app/views/static_pages/show.html.erb +0 -0
- data/sample_app_rails_4/app/views/users/_follow.html.erb +0 -5
- data/sample_app_rails_4/app/views/users/_follow_form.html.erb +0 -9
- data/sample_app_rails_4/app/views/users/_unfollow.html.erb +0 -5
- data/sample_app_rails_4/app/views/users/_user.html.erb +0 -8
- data/sample_app_rails_4/app/views/users/edit.html.erb +0 -27
- data/sample_app_rails_4/app/views/users/index.html.erb +0 -10
- data/sample_app_rails_4/app/views/users/new.html.erb +0 -24
- data/sample_app_rails_4/app/views/users/show.html.erb +0 -24
- data/sample_app_rails_4/app/views/users/show_follow.html.erb +0 -30
- data/sample_app_rails_4/bin/bundle +0 -3
- data/sample_app_rails_4/bin/rails +0 -4
- data/sample_app_rails_4/bin/rake +0 -4
- data/sample_app_rails_4/bin/rspec +0 -16
- data/sample_app_rails_4/config.ru +0 -4
- data/sample_app_rails_4/config/application.rb +0 -28
- data/sample_app_rails_4/config/boot.rb +0 -4
- data/sample_app_rails_4/config/database.yml +0 -27
- data/sample_app_rails_4/config/environment.rb +0 -5
- data/sample_app_rails_4/config/environments/development.rb +0 -25
- data/sample_app_rails_4/config/environments/production.rb +0 -79
- data/sample_app_rails_4/config/environments/test.rb +0 -38
- data/sample_app_rails_4/config/initializers/active_mocker.rb +0 -12
- data/sample_app_rails_4/config/initializers/backtrace_silencers.rb +0 -7
- data/sample_app_rails_4/config/initializers/filter_parameter_logging.rb +0 -4
- data/sample_app_rails_4/config/initializers/inflections.rb +0 -16
- data/sample_app_rails_4/config/initializers/mime_types.rb +0 -5
- data/sample_app_rails_4/config/initializers/secret_token.rb +0 -22
- data/sample_app_rails_4/config/initializers/session_store.rb +0 -3
- data/sample_app_rails_4/config/initializers/wrap_parameters.rb +0 -14
- data/sample_app_rails_4/config/locales/en.yml +0 -23
- data/sample_app_rails_4/config/routes.rb +0 -17
- data/sample_app_rails_4/db/migrate/20130311191400_create_users.rb +0 -10
- data/sample_app_rails_4/db/migrate/20130311194153_add_index_to_users_email.rb +0 -5
- data/sample_app_rails_4/db/migrate/20130311201841_add_password_digest_to_users.rb +0 -5
- data/sample_app_rails_4/db/migrate/20130314184954_add_remember_token_to_users.rb +0 -6
- data/sample_app_rails_4/db/migrate/20130315015932_add_admin_to_users.rb +0 -5
- data/sample_app_rails_4/db/migrate/20130315175534_create_microposts.rb +0 -11
- data/sample_app_rails_4/db/migrate/20130315230445_create_relationships.rb +0 -13
- data/sample_app_rails_4/db/schema.rb +0 -51
- data/sample_app_rails_4/db/seeds.rb +0 -7
- data/sample_app_rails_4/features/signing_in.feature +0 -13
- data/sample_app_rails_4/features/step_definitions/authentication_steps.rb +0 -30
- data/sample_app_rails_4/features/support/env.rb +0 -59
- data/sample_app_rails_4/lib/assets/.keep +0 -0
- data/sample_app_rails_4/lib/tasks/.keep +0 -0
- data/sample_app_rails_4/lib/tasks/active_mocker.rake +0 -10
- data/sample_app_rails_4/lib/tasks/cucumber.rake +0 -65
- data/sample_app_rails_4/lib/tasks/sample_data.rake +0 -42
- data/sample_app_rails_4/lib/unit_logger.rb +0 -22
- data/sample_app_rails_4/public/404.html +0 -27
- data/sample_app_rails_4/public/422.html +0 -26
- data/sample_app_rails_4/public/500.html +0 -26
- data/sample_app_rails_4/public/assets/application-4962059d8f80f9bb096692bacc29c4e8.css +0 -5091
- data/sample_app_rails_4/public/assets/application-4962059d8f80f9bb096692bacc29c4e8.css.gz +0 -0
- data/sample_app_rails_4/public/assets/application-eeb856e3fe2c8f879c91d0e81d59cb40.js +0 -12952
- data/sample_app_rails_4/public/assets/application-eeb856e3fe2c8f879c91d0e81d59cb40.js.gz +0 -0
- data/sample_app_rails_4/public/assets/glyphicons-halflings-c806376f05e4ccabe2c5315a8e95667c.png +0 -0
- data/sample_app_rails_4/public/assets/glyphicons-halflings-white-62b67d9edee3db90d18833087f848d6e.png +0 -0
- data/sample_app_rails_4/public/assets/manifest-802de9eb1c853769101852422b620883.json +0 -1
- data/sample_app_rails_4/public/assets/rails-231a680f23887d9dd70710ea5efd3c62.png +0 -0
- data/sample_app_rails_4/public/favicon.ico +0 -0
- data/sample_app_rails_4/public/robots.txt +0 -5
- data/sample_app_rails_4/script/cucumber +0 -10
- data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +0 -1038
- data/sample_app_rails_4/spec/factories.rb +0 -17
- data/sample_app_rails_4/spec/micropost_mock_spec.rb +0 -145
- data/sample_app_rails_4/spec/mocks/micropost_mock.rb +0 -153
- data/sample_app_rails_4/spec/mocks/relationship_mock.rb +0 -151
- data/sample_app_rails_4/spec/mocks/user_mock.rb +0 -215
- data/sample_app_rails_4/spec/reload_spec.rb +0 -45
- data/sample_app_rails_4/spec/spec_helper.rb +0 -46
- data/sample_app_rails_4/spec/user_mock_spec.rb +0 -197
- data/sample_app_rails_4/vendor/assets/javascripts/.keep +0 -0
- data/sample_app_rails_4/vendor/assets/stylesheets/.keep +0 -0
- data/spec/lib/acitve_mock/queriable_spec.rb +0 -207
- data/spec/lib/active_mocker/db_to_ruby_type_spec.rb +0 -124
- data/spec/lib/active_mocker/generate_spec.rb +0 -40
- data/spec/lib/active_mocker/loaded_mocks_spec.rb +0 -167
- data/spec/lib/active_mocker/logger_spec.rb +0 -32
- data/spec/lib/active_mocker/model_reader_spec.rb +0 -206
- data/spec/lib/active_mocker/model_schema/generate_spec.rb +0 -111
- data/spec/lib/active_mocker/model_schema_spec.rb +0 -145
- data/spec/lib/active_mocker/schema_reader_spec.rb +0 -109
- data/spec/lib/model.rb +0 -43
- data/spec/lib/person.rb +0 -9
- data/spec/lib/readme_spec.rb +0 -199
- data/spec/lib/reparameterize_spec.rb +0 -202
- data/spec/lib/schema.rb +0 -40
- data/spec/unit_logger.rb +0 -24
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
$:.unshift File.expand_path('../../', __FILE__)
|
3
|
-
require 'active_mocker/logger'
|
4
|
-
|
5
|
-
describe ActiveMocker::Logger do
|
6
|
-
|
7
|
-
describe '::set' do
|
8
|
-
|
9
|
-
let(:logger){double()}
|
10
|
-
|
11
|
-
around do
|
12
|
-
described_class.class_variable_set(:@@logger, nil)
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'set the logger to be used by the mock class' do
|
16
|
-
described_class.set(logger)
|
17
|
-
expect(described_class.class_variable_get(:@@logger)).to eq(logger)
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'will pass any methods to the set logger' do
|
21
|
-
described_class.set(logger)
|
22
|
-
expect(logger).to receive(:info)
|
23
|
-
described_class.info
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'will return nil if no logger is set' do
|
27
|
-
expect(described_class.any_method).to eq nil
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
@@ -1,206 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
$:.unshift File.expand_path('../../', __FILE__)
|
3
|
-
require 'active_support/core_ext/hash/indifferent_access'
|
4
|
-
require 'singleton'
|
5
|
-
require 'logger'
|
6
|
-
require 'active_mocker/logger'
|
7
|
-
require 'string_reader'
|
8
|
-
require 'file_reader'
|
9
|
-
require 'active_mocker/logger'
|
10
|
-
require 'active_mocker/active_record'
|
11
|
-
require 'active_mocker/model_reader'
|
12
|
-
require 'active_mocker/reparameterize'
|
13
|
-
require_relative '../../unit_logger'
|
14
|
-
|
15
|
-
describe ActiveMocker::ModelReader do
|
16
|
-
|
17
|
-
before(:all) do
|
18
|
-
ActiveMocker::Logger.set(UnitLogger)
|
19
|
-
end
|
20
|
-
|
21
|
-
let(:model_reader){ described_class.new({model_dir: File.expand_path('../../', __FILE__)}).parse('model') }
|
22
|
-
|
23
|
-
let(:subject){ model_reader}
|
24
|
-
|
25
|
-
describe '#parse' do
|
26
|
-
|
27
|
-
let(:subject){ described_class.new({model_dir: File.expand_path('../../', __FILE__)}) }
|
28
|
-
|
29
|
-
it 'takes a model name to the active_record model class' do
|
30
|
-
subject.parse('model')
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
describe '#class_methods' do
|
36
|
-
|
37
|
-
it 'returns all public class methods' do
|
38
|
-
expect(subject.class_methods).to eq([:duper, :named, :foo, :bang!])
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
describe '#instance methods' do
|
44
|
-
|
45
|
-
it 'returns all public instance methods' do
|
46
|
-
expect(subject.instance_methods).to eq([:foo, :super])
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
describe '#instance_methods_with_arguments' do
|
52
|
-
|
53
|
-
it 'returns all public instance methods' do
|
54
|
-
expect(subject.instance_methods_with_arguments).to eq([{:foo=>[[:req, :foobar], [:req, :value]]}, {:super=>[]}])
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
describe '#class_methods_with_arguments' do
|
60
|
-
|
61
|
-
it 'returns all public instance methods' do
|
62
|
-
expect(subject.class_methods_with_arguments).to eq( [{:duper=>[[:req, :value], [:rest, :args]]}, {:named=>[[:req, :name], [:opt, :value], [:opt, :options]]}, {:foo=>[]}, {:bang! =>[]}])
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
describe '#belongs_to' do
|
68
|
-
|
69
|
-
let(:subject){model_reader.belongs_to}
|
70
|
-
|
71
|
-
it 'name of association' do
|
72
|
-
expect(subject.first.name).to eq(:company)
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'class_name override' do
|
76
|
-
expect(subject.first.class_name).to eq('PlanServiceCategory')
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'infer class_name' do
|
80
|
-
expect(subject.last.class_name).to eq('Person')
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'infer foreign key' do
|
84
|
-
expect(subject.last.foreign_key).to eq('person_id')
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'foreign key override' do
|
88
|
-
expect(subject.first.foreign_key).to eq('category_id')
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'through' do
|
92
|
-
expect(subject.last.through).to eq('customer')
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
describe '#has_one' do
|
98
|
-
|
99
|
-
let(:subject) { model_reader.has_one }
|
100
|
-
|
101
|
-
it 'name of association' do
|
102
|
-
expect(subject.first.name).to eq(:account)
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|
106
|
-
|
107
|
-
describe '#has_many' do
|
108
|
-
|
109
|
-
let(:subject) { model_reader.has_many }
|
110
|
-
|
111
|
-
it 'name of association' do
|
112
|
-
expect(subject.first.name).to eq(:users)
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
describe '#has_and_belongs_to_many' do
|
118
|
-
|
119
|
-
let(:subject) { model_reader.has_and_belongs_to_many }
|
120
|
-
|
121
|
-
it 'name of association' do
|
122
|
-
expect(subject.first.name).to eq(:disclosures)
|
123
|
-
end
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
# TODO JoinTable
|
128
|
-
# class Assembly < ActiveRecord::Base
|
129
|
-
# has_and_belongs_to_many :parts
|
130
|
-
# end
|
131
|
-
#
|
132
|
-
# class Part < ActiveRecord::Base
|
133
|
-
# has_and_belongs_to_many :assemblies
|
134
|
-
# end
|
135
|
-
# These need to be backed up by a migration to create the assemblies_parts table.This table should be created without a primary key:
|
136
|
-
|
137
|
-
# class CreateAssembliesPartsJoinTable < ActiveRecord::Migration
|
138
|
-
# def change
|
139
|
-
# create_table :assemblies_parts, id: false do |t|
|
140
|
-
# t.integer :assembly_id
|
141
|
-
# t.integer :part_id
|
142
|
-
# end
|
143
|
-
# end
|
144
|
-
# end
|
145
|
-
|
146
|
-
# TODO Polymorphic Associations
|
147
|
-
# A slightly more advanced twist on associations is the polymorphic association.With polymorphic associations, a model can belong to more than one other model, on a single association.For example, you might have a picture model that belongs to either an employee model or a product model.Here 's how this could be declared:
|
148
|
-
#
|
149
|
-
# class Picture < ActiveRecord::Base
|
150
|
-
# belongs_to :imageable, polymorphic: true
|
151
|
-
# end
|
152
|
-
#
|
153
|
-
# class Employee < ActiveRecord::Base
|
154
|
-
# has_many :pictures, as: :imageable
|
155
|
-
# end
|
156
|
-
#
|
157
|
-
# class Product < ActiveRecord::Base
|
158
|
-
# has_many :pictures, as: :imageable
|
159
|
-
# end
|
160
|
-
|
161
|
-
# class CreatePictures < ActiveRecord::Migration
|
162
|
-
# def change
|
163
|
-
# create_table :pictures do |t|
|
164
|
-
# t.string :name
|
165
|
-
# t.integer :imageable_id
|
166
|
-
# t.string :imageable_type
|
167
|
-
# t.timestamps
|
168
|
-
# end
|
169
|
-
# end
|
170
|
-
# end
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
context 'inject string_reader as file_reader' do
|
175
|
-
|
176
|
-
let(:example_model){
|
177
|
-
StringReader.new(
|
178
|
-
<<-eos
|
179
|
-
class Person < ActiveRecord::Base
|
180
|
-
|
181
|
-
belongs_to :zip_code
|
182
|
-
|
183
|
-
def full_name(first_name, last_name)
|
184
|
-
|
185
|
-
end
|
186
|
-
|
187
|
-
end
|
188
|
-
eos
|
189
|
-
)
|
190
|
-
}
|
191
|
-
|
192
|
-
let(:subject){described_class.new({model_dir: File.expand_path('../../', __FILE__), file_reader: example_model})}
|
193
|
-
|
194
|
-
let(:search){subject.parse('person')}
|
195
|
-
|
196
|
-
it 'let not read a file but return a string instead to be evaluated' do
|
197
|
-
expect(search.relationships_types.belongs_to.first.name).to eq :zip_code
|
198
|
-
expect(subject.instance_methods).to eq([:full_name])
|
199
|
-
expect(subject.instance_methods_with_arguments).to eq([{:full_name=>[[:req, :first_name], [:req, :last_name]]}])
|
200
|
-
end
|
201
|
-
|
202
|
-
end
|
203
|
-
|
204
|
-
|
205
|
-
end
|
206
|
-
|
@@ -1,111 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
$:.unshift File.expand_path('../../../../../lib', __FILE__)
|
3
|
-
require 'active_mocker'
|
4
|
-
require 'json'
|
5
|
-
require 'active_support/core_ext/string/inflections'
|
6
|
-
require 'active_support/core_ext/object/deep_dup.rb'
|
7
|
-
require 'active_mocker/model_schema'
|
8
|
-
require 'active_mocker/model_schema'
|
9
|
-
require 'active_mocker/model_schema/generate'
|
10
|
-
require_relative '../../../unit_logger'
|
11
|
-
|
12
|
-
describe ActiveMocker::ModelSchema::Generate do
|
13
|
-
|
14
|
-
let(:app_root) { File.expand_path('../../../../../', __FILE__) }
|
15
|
-
let(:schema_file){ File.join(app_root, 'sample_app_rails_4/db/schema.rb') }
|
16
|
-
# let(:schema_file){ '/Users/zeisler/dev/fbi/db/schema.rb' }
|
17
|
-
let(:models_dir){ File.join(app_root, 'sample_app_rails_4/app/models') }
|
18
|
-
# let(:models_dir){'/Users/zeisler/dev/fbi/app/models' }
|
19
|
-
let(:run){described_class.new(schema_file: schema_file, models_dir: models_dir, logger: UnitLogger.unit).run.sort_by{|m| m.class_name}
|
20
|
-
}
|
21
|
-
|
22
|
-
it 'test' do
|
23
|
-
result = described_class.new(schema_file: schema_file, models_dir: models_dir, logger: UnitLogger.unit).run
|
24
|
-
expect(result.count).to eq 3
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'relationships' do
|
28
|
-
expect(run[-1].relationships.map{ |r| JSON.parse(r.to_json)})
|
29
|
-
.to eq [{"name" => "microposts", "class_name" => "Micropost", "type" => "has_many", "foreign_key" => "user_id"},
|
30
|
-
{"name" => "relationships", "class_name" => "Relationship", "type" => "has_many", "foreign_key" => "follower_id"},
|
31
|
-
{"name" => "followed_users", "class_name" => "FollowedUser", "type" => "has_many", "foreign_key" => "user_id", "through" => "relationships"},
|
32
|
-
{"name" => "reverse_relationships", "class_name" => "Relationship", "type" => "has_many", "foreign_key" => "followed_id"},
|
33
|
-
{"name" => "followers", "class_name" => "Follower", "type" => "has_many", "foreign_key" => "user_id", "through" => "reverse_relationships"}]
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'methods' do
|
37
|
-
expect(run[-1].methods.map { |r| JSON.parse(r.to_json) })
|
38
|
-
.to eq([{"name" => "feed", "arguments" => [], "type" => "instance"},
|
39
|
-
{"name" => "following?", "arguments" => [["req", "other_user"]], "type" => "instance"},
|
40
|
-
{"name" => "follow!", "arguments" => [["req", "other_user"]], "type" => "instance"},
|
41
|
-
{"name" => "unfollow!", "arguments" => [["req", "other_user"]], "type" => "instance"},
|
42
|
-
{"name" => "new_remember_token", "arguments" => [], "type" => "class"},
|
43
|
-
{"name" => "digest", "arguments" => [["req", "token"]], "type" => "class"}])
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'arguments' do
|
47
|
-
expect(run[-1].methods.last.arguments.passable).to eq('token')
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'attributes' do
|
51
|
-
expect(run[-1].attributes.map { |r| JSON.parse(r.to_json) })
|
52
|
-
.to eq([{"name" => "id", "type" => "integer", "primary_key" => true},
|
53
|
-
{"name" => "name", "type" => "string"},
|
54
|
-
{"name" => "email", "type" => "string", "default_value" => ""},
|
55
|
-
{"name" => "credits", "type" => "decimal", "precision" => 19, "scale" => 6},
|
56
|
-
{"name" => "created_at", "type" => "datetime"},
|
57
|
-
{"name" => "updated_at", "type" => "datetime"},
|
58
|
-
{"name" => "password_digest", "type" => "string"},
|
59
|
-
{"name" => "remember_token", "type" => "boolean", "default_value" => true},
|
60
|
-
{"name" => "admin", "type" => "boolean", "default_value" => false}])
|
61
|
-
end
|
62
|
-
|
63
|
-
describe '#primary_key' do
|
64
|
-
|
65
|
-
context 'when primary model has a primary key' do
|
66
|
-
|
67
|
-
let(:given_model){OpenStruct.new(primary_key: 'user_id')}
|
68
|
-
let(:given_attributes){ [OpenStruct.new(name: 'user_id'), OpenStruct.new(name: 'name')] }
|
69
|
-
|
70
|
-
it 'will return attributes with that name' do
|
71
|
-
expect(described_class.new.primary_key(given_attributes, given_model)).to eq(given_attributes.first)
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
context 'when attributes has a primary key' do
|
77
|
-
|
78
|
-
let(:given_model) { OpenStruct.new(primary_key: false) }
|
79
|
-
let(:given_attributes) { [OpenStruct.new(name: 'user_id', primary_key: true), OpenStruct.new(name: 'name')] }
|
80
|
-
|
81
|
-
it 'will return attributes with that name' do
|
82
|
-
expect(described_class.new.primary_key(given_attributes, given_model)).to eq(given_attributes.first)
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
context 'when attributes or model has no primary key' do
|
88
|
-
|
89
|
-
let(:given_model) { OpenStruct.new(primary_key: false) }
|
90
|
-
let(:given_attributes) { [OpenStruct.new(name: 'id'), OpenStruct.new(name: 'name')] }
|
91
|
-
|
92
|
-
it 'will default to attribute with id' do
|
93
|
-
expect(described_class.new.primary_key(given_attributes, given_model)).to eq(given_attributes.first)
|
94
|
-
end
|
95
|
-
|
96
|
-
end
|
97
|
-
|
98
|
-
context 'when attributes or model has no primary key and there is no id attribute' do
|
99
|
-
|
100
|
-
let(:given_model) { OpenStruct.new(primary_key: false) }
|
101
|
-
let(:given_attributes) { [OpenStruct.new(name: 'something'), OpenStruct.new(name: 'name')] }
|
102
|
-
|
103
|
-
it 'will return nil' do
|
104
|
-
expect(described_class.new.primary_key(given_attributes, given_model)).to eq(nil)
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
@@ -1,145 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
$:.unshift File.expand_path('../../../../lib', __FILE__)
|
3
|
-
require 'json'
|
4
|
-
require_relative '../../unit_logger'
|
5
|
-
require 'active_mocker/model_schema'
|
6
|
-
|
7
|
-
describe ActiveMocker::ModelSchema do
|
8
|
-
|
9
|
-
subject{described_class.new(class_name: 'Model',
|
10
|
-
table_name: 'models'
|
11
|
-
)
|
12
|
-
}
|
13
|
-
|
14
|
-
it '#class_name' do
|
15
|
-
expect(subject.class_name).to eq 'Model'
|
16
|
-
end
|
17
|
-
|
18
|
-
it '#table_name' do
|
19
|
-
expect(subject.table_name).to eq 'models'
|
20
|
-
end
|
21
|
-
|
22
|
-
describe '#primary_key' do
|
23
|
-
|
24
|
-
let(:a_primary_key_attribute){ ActiveMocker::ModelSchema::Attributes.new(name: 'key', primary_key: true, type: nil)}
|
25
|
-
|
26
|
-
it 'will return the first attribute that is a primary key' do
|
27
|
-
subject = described_class.new(attributes: [a_primary_key_attribute])
|
28
|
-
expect(subject.primary_key).to eq a_primary_key_attribute
|
29
|
-
end
|
30
|
-
|
31
|
-
let(:a_non_primary_key_attribute){ ActiveMocker::ModelSchema::Attributes.new(name: 'key', primary_key: false, type: nil)}
|
32
|
-
|
33
|
-
it 'will create a default primary_key of id if non is found' do
|
34
|
-
subject = described_class.new(attributes: [a_non_primary_key_attribute])
|
35
|
-
expect(subject.primary_key.name).to eq 'id'
|
36
|
-
expect(subject.attributes.count).to eq 2
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
describe '#attributes -> Array' do
|
42
|
-
|
43
|
-
subject { described_class.new(
|
44
|
-
attributes: [ActiveMocker::ModelSchema::Attributes.new(name: 'name',
|
45
|
-
type: :string,
|
46
|
-
precision: nil,
|
47
|
-
scale: nil,
|
48
|
-
default_value: 'default'
|
49
|
-
)]
|
50
|
-
).attributes.first
|
51
|
-
}
|
52
|
-
|
53
|
-
it 'name' do
|
54
|
-
expect(subject.name).to eq 'name'
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'type' do
|
58
|
-
expect(subject.type).to eq :string
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'precision' do
|
62
|
-
expect(subject.precision).to eq nil
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'scale' do
|
66
|
-
expect(subject.scale).to eq nil
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'default_value' do
|
70
|
-
expect(subject.default_value).to eq 'default'
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
describe '#relationships -> Array' do
|
76
|
-
|
77
|
-
subject { described_class.new(
|
78
|
-
relationships:
|
79
|
-
[
|
80
|
-
ActiveMocker::ModelSchema::Relationships.new(name: 'name',
|
81
|
-
class_name: 'ClassName',
|
82
|
-
type: 'Relationship Type',
|
83
|
-
through: 'link',
|
84
|
-
foreign_key:'relationship_id',
|
85
|
-
join_table: 'join_table'
|
86
|
-
)
|
87
|
-
]
|
88
|
-
).relationships.first
|
89
|
-
}
|
90
|
-
|
91
|
-
it 'name' do
|
92
|
-
expect(subject.name).to eq 'name'
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'class_name' do
|
96
|
-
expect(subject.class_name).to eq 'ClassName'
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'type' do
|
100
|
-
expect(subject.type).to eq 'Relationship Type'
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'through' do
|
104
|
-
expect(subject.through).to eq 'link'
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'foreign_key' do
|
108
|
-
expect(subject.foreign_key).to eq 'relationship_id'
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'join_table' do
|
112
|
-
expect(subject.join_table).to eq 'join_table'
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
describe '#methods -> Array' do
|
118
|
-
|
119
|
-
subject { described_class.new(
|
120
|
-
methods:
|
121
|
-
[
|
122
|
-
ActiveMocker::ModelSchema::Methods.new(name: 'method_name',
|
123
|
-
arguments: 'argument_array',
|
124
|
-
type: 'class_method'
|
125
|
-
)
|
126
|
-
]
|
127
|
-
).methods.first
|
128
|
-
}
|
129
|
-
|
130
|
-
it 'name' do
|
131
|
-
expect(subject.name).to eq 'method_name'
|
132
|
-
end
|
133
|
-
|
134
|
-
it 'arguments' do
|
135
|
-
expect(subject.arguments.class).to eq ActiveMocker::ModelSchema::Methods::Arguments
|
136
|
-
expect(subject.arguments.arguments).to eq 'argument_array'
|
137
|
-
end
|
138
|
-
|
139
|
-
it 'type' do
|
140
|
-
expect(subject.type).to eq 'class_method'
|
141
|
-
end
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
end
|