/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs; import gplx.*;
import org.junit.*;
public class Db_qry_select_tst {
	@Before public void setup() {
		cmd = Db_qry_select.new_();
	}	Db_qry_select cmd; String expd;
	@Test  public void Basic() {
		cmd.Cols_("fld0", "fld1").From_("tbl0");
		expd = "SELECT fld0, fld1 FROM tbl0";

		tst_XtoStr(cmd, expd);
	}
	@Test  public void OrderDoesNotMatter() {
		cmd.From_("tbl0").Cols_("fld0", "fld1");
		expd = "SELECT fld0, fld1 FROM tbl0";

		tst_XtoStr(cmd, expd);
	}
	@Test  public void DefaultAllFields() {
		cmd.From_("tbl0");
		expd = "SELECT * FROM tbl0";

		tst_XtoStr(cmd, expd);
	}		
	@Test  public void Where() {
		cmd.From_("tbl0").Where_(Db_crt_.eq_("fld0", 0));
		expd = "SELECT * FROM tbl0 WHERE fld0=0";

		tst_XtoStr(cmd, expd);
	}
	@Test  public void Join() {
		cmd.From_("tbl0").Join_("tbl1", "t1", Sql_join_itm.new_("fld1", "tbl0", "fld0"));
		expd = "SELECT * FROM tbl0 INNER JOIN tbl1 t1 ON tbl0.fld0=t1.fld1";

		tst_XtoStr(cmd, expd);
	}		
	@Test  public void OrderBy() {
		cmd.From_("tbl0").OrderBy_("fld0", true);
		expd = "SELECT * FROM tbl0 ORDER BY fld0";

		tst_XtoStr(cmd, expd);
	}		
	@Test  public void OrderByMany() {
		cmd.From_("tbl0").OrderBy_many_("fld0", "fld1");
		expd = "SELECT * FROM tbl0 ORDER BY fld0, fld1";

		tst_XtoStr(cmd, expd);
	}		
	@Test  public void Limit() {
		cmd.From_("tbl0").Limit_(10);
		expd = "SELECT * FROM tbl0 LIMIT 10";

		tst_XtoStr(cmd, expd);
	}		
//		@Test  public void GroupBy() {
//			cmd.From_("tbl0").groupBy_("fld0", "fld1");
//			expd = "SELECT fld0, fld1 FROM tbl0 GROUP BY fld0, fld1";
//			Tfds.Eq(cmd.XtoStr(), expd);
//		}
//		@Test  public void Union() {
//			cmd.From_("tbl0").select("fld0").union_(qry2.from("tbl1").select("fld0"));
//			cmd.From_("tbl0").select("fld0").union_().from("tbl1").select("fld0"); // feasible, but will be bad later when trying to access Db_qry_select props
//			expd = "SELECT fld0 FROM tbl0 UNION SELECT fld0 FROM tbl1";
//			Tfds.Eq(cmd.XtoStr(), expd);
//		}
//		@Test  public void Having() {
//			cmd.From_("tbl0").groupBy_("fld0", "fld1");
//			expd = "SELECT fld0, fld1 FROM tbl0 GROUP BY fld0, fld1 HAVING Count(fld0) > 1";
//			Tfds.Eq(cmd.XtoStr(), expd);
//		}
	void tst_XtoStr(Db_qry qry, String expd) {Tfds.Eq(expd, cmd.XtoSql());}
}
