package com.redspr.redquerybuilder.js.client;

import org.junit.Test;

import com.google.gwt.core.client.JsonUtils;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.ui.RootPanel;
import com.redspr.redquerybuilder.core.client.table.TableFilter;

public class GwtTestBasics extends GWTTestCase {

    @Override
    public void gwtSetUp() {
        TableFilter.resetAlias();
    }

    private void test(String json, String msg) throws Exception {
        RootPanel.get().getElement().setAttribute("id", "rqb");
        try {
            JsConfiguration conf = (JsConfiguration) JsonUtils.unsafeEval(json);
            RedQueryBuilder.configure(conf, "SELECT id FROM Foo", null);
            fail();
        } catch (Throwable th) {
            assertEquals(msg, th.getMessage());
        }
    }

    @Test
    public void testNothing() throws Exception {
        try {
            RedQueryBuilder.configure(null, null, null);
            fail();
        } catch (Throwable th) {
            assertEquals("Config is null.", th.getMessage());
        }
    }

    @Test
    public void testEmptyConfig() throws Exception {
        test("{}", "Meta is null.");
    }

    @Test
    public void testNoTables() throws Exception {
        test("{meta:{}}", "Unable to find table FOO");
    }

    @Test
    public void testNoFksInJson() throws Exception {
        test("{meta:{tables:[{name:'Foo'}]}}", "No tables defined");
    }


    @Override
    public String getModuleName() {
        return "com.redspr.redquerybuilder.js.RedQueryBuilder";
    }
}

@@ -707,39 +707,129 @@
 l);%0A            
-fail();
+if (msg != null) %7B%0A                fail(%22Was expecting the error message: %22 + msg);%0A            %7D
 %0A        %7D catch
@@ -1516,27 +1516,12 @@
 %7D%22, 
-%22No tables defined%22
+null
 );%0A 

Fix unit test for null FKs in JSON.