<commit_before><?php
/**
 * Created by JetBrains PhpStorm.
 * User: martinhalamicek
 * Date: 22/05/14
 * Time: 16:38
 * To change this template use File | Settings | File Templates.
 */

/**
 *
 * User: <NAME>
 * Date: 16.5.12
 * Time: 11:46
 *
 */
namespace Keboola\Test\Backend\CommonPart1;

use Keboola\StorageApi\ClientException;
use Keboola\Test\StorageApiTestCase;
use Keboola\Csv\CsvFile;

class AlterTableTest extends StorageApiTestCase
{

    public function setUp()
    {
        parent::setUp();
        $this->_initEmptyTestBuckets();
    }

    public function testTableColumnAdd()
    {
        $importFile = __DIR__ . '/../../_data/languages.csv';
        $tableId = $this->_client->createTable($this->getTestBucketId(self::STAGE_IN), 'languages', new CsvFile($importFile));

        $this->_client->addTableColumn($tableId, 'State');

        $detail = $this->_client->getTable($tableId);

        $this->assertArrayHasKey('columns', $detail);
        $this->assertContains('State', $detail['columns']);
        $this->assertEquals(array('id', 'name', 'State'), $detail['columns']);

        $importFileWithNewCol = $importFile = __DIR__ . '/../../_data/languages.with-state.csv';
        $this->_client->writeTable($tableId, new CsvFile($importFileWithNewCol));
        $this->assertLinesEqualsSorted(
            file_get_contents($importFileWithNewCol),
            $this->_client->getTableDataPreview($tableId),
            'new column is imported'
        );
    }

    /**
     * @expectedException \Keboola\StorageApi\ClientException
     */
    public function testTableExistingColumnAdd()
    {
        $importFile = __DIR__ . '/../../_data/languages.csv';
        $tableId = $this->_client->createTable($this->getTestBucketId(), 'languages', new CsvFile($importFile));
        $this->_client->addTableColumn($tableId, 'id');
    }

    public function testsTableExistingColumnAddWithDifferentCaseShouldThrowError()
    {
        $importFile = __DIR__ . '/../../_data/languages.csv';
        $tableId = $this->_client->createTable($this->getTestBucketId(), 'languages', new CsvFile($importFile));
        try {
            $this->_client->addTableColumn($tableId, 'ID');
        } catch (ClientException $e) {
            $this->assertEquals('storage.tables.columnAlreadyExists', $e->getStringCode());
        }
    }

    /**
     * @dataProvider invalidColumnNameProvider
     * @param string $columnName
     */
    public function testAddColumnWithInvalidNameShouldThrowError($columnName)
    {
        $importFile = __DIR__ . '/../../_data/languages.csv';
        $tableId = $this->_client->createTable($this->getTestBucketId(), 'languages', new CsvFile($importFile));
        try {
            $this->_client->addTableColumn(
                $tableId,
                $columnName
            );
            $this->fail('Column should not be created');
        } catch (ClientException $e) {
            $this->assertEquals('storage.tables.invalidColumnName', $e->getStringCode());
        }
    }

    public function invalidColumnNameProvider()
    {
        return [
            'too long column' => [
                str_repeat('x', 100),
            ],
            'empty column' => [
                '',
            ]
        ];
    }

    public function testTableColumnDelete()
    {
        $importFile = __DIR__ . '/../../_data/languages.camel-case-columns.csv';<commit_msg>test - max column identifier length<commit_after><?php
/**
 * Created by JetBrains PhpStorm.
 * User: martinhalamicek
 * Date: 22/05/14
 * Time: 16:38
 * To change this template use File | Settings | File Templates.
 */

/**
 *
 * User: <NAME>
 * Date: 16.5.12
 * Time: 11:46
 *
 */
namespace Keboola\Test\Backend\CommonPart1;

use Keboola\StorageApi\ClientException;
use Keboola\Test\StorageApiTestCase;
use Keboola\Csv\CsvFile;

class AlterTableTest extends StorageApiTestCase
{

    public function setUp()
    {
        parent::setUp();
        $this->_initEmptyTestBuckets();
    }

    public function testTableColumnAdd()
    {
        $importFile = __DIR__ . '/../../_data/languages.csv';
        $tableId = $this->_client->createTable($this->getTestBucketId(self::STAGE_IN), 'languages', new CsvFile($importFile));

        $this->_client->addTableColumn($tableId, 'State');

        $detail = $this->_client->getTable($tableId);

        $this->assertArrayHasKey('columns', $detail);
        $this->assertContains('State', $detail['columns']);
        $this->assertEquals(array('id', 'name', 'State'), $detail['columns']);

        $importFileWithNewCol = $importFile = __DIR__ . '/../../_data/languages.with-state.csv';
        $this->_client->writeTable($tableId, new CsvFile($importFileWithNewCol));
        $this->assertLinesEqualsSorted(
            file_get_contents($importFileWithNewCol),
            $this->_client->getTableDataPreview($tableId),
            'new column is imported'
        );
    }

    /**
     * @expectedException \Keboola\StorageApi\ClientException
     */
    public function testTableExistingColumnAdd()
    {
        $importFile = __DIR__ . '/../../_data/languages.csv';
        $tableId = $this->_client->createTable($this->getTestBucketId(), 'languages', new CsvFile($importFile));
        $this->_client->addTableColumn($tableId, 'id');
    }

    public function testsTableExistingColumnAddWithDifferentCaseShouldThrowError()
    {
        $importFile = __DIR__ . '/../../_data/languages.csv';
        $tableId = $this->_client->createTable($this->getTestBucketId(), 'languages', new CsvFile($importFile));
        try {
            $this->_client->addTableColumn($tableId, 'ID');
        } catch (ClientException $e) {
            $this->assertEquals('storage.tables.columnAlreadyExists', $e->getStringCode());
        }
    }

    /**
     * @dataProvider invalidColumnNameProvider
     * @param string $columnName
     */
    public function testAddColumnWithInvalidNameShouldThrowError($columnName)
    {
        $importFile = __DIR__ . '/../../_data/languages.csv';
        $tableId = $this->_client->createTable($this->getTestBucketId(), 'languages', new CsvFile($importFile));
        try {
            $this->_client->addTableColumn(
                $tableId,
                $columnName
            );
            $this->fail('Column should not be created');
        } catch (ClientException $e) {
            $this->assertEquals('storage.tables.invalidColumnName', $e->getStringCode());
        }
    }

    public function invalidColumnNameProvider()
    {
        return [
            'too long column' => [
                str_repeat('x', 65),
            ],
            'empty column' => [
                '',
            ]
        ];
    }

    public function testTableColumnDelete()
    {
        $importFile = __DIR__ . '/../../_data/languages.camel-case-columns.csv';
{'fraction_non_alphanumeric': 0.15199299167761718, 'fraction_numerical': 0.006570302233902759, 'mean_word_length': 2.569567483064096, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 81, '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': '1681260', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1973, 'n_words': 325}