package supahnickie.caffeineTester;

import static org.junit.Assert.*;

import org.junit.*;
import supahnickie.caffeine.*;
import supahnickie.testClasses.*;

public class CaffeineTest {
	CaffeineObject databaseHandle;
	CaffeineObject userLookup;
	CaffeineObject downloadLookup;

	@Before
	public void setUp() throws Exception {
		// The database must already exist, but should be blank otherwise.
		new Caffeine(System.getenv("CAFFEINE_DB_DRIVER"), System.getenv("CAFFEINE_DB_TEST_URL"), System.getenv("CAFFEINE_DB_USERNAME"), System.getenv("CAFFEINE_DB_PASSWORD"));
		databaseHandle = new User();
		userLookup = new User();
		downloadLookup = new Download();
		insertTables();
		insertUsers();
	}

	@Test
	public void find() throws Exception {
		User user = (User) userLookup.find(13);
		assertEquals("ids should match", user.id, 13);
		assertEquals("first name should match", user.first_name, "Nick");
		assertEquals("last name should match", user.last_name, "Case");
		assertNotEquals("ids should not match others", user.id, 5);
		assertNotEquals("first name should not match others", user.first_name, "Grawr");
		assertNotEquals("last name should not match others", user.last_name, "McPhee");
	}

	@After
	public void tearDown() throws Exception {
		databaseHandle.executeUpdate("drop table if exists users");
		databaseHandle.executeUpdate("drop table if exists downloads");
	}

	// Test helper methods

	public void insertTables() throws Exception {
		databaseHandle.executeUpdate("drop table if exists users");
		databaseHandle.executeUpdate("drop table if exists downloads");
		databaseHandle.executeUpdate("create table if not exists users (" +
			"id integer not null, " +
			"first_name varchar(255), " +
			"last_name varchar(255), " +
			"encrypted_password varchar(255), " +
			"sign_in_count integer, " +
			"role varchar(255))"
		);
		databaseHandle.executeUpdate("create table if not exists downloads (" +
			"id integer not null, " +
			"file_file_name varchar(255), " +
			"org_id integer, " +
			"user_id integer)"
		);
	}

	private void insertUsers() throws Exception {
		databaseHandle.executeUpdate("insert into users (id, first_name, last_name) values (13, 'Nick', 'Case')");
		databaseHandle.executeUpdate("insert into users (id, first_name, last_name) values (5, 'Grawr', 'McPhee')");
	}
}

@@ -81,24 +81,47 @@
 rg.junit.*;%0A
+import java.util.List;%0A
 import supah
@@ -715,16 +715,891 @@
 ();%0A%09%7D%0A%0A
+%09@Test%0A%09public void executeUpdate() throws Exception %7B%0A%09%09downloadLookup.executeUpdate(%22insert into downloads (id, org_id, file_file_name) values (15, 13, 'download1'), (16, 13, 'download2')%22);%0A%09%09List%3CCaffeineObject%3E downloads = downloadLookup.executeQuery(%22select * from downloads where id in (15, 16)%22);%0A%09%09Download download15 = (Download) downloads.get(0);%0A%09%09Download download16 = (Download) downloads.get(1);%0A%09%09assertArrayEquals(%22returned downloads should match expected ids%22, new int%5B%5D %7Bdownload15.id, download16.id%7D, new int%5B%5D %7B15, 16%7D);%0A%09%09assertArrayEquals(%22returned downloads should match expected file names%22, new String%5B%5D %7Bdownload15.file_file_name, download16.file_file_name%7D, new String%5B%5D %7B%22download1%22, %22download2%22%7D);%0A%09%09assertArrayEquals(%22returned downloads should match expected org_ids%22, new int%5B%5D %7Bdownload15.org_id, download16.org_id%7D, new int%5B%5D %7B13, 13%7D);%0A%09%7D%0A%0A
 %09@Test%0A%09

add test for executeUpdate()