to_factory 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/.gitignore +6 -0
  2. data/.rspec +2 -1
  3. data/.travis.yml +23 -0
  4. data/Gemfile +3 -13
  5. data/README.md +59 -20
  6. data/Rakefile +20 -14
  7. data/bin/ci +2 -0
  8. data/bin/spec +2 -0
  9. data/lib/to_factory/collation.rb +34 -0
  10. data/lib/to_factory/config.rb +18 -0
  11. data/lib/to_factory/definition_group.rb +47 -0
  12. data/lib/to_factory/file_sync.rb +48 -0
  13. data/lib/to_factory/file_writer.rb +39 -0
  14. data/lib/to_factory/finders/factory.rb +17 -0
  15. data/lib/to_factory/finders/model.rb +47 -0
  16. data/lib/to_factory/generation/attribute.rb +52 -0
  17. data/lib/to_factory/generation/factory.rb +74 -0
  18. data/lib/to_factory/parsing/file.rb +38 -0
  19. data/lib/to_factory/parsing/new_syntax.rb +9 -0
  20. data/lib/to_factory/parsing/old_syntax.rb +9 -0
  21. data/lib/to_factory/parsing/syntax.rb +86 -0
  22. data/lib/to_factory/version.rb +1 -1
  23. data/lib/to_factory.rb +55 -1
  24. data/spec/db/migrate/{201108201012010100_create_users.rb → 1_create_users.rb} +0 -0
  25. data/spec/db/migrate/2_create_projects.rb +13 -0
  26. data/spec/db/migrate/3_create_not_namespaced.rb +11 -0
  27. data/spec/db/migrate/4_add_birthday_to_users.rb +9 -0
  28. data/spec/example_factories/new_syntax/admin.rb +6 -0
  29. data/spec/example_factories/new_syntax/admin_with_header.rb +8 -0
  30. data/spec/example_factories/new_syntax/project_with_header.rb +7 -0
  31. data/spec/example_factories/new_syntax/user.rb +6 -0
  32. data/spec/example_factories/new_syntax/user_admin.rb +13 -0
  33. data/spec/example_factories/new_syntax/user_admin_with_header.rb +15 -0
  34. data/spec/example_factories/new_syntax/user_with_header.rb +8 -0
  35. data/spec/example_factories/old_syntax/admin.rb +6 -0
  36. data/spec/example_factories/old_syntax/project_with_header.rb +5 -0
  37. data/spec/example_factories/old_syntax/user.rb +6 -0
  38. data/spec/example_factories/old_syntax/user_admin.rb +13 -0
  39. data/spec/example_factories/old_syntax/user_admin_with_header.rb +13 -0
  40. data/spec/example_factories/old_syntax/user_with_header.rb +6 -0
  41. data/spec/integration/config_spec.rb +19 -0
  42. data/spec/integration/file_sync_spec.rb +77 -0
  43. data/spec/integration/file_writer_spec.rb +16 -0
  44. data/spec/integration/generate_class_method_spec.rb +107 -0
  45. data/spec/integration/lint_spec.rb +16 -0
  46. data/spec/spec_helper.rb +90 -14
  47. data/spec/support/match_sexp.rb +24 -0
  48. data/spec/support/models/not_active_record.rb +2 -0
  49. data/spec/support/models/project.rb +3 -0
  50. data/spec/support/models/user.rb +3 -0
  51. data/spec/support/terse_expect_syntax.rb +23 -0
  52. data/spec/unit/collation_spec.rb +35 -0
  53. data/spec/unit/definition_group_spec.rb +19 -0
  54. data/spec/unit/file_writer_spec.rb +28 -0
  55. data/spec/unit/finders/factory_spec.rb +27 -0
  56. data/spec/unit/finders/model_spec.rb +28 -0
  57. data/spec/unit/generator_spec.rb +71 -0
  58. data/spec/unit/parsing/file_spec.rb +83 -0
  59. data/tmp/.keep +0 -0
  60. data/to_factory.gemspec +49 -22
  61. metadata +225 -25
  62. data/.rvmrc +0 -1
  63. data/Gemfile.lock +0 -113
  64. data/lib/to_factory/generator.rb +0 -115
  65. data/spec/config/database.yml +0 -2
  66. data/spec/db/test.sqlite +0 -0
  67. data/spec/generator_spec.rb +0 -166
@@ -1,166 +0,0 @@
1
- require File.expand_path('spec/spec_helper')
2
-
3
- describe ToFactory::Generator do
4
- def options
5
- YAML::load File.read('spec/config/database.yml')
6
- end
7
-
8
- def connect
9
- AR::Base.establish_connection options rescue nil
10
- AR::Base.connection
11
- end
12
-
13
- before(:all) do
14
- ActiveRecord.tap do |AR|
15
- connect
16
- AR::Base.connection.drop_database options[:database] rescue nil
17
-
18
- begin
19
- # Create the SQLite database
20
- connect
21
- ActiveRecord::Base.connection
22
- rescue Exception => e
23
- $stderr.puts e, *(e.backtrace)
24
- $stderr.puts "Couldn't create database for #{config.inspect}"
25
- end
26
-
27
- AR::Migrator.migrate('db/migrate')
28
- class User < AR::Base; end
29
- end
30
- end
31
-
32
- before(:each) do
33
- User.destroy_all
34
- ActiveRecord::Base.connection.execute "delete from sqlite_sequence where name = 'users'"
35
- end
36
-
37
- context "with an active record class but no instance" do
38
- before do
39
- @generator = ToFactory::Generator.new User
40
- User.create :id => 1, :name => 'Tom', :email => 'blah@example.com', :some_id => 7
41
- User.create :id => 2, :name => 'James', :email => 'james@example.com', :some_id => 8
42
- end
43
-
44
- it "initializes" do
45
- @generator.model_class.should == User
46
- end
47
-
48
- let(:user_factory_1) do
49
- user_factory_1 = <<-eof
50
- Factory.define :user do |u|
51
- u.email "blah@example.com"
52
- u.name "Tom"
53
- u.some_id 7
54
- end
55
- eof
56
- user_factory_1.chop
57
- end
58
- let(:user_factory_2) do
59
- user_factory_2 = <<-eof
60
- Factory.define :user do |u|
61
- u.email "james@example.com"
62
- u.name "James"
63
- u.some_id 8
64
- end
65
- eof
66
- user_factory_2.chop
67
- end
68
- context "looking up attributes" do
69
- it "takes a key value for id" do
70
- out = @generator.factory_for :id => 1
71
- out.should == user_factory_1
72
- end
73
-
74
- it "takes an integer for id" do
75
- out = @generator.factory_for 1
76
- out.should == user_factory_1
77
- end
78
-
79
- it "takes a key value and does a lookup" do
80
- out = @generator.factory_for :name => 'Tom'
81
- out.should == user_factory_1
82
-
83
- out = @generator.factory_for :name => 'James'
84
- out.should == user_factory_2
85
- end
86
-
87
- it "takes multiple keys and does a lookup" do
88
- out = @generator.factory_for :id => 1, :name => 'Tom'
89
- end
90
-
91
- context "with invalid attributes" do
92
- it "raises an exception" do
93
- lambda{@generator.factory_for :id => 1000}.should raise_error ActiveRecord::RecordNotFound
94
- end
95
- end
96
- end
97
- end
98
-
99
- it "initializes with an activerecord instance" do
100
- user = User.create :name => "Jeff"
101
- @generator = ToFactory::Generator.new user
102
- @generator.model_class.should == "User"
103
- end
104
-
105
- it "initializes without an object" do
106
- @generator = ToFactory::Generator.new
107
- @generator.model_class.should be_nil
108
- end
109
-
110
- it "generates the first line of the factory" do
111
- @generator = ToFactory::Generator.new User
112
- f = @generator.factory
113
- output = <<-eof
114
- Factory.define :user do |u|
115
- end
116
- eof
117
- f.should == output.chop
118
- end
119
-
120
-
121
- it "raises an exception without an AR object, when requesting attributes" do
122
- @generator = ToFactory::Generator.new User
123
- lambda{@generator.factory_attribute :foo}.should raise_error ToFactory::MissingActiveRecordInstanceException
124
- end
125
-
126
- context "with a user in the database" do
127
- before do
128
- User.create :name => "Jeff", :email => "test@example.com", :some_id => 8
129
- @generator = ToFactory::Generator.new user
130
- end
131
-
132
- let(:user){ User.first}
133
-
134
- it "generates lines for multiple the attributes" do
135
- @generator.factory_attribute(:name). should == ' u.name "Jeff"'
136
- @generator.factory_attribute(:email).should == ' u.email "test@example.com"'
137
- end
138
-
139
- let(:expected) do
140
- expected = <<-eof
141
- Factory.define :user do |u|
142
- u.email "test@example.com"
143
- u.name "Jeff"
144
- u.some_id 8
145
- end
146
- eof
147
- expected.chop
148
- end
149
-
150
- it "generates the full factory" do
151
- @generator.factory_with_attributes.should == expected
152
- end
153
-
154
- it "adds the to_factory method to an active record object" do
155
- ActiveRecord::Base.send :include, ToFactory
156
- user.to_factory.should == expected
157
- end
158
-
159
- it "raises an error if you try to inlude in a non ActiveRecord object" do
160
- class Egg;end
161
-
162
- lambda{Egg.send :include, ToFactory}.should raise_error ToFactory::MustBeActiveRecordSubClassException
163
- end
164
- end
165
-
166
- end