Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3363 (closed defect: wontfix)

Opened 4 years ago

Last modified 4 years ago

[PATCH] Binary support for YAML fixtures

Reported by: jamie@bravenet.com Assigned to: rails@bitsweat.net
Priority: normal Milestone:
Component: ActiveRecord Version: 1.0.0
Severity: normal Keywords:
Cc:

Description

I have a blob field in my model for storing an image, in raw binary. I want to test that my action that returns the model's image does return the correct image, as matched from the filesystem.

I've loaded up the image and taken a Base64 encoding of it, and formatted it properly for YAML as specified at http://yaml.org/type/binary.html

canonical: !!binary "\
 R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\
 OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\
 +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\
 AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="

(the other one, !binary |, doesn't seem to work)

Unfortunately, the value I get from the database after loading the fixture looks like this:

--- !ruby/object:YAML::Syck::PrivateType 
type_id: binary
value: R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

In order to make use of this data, I need to un-yamlize it, get the value from it, and decode64 it.

This patch automatically converts from the YAML::Syck::PrivateType object to a raw binary string for import into the database. This is only done if the PrivateType's type_id is binary, so that it shouldn't affect any other YAML craziness.

This does break old behaviour, but I can't see anyone actually *wanting* to rely on storing base64-encoded, object-wrapped, yaml-ified data in their DB on a regular basis.

Attachments

3363_binary_in_yaml_fixtures.diff (0.9 kB) - added by jamie@bravenet.com on 12/30/05 22:07:20.
ensures binary-encoded data in YAML fixtures makes it to the database as the intended binary data

Change History

12/30/05 22:07:20 changed by jamie@bravenet.com

  • attachment 3363_binary_in_yaml_fixtures.diff added.

ensures binary-encoded data in YAML fixtures makes it to the database as the intended binary data

12/31/05 00:50:16 changed by jamie@bravenet.com

This might not be necessary, actually... I removed this from /vendor in my own project and it still seems to work....

01/21/06 23:57:10 changed by david

  • owner changed from David to rails@bitsweat.net.

02/09/06 22:14:15 changed by bitsweat

  • status changed from new to closed.
  • resolution set to wontfix.

YAML should be handling the decoding.

02/09/06 23:16:22 changed by jamie@bravenet.com

We did some testing in the office, and managed to figure out what was wrong.

There's two ways to do binary data in Yaml (see url in main ticket): canonical and generic. I don't know why I dismissed generic as not working originally, but we've tested it on ruby 1.8.2 (OS X 10.4), 1.8.3 (ubuntu 5.10) and 1.8.4 (gentoo), and without this patch, the generic method works but canonical doesn't work at all. With this patch, Canonical version works on 1.8.2 and 1.8.3, but not 1.8.4.

As original submitter, agree on wontfix status, but perhaps the fixture documentation should mention that developers should use the generic format for binary data in their fixtures.

02/13/06 03:09:23 changed by bitsweat

Could you include an example of each format?

02/13/06 17:37:02 changed by jamie@bravenet.com

From http://yaml.org/type/binary.html:

canonical: !!binary "\
 R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\
 OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\
 +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\
 AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="
generic: !binary |
 R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
 OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
 +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
 AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
description:
 The binary value above is a tiny arrow encoded as a gif image.

The canonical version does not work, but the generic version seems to work fine, at least for Rails 1.0.

To verify this, just create a table with a type :binary (in the migration) and stuff the generic data piece in a fixture. Then manually Base64.decode64 the sample below, and compare that binary string to what comes out of the database.