Forum: JRuby Unit test of sequel column value

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
nabblee (Guest)
on 2008-12-14 18:42
(Received via mailing list)
I'm trying to do a simple assert of the value of a column returned from
MySQL
using the sequel gem.  However, the type is an Array, rather than a
String.

Code snippet:
    cur_user = @db[:users].where(:username => 'test1', :password =>
'test1')
    puts "Phone = [#{cur_user.map(:phone)}]"
    puts "Class = #{cur_user.map(:phone).class}"
    puts "not equal" if cur_user.map(:phone) != '9995551212'
    puts "equal" if cur_user.map(:phone) == '9995551212'
    assert(cur_user.map(:phone) == '9995551212')

Output:
Phone = [9995551212]
Class = Array
not equal

1) Failure:

Why is the content of the phone column considered an Array?  What is the
proper syntax to make the assert succeed?

Thank you!
--
View this message in context:
http://www.nabble.com/Unit-test-of-sequel-column-v...
Sent from the JRuby - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
nabblee (Guest)
on 2008-12-14 19:40
(Received via mailing list)
I found that cur_user.map(:phone).to_s works, but I don't understand why
the
default type of a varchar column comes back as an Array.  Any insight
would
be appreciated.



nabblee wrote:
>     puts "not equal" if cur_user.map(:phone) != '9995551212'
> Why is the content of the phone column considered an Array?  What is the
> proper syntax to make the assert succeed?
>
> Thank you!
>

--
View this message in context:
http://www.nabble.com/Unit-test-of-sequel-column-v...
Sent from the JRuby - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Joseph A. (Guest)
on 2008-12-14 21:46
(Received via mailing list)
I've never used the sequel gem, I think your problem here is that
this:@db[:users].where(:username
=> 'test1', :password => 'test1')

returns an array of users, not just a single user object.  Also, calling
the
map method on an array will return another array, not a single value.
So
you are trying to compare of values to a single value.  Here's my hunch
how
you want your test to look:

   cur_user = @db[:users].where(:username => 'test1', :password =>
'test1').first
   puts "Phone = [#{cur_user[:phone]}]"
   puts "Class = #{cur_user[:phone].class}"
   puts "not equal" if cur_user[:phone] != '9995551212'
   puts "equal" if cur_user:phone] == '9995551212'
   assert(cur_user[:phone] == '9995551212')

This way you are getting the first record from the original query, so
now
cur_user is a single object, not an array.  And if I read the sequel
documentation right, you can reference individual columns like a hash or
something.  So you use cur_user[:phone] to get the phone column from the
record.  Good luck.

Joe
nabblee (Guest)
on 2008-12-15 01:15
(Received via mailing list)
I'm with you, Joe.  My first inclination was cur_user[:phone], but that
doesn't work at all.  You have to use the map method to gain access to
the
columns.

The first line fails, but the second one succeeds:

    assert(cur_user[:phone] == '9995551212')
    assert(cur_user.map(:phone).to_s == '9995551212')

I think the principle of least surprise has been violated.  :-)

Thanks,
Lee



Joseph A. wrote:
> you want your test to look:
> cur_user is a single object, not an array.  And if I read the sequel
>> the
>> a
>> >
>> >
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>

--
View this message in context:
http://www.nabble.com/Unit-test-of-sequel-column-v...
Sent from the JRuby - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
This topic is locked and can not be replied to.