/*
 * Copyright 2015, Yahoo Inc.
 * Copyrights licensed under the Apache 2.0 License.
 * See the accompanying LICENSE file for terms.
 */
package com.yahoo.squidb.data;

import com.yahoo.squidb.sql.Property.LongProperty;
import com.yahoo.squidb.sql.Query;
import com.yahoo.squidb.test.SquidTestCase;
import com.yahoo.squidb.test.TestModel;
import com.yahoo.squidb.test.TestViewModel;
import com.yahoo.squidb.test.TestVirtualModel;

public class PropertyTest extends SquidTestCase {

    public void testPropertyAliasing() {
        LongProperty p = TestModel.ID;
        assertEquals(p.getQualifiedExpression(), "testModels._id");
        assertEquals(p.getExpression(), "_id");
        assertEquals(p.getName(), "_id");

        LongProperty basicAlias = p.as("newAlias");
        assertEquals(p.table, basicAlias.table);
        assertEquals(p.getExpression(), basicAlias.getExpression());
        assertEquals("newAlias", basicAlias.getName());
        assertEquals("SELECT testModels._id AS newAlias", Query.select(basicAlias).toString());

        LongProperty aliasWithTable = p.as("newTable", "newAlias");
        assertEquals("newTable", aliasWithTable.table.getName());
        assertEquals(p.getExpression(), aliasWithTable.getExpression());
        assertEquals("newAlias", aliasWithTable.getName());
        assertEquals("SELECT newTable._id AS newAlias", Query.select(aliasWithTable).toString());

        LongProperty asSelectionFromTable = basicAlias.asSelectionFromTable(TestViewModel.VIEW, "superAlias");
        assertEquals(TestViewModel.VIEW.getName(), asSelectionFromTable.table.getName());
        assertEquals(basicAlias.getName(), asSelectionFromTable.getExpression());
        assertEquals("superAlias", asSelectionFromTable.getName());
        assertEquals("SELECT testView.newAlias AS superAlias", Query.select(asSelectionFromTable).toString());

        LongProperty virtualP = TestVirtualModel.ID;
        assertEquals(virtualP.getQualifiedExpression(), "virtual_models.rowid");
        assertEquals(virtualP.getExpression(), "rowid");
        assertEquals(virtualP.getName(), "rowid");
    }

}

@@ -423,16 +423,52 @@
 alModel;
+%0Aimport com.yahoo.squidb.test.Thing;
 %0A%0Apublic
@@ -1915,51 +1915,158 @@
     
-LongProperty virtualP = TestVirtualModel.ID
+assertEquals(TestVirtualModel.ID.getQualifiedExpression(), %22virtual_models.rowid%22);%0A        assertEquals(TestVirtualModel.ID.getExpression(), %22rowid%22)
 ;%0A  
@@ -2080,32 +2080,95 @@
 ssertEquals(
-v
+TestV
 irtual
-P
+Model.ID.getName(), %22rowid%22);%0A%0A        assertEquals(Thing.ID
 .getQualifie
@@ -2183,34 +2183,23 @@
 ion(), %22
-virtual_models.row
+things.
 id%22);%0A  
@@ -2213,32 +2213,32 @@
 ssertEquals(
-virtualP
+Thing.ID
 .getExpressi
@@ -2236,35 +2236,32 @@
 tExpression(), %22
-row
 id%22);%0A        as
@@ -2275,16 +2275,16 @@
 als(
-virtualP
+Thing.ID
 .get
@@ -2288,27 +2288,24 @@
 getName(), %22
-row
 id%22);%0A    %7D%0A

Enhance PropertyTest to check non-default primary keys