package simpledb.unittest;

import simpledb.*;
import java.util.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import junit.framework.JUnit4TestAdapter;

/**
 * We reserve more heavy-duty insertion testing for HeapFile and HeapPage.
 * This suite is superficial.
 */
public class InsertTest {

  private DbIterator scan1;
  private HeapFile empty;
  private TransactionId tid;
  private static final String emptyPath = 
    "src/simpledb/unittest/testdata/empty.dat";

  /**
   * Initialize each unit test
   */
  @Before public void setUp() throws Exception {
    this.scan1 = Utility.createTupleList(2, 
        new int[] { 1, 2,
                    1, 4,
                    1, 6,
                    3, 2,
                    3, 4,
                    3, 6,
                    5, 7 });
    this.empty = Utility.createEmptyHeapFile(emptyPath, 2);
    this.tid = new TransactionId();
  }

  /**
   * Unit test for Insert.getTupleDesc()
   */
  @Test public void getTupleDesc() throws Exception {
    Insert op = new Insert(tid, scan1, empty.id()); 
    TupleDesc expected = Utility.getTupleDesc(1);
    TupleDesc actual = op.getTupleDesc();
    assertTrue(expected.equals(actual));
  }

  /**
   * Unit test for Insert.getNext(), inserting elements into an empty file
   */
  @Test public void getNext() throws Exception {
    Insert op = new Insert(tid, scan1, empty.id()); 
    op.open();
    assertTrue(Utility.compareTuples(
          Utility.getHeapTuple(7, 1), // the length of scan1 
          op.getNext()));

    // we should fit on one page
    assertEquals(1, empty.numPages());
  }

  /**
   * JUnit suite target
   */
  public static junit.framework.Test suite() {
    return new JUnit4TestAdapter(InsertTest.class);
  }
}

