Hi, I have an Image model. The images table contains stuff like content_type, file_name, size, and data. data is a blob that contains the image. I want to unit test my Image class. What''s the best way to do this? Currently, I''m doing something like # in test/fixtures/images.yml some_image: id: 1 # in test/unit/image.rb def setup image = File.read("test/fixtures/photos/image.jpg") @some_image.data = image @some_image.size = image.size @some_image.content_type = "image/jpeg" end Which is bad. I know I can put <%= ... %> into the YAML file, but I haven''t been able to correctly get an image to load. What do you people do for stuff like this?
On 9/6/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have an Image model. The images table contains stuff like > content_type, file_name, size, and data. data is a blob that contains > the image. > > I want to unit test my Image class. What''s the best way to do this? > > Currently, I''m doing something like > # in test/fixtures/images.yml > some_image: > id: 1 > > # in test/unit/image.rb > def setup > image = File.read("test/fixtures/photos/image.jpg") > @some_image.data = image > @some_image.size = image.size > @some_image.content_type = "image/jpeg" > end > > Which is bad. I know I can put <%= ... %> into the YAML file, but I > haven''t been able to correctly get an image to load. > > What do you people do for stuff like this?No one? I could''ve sworn I saw a way to embed file stuff into a YAML file.
Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> I have an Image model. The images table contains stuff like > content_type, file_name, size, and data. data is a blob that contains > the image. > > I want to unit test my Image class. What''s the best way to do this?I''ll preface this by saying I store images on the filesystem. That said, I use the helper method from the wiki in my test_helper.rb. class Test::Unit::TestCase def uploaded_file(path, content_type, filename) t = Tempfile.new(filename); FileUtils.copy_file(path, t.path) (class << t; self; end).class_eval do alias local_path path define_method(:original_filename) {filename} define_method(:content_type) {content_type} end return t end end I use that both in my unit tests and in my functional tests to take a file off my filesystem and convert it into the format that my app will see from CGI. I check an image into CVS and then reference that from the tests files. So, I''m not storing the image as a blob in the fixture; but I am testing my classes (both models and controllers) with data similar to what I expect to see. Oh, by the way, you should test with both a smallish image and a large image as CGI handles them slightly differently. -- Doug Alcorn doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
On 9/7/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/6/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, > > > > I have an Image model. The images table contains stuff like > > content_type, file_name, size, and data. data is a blob that contains > > the image. > > > > I want to unit test my Image class. What''s the best way to do this? > > > > Currently, I''m doing something like > > # in test/fixtures/images.yml > > some_image: > > id: 1 > > > > # in test/unit/image.rb > > def setup > > image = File.read("test/fixtures/photos/image.jpg") > > @some_image.data = image > > @some_image.size = image.size > > @some_image.content_type = "image/jpeg" > > end > > > > Which is bad. I know I can put <%= ... %> into the YAML file, but I > > haven''t been able to correctly get an image to load. > > > > What do you people do for stuff like this? > > No one? I could''ve sworn I saw a way to embed file stuff into a YAML file.Perhaps I need to redefine my question: I have a blob in a database table. How can I fill that blob with stuff (i.e. an image file) in a Rails yaml test fixture when running unit tests?
Joe Van Dyk wrote:>On 9/7/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>On 9/6/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >>>Hi, >>> >>>I have an Image model. The images table contains stuff like >>>content_type, file_name, size, and data. data is a blob that contains >>>the image. >>> >>>I want to unit test my Image class. What''s the best way to do this? >>> >>>Currently, I''m doing something like >>># in test/fixtures/images.yml >>>some_image: >>> id: 1 >>> >>># in test/unit/image.rb >>>def setup >>> image = File.read("test/fixtures/photos/image.jpg") >>> @some_image.data = image >>> @some_image.size = image.size >>> @some_image.content_type = "image/jpeg" >>>end >>> >>>Which is bad. I know I can put <%= ... %> into the YAML file, but I >>>haven''t been able to correctly get an image to load. >>> >>>What do you people do for stuff like this? >>> >>> >>No one? I could''ve sworn I saw a way to embed file stuff into a YAML file. >> >> > > >Perhaps I need to redefine my question: > >I have a blob in a database table. How can I fill that blob with >stuff (i.e. an image file) in a Rails yaml test fixture when running >unit tests? > >YAML can do binary data but it either has to be quoted and escaped: image: "GIF89a\x16\x00\xF5\x00\x47\xFF\x00..." Or base64 encoded and typed: image: !binary | R0lGODlhjgCNAOf/AAABAAcAAAQ... I''d recommend the latter in this case, as it''s probably easier to dump base64 into your test cases rather than trying to figure out YAML''s escaping rules. Try something like this: image: !binary | <%= [your_image].pack("m").gsub( /^/, '' '' ) %> _why
On 9/12/05, why the lucky stiff <why+rails-INVRFxB3SCbTr1N2uX3BwuTW4wlIGRCZ@public.gmane.org> wrote:> Joe Van Dyk wrote: > > >On 9/7/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > >>On 9/6/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> > >> > >>>Hi, > >>> > >>>I have an Image model. The images table contains stuff like > >>>content_type, file_name, size, and data. data is a blob that contains > >>>the image. > >>> > >>>I want to unit test my Image class. What''s the best way to do this? > >>> > >>>Currently, I''m doing something like > >>># in test/fixtures/images.yml > >>>some_image: > >>> id: 1 > >>> > >>># in test/unit/image.rb > >>>def setup > >>> image = File.read("test/fixtures/photos/image.jpg") > >>> @some_image.data = image > >>> @some_image.size = image.size > >>> @some_image.content_type = "image/jpeg" > >>>end > >>> > >>>Which is bad. I know I can put <%= ... %> into the YAML file, but I > >>>haven''t been able to correctly get an image to load. > >>> > >>>What do you people do for stuff like this? > >>> > >>> > >>No one? I could''ve sworn I saw a way to embed file stuff into a YAML file. > >> > >> > > > > > >Perhaps I need to redefine my question: > > > >I have a blob in a database table. How can I fill that blob with > >stuff (i.e. an image file) in a Rails yaml test fixture when running > >unit tests? > > > > > YAML can do binary data but it either has to be quoted and escaped: > > image: "GIF89a\x16\x00\xF5\x00\x47\xFF\x00..." > > Or base64 encoded and typed: > > image: !binary | > R0lGODlhjgCNAOf/AAABAAcAAAQ... > > I''d recommend the latter in this case, as it''s probably easier to dump > base64 into your test cases rather than trying to figure out YAML''s > escaping rules. > > Try something like this: > > image: !binary | > <%= [your_image].pack("m").gsub( /^/, '' '' ) %> > > _whyI finally got around to trying this, and I think it almost worked, but not quite. Here''s the fixture: joe_house_image: id: 1 house_id: 1 suitable_for_thumbnail: 1 data: !binary | <%= [File.read("test/fixtures/photos/lodge.jpg")].pack("m").gsub(/^/, '' '') %> Here''s what I get when I try to load it into the test database. Why the gsub bit? Fixture::FormatError: a YAML error occured parsing ./test/unit/../fixtures/images.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.htmlThe exact error was: ArgumentError: parse error on line 7, col 1: ` ABABAgAGAAAAygAAABIBAwABAAAAAQAAABoBBQABAAAA2AAAABsBBQABAAAA'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:291:in `read_fixture_files'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:270:in `initialize'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:231:in `create_fixtures'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:230:in `map'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:230:in `create_fixtures'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:522:in `load_fixtures'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:479:in `setup_with_fixtures'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:504:in `setup'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:503:in `setup''
On 10/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/12/05, why the lucky stiff <why+rails-INVRFxB3SCbTr1N2uX3BwuTW4wlIGRCZ@public.gmane.org> wrote: > > Joe Van Dyk wrote: > > > > >On 9/7/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > >>On 9/6/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> > > >> > > >>>Hi, > > >>> > > >>>I have an Image model. The images table contains stuff like > > >>>content_type, file_name, size, and data. data is a blob that contains > > >>>the image. > > >>> > > >>>I want to unit test my Image class. What''s the best way to do this? > > >>> > > >>>Currently, I''m doing something like > > >>># in test/fixtures/images.yml > > >>>some_image: > > >>> id: 1 > > >>> > > >>># in test/unit/image.rb > > >>>def setup > > >>> image = File.read("test/fixtures/photos/image.jpg") > > >>> @some_image.data = image > > >>> @some_image.size = image.size > > >>> @some_image.content_type = "image/jpeg" > > >>>end > > >>> > > >>>Which is bad. I know I can put <%= ... %> into the YAML file, but I > > >>>haven''t been able to correctly get an image to load. > > >>> > > >>>What do you people do for stuff like this? > > >>> > > >>> > > >>No one? I could''ve sworn I saw a way to embed file stuff into a YAML file. > > >> > > >> > > > > > > > > >Perhaps I need to redefine my question: > > > > > >I have a blob in a database table. How can I fill that blob with > > >stuff (i.e. an image file) in a Rails yaml test fixture when running > > >unit tests? > > > > > > > > YAML can do binary data but it either has to be quoted and escaped: > > > > image: "GIF89a\x16\x00\xF5\x00\x47\xFF\x00..." > > > > Or base64 encoded and typed: > > > > image: !binary | > > R0lGODlhjgCNAOf/AAABAAcAAAQ... > > > > I''d recommend the latter in this case, as it''s probably easier to dump > > base64 into your test cases rather than trying to figure out YAML''s > > escaping rules. > > > > Try something like this: > > > > image: !binary | > > <%= [your_image].pack("m").gsub( /^/, '' '' ) %> > > > > _why > > I finally got around to trying this, and I think it almost worked, but > not quite. > > Here''s the fixture: > joe_house_image: > id: 1 > house_id: 1 > suitable_for_thumbnail: 1 > data: !binary | > <%= [File.read("test/fixtures/photos/lodge.jpg")].pack("m").gsub(/^/, > '' '') %> > > Here''s what I get when I try to load it into the test database. > > Why the gsub bit? > > Fixture::FormatError: a YAML error occured parsing > ./test/unit/../fixtures/images.yml. Please note that YAML must be > consistently indented using spaces. Tabs are not allowed. Please have > a look at http://www.yaml.org/faq.htmlThe exact error was: > ArgumentError: parse error on line 7, col 1: ` > ABABAgAGAAAAygAAABIBAwABAAAAAQAAABoBBQABAAAA2AAAABsBBQABAAAA'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:291:in > `read_fixture_files'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:270:in > `initialize'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:231:in > `create_fixtures'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:230:in > `map'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:230:in > `create_fixtures'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:522:in > `load_fixtures'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:479:in > `setup_with_fixtures'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:504:in > `setup'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:503:in > `setup''Doing this *almost* works: image: data: !binary | <%= [File.read(''test/fixtures/photos/joe.jpg'')].pack(''m'').gsub(/\n/m, "\n ") %> So, I''m indenting the base64 strings by four spaces. It loads it into the database fine. But ImageMagick barfs on some images, and other images are coming out with strange breaks and tears in the image. I''m guessing that the base64 string still isn''t quite right, or there''s a bug in the YAML binary code, or there''s a bug in my code. :(
On 10/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 9/12/05, why the lucky stiff <why+rails-INVRFxB3SCbTr1N2uX3BwuTW4wlIGRCZ@public.gmane.org> wrote: > > > Joe Van Dyk wrote: > > > > > > >On 9/7/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > >>On 9/6/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >> > > > >> > > > >>>Hi, > > > >>> > > > >>>I have an Image model. The images table contains stuff like > > > >>>content_type, file_name, size, and data. data is a blob that contains > > > >>>the image. > > > >>> > > > >>>I want to unit test my Image class. What''s the best way to do this? > > > >>> > > > >>>Currently, I''m doing something like > > > >>># in test/fixtures/images.yml > > > >>>some_image: > > > >>> id: 1 > > > >>> > > > >>># in test/unit/image.rb > > > >>>def setup > > > >>> image = File.read("test/fixtures/photos/image.jpg") > > > >>> @some_image.data = image > > > >>> @some_image.size = image.size > > > >>> @some_image.content_type = "image/jpeg" > > > >>>end > > > >>> > > > >>>Which is bad. I know I can put <%= ... %> into the YAML file, but I > > > >>>haven''t been able to correctly get an image to load. > > > >>> > > > >>>What do you people do for stuff like this? > > > >>> > > > >>> > > > >>No one? I could''ve sworn I saw a way to embed file stuff into a YAML file. > > > >> > > > >> > > > > > > > > > > > >Perhaps I need to redefine my question: > > > > > > > >I have a blob in a database table. How can I fill that blob with > > > >stuff (i.e. an image file) in a Rails yaml test fixture when running > > > >unit tests? > > > > > > > > > > > YAML can do binary data but it either has to be quoted and escaped: > > > > > > image: "GIF89a\x16\x00\xF5\x00\x47\xFF\x00..." > > > > > > Or base64 encoded and typed: > > > > > > image: !binary | > > > R0lGODlhjgCNAOf/AAABAAcAAAQ... > > > > > > I''d recommend the latter in this case, as it''s probably easier to dump > > > base64 into your test cases rather than trying to figure out YAML''s > > > escaping rules. > > > > > > Try something like this: > > > > > > image: !binary | > > > <%= [your_image].pack("m").gsub( /^/, '' '' ) %> > > > > > > _why > > > > I finally got around to trying this, and I think it almost worked, but > > not quite. > > > > Here''s the fixture: > > joe_house_image: > > id: 1 > > house_id: 1 > > suitable_for_thumbnail: 1 > > data: !binary | > > <%= [File.read("test/fixtures/photos/lodge.jpg")].pack("m").gsub(/^/, > > '' '') %> > > > > Here''s what I get when I try to load it into the test database. > > > > Why the gsub bit? > > > > Fixture::FormatError: a YAML error occured parsing > > ./test/unit/../fixtures/images.yml. Please note that YAML must be > > consistently indented using spaces. Tabs are not allowed. Please have > > a look at http://www.yaml.org/faq.htmlThe exact error was: > > ArgumentError: parse error on line 7, col 1: ` > > ABABAgAGAAAAygAAABIBAwABAAAAAQAAABoBBQABAAAA2AAAABsBBQABAAAA'' > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:291:in > > `read_fixture_files'' > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:270:in > > `initialize'' > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:231:in > > `create_fixtures'' > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:230:in > > `map'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:230:in > > `create_fixtures'' > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:522:in > > `load_fixtures'' > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:479:in > > `setup_with_fixtures'' > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:504:in > > `setup'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/fixtures.rb:503:in > > `setup'' > > Doing this *almost* works: > image: > data: !binary | > <%= [File.read(''test/fixtures/photos/joe.jpg'')].pack(''m'').gsub(/\n/m, > "\n ") %> > > So, I''m indenting the base64 strings by four spaces. It loads it into > the database fine. But ImageMagick barfs on some images, and other > images are coming out with strange breaks and tears in the image. I''m > guessing that the base64 string still isn''t quite right, or there''s a > bug in the YAML binary code, or there''s a bug in my code. :(Narrowed it down some more. Here''s my current fixture file: ========================<% require ''base64'' %> <% def format_image_for_fixture image_path Base64.encode64(File.read(image_path)).gsub(/\n/, "\n ") end %> image: id: 1 data: !binary > <%= format_image_for_fixture ''test/fixtures/photos/joe.jpg'' %> =========================== Here''s part of the image unit test: =========================== def test_something assert_equal File.read(''test/fixtures/photos/joe.jpg''), YAML.load(`erb ''test/fixtures/images.yml''`)[''image''][''data''] assert_equal @image.data, Image.find(1).data assert File.read(''test/fixtures/photos/joe.jpg'') != @image.data end =========================== That unit test passes successfully. Why on earth are Image.find(1).data and @image.data not the same as File.read(''test/fixtures/photos/joe.jpg'')? It''s really weird. Maybe a problem with the way Rails loads fixtures that contain binary data?