/* Copyright (c) 2010-2012, Revori Contributors

   Permission to use, copy, modify, and/or distribute this software
   for any purpose with or without fee is hereby granted, provided
   that the above copyright notice and this permission notice appear
   in all copies. */

package unittests;

import static com.readytalk.revori.ExpressionFactory.reference;
import static com.readytalk.revori.util.Util.cols;
import static com.readytalk.revori.util.Util.list;
import static org.junit.Assert.*;

import org.junit.Test;

import com.readytalk.revori.Column;
import com.readytalk.revori.Constant;
import com.readytalk.revori.DuplicateKeyResolution;
import com.readytalk.revori.Expression;
import com.readytalk.revori.InsertTemplate;
import com.readytalk.revori.Parameter;
import com.readytalk.revori.PatchTemplate;
import com.readytalk.revori.QueryResult;
import com.readytalk.revori.QueryResult.Type;
import com.readytalk.revori.QueryTemplate;
import com.readytalk.revori.Revision;
import com.readytalk.revori.RevisionBuilder;
import com.readytalk.revori.Revisions;
import com.readytalk.revori.Table;
import com.readytalk.revori.TableReference;

public class Diffs {

  @Test
  public void testUpdateNonQueriedColumns() {
    Column<Integer> number = new Column<Integer>(Integer.class);
    Column<String> name = new Column<String>(String.class);
    Column<String> origin = new Column<String>(String.class);
    Table numbers = new Table(cols(number));
  
    Revision tail = Revisions.Empty;
  
    PatchTemplate insert = new InsertTemplate
      (numbers,
       cols(number, name, origin),
       list((Expression) new Parameter(), new Parameter(), new Parameter()),
       DuplicateKeyResolution.Throw);
  
    RevisionBuilder builder = tail.builder();
  
    builder.apply(insert, 0, "zero", "far too recently");
    builder.apply(insert, 1, "one", "long ago");
    builder.apply(insert, 2, "two", "long ago");
    builder.apply(insert, 3, "three", "a little less long ago");

    Revision first = builder.commit();
    
    TableReference numbersReference = new TableReference(numbers);
    
    QueryTemplate query = new QueryTemplate
    (list((Expression) reference(numbersReference, name)),
     numbersReference,
     new Constant(true));

    QueryResult result = tail.diff(first, query);

    assertEquals(QueryResult.Type.Inserted, result.nextRow());
    assertEquals("zero", result.nextItem());
    assertEquals(QueryResult.Type.Inserted, result.nextRow());
    assertEquals("one", result.nextItem());
    assertEquals(QueryResult.Type.Inserted, result.nextRow());
    assertEquals("two", result.nextItem());
    assertEquals(QueryResult.Type.Inserted, result.nextRow());
    assertEquals("three", result.nextItem());
    assertEquals(QueryResult.Type.End, result.nextRow());

    
    builder = first.builder();
  
    // rewrite history
    builder.table(numbers)
      .row(0)
      .column(origin, "not long after <one>")
      .up().up();

    Revision second = builder.commit();

    result = first.diff(second, query);

    // we didn't change any of the queried columns, so we shouldn't see any difference.
    assertEquals(QueryResult.Type.End, result.nextRow());
    
  }
}

@@ -352,16 +352,135 @@
 erence;%0A
+import static com.readytalk.revori.ExpressionFactory.not;%0Aimport static com.readytalk.revori.ExpressionFactory.isNull;%0A
 import s
@@ -3307,11 +3307,1746 @@
     %0A  %7D
+%0A%0A  @Test%0A  public void testRemoveQueriedColumns() %7B%0A    Column%3CInteger%3E number = new Column%3CInteger%3E(Integer.class);%0A    Column%3CString%3E name = new Column%3CString%3E(String.class);%0A    Column%3CString%3E origin = new Column%3CString%3E(String.class);%0A    Table numbers = new Table(cols(number));%0A  %0A    Revision tail = Revisions.Empty;%0A  %0A    PatchTemplate insert = new InsertTemplate%0A      (numbers,%0A       cols(number, name, origin),%0A       list((Expression) new Parameter(), new Parameter(), new Parameter()),%0A       DuplicateKeyResolution.Throw);%0A  %0A    RevisionBuilder builder = tail.builder();%0A  %0A    builder.apply(insert, 0, %22zero%22, %22far too recently%22);%0A    builder.apply(insert, 1, %22one%22, %22long ago%22);%0A    builder.apply(insert, 2, %22two%22, %22long ago%22);%0A    builder.apply(insert, 3, %22three%22, %22a little less long ago%22);%0A%0A    Revision first = builder.commit();%0A    %0A    TableReference numbersReference = new TableReference(numbers);%0A    %0A    QueryTemplate query = new QueryTemplate%0A    (list((Expression)%0A      reference(numbersReference, number),%0A      reference(numbersReference, name),%0A      reference(numbersReference, origin)),%0A     numbersReference,%0A     not(isNull(reference(numbersReference, origin))));%0A    %0A    builder = first.builder();%0A  %0A    builder.table(numbers)%0A      .row(0)%0A      .delete(origin)%0A      .up().up();%0A%0A    Revision second = builder.commit();%0A%0A    QueryResult result = first.diff(second, query);%0A%0A    assertEquals(QueryResult.Type.Deleted, result.nextRow());%0A    assertEquals(false, result.rowUpdated());%0A    assertEquals(0, result.nextItem());%0A    assertEquals(%22zero%22, result.nextItem());%0A    assertEquals(%22far too recently%22, result.nextItem());%0A    assertEquals(QueryResult.Type.End, result.nextRow());%0A    %0A  %7D
 %0A%7D%0A

add (failing) test for querying deleted columns