<commit_before><?php

require_once(sprintf(
	'%s/_PackageGetDatabaseMock.php',
	dirname(__FILE__)
));

use Nether\Database;
use Nether\Database\Verse;

class VerseQueryTest
extends PHPUnit\Framework\TestCase {

	use
	GetDatabaseMock;

	////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////

	const
	TestTableName = 'TableName';

	const
	TestTableJoin = 'Table2 ON TableName.Field1=Table2.Field1';

	const
	TestTableJoins = [
		'Table2 ON TableName.Field1=Table2.Field1',
		'Three' => 'Table3 ON TableName.Field1=Table3.Field1'
	];

	const
	TestSelectFields = [
		'Field1',
		'Field2',
		'Field3'
	];

	const
	TestInsertValues = [
		'Field1' => ':Value1',
		'Field2' => ':Value2',
		'Field3' => ':Value3'
	];

	const
	TestCreateFields = [
		'Field1 INT',
		'Field2 BIGINT',
		'Field3 VARCHAR(255)'
	];

	const
	TestConditionBasic = 'Field1=:Value1';

	const
	TestConditionBasic2 = 'Field2=:Value2';

	const
	TestConditionArray = 'Field1 IN(:ValueList)';

	const
	TestValueList = [ ':ValueList' => [42, 69, 1080] ];

	////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////

	/** @test */
	public function
	TestSelectQuery() {

		$DB = $this->GetDatabaseMock();
		$Verse = $DB->NewVerse();
		$Dataset = [];
		$QuerySimple = "SELECT Field1, Field2, Field3 FROM TableName WHERE (Field1 IN(:ValueList))";
		$QueryExpanded = "SELECT Field1, Field2, Field3 FROM TableName WHERE (Field1 IN(:__ValueList__0,:__ValueList__1,:__ValueList__2))";
		$QueryPretty = "SELECT Field1, Field2, Field3\nFROM TableName\nWHERE (Field1 IN(:ValueList))";

		$SQL = (
			$Verse
			->Select(static::TestTableName)
			->Values(static::TestSelectFields)
			->Where(static::TestConditionArray)
			->GetSQL()
		);

		////////

		// check that the verse looks good.

		$this->AssertEquals($QuerySimple, $SQL);
		$this->AssertEquals($QuerySimple, (string)$Verse);

		// the rest of this is sanity checking a few other core features
		// of the database lib as well.

		// check that the query builder was able to map our data.

		$DB->Query_BuildDataset(
			$SQL,
			(object)static::TestValueList,
			$Dataset
		);

		$this->AssertTrue(is_array($Dataset));
		$this->AssertTrue(count($Dataset) === 1);
		$this->AssertTrue(count($Dataset[':ValueList']) === 3);

		// check that the query builder was able to expand our data and sql.

		$DB->Query_ExpandDataset(
			$SQL,
			(object)static::TestValueList,
			$Dataset
		);

		$this->AssertEquals($QueryExpanded, $SQL);
		$this->AssertTrue(is_array($Dataset));
		$this->AssertTrue(count($Dataset) === 3);
		$this->AssertTrue(array_key_exists(':__ValueList__0', $Dataset));
		$this->AssertTrue(array_key_exists(':__ValueList__1', $Dataset));
		$this->AssertTrue(array_key_exists(':__ValueList__2', $Dataset));

		// check that pretty print does something.

		$Verse->Pretty = TRUE;
		$this->AssertEquals($QueryPretty, $Verse->GetSQL());

		return;
	}

	/** @test */
	public function
	TestSelectJoinedQuery() {

		$DB = $this->GetDatabaseMock();
		$Verse = $DB->NewVerse();

		$QueryJoin1 = "SELECT Field1, Field2, Field3 FROM TableName LEFT JOIN (Table2 ON TableName.Field1=Table2.Field1) WHERE (Field1 IN(:ValueList))";
		$QueryJoin2 = "SELECT Field1, Field2, Field3 FROM TableName LEFT JOIN (Table2 ON TableName.Field1=Table2.Field1) LEFT JOIN (Table3 ON TableName.Field1=Table3.Field1) WHERE (Field1 IN(:ValueList))";

		////////

		$SQL = (
			$Verse
			->Select(static::TestTableName)
			->Join(static::TestTableJoin)
			->Values(static::TestSelectFields)
			->Where(static::TestConditionArray)
			->GetSQL()
		);
<commit_msg>fix for fixes<commit_after><?php

require_once(sprintf(
	'%s/_PackageGetDatabaseMock.php',
	dirname(__FILE__)
));

use Nether\Database;
use Nether\Database\Verse;

class VerseQueryTest
extends PHPUnit\Framework\TestCase {

	use
	GetDatabaseMock;

	////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////

	const
	TestTableName = 'TableName';

	const
	TestTableJoin = 'Table2 ON TableName.Field1=Table2.Field1';

	const
	TestTableJoins = [
		'Table2 ON TableName.Field1=Table2.Field1',
		'Three' => 'Table3 ON TableName.Field1=Table3.Field1'
	];

	const
	TestSelectFields = [
		'Field1',
		'Field2',
		'Field3'
	];

	const
	TestInsertValues = [
		'Field1' => ':Value1',
		'Field2' => ':Value2',
		'Field3' => ':Value3'
	];

	const
	TestCreateFields = [
		'Field1 INT',
		'Field2 BIGINT',
		'Field3 VARCHAR(255)'
	];

	const
	TestConditionBasic = 'Field1=:Value1';

	const
	TestConditionBasic2 = 'Field2=:Value2';

	const
	TestConditionArray = 'Field1 IN(:ValueList)';

	const
	TestValueList = [ ':ValueList' => [42, 69, 1080] ];

	////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////

	/** @test */
	public function
	TestSelectQuery() {

		$DB = $this->GetDatabaseMock();
		$Verse = $DB->NewVerse();
		$Dataset = [];
		$QuerySimple = "SELECT Field1, Field2, Field3 FROM TableName WHERE (Field1 IN(:ValueList))";
		$QueryExpanded = "SELECT Field1, Field2, Field3 FROM TableName WHERE (Field1 IN(:__ValueList__0,:__ValueList__1,:__ValueList__2))";
		$QueryPretty = "SELECT Field1, Field2, Field3\nFROM TableName\nWHERE (Field1 IN(:ValueList))";

		$SQL = (
			$Verse
			->Select(static::TestTableName)
			->Values(static::TestSelectFields)
			->Where(static::TestConditionArray)
			->GetSQL()
		);

		////////

		// check that the verse looks good.

		$this->AssertEquals($QuerySimple, $SQL);
		$this->AssertEquals($QuerySimple, (string)$Verse);

		// the rest of this is sanity checking a few other core features
		// of the database lib as well.

		// check that the query builder was able to map our data.

		$DB->Query_BuildDataset(
			$SQL,
			(object)static::TestValueList,
			$Dataset
		);

		$this->AssertTrue(is_array($Dataset));
		$this->AssertTrue(count($Dataset) === 1);
		$this->AssertTrue(count($Dataset[':ValueList']) === 3);

		// check that the query builder was able to expand our data and sql.

		$DB->Query_ExpandDataset(
			$SQL,
			(object)static::TestValueList,
			$Dataset
		);

		$this->AssertEquals($QueryExpanded, $SQL);
		$this->AssertTrue(is_array($Dataset));
		$this->AssertTrue(count($Dataset) === 3);
		$this->AssertTrue(array_key_exists(':__ValueList__0', $Dataset));
		$this->AssertTrue(array_key_exists(':__ValueList__1', $Dataset));
		$this->AssertTrue(array_key_exists(':__ValueList__2', $Dataset));

		// check that pretty print does something.

		$Verse->Pretty = TRUE;
		$this->AssertEquals($QueryPretty, $Verse->GetSQL());

		return;
	}

	/** @test */
	public function
	TestSelectJoinedQuery() {

		$DB = $this->GetDatabaseMock();
		$Verse = $DB->NewVerse();

		$QueryJoin1 = "SELECT Field1, Field2, Field3 FROM TableName LEFT JOIN Table2 ON TableName.Field1=Table2.Field1 WHERE (Field1 IN(:ValueList))";
		$QueryJoin2 = "SELECT Field1, Field2, Field3 FROM TableName LEFT JOIN Table2 ON TableName.Field1=Table2.Field1 LEFT JOIN Table3 ON TableName.Field1=Table3.Field1 WHERE (Field1 IN(:ValueList))";

		////////

		$SQL = (
			$Verse
			->Select(static::TestTableName)
			->Join(static::TestTableJoin)
			->Values(static::TestSelectFields)
			->Where(static::TestConditionArray)
			->GetSQL()
		);

{'fraction_non_alphanumeric': 0.21689683184402925, 'fraction_numerical': 0.022745735174654752, 'mean_word_length': 3.1476698483997754, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, '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': '6177171', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2398, 'n_words': 498}