On Thursday, March 10, 2016 at 11:50:56 AM UTC-8, Denny Wang wrote:
>
> Thanks for looking into this quick.
>
> Yes this works. However, if I change fetch to other table name, it will 
> not.
>
> $sequel -E
> Your database is stored in DB...
> irb(main):001:0> DB = Sequel.mock(:a_table=>[{:id=>1, :name=>'name'}])
> => #<Sequel::Mock::Database: {:adapter=>:mock, :a_table=>[{:id=>1, 
> :name=>"name"}]}>
> irb(main):002:0> class FetchSomething < Sequel::Model(:a_table); end
> I, [2016-03-10T11:48:19.944334 #6292]  INFO -- : SELECT * FROM a_table 
> LIMIT 1
> I, [2016-03-10T11:48:19.945511 #6292]  INFO -- : SELECT * FROM a_table 
> LIMIT 1
> => nil
> irb(main):003:0> fetchedData = FetchSomething.first
> I, [2016-03-10T11:48:45.673094 #6292]  INFO -- : SELECT * FROM a_table 
> LIMIT 1
> => nil
> irb(main):005:0> fetchedData = FetchSomething[:name => 'name']
> I, [2016-03-10T11:49:19.438946 #6292]  INFO -- : SELECT * FROM a_table 
> WHERE (name = 'name') LIMIT 1
> => nil
> irb(main):006:0>
>

You are making two errors here.  The first is that you are overriding the 
DB object on line 1.  That creates a new Sequel::Database object, but the 
previously existing Sequel::Database object will still be the default 
database for model classes.  Ruby should have printed a warning in this 
case:

  (irb):1: warning: already initialized constant DB

I'm guessing that you aren't posting exactly the same code you used, 
otherwise the warning would have been included.

Second, appear to be misunderstanding the mock adapter API.  You need to 
use a :fetch key when creating the Database to set the data that will be 
returned by default for all datasets, or use Database#fetch= to set the 
data after creating the Database.  If you want to override what will be 
returned for a specific dataset, you can use Dataset#_fetch=:

  FetchSomething.dataset._fetch = [{:id=>1, :name=>'name'}]

Thanks,
Jeremy
 

-- 
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.

Reply via email to