sprig 0.1.6 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +61 -10
  3. data/lib/generators/sprig/install_generator.rb +5 -1
  4. data/lib/sprig.rb +21 -0
  5. data/lib/sprig/configuration.rb +19 -2
  6. data/lib/sprig/directive.rb +3 -3
  7. data/lib/sprig/helpers.rb +15 -3
  8. data/lib/sprig/planter.rb +1 -1
  9. data/lib/sprig/process_notifier.rb +4 -4
  10. data/lib/sprig/seed/attribute.rb +27 -3
  11. data/lib/sprig/seed/entry.rb +8 -4
  12. data/lib/sprig/seed/factory.rb +1 -1
  13. data/lib/sprig/source.rb +10 -1
  14. data/lib/sprig/version.rb +8 -1
  15. data/spec/adapters/active_record.rb +34 -0
  16. data/spec/adapters/mongoid.rb +19 -0
  17. data/spec/adapters/mongoid.yml +12 -0
  18. data/spec/feature/configurations_spec.rb +5 -5
  19. data/spec/fixtures/models/{comment.rb → active_record/comment.rb} +0 -0
  20. data/spec/fixtures/models/{post.rb → active_record/post.rb} +0 -0
  21. data/spec/fixtures/models/{tag.rb → active_record/tag.rb} +0 -0
  22. data/spec/fixtures/models/{user.rb → active_record/user.rb} +0 -0
  23. data/spec/fixtures/models/mongoid/comment.rb +9 -0
  24. data/spec/fixtures/models/mongoid/post.rb +18 -0
  25. data/spec/fixtures/models/mongoid/tag.rb +7 -0
  26. data/spec/fixtures/models/mongoid/user.rb +9 -0
  27. data/spec/fixtures/seeds/shared/comments.yml +4 -0
  28. data/spec/fixtures/seeds/shared/files/cat.png +0 -0
  29. data/spec/fixtures/seeds/shared/invalid_users.yml +4 -0
  30. data/spec/fixtures/seeds/shared/legacy_posts.yml +4 -0
  31. data/spec/fixtures/seeds/shared/posts.csv +2 -0
  32. data/spec/fixtures/seeds/shared/posts.json +1 -0
  33. data/spec/fixtures/seeds/shared/posts.md +5 -0
  34. data/spec/fixtures/seeds/shared/posts.yml +4 -0
  35. data/spec/fixtures/seeds/shared/posts_delete_existing_by.yml +7 -0
  36. data/spec/fixtures/seeds/shared/posts_find_existing_by_missing.yml +7 -0
  37. data/spec/fixtures/seeds/shared/posts_find_existing_by_multiple.yml +8 -0
  38. data/spec/fixtures/seeds/shared/posts_find_existing_by_single.yml +7 -0
  39. data/spec/fixtures/seeds/shared/posts_missing_dependency.yml +5 -0
  40. data/spec/fixtures/seeds/shared/posts_missing_record.yml +5 -0
  41. data/spec/fixtures/seeds/shared/posts_partially_dynamic_value.yml +4 -0
  42. data/spec/fixtures/seeds/shared/posts_with_cyclic_dependencies.yml +4 -0
  43. data/spec/fixtures/seeds/shared/posts_with_files.yml +5 -0
  44. data/spec/fixtures/seeds/shared/posts_with_habtm.yml +7 -0
  45. data/spec/fixtures/seeds/shared/tags.yml +5 -0
  46. data/spec/fixtures/seeds/test/posts_partially_dynamic_value.yml +4 -0
  47. data/spec/lib/generators/sprig/install_generator_spec.rb +6 -2
  48. data/spec/lib/sprig/configuration_spec.rb +6 -6
  49. data/spec/lib/sprig/directive_list_spec.rb +4 -4
  50. data/spec/lib/sprig/directive_spec.rb +21 -11
  51. data/spec/lib/sprig/null_record_spec.rb +2 -2
  52. data/spec/lib/sprig/parser/base_spec.rb +1 -1
  53. data/spec/lib/sprig/process_notifier_spec.rb +5 -3
  54. data/spec/lib/sprig/seed/entry_spec.rb +3 -3
  55. data/spec/lib/sprig/seed/record_spec.rb +3 -3
  56. data/spec/lib/sprig/source_spec.rb +2 -2
  57. data/spec/lib/sprig_spec.rb +59 -8
  58. data/spec/spec_helper.rb +35 -31
  59. data/spec/sprig_shared_spec.rb +467 -0
  60. data/spec/sprig_spec.rb +105 -34
  61. data/spec/support/helpers/logger_mock.rb +2 -1
  62. metadata +167 -87
  63. data/lib/sprig/data.rb +0 -6
  64. data/spec/db/activerecord.db +0 -0
@@ -0,0 +1,34 @@
1
+ require "database_cleaner"
2
+
3
+ RSpec.configure do |c|
4
+ c.before(:suite) do
5
+ DatabaseCleaner[:active_record].strategy = :transaction
6
+ DatabaseCleaner[:active_record].clean_with(:truncation)
7
+ end
8
+
9
+ c.before(:each) do
10
+ DatabaseCleaner[:active_record].start
11
+ end
12
+
13
+ c.after(:each) do
14
+ DatabaseCleaner[:active_record].clean
15
+ end
16
+ end
17
+
18
+ # Database
19
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "spec/db/activerecord.db")
20
+
21
+ User.connection.execute "DROP TABLE IF EXISTS users;"
22
+ User.connection.execute "CREATE TABLE users (id INTEGER PRIMARY KEY , first_name VARCHAR(255), last_name VARCHAR(255), type VARCHAR(255));"
23
+
24
+ Post.connection.execute "DROP TABLE IF EXISTS posts;"
25
+ Post.connection.execute "CREATE TABLE posts (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(255), content VARCHAR(255), photo VARCHAR(255), published BOOLEAN , user_id INTEGER);"
26
+
27
+ Comment.connection.execute "DROP TABLE IF EXISTS comments;"
28
+ Comment.connection.execute "CREATE TABLE comments (id INTEGER PRIMARY KEY , post_id INTEGER, body VARCHAR(255));"
29
+
30
+ Tag.connection.execute "DROP TABLE IF EXISTS tags;"
31
+ Tag.connection.execute "CREATE TABLE tags (id INTEGER PRIMARY KEY , name VARCHAR(255));"
32
+
33
+ Tag.connection.execute "DROP TABLE IF EXISTS posts_tags;"
34
+ Tag.connection.execute "CREATE TABLE posts_tags (id INTEGER PRIMARY KEY , post_id INTEGER, tag_id INTEGER);"
@@ -0,0 +1,19 @@
1
+ require "database_cleaner"
2
+
3
+ RSpec.configure do |c|
4
+ c.before(:suite) do
5
+ DatabaseCleaner[:mongoid].strategy = :truncation
6
+ DatabaseCleaner[:mongoid].clean_with(:truncation)
7
+ end
8
+
9
+ c.before(:each) do
10
+ DatabaseCleaner[:mongoid].start
11
+ end
12
+
13
+ c.after(:each) do
14
+ DatabaseCleaner[:mongoid].clean
15
+ end
16
+ end
17
+
18
+ # Datastore
19
+ Mongoid.load!(File.join File.dirname(__FILE__), 'mongoid.yml')
@@ -0,0 +1,12 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: sprig_test
5
+ hosts:
6
+ - localhost:27017
7
+ options:
8
+ read: primary
9
+ # In the test environment we lower the retries and retry interval to
10
+ # low amounts for fast failures.
11
+ max_retries: 1
12
+ retry_interval: 0
@@ -1,19 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "Configurating Sprig" do
3
+ RSpec.describe "Configurating Sprig" do
4
4
  before do
5
5
  stub_rails_root '~'
6
6
  stub_rails_env 'development'
7
7
  end
8
8
 
9
9
  it "can set the directory" do
10
- Sprig.configuration.directory.to_path.should == '~/db/seeds/development'
10
+ expect(Sprig.configuration.directory.to_path).to eq('~/db/seeds/development')
11
11
 
12
12
  Sprig.configure do |c|
13
13
  c.directory = 'seed_files'
14
14
  end
15
15
 
16
- Sprig.configuration.directory.to_path.should == '~/seed_files/development'
16
+ expect(Sprig.configuration.directory.to_path).to eq('~/seed_files/development')
17
17
  end
18
18
 
19
19
  it "can reset the configuration" do
@@ -21,10 +21,10 @@ describe "Configurating Sprig" do
21
21
  c.directory = 'seed_files'
22
22
  end
23
23
 
24
- Sprig.configuration.directory.to_path.should == '~/seed_files/development'
24
+ expect(Sprig.configuration.directory.to_path).to eq('~/seed_files/development')
25
25
 
26
26
  Sprig.reset_configuration
27
27
 
28
- Sprig.configuration.directory.to_path.should == '~/db/seeds/development'
28
+ expect(Sprig.configuration.directory.to_path).to eq('~/db/seeds/development')
29
29
  end
30
30
  end
@@ -0,0 +1,9 @@
1
+ class Comment
2
+ include Mongoid::Document
3
+
4
+ field :body, :type => String
5
+
6
+ belongs_to :post
7
+
8
+ validates :post, :presence => true
9
+ end
@@ -0,0 +1,18 @@
1
+ class Post
2
+ include Mongoid::Document
3
+
4
+ if Mongoid::VERSION.split('.').first == "3"
5
+ field :user_id, :type => Moped::BSON::ObjectId
6
+ else
7
+ field :user_id, :type => BSON::ObjectId
8
+ end
9
+ field :title, :type => String
10
+ field :content, :type => String
11
+ field :published, :type => Boolean
12
+
13
+ has_and_belongs_to_many :tags
14
+
15
+ def photo=(file)
16
+ write_attribute(:photo, File.basename(file.path))
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ class Tag
2
+ include Mongoid::Document
3
+
4
+ field :name, :type => String
5
+
6
+ validates :name, :presence => true
7
+ end
@@ -0,0 +1,9 @@
1
+ class User
2
+ include Mongoid::Document
3
+
4
+ field :first_name, :type => String
5
+ field :last_name, :type => String
6
+ field :type, :type => String
7
+
8
+ validates :first_name, :last_name, :presence => true
9
+ end
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ post_id: "<%= sprig_record(Post, 1).id %>"
4
+ body: "Yaml Comment body"
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ first_name: "Clark"
4
+ # intentionally leaving out last_name so record fails to create
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 'l_1'
3
+ title: 'Legacy yaml title'
4
+ content: 'Legacy yaml content'
@@ -0,0 +1,2 @@
1
+ sprig_id,title,content
2
+ 1,Csv title,Csv content
@@ -0,0 +1 @@
1
+ {"records":[{"sprig_id":1,"title":"Json title","content":"Json content"}]}
@@ -0,0 +1,5 @@
1
+ # Posts
2
+
3
+ - Post 1
4
+ - Post 2
5
+ - Post 3
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Yaml title'
4
+ content: 'Yaml content'
@@ -0,0 +1,7 @@
1
+ options:
2
+ delete_existing_by: 'title'
3
+
4
+ records:
5
+ - sprig_id: 1
6
+ title: 'Such Title'
7
+ content: 'New content'
@@ -0,0 +1,7 @@
1
+ options:
2
+ find_existing_by: 'unicorn'
3
+
4
+ records:
5
+ - sprig_id: 1
6
+ title: 'Existing title'
7
+ content: 'Updated content'
@@ -0,0 +1,8 @@
1
+ options:
2
+ find_existing_by: ['title', 'content']
3
+
4
+ records:
5
+ - sprig_id: 1
6
+ title: 'Existing title'
7
+ content: 'Existing content'
8
+ published: true
@@ -0,0 +1,7 @@
1
+ options:
2
+ find_existing_by: 'title'
3
+
4
+ records:
5
+ - sprig_id: 1
6
+ title: 'Existing title'
7
+ content: 'Updated content'
@@ -0,0 +1,5 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Yaml title'
4
+ content: 'Yaml content'
5
+ comment_id: "<%= sprig_record(Comment, 42).id %>" # This sprig record does not exist.
@@ -0,0 +1,5 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Yaml title'
4
+ content: 'Yaml content'
5
+ user_id: "<%= sprig_record(User, 1).id %>" # This sprig record did not save.
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: '<%= "Partially" %> <%= "Dynamic" %> Title'
4
+ content: 'Yaml content'
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: "Title"
4
+ content: "<%= sprig_record(Comment, 1) %>"
@@ -0,0 +1,5 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Staging yaml title'
4
+ content: 'Staging yaml content'
5
+ photo: '<%= sprig_file("cat.png") %>'
@@ -0,0 +1,7 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: 'Yaml title'
4
+ content: 'Yaml content'
5
+ tag_ids:
6
+ - '<%= sprig_record(Tag, 1).id %>'
7
+ - '<%= sprig_record(Tag, 2).id %>'
@@ -0,0 +1,5 @@
1
+ records:
2
+ - sprig_id: 1
3
+ name: 'Botany'
4
+ - sprig_id: 2
5
+ name: 'Biology'
@@ -0,0 +1,4 @@
1
+ records:
2
+ - sprig_id: 1
3
+ title: '<%= "Partially" %> <%= "Dynamic" %> Title'
4
+ content: 'Yaml content'
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'generators/sprig/install_generator'
3
3
 
4
- describe Sprig::Generators::InstallGenerator do
4
+ RSpec.describe Sprig::Generators::InstallGenerator, type: :generator do
5
5
  destination File.expand_path("../../tmp", __FILE__)
6
6
 
7
7
  before do
@@ -14,6 +14,10 @@ describe Sprig::Generators::InstallGenerator do
14
14
  assert_directory "db/seeds"
15
15
  end
16
16
 
17
+ it "creates empty shared directory" do
18
+ assert_directory "db/seeds/shared"
19
+ end
20
+
17
21
  it "creates empty seed environment directories" do
18
22
  [
19
23
  :development,
@@ -29,7 +33,7 @@ end
29
33
  # Generator arguments are set on a class basis. We need to open
30
34
  # a new describe block to make these examples work.
31
35
 
32
- describe Sprig::Generators::InstallGenerator do
36
+ RSpec.describe Sprig::Generators::InstallGenerator, type: :generator do
33
37
  context "with arguments" do
34
38
  destination File.expand_path("../../tmp", __FILE__)
35
39
  arguments %w(development test integration)
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Sprig::Configuration do
3
+ RSpec.describe Sprig::Configuration do
4
4
  before do
5
5
  stub_rails_root '~'
6
6
  stub_rails_env 'development'
@@ -8,29 +8,29 @@ describe Sprig::Configuration do
8
8
 
9
9
  describe "#directory" do
10
10
  it "returns db/seeds/:env by default" do
11
- subject.directory.to_path.should == '~/db/seeds/development'
11
+ expect(subject.directory.to_path).to eq('~/db/seeds/development')
12
12
  end
13
13
 
14
14
  it "returns a custom directory" do
15
15
  subject.directory = 'seed_files'
16
16
 
17
- subject.directory.to_path.should == '~/seed_files/development'
17
+ expect(subject.directory.to_path).to eq('~/seed_files/development')
18
18
  end
19
19
  end
20
20
 
21
21
  describe "#logger" do
22
22
  it "returns an stdout logger by default" do
23
23
  logger = double('Logger')
24
- Logger.stub(:new).with($stdout).and_return(logger)
24
+ allow(Logger).to receive(:new).with($stdout).and_return(logger)
25
25
 
26
- subject.logger.should == logger
26
+ expect(subject.logger).to eq(logger)
27
27
  end
28
28
 
29
29
  it "returns a custom logger" do
30
30
  logger = double('Logger')
31
31
  subject.logger = logger
32
32
 
33
- subject.logger.should == logger
33
+ expect(subject.logger).to eq(logger)
34
34
  end
35
35
  end
36
36
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Sprig::DirectiveList do
3
+ RSpec.describe Sprig::DirectiveList do
4
4
 
5
5
  describe "#add_seeds_to_hopper" do
6
6
  let(:hopper) { Array.new }
@@ -10,13 +10,13 @@ describe Sprig::DirectiveList do
10
10
  subject { described_class.new(Post) }
11
11
 
12
12
  before do
13
- Sprig::Directive.stub(:new).with(Post).and_return(directive)
13
+ allow(Sprig::Directive).to receive(:new).with(Post).and_return(directive)
14
14
 
15
- Sprig::Seed::Factory.stub(:new_from_directive).with(directive).and_return(seed_factory)
15
+ allow(Sprig::Seed::Factory).to receive(:new_from_directive).with(directive).and_return(seed_factory)
16
16
  end
17
17
 
18
18
  it "builds seeds from directives and adds to the given array" do
19
- seed_factory.should_receive(:add_seeds_to_hopper).with(hopper)
19
+ expect(seed_factory).to receive(:add_seeds_to_hopper).with(hopper)
20
20
 
21
21
  subject.add_seeds_to_hopper(hopper)
22
22
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Sprig::Directive do
3
+ RSpec.describe Sprig::Directive do
4
4
 
5
5
  module Users
6
6
  class Admin < User
@@ -11,19 +11,25 @@ describe Sprig::Directive do
11
11
  context "given a class" do
12
12
  subject { described_class.new(Post) }
13
13
 
14
- its(:klass) { should == Post }
14
+ it "returns the class" do
15
+ expect(subject.klass).to eq(Post)
16
+ end
15
17
  end
16
18
 
17
19
  context "given a class within a module" do
18
20
  subject { described_class.new(Users::Admin) }
19
21
 
20
- its(:klass) { should == Users::Admin }
22
+ it "returns the full class" do
23
+ expect(subject.klass).to eq(Users::Admin)
24
+ end
21
25
  end
22
26
 
23
27
  context "given options with a class" do
24
28
  subject { described_class.new(:class => Post) }
25
29
 
26
- its(:klass) { should == Post }
30
+ it "returns the class" do
31
+ expect(subject.klass).to eq(Post)
32
+ end
27
33
  end
28
34
 
29
35
  context "given options without a class" do
@@ -34,8 +40,8 @@ describe Sprig::Directive do
34
40
  subject.klass
35
41
  }.to raise_error(
36
42
  ArgumentError,
37
- 'Sprig::Directive must be instantiated with an '\
38
- 'ActiveRecord subclass or a Hash with :class defined'
43
+ 'Sprig::Directive must be instantiated with a(n) '\
44
+ "#{Sprig.adapter_model_class} class or a Hash with :class defined"
39
45
  )
40
46
  end
41
47
  end
@@ -45,13 +51,17 @@ describe Sprig::Directive do
45
51
  context "given no options" do
46
52
  subject { described_class.new(Post) }
47
53
 
48
- its(:options) { should == {} }
54
+ it "returns an empty hash" do
55
+ expect(subject.options).to eq({})
56
+ end
49
57
  end
50
58
 
51
59
  context "given options" do
52
60
  subject { described_class.new(:class => Post, :source => 'source') }
53
61
 
54
- its(:options) { should == { :source => 'source' } }
62
+ it "returns a the options" do
63
+ expect(subject.options).to eq(:source => 'source')
64
+ end
55
65
  end
56
66
  end
57
67
 
@@ -62,11 +72,11 @@ describe Sprig::Directive do
62
72
  subject { described_class.new(:class => Post, :source => 'source') }
63
73
 
64
74
  before do
65
- Sprig::Source.stub(:new).with('posts', { :source => 'source' }).and_return(datasource)
75
+ allow(Sprig::Source).to receive(:new).with('posts', { :source => 'source' }).and_return(datasource)
66
76
  end
67
77
 
68
78
  it "returns a sprig data source" do
69
- subject.datasource.should == datasource
79
+ expect(subject.datasource).to eq(datasource)
70
80
  end
71
81
  end
72
82
 
@@ -74,7 +84,7 @@ describe Sprig::Directive do
74
84
  subject { described_class.new(Users::Admin) }
75
85
 
76
86
  it "passes the correct path to Source" do
77
- Sprig::Source.should_receive(:new).with("users_admins", {})
87
+ expect(Sprig::Source).to receive(:new).with("users_admins", {})
78
88
 
79
89
  subject.datasource
80
90
  end