Skip to content

Commit 52bd555

Browse files
committed
Add tests replicating the issue with custom types
1 parent a19be89 commit 52bd555

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
@@ -178,6 +178,22 @@ def self.[](input)
178178
])
179179
end
180180

181+
context "with json notes" do
182+
include_context "json_notes"
183+
184+
before do
185+
conf.commands(:json_notes) do
186+
define(:create)
187+
end
188+
end
189+
190+
it "writes and reads back custom type" do
191+
json_notes = commands[:json_notes]
192+
193+
expect(json_notes[:create].call(note: "this is my note")).to eq([{id: 1, note: "this is my note"}])
194+
end
195+
end
196+
181197
it "re-raises not-null constraint violation error" do
182198
expect {
183199
create_user.call(name: nil)

spec/integration/commands/update_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ def by_name(name)
109109

110110
expect(result).to eq([{id: 1, name: "Josie"}, {id: 2, name: "Josie"}])
111111
end
112+
113+
context "with json notes" do
114+
include_context "json_notes"
115+
116+
before do
117+
conf.commands(:json_notes) do
118+
define(:update)
119+
end
120+
end
121+
122+
let(:json_notes) { container.relations[:json_notes] }
123+
124+
it "writes and reads back custom type" do
125+
note_id = conn[:json_notes].insert(note: "note version 1")
126+
result = json_notes.by_pk(note_id).command(:update).call(note: "note version 2")
127+
128+
expect(result).to eq([{id: 1, note: "note version 2"}])
129+
end
130+
end
112131
end
113132
end
114133
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)