Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework Image BLOB File for Test Object Yaml

How do you set up a Test Blob Image using the yaml structure?

Also, what is the database structure for a BLOB file? (MySQL)

like image 642
Jon R Avatar asked Aug 03 '11 15:08

Jon R


2 Answers

I have experienced the same kind of problem a while ago on a project. However as I could not find a way to solve this with the fixtures (as the database stores the blob object as a string as Pere explained above), I created a workaround to at least solve this problem in a test-case-scenario. I created the following file /app/job/Bootstrap.java:

import play.test.*;
import play.jobs.*;
import play.db.DB;
import models.*;

import java.util.List;

@OnApplicationStart
public class Bootstrap extends Job {
     public void doJob() {
        // Load default data if the database is empty
        if(Item.count() == 0) {
            Fixtures.loadModels("my_fixtures.yml");
            List<Item> allItems = Item.findAll();
            for (Item a: allItems){
                DB.execute("UPDATE `Item` SET image='item_" + a.name.toLowerCase() + ".png|image/png' WHERE id=" + a.getId());
            }
        }
    }
}

The first thing I do is filling the database with initial data if there are no 'Item' already stored in the database.
The second thing is iterating over all the 'Item' which play! just stored in the database, which are read from the "my_fixtures.yml" file. Here for each item the string field will get updated as shown in the example above.

I know this is not exactly the answer to question in the OP, but it gives some kind idea to work around this issue..

EDIT: In the example given above I assume that the pictures are uploaded manually to your attachment folder as given in your application.conf, and that each image name is like: "item_<item_name_in_lowercase>" with a ".png" extension

like image 78
Unji Avatar answered Oct 04 '22 05:10

Unji


Well, play is quite weird on that point.

The blob is not saved into the database but in a upload folder defined in your application.conf. It is the path toward the file that is saved in the database.

I cannot check it right now, but I seem to recall they are saved as textuel representations (VARCHAR, TEXT)

like image 44
i.am.michiel Avatar answered Oct 04 '22 03:10

i.am.michiel