Skip to content

Instantly share code, notes, and snippets.

@v-kolesnikov
Created December 6, 2018 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v-kolesnikov/2c9bb5bce05990ee66c88425dc96b5f1 to your computer and use it in GitHub Desktop.
Save v-kolesnikov/2c9bb5bce05990ee66c88425dc96b5f1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
gem 'dry-types', '0.13.3'
gem 'mysql2'
gem 'rom-repository', '2.0.2', require: false
gem 'rom-sql', '2.5.0', require: false
gem 'pry-byebug'
gem 'rspec'
end
require 'pry'
require 'rspec'
require 'rspec/autorun'
require 'dry-types'
require 'dry/types/compat/int'
require 'mysql2'
require 'rom-core'
require 'rom-repository'
require 'rom-sql'
ROM::SQL.load_extensions :mysql
RSpec.configure do |config|
config.color = true
config.formatter = :documentation
end
RSpec.describe 'insert tuple without primary key' do
let(:uri) { 'mysql2://root@127.0.0.1/rom_tests' }
let(:conn) { Sequel.connect(uri) }
let(:conf) { ROM::Configuration.new(:sql, conn) }
let(:container) { ROM.container(conf) }
let(:users) { container.relations[:users] }
before do
conf.relation(:users) do
schema do
use :timestamps
attribute :id, ROM::SQL::Types::Serial
attribute :uuid, ROM::SQL::Types::String
attribute :email, ROM::SQL::Types::String
attribute :password_hash, ROM::SQL::Types::String
timestamps :created_at, :updated_at
end
end
end
context do
subject(:command) do
users.command(:create).(
uuid: '1',
email: 'foo',
password_hash: 'bar',
created_at: Time.now,
updated_at: Time.now
)
end
it 'creates user directly in relation' do
expect { command }.not_to raise_error
end
end
context 'with a repository' do
subject(:command) do
repo.create(
uuid: '1',
email: 'foo',
password_hash: 'bar'
)
end
let(:repo) do
Class.new(ROM::Repository::Root[:users]) do
commands :create,
use: :timestamps,
plugins_options: {
timestamps: { timestamps: %i[created_at updated_at] }
}
end.new(container)
end
it 'creates user through a repository' do
expect { command }.not_to raise_error
end
end
end
λ ./id_in_schema_spec.rb
Resolving dependencies...
Using bundler 1.17.1
Using byebug 10.0.2
Using coderay 1.1.2
Using concurrent-ruby 1.1.3
Using diff-lcs 1.3
Using dry-configurable 0.7.0
Using dry-container 0.6.0
Using dry-core 0.4.7
Using dry-equalizer 0.2.1
Using dry-inflector 0.1.2
Using dry-initializer 2.5.0
Using dry-logic 0.4.2
Using dry-types 0.13.3
Using ice_nine 0.11.2
Using dry-struct 0.6.0
Using method_source 0.9.2
Using mysql2 0.5.2
Using pry 0.12.2
Using pry-byebug 3.6.0
Using transproc 1.0.3
Using rom-mapper 1.2.0
Using rom-core 4.2.0
Using rom-repository 2.0.2
Using sequel 5.15.0
Using rom-sql 2.5.0
Using rspec-support 3.8.0
Using rspec-core 3.8.0
Using rspec-expectations 3.8.2
Using rspec-mocks 3.8.0
Using rspec 3.8.0
[dry-types] Int type was renamed to Integer
insert tuple without primary key
creates user directly in relation (FAILED - 1)
with a repository
creates user through a repository (FAILED - 2)
Failures:
1) insert tuple without primary key creates user directly in relation
Failure/Error: expect { command }.not_to raise_error
expected no Exception, got #<Dry::Types::MissingKeyError: :id is missing in Hash input> with backtrace:
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:164:in `block in resolve_missing_keys'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:158:in `each'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:158:in `resolve_missing_keys'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:151:in `resolve'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:186:in `coerce'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:42:in `call'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/constructor.rb:54:in `call'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:24:in `block in execute'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:64:in `block in with_input_tuples'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:64:in `map'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:64:in `each'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:64:in `with_input_tuples'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:23:in `execute'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-core-4.2.0/lib/rom/command.rb:280:in `call'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/error_wrapper.rb:16:in `call'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-core-4.2.0/lib/rom/commands/composite.rb:17:in `call'
# ./id_in_schema_spec.rb:60:in `block (3 levels) in <main>'
# ./id_in_schema_spec.rb:70:in `block (4 levels) in <main>'
# ./id_in_schema_spec.rb:70:in `block (3 levels) in <main>'
# ./id_in_schema_spec.rb:70:in `block (3 levels) in <main>'
2) insert tuple without primary key with a repository creates user through a repository
Failure/Error: expect { command }.not_to raise_error
expected no Exception, got #<Dry::Types::MissingKeyError: :id is missing in Hash input> with backtrace:
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:164:in `block in resolve_missing_keys'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:158:in `each'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:158:in `resolve_missing_keys'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:151:in `resolve'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:186:in `coerce'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/hash/schema.rb:42:in `call'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/dry-types-0.13.3/lib/dry/types/constructor.rb:54:in `call'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:24:in `block in execute'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:64:in `block in with_input_tuples'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:64:in `map'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:64:in `each'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:64:in `with_input_tuples'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:23:in `execute'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-core-4.2.0/lib/rom/command.rb:280:in `call'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-sql-2.5.0/lib/rom/sql/commands/error_wrapper.rb:16:in `call'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-core-4.2.0/lib/rom/commands/composite.rb:17:in `call'
# /Users/v.kolesnikov/.rvm/gems/ruby-2.5.3/gems/rom-repository-2.0.2/lib/rom/repository/class_interface.rb:111:in `block in define_command_method'
# ./id_in_schema_spec.rb:76:in `block (3 levels) in <main>'
# ./id_in_schema_spec.rb:94:in `block (4 levels) in <main>'
# ./id_in_schema_spec.rb:94:in `block (3 levels) in <main>'
# ./id_in_schema_spec.rb:94:in `block (3 levels) in <main>'
Finished in 0.04034 seconds (files took 0.27704 seconds to load)
2 examples, 2 failures
Failed examples:
rspec ./id_in_schema_spec.rb:69 # insert tuple without primary key creates user directly in relation
rspec ./id_in_schema_spec.rb:93 # insert tuple without primary key with a repository creates user through a repository
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment