package ru.spbau.mit.dbmsau.command;

import static org.hamcrest.CoreMatchers.*;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import ru.spbau.mit.dbmsau.BaseTest;
import ru.spbau.mit.dbmsau.command.exception.CommandExecutionException;
import ru.spbau.mit.dbmsau.exception.UserError;
import ru.spbau.mit.dbmsau.table.RecordManager;
import ru.spbau.mit.dbmsau.table.exception.SemanticError;

import java.util.ArrayList;

public class InsertCommandTest extends BaseTest {

    @Test
    public void testInsert() throws Exception {
        ArrayList<String> columns = new ArrayList<>();
        ArrayList<String> values = new ArrayList<>();
        columns.add("id");values.add("1");
        columns.add("name");values.add("2");

        InsertCommand command = new InsertCommand("test", columns, values);
        command.setContext(context);

        assertNotNull(command.execute());
    }

    @Test
    public void testInsertFail() throws Exception {
        thrown.expect(UserError.class);
        thrown.expectMessage("No such table `test123`");

        ArrayList<String> columns = new ArrayList<>();
        ArrayList<String> values = new ArrayList<>();
        columns.add("id");values.add("1");
        columns.add("name");values.add("2");

        InsertCommand command = new InsertCommand("test123", columns, values);
        command.setContext(context);

        assertNotNull(command.execute());
    }

    @Test
    public void testInsertWrongType() throws Exception {
        thrown.expect(SemanticError.class);
        thrown.expectMessage("`id` should be an integer");

        ArrayList<String> columns = new ArrayList<>();
        ArrayList<String> values = new ArrayList<>();
        columns.add("id");values.add("abcde");
        columns.add("name");values.add("2");

        InsertCommand command = new InsertCommand("test", columns, values);
        command.setContext(context);

        assertNotNull(command.execute());
    }

    @Before
    public void setUp() throws Exception {
        FileUtils.copyFile(
                FileUtils.toFile(RecordManager.class.getResource("test.tbl")),
                tempFolder.newFile("test.tbl")
        );
        super.setUp();
    }
}

@@ -1982,24 +1982,567 @@
 ());%0A    %7D%0A%0A
+    @Test%0A    public void testColumnsUnique() throws Exception %7B%0A        thrown.expect(SemanticError.class);%0A        thrown.expectMessage(%22Column %60id%60 referenced more than once%22);%0A%0A        ArrayList%3CString%3E columns = new ArrayList%3C%3E();%0A        ArrayList%3CString%3E values = new ArrayList%3C%3E();%0A%0A        columns.add(%22id%22);values.add(%2210%22);%0A        columns.add(%22id%22);values.add(%2210%22);%0A%0A        InsertCommand command = new InsertCommand(%22test%22, columns, values);%0A        command.setContext(context);%0A%0A        assertNotNull(command.execute());%0A    %7D%0A%0A
     @Before%0A

insert with duplicate columns