<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.
 */

use Keboola\Csv\CsvFile;

class Keboola_StorageApi_Tables_AlterTest extends StorageApiTestCase
{


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

	/**
	 * @dataProvider backends
	 * @param $backend
	 */
	public function testTableColumnAdd($backend)
	{
		$importFile =  __DIR__ . '/../_data/languages.csv';
		$tableId = $this->_client->createTable($this->getTestBucketId(self::STAGE_IN, $backend), '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']);
	}

	/**
	 * @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');
	}

	/**
	 * @dataProvider backends
	 * @param $backend
	 */
	public function testTableColumnDelete($backend)
	{
		$importFile =  __DIR__ . '/../_data/languages.camel-case-columns.csv';
		$tableId = $this->_client->createTable($this->getTestBucketId(self::STAGE_IN, $backend), 'languages', new CsvFile($importFile));

		$this->_client->deleteTableColumn($tableId, 'Name');

		$detail = $this->_client->getTable($tableId);
		$this->assertEquals(array('Id'), $detail['columns']);

		try {
			$this->_client->deleteTableColumn($tableId, 'Id');
			$this->fail("Exception should be thrown when last column is remaining");
		} catch (\Keboola\StorageApi\ClientException $e) {
		}
	}

	public function testTablePkColumnDelete()
	{
		$importFile =  __DIR__ . '/../_data/languages.csv';
		$tableId = $this->_client->createTable(
			$this->getTestBucketId(),
			'languages',
			new CsvFile($importFile),
			array(
				'primaryKey' => "id,name",
			)
		);

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

		$this->assertEquals(array('id', 'name'), $detail['primaryKey']);
		$this->assertEquals(array('id', 'name'), $detail['indexedColumns']);

		$this->_client->deleteTableColumn($tableId, 'name');
		$detail = $this->_client->getTable($tableId);

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

		$this->assertEquals(array('id'), $detail['primaryKey']);
		$this->assertEquals(array('id'), $detail['indexedColumns']);
	}

	public function testPrimaryKeyAddWithDuplicty()
	{
		$primaryKeyColumns = array('id');
		$importFile = __DIR__ . '/../_data/users.csv';

		$tableId = $this->_client->createTable(
			$this->getTestBucketId(self::STAGE_IN),
			'users',
			new CsvFile($importFile),
			array()
		);<commit_msg>tests: primary key add requires params<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.
 */

use Keboola\Csv\CsvFile;

class Keboola_StorageApi_Tables_AlterTest extends StorageApiTestCase
{


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

	/**
	 * @dataProvider backends
	 * @param $backend
	 */
	public function testTableColumnAdd($backend)
	{
		$importFile =  __DIR__ . '/../_data/languages.csv';
		$tableId = $this->_client->createTable($this->getTestBucketId(self::STAGE_IN, $backend), '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']);
	}

	/**
	 * @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');
	}

	/**
	 * @dataProvider backends
	 * @param $backend
	 */
	public function testTableColumnDelete($backend)
	{
		$importFile =  __DIR__ . '/../_data/languages.camel-case-columns.csv';
		$tableId = $this->_client->createTable($this->getTestBucketId(self::STAGE_IN, $backend), 'languages', new CsvFile($importFile));

		$this->_client->deleteTableColumn($tableId, 'Name');

		$detail = $this->_client->getTable($tableId);
		$this->assertEquals(array('Id'), $detail['columns']);

		try {
			$this->_client->deleteTableColumn($tableId, 'Id');
			$this->fail("Exception should be thrown when last column is remaining");
		} catch (\Keboola\StorageApi\ClientException $e) {
		}
	}

	public function testTablePkColumnDelete()
	{
		$importFile =  __DIR__ . '/../_data/languages.csv';
		$tableId = $this->_client->createTable(
			$this->getTestBucketId(),
			'languages',
			new CsvFile($importFile),
			array(
				'primaryKey' => "id,name",
			)
		);

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

		$this->assertEquals(array('id', 'name'), $detail['primaryKey']);
		$this->assertEquals(array('id', 'name'), $detail['indexedColumns']);

		$this->_client->deleteTableColumn($tableId, 'name');
		$detail = $this->_client->getTable($tableId);

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

		$this->assertEquals(array('id'), $detail['primaryKey']);
		$this->assertEquals(array('id'), $detail['indexedColumns']);
	}

	public function testPrimaryKeyAddRequiredParam()
	{
		$indexColumn = 'city';
		$primaryKeyColumns = array();
		$importFile = __DIR__ . '/../_data/users.csv';

		$tableId = $this->_client->createTable(
			$this->getTestBucketId(self::STAGE_IN),
			'users',
			new CsvFile($importFile),
			array()
		);

		$this->_client->markTableColumnAsIndexed($tableId, $indexColumn);

		$tables = array(
			$this->_client->getTable($tableId),
		);

		foreach ($tables AS $tableDetail) {
			$this->assertArrayHasKey('primaryKey', $tableDetail);
			$this->assertEmpty($tableDetail['primaryKey']);

			$this->assertArrayHasKey('indexedColumns', $tableDetail);
			$this->assertEquals(array($indexColumn), $tableDetail['indexedColumns']);
		}


		try {
			$this->_client->createTablePrimaryKey($tableId, $primaryKeyColumns);
		} catch (\Keboola\StorageApi\ClientException $e) {
			if ($e->getStringCode() !== 'storage.validation.primaryKey') {
				throw $e;
			}
		}

		$tables = array(
			$this->_client->getTable($tableId),
		);

		foreach ($tables AS $tableDetail) {
			$this->assertArrayHasKey('primaryKey', $tableDetail);
			$this->assertEmpty($tableDetail['primaryKey']);

			$this->assertArrayHasKey('indexedColumns', $tableDetail);
			$this->assertEquals(array($indexColumn), $tableDetail['indexedColumns']);
		}
	}

	public function testPrimaryKeyAddWithDuplicty()
	{
		$primaryKeyColumns = array('id');
		$importFile = __DIR__ . '/../_data/users.csv';

		$tableId = $this->_client->createTable(
			$this->getTestBucketId(self::STAGE_IN),
			'users',
			new CsvFile($importFile),
			array()
		);
{'fraction_non_alphanumeric': 0.18743169398907103, 'fraction_numerical': 0.00273224043715847, 'mean_word_length': 5.070480928689884, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 113, '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': '361852', 'n_tokens_mistral': 2644, 'n_tokens_neox': 2389, 'n_words': 377}