package net.ernstsson.sqlbuilder;

import org.junit.Test;

import static java.lang.String.format;
import static net.ernstsson.sqlbuilder.Column.column;
import static net.ernstsson.sqlbuilder.ColumnSelector.columns;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

public final class ColumnSelectorTest {
    private static final String COLUMN_NAME = "column_name";
    private static final String COLUMN_NAME2 = "column_name2";
    private static final Column COLUMN = column(COLUMN_NAME);
    private static final Column COLUMN2 = column(COLUMN_NAME2);
    private static final String SHOULD_CREATE_SQL_FOR_MULTIPLE_COLUMNS_FORMATTER = "%s, %s";
    private static final String EXPECTED_SHOULD_CREATE_SQL_FOR_MULTIPLE_COLUMNS_SQL = format(
            SHOULD_CREATE_SQL_FOR_MULTIPLE_COLUMNS_FORMATTER,
            COLUMN_NAME, COLUMN_NAME2);

    @Test
    public void shouldCreateSqlForColumn() {
        assertThat(columns(COLUMN).toSql(), is(COLUMN_NAME));
    }

    @Test(expected = AssertionError.class)
    public void shouldThrowExceptionForNoColumns() {
        columns();
    }

    @Test
    public void shouldCreateSqlForMultipleColumns() {
        assertThat(columns(COLUMN, COLUMN2).toSql(),
                is(EXPECTED_SHOULD_CREATE_SQL_FOR_MULTIPLE_COLUMNS_SQL));
    }

    @Test(expected = AssertionError.class)
    public void shouldThrowExceptionForSameColumnMoreThanOnce() {
        columns(COLUMN, COLUMN);
    }

    @Test
    public void shouldHaveHashCode() {
        assertThat(columns(COLUMN).hashCode(), notNullValue());
    }

    @Test
    public void shouldHaveEquals() {
        assertThat(columns(COLUMN).equals(columns(COLUMN)), is(true));
    }

    @Test
    public void shouldHaveToString() {
        assertThat(columns(COLUMN).toString(), notNullValue());
    }
}
