<commit_before>package org.apache.derby.impl.sql.execute.operations;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.junit.Assert;
import org.apache.log4j.Logger;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import com.google.common.collect.Lists;
import com.splicemachine.derby.test.framework.SpliceDataWatcher;
import com.splicemachine.derby.test.framework.SpliceSchemaWatcher;
import com.splicemachine.derby.test.framework.SpliceTableWatcher;
import com.splicemachine.derby.test.framework.SpliceUnitTest;
import com.splicemachine.derby.test.framework.SpliceWatcher;

/**
 * @author <NAME>
 *         Created on: 3/1/13
 */
public class PrimaryKeyTest extends SpliceUnitTest {
    private static final Logger LOG = Logger.getLogger(PrimaryKeyTest.class);
    protected static SpliceWatcher spliceClassWatcher = new SpliceWatcher();
	public static final String CLASS_NAME = PrimaryKeyTest.class.getSimpleName().toUpperCase();
	public static final String TABLE_NAME = "A";
	protected static SpliceSchemaWatcher spliceSchemaWatcher = new SpliceSchemaWatcher(CLASS_NAME);	
	protected static SpliceTableWatcher spliceTableWatcher = new SpliceTableWatcher(TABLE_NAME,CLASS_NAME,"(name varchar(50),val int, PRIMARY KEY(name))");
	protected static String INSERT = String.format("insert into %s.%s (name, val) values (?,?)",CLASS_NAME, TABLE_NAME);
	protected static String SELECT_BY_NAME = String.format("select * from %s.%s where name = ?",CLASS_NAME, TABLE_NAME);
	protected static String SELECT_NAME_BY_NAME = String.format("select name from %s.%s where name = ?",CLASS_NAME, TABLE_NAME);	
	protected static String UPDATE_NAME_BY_NAME = String.format("update %s.%s set name = ? where name = ?",CLASS_NAME, TABLE_NAME);
	protected static String UPDATE_VALUE_BY_NAME = String.format("update %s.%s set val = ? where name = ?",CLASS_NAME, TABLE_NAME);
	
	@ClassRule 
	public static TestRule chain = RuleChain.outerRule(spliceClassWatcher)
		.around(spliceSchemaWatcher)
		.around(spliceTableWatcher)
		.around(new SpliceDataWatcher(){
		@Override
		protected void starting(Description description) {
			try {
				PreparedStatement ps = spliceClassWatcher.prepareStatement(INSERT);
				ps.setString(1,"sfines");
				ps.setInt(2,1);
				ps.executeUpdate();
				ps.setString(1,"jleach");
				ps.setInt(2,2);
				ps.executeUpdate();
				ps.setString(1,"mzweben");
				ps.setInt(2,3);
				ps.executeUpdate();				
				ps.setString(1,"gdavis");
				ps.setInt(2,4);
				ps.executeUpdate();				
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			finally {
				spliceClassWatcher.closeAll();
			}
		}
		
	});
	@Rule public SpliceWatcher methodWatcher = new SpliceWatcher();

   
    @Test(expected=SQLException.class)
    public void cannotInsertDuplicatePks() throws Exception{
    	try {
    		PreparedStatement ps = methodWatcher.prepareStatement(INSERT);
    		ps.setString(1,"sfines");
    		ps.setInt(2,1);
    		ps.executeUpdate();
    		Assert.assertTrue("Did not throw an exception on duplicate records on primary key",false);
    	} catch (SQLException e) {
    		 PreparedStatement validator = methodWatcher.prepareStatement(SELECT_BY_NAME);
    		 validator.setString(1,"sfines");
    	        ResultSet rs = validator.executeQuery();
    	        int matchCount=0;
    	        while(rs.next()){
    	            if("sfines".equalsIgnoreCase(rs.getString(1))){
    	                matchCount++;
    	            }
    	        }
    	        Assert.assertEquals("Incorrect number of matching rows found!",1,matchCount);
    	        throw e;
    	}
    }

    @Test
    public void canUpdatePrimaryKeyCorrectly() throws Exception{
        PreparedStatement updateStatement = methodWatcher.prepareStatement(UPDATE_NAME_BY_NAME);
        updateStatement.setString(1,"jzhang");
        updateStatement.setString(2,"jleach");
        updateStatement.executeUpdate();
        PreparedStatement validator = methodWatcher.prepareStatement(SELECT_BY_NAME);
        validator.setString(1,"jleach");
        ResultSet rs = validator.executeQuery();
        while(rs.next()){
            Assert.fail("Should have returned nothing");
        }
        validator.setString(1,"jzhang");
        rs = validator.executeQuery();
        int matchCount = 0;
        while(rs.next()){
            if("jzhang".equalsIgnoreCase(rs.getString(1))){
                matchCount++;
                Assert.assertEquals("Column incorrect!",2,rs.getInt(2));
            }
        }
        Assert.assertEquals("Incorrect number of updated rows!",1,matchCount);
    }

    @Test
    public void canUpdateNonPrimaryKeyCorrectly() throws Exception{
        PreparedStatement updateStatement = methodWatcher.prepareStatement(UPDATE_VALUE_BY_NAME);
        updateStatement.setInt(1,20);
        updateStatement.setString(2,"mzweben");
        updateStatement.executeUpdate();

        PreparedStatement validator = methodWatcher.prepareStatement(SELECT_BY_NAME);
        validator.setString(1,"mzweben");
        ResultSet rs = validator.executeQuery();
        int matchCount =0;
        while(rs.next()){
            if("mzweben".equalsIgnoreCase(rs.getString(1))){
                matchCount++;
                int val = rs.getInt(2);<commit_msg>PrimaryKeyTest - assert update works<commit_after>package org.apache.derby.impl.sql.execute.operations;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.junit.Assert;
import org.apache.log4j.Logger;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import com.google.common.collect.Lists;
import com.splicemachine.derby.test.framework.SpliceDataWatcher;
import com.splicemachine.derby.test.framework.SpliceSchemaWatcher;
import com.splicemachine.derby.test.framework.SpliceTableWatcher;
import com.splicemachine.derby.test.framework.SpliceUnitTest;
import com.splicemachine.derby.test.framework.SpliceWatcher;

/**
 * @author <NAME>
 *         Created on: 3/1/13
 */
public class PrimaryKeyTest extends SpliceUnitTest {
    private static final Logger LOG = Logger.getLogger(PrimaryKeyTest.class);
    protected static SpliceWatcher spliceClassWatcher = new SpliceWatcher();
	public static final String CLASS_NAME = PrimaryKeyTest.class.getSimpleName().toUpperCase();
	public static final String TABLE_NAME = "A";
	protected static SpliceSchemaWatcher spliceSchemaWatcher = new SpliceSchemaWatcher(CLASS_NAME);	
	protected static SpliceTableWatcher spliceTableWatcher = new SpliceTableWatcher(TABLE_NAME,CLASS_NAME,"(name varchar(50),val int, PRIMARY KEY(name))");
	protected static String INSERT = String.format("insert into %s.%s (name, val) values (?,?)",CLASS_NAME, TABLE_NAME);
	protected static String SELECT_BY_NAME = String.format("select * from %s.%s where name = ?",CLASS_NAME, TABLE_NAME);
	protected static String SELECT_NAME_BY_NAME = String.format("select name from %s.%s where name = ?",CLASS_NAME, TABLE_NAME);	
	protected static String UPDATE_NAME_BY_NAME = String.format("update %s.%s set name = ? where name = ?",CLASS_NAME, TABLE_NAME);
	protected static String UPDATE_VALUE_BY_NAME = String.format("update %s.%s set val = ? where name = ?",CLASS_NAME, TABLE_NAME);
	
	@ClassRule 
	public static TestRule chain = RuleChain.outerRule(spliceClassWatcher)
		.around(spliceSchemaWatcher)
		.around(spliceTableWatcher)
		.around(new SpliceDataWatcher(){
		@Override
		protected void starting(Description description) {
			try {
				PreparedStatement ps = spliceClassWatcher.prepareStatement(INSERT);
				ps.setString(1,"sfines");
				ps.setInt(2,1);
				ps.executeUpdate();
				ps.setString(1,"jleach");
				ps.setInt(2,2);
				ps.executeUpdate();
				ps.setString(1,"mzweben");
				ps.setInt(2,3);
				ps.executeUpdate();				
				ps.setString(1,"gdavis");
				ps.setInt(2,4);
				ps.executeUpdate();				
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			finally {
				spliceClassWatcher.closeAll();
			}
		}
		
	});
	@Rule public SpliceWatcher methodWatcher = new SpliceWatcher();

   
    @Test(expected=SQLException.class)
    public void cannotInsertDuplicatePks() throws Exception{
    	try {
    		PreparedStatement ps = methodWatcher.prepareStatement(INSERT);
    		ps.setString(1,"sfines");
    		ps.setInt(2,1);
    		ps.executeUpdate();
    		Assert.assertTrue("Did not throw an exception on duplicate records on primary key",false);
    	} catch (SQLException e) {
    		 PreparedStatement validator = methodWatcher.prepareStatement(SELECT_BY_NAME);
    		 validator.setString(1,"sfines");
    	        ResultSet rs = validator.executeQuery();
    	        int matchCount=0;
    	        while(rs.next()){
    	            if("sfines".equalsIgnoreCase(rs.getString(1))){
    	                matchCount++;
    	            }
    	        }
    	        Assert.assertEquals("Incorrect number of matching rows found!",1,matchCount);
    	        throw e;
    	}
    }

    @Test
    public void updateKeyColumn() throws Exception{
        PreparedStatement updateStatement = methodWatcher.prepareStatement(UPDATE_NAME_BY_NAME);
        updateStatement.setString(1,"jzhang");
        updateStatement.setString(2,"jleach");
        updateStatement.executeUpdate();
        PreparedStatement validator = methodWatcher.prepareStatement(SELECT_BY_NAME);
        validator.setString(1,"jleach");
        ResultSet rs = validator.executeQuery();
        while(rs.next()){
            Assert.fail("Should have returned nothing");
        }
        validator.setString(1,"jzhang");
        rs = validator.executeQuery();
        int matchCount = 0;
        while(rs.next()){
            if("jzhang".equalsIgnoreCase(rs.getString(1))){
                matchCount++;
                Assert.assertEquals("Column incorrect!",2,rs.getInt(2));
            }
        }
        Assert.assertEquals("Incorrect number of updated rows!",1,matchCount);
    }

    @Test
    public void updateNonKeyColumn() throws Exception{
        PreparedStatement updateStatement = methodWatcher.prepareStatement(UPDATE_VALUE_BY_NAME);
        updateStatement.setInt(1,20);
        updateStatement.setString(2,"mzweben");
        Assert.assertEquals(1, updateStatement.executeUpdate());

        PreparedStatement validator = methodWatcher.prepareStatement(SELECT_BY_NAME);
        validator.setString(1,"mzweben");
        ResultSet rs = validator.executeQuery();
        int matchCount =0;
        while(rs.next()){
            if("mzweben".equalsIgnoreCase(rs.getString(1))){
                matchCount++;
                int val = rs.getInt(2);
{'fraction_non_alphanumeric': 0.11307839737154331, 'fraction_numerical': 0.007940129597517569, 'mean_word_length': 3.611952861952862, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3408290', 'n_tokens_mistral': 3493, 'n_tokens_neox': 3250, 'n_words': 621}