Skip to content

Commit 577b9b6

Browse files
committed
Add tests replicating the issue with custom types
1 parent 3f91fb0 commit 577b9b6

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

spec/integration/commands/create_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,22 @@ def self.[](input)
187187
}.to raise_error(ROM::SQL::NotNullConstraintError)
188188
end
189189

190+
context 'with json notes' do
191+
include_context 'json_notes'
192+
193+
before do
194+
conf.commands(:json_notes) do
195+
define(:create)
196+
end
197+
end
198+
199+
it 'writes and reads back custom type' do
200+
json_notes = commands[:json_notes]
201+
202+
expect(json_notes[:create].call(note: 'this is my note')).to eq([{id: 1, note: 'this is my note'}])
203+
end
204+
end
205+
190206
# Because Oracle doesn't have boolean in SQL
191207
unless metadata[:oracle]
192208
context 'with puppies' do

spec/integration/commands/update_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ def by_name(name)
110110
{ id: 2, name: 'Josie' }
111111
])
112112
end
113+
114+
context "with json notes" do
115+
include_context "json_notes"
116+
117+
before do
118+
conf.commands(:json_notes) do
119+
define(:update)
120+
end
121+
end
122+
123+
let(:json_notes) { container.relations[:json_notes] }
124+
125+
it "writes and reads back custom type" do
126+
note_id = conn[:json_notes].insert(note: "note version 1")
127+
result = json_notes.by_pk(note_id).command(:update).call(note: "note version 2")
128+
129+
expect(result).to eq([{id: 1, note: "note version 2"}])
130+
end
131+
end
113132
end
114133
end
115134
end

spec/shared/json_notes.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.shared_context "json_notes" do
4+
before do
5+
inferrable_relations.concat %i[json_notes]
6+
end
7+
8+
before do |_example|
9+
conn.create_table :json_notes do
10+
primary_key :id
11+
String :note
12+
end
13+
14+
write_type = Dry.Types.Constructor(String) { |value| JSON.dump({content: value}) }
15+
read_type = Dry.Types.Constructor(String) { |value| JSON.parse(value)["content"] }
16+
17+
conf.relation(:json_notes) do
18+
schema(infer: true) do
19+
attribute :note, write_type, read: read_type
20+
end
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)