We're going to just try to do a little bit of work in verifying that what we have is
a base 64 encoded string.
So we'll start out with a property object generated
by the FactoryGirl.attributes_for.
Then let's base 64 decode the content property of that property map.
And at this point, data should just be binary data.
And then we can use our helper method to turn what we
believe is a base 64 encoded string into a BSON binary.
And now what we expect is that the binary data size
should equal the binary, the BSON binary data size.
And then of course, the two pieces of data should be identical.
I don't know whether this exactly proves whether it's base 64, but
it at least does some exercise in that area.
So if we look at the results of the test, we're able to have a good indication
that our attribute that came back was base64 encoded.
At least it wasn't binary.
All right, these next two tasks are just lead up to the next topic,
which is Image has ImageContent.
And what we're going to do is a lot of the work that's going to happen inside
the factories we're going to do inside the test.
So let go ahead and just do the whole thing.
What's going to happen is we're going to off and create an image.
And let's just give it some required information like a creator_id.
Now we're going to create a ImageContent from that and pass it the image.
And when we validate it, our image should be valid.
All the properties that we worked to validate, including the image id,
should be set.
And it should be equal the image_id of the image we associated it with.
This is kind of a no brainer, right?
And we're looking good on that front.
We can assign the image ID from the factory.
Okay, in this next one, what we're going to do is pretty much
like the previous one, where we'll create an image.
But then we'll go out and create properties for that image, and
then verify that we can create an instance of the image content
from the properties that have been generated.
So here come attributes for ImageContent that know not what its image id is.
And then we're going to tack on what the image id is, and
then try to create, and expect that to be valid.
And we're actually creating these in the database.
And since we are creating these in the database, let's use the context,
where we'll delete things after each test within this area, okay?
Our ImageContent factory is functional.
Now, in this next section,
what we need to do is associate the image with image_content.
To date, they've been two separate objects talking about one another.
So in this first test, we'll just verify that image knows
what ImageContent is by verifying it has a method to get ImageContent.
And now we know that's going to fail, because image does not yet
reference anything about ImageContent, not in its properties and not in its table.
And what we'll do is we'll add an attribute, a transient attribute
to image that will be capable of being able to set and get image content.
And now our first test is passing.
In this next test, we're going to verify that
the image factory now produces image_content
in addition to all its other properties.
And go one step further and verify that the image_content that it contains
Is a hash for when we call attributes_for.
And that hash contains content_type and content_type is image/jpg.
All right, this one's going to require a little work.
But we've already demonstrated a fair amount of the work that needs to be
done up here.
We need the attributes for
an image to return the attributes also for ImageContent.
And this starts off simple.
We just add a property, image content, and then a code block.
We call FactoryGirl again, and say, give me something for ImageContent, default.
And when we're in attribute form, it doesn't have the image ID,
it's within the image, so it's as we expect.