Sharing this here after a brief discussion on the sequel issue tracker
<https://github.com/jeremyevans/sequel/issues/1293>.
It can be very confusing to test code that involves blobs because it is
very unclear whether you have a bug or one of your values is secretly a
Sequel::SQL::Blob type. This ended up being a problem for me so many times
that I ended up adding this to my spec helper
class Sequel::SQL::Blob
def inspect
"#<#{Sequel::SQL::Blob} #{super}>"
endend
and I view this as a usability bug which is why I'm reporting here and not
on the mailing list. I'll give an example below
Simplest Possible Self-Contained Example Showing the Problem
Sample DB
createdb exampledb
CREATE TABLE things (
id integer,
data bytea
);
Example confusing test
# frozen_string_literal: truerequire 'sequel'DB =
Sequel.connect('postgres://localhost/exampledb')
require 'rspec'
RSpec.describe 'confusing failure' do
it 'should match what was inserted' do
data = "\x7a\x85\x99\xa6\x38\x7d\x1b\x69"
DB[:things].insert(id: 1, data: Sequel.blob(data))
expect(DB[:things].first).to eq(
id: 1,
data: data
)
endend
Output
$ rspec example.rb
F
Failures:
1) confusing failure should match what was inserted
Failure/Error:
expect(DB[:things].first).to eq(
id: 1,
data: data
)
expected: {:id=>1, :data=>"z\x85\x99\xA68}\ei"}
got: {:id=>1, :data=>"z\x85\x99\xA68}\ei"}
(compared using ==)
# ./example.rb:13:in `block (2 levels) in <top (required)>'
Finished in 0.0526 seconds (files took 0.64033 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./example.rb:9 # confusing failure should match what was inserted
Now in this case the inserted record is pretty simple so it would only take
a few seconds to be able to tell that the data is exactly the same. In my
case though (and I think this is similar to the average use case) the blob
data is much bigger and it is almost impossible to be able to tell by
eyeballing the output whether the bytea fields involved in the failure
output are actually the same values.
Applying the patch to the specs I mentioned above makes this a non issue:
$ rspec example.rb
F
Failures:
1) confusing failure should match what was inserted
Failure/Error:
expect(DB[:things].first).to eq(
id: 1,
data: data
)
expected: {:id=>1, :data=>"z\x85\x99\xA68}\ei"}
got: {:id=>1, :data=>#<Sequel::SQL::Blob "z\x85\x99\xA68}\ei">}
(compared using ==)
Diff:
@@ -1,3 +1,3 @@
-:data => "z\x85\x99\xA68}\ei",
+:data => #<Sequel::SQL::Blob "z\x85\x99\xA68}\ei">,
:id => 1,
# ./example.rb:19:in `block (2 levels) in <top (required)>'
Finished in 0.0504 seconds (files took 0.57833 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./example.rb:15 # confusing failure should match what was inserted
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.