Unit test of sequel column value

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-value-tp21001982p21001982.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

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-value-tp21001982p21002533.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

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

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

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-value-tp21001982p21005931.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email