Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for SQL codegen #11

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
<testsuites>
<testsuite name="tests">
<directory>test/tests/</directory>
Expand Down
10 changes: 7 additions & 3 deletions src/Query/Generator/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ private function generateWhere(Query $query, WhereCondition|WhereGroup $where =
$where = $query->getWhere();
}
if ($where instanceof WhereCondition) {
if ($where->value === null && $where->operator === "=") {
$where->operator = "IS";
if ($where->value === null) {
if ($where->operator === "=") {
$where->operator = "IS";
} elseif ($where->operator === "!=") {
$where->operator = "IS NOT";
}
}

$value = $where->value;
Expand Down Expand Up @@ -233,7 +237,7 @@ private function generateFields(Query $query): string
}

} else if ($field instanceof UpdateField) {
$fieldStrings[] = $this->columnEnclosure . $field->key . $this->columnEnclosure . "=" . $this->generateValue($field->value);
$fieldStrings[] = $this->columnEnclosure . $field->key . $this->columnEnclosure . " = " . $this->generateValue($field->value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Query/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(null|WhereCondition|array|WhereGroup $where = null,
null|array $order = null,
null|array $fields = null,
null|Limit|array|int $limit = null,
null|array $group = null)
null|array $group = null)
{
if ($where) {
$this->where($where);
Expand Down
287 changes: 287 additions & 0 deletions test/tests/SQLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
<?php

namespace Aternos\Model\Test\Tests;

use Aternos\Model\Query\DeleteQuery;
use Aternos\Model\Query\Generator\SQL;
use Aternos\Model\Query\Limit;
use Aternos\Model\Query\SelectField;
use Aternos\Model\Query\SelectQuery;
use Aternos\Model\Query\UpdateQuery;
use Aternos\Model\Query\WhereCondition;
use Aternos\Model\Query\WhereGroup;
use Aternos\Model\Test\Src\TestModel;
use PHPUnit\Framework\TestCase;

class SQLTest extends TestCase
{
protected SQL $sql;

protected function setUp(): void
{
$this->sql = new SQL("addslashes");
}

public function testSelect()
{
$query = new SelectQuery();
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test`", $this->sql->generate($query));
}

public function testSelectWhereCondition()
{
$query = new SelectQuery(
new WhereCondition('text', 'value'),
);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`text` = 'value')", $this->sql->generate($query));
}

public function testSelectWhereConditionNumber()
{
$query = new SelectQuery(
new WhereCondition('number', 1),
);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`number` = 1)", $this->sql->generate($query));
}

public function testSelectWhereConditionFloat()
{
$query = new SelectQuery(
new WhereCondition('number', 1.5),
);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`number` = 1.5)", $this->sql->generate($query));
}

public function testSelectWhereConditionOperator()
{
$query = new SelectQuery(
new WhereCondition('text', 'value', '!='),
);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`text` != 'value')", $this->sql->generate($query));
}

public function testSelectWhereConditionNull()
{
$query = new SelectQuery(
new WhereCondition('text', null),
);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`text` IS NULL)", $this->sql->generate($query));
}

public function testSelectWhereConditionNotNull()
{
$query = new SelectQuery(
new WhereCondition('text', null, '!='),
);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`text` IS NOT NULL)", $this->sql->generate($query));
}

public function testSelectWhereConditionIN()
{
$query = new SelectQuery(
new WhereCondition('number', [1.5, 5, "a", ["b", "c", "d"]], 'IN'),
);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`number` IN (1.5, 5, 'a', ('b', 'c', 'd')))", $this->sql->generate($query));
}

public function testSelectWhereArray()
{
$query = new SelectQuery([
'number' => 1,
'text' => 'value',
]);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`number` = 1 AND `text` = 'value')", $this->sql->generate($query));
}

public function testSelectWhereArrayOperator()
{
$query = new SelectQuery([
['number', 0.5],
['number', '<', 1],
'text' => 'value',
]);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`number` = 0.5 AND `number` < 1 AND `text` = 'value')", $this->sql->generate($query));
}

public function testSelectWhereGroup()
{
$query = new SelectQuery(
new WhereGroup([
new WhereCondition('number', 1, '<'),
new WhereCondition('number', 0, '>'),
]),
);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`number` < 1 AND `number` > 0)", $this->sql->generate($query));
}

public function testSelectWhereGroupOR()
{
$query = new SelectQuery(
new WhereGroup([
new WhereCondition('number', 1, '>'),
new WhereCondition('number', 0, '<'),
], WhereGroup::OR),
);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` WHERE (`number` > 1 OR `number` < 0)", $this->sql->generate($query));
}

public function testSelectSingleField()
{
$query = new SelectQuery(fields: ['text']);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT `text` FROM `test`", $this->sql->generate($query));
}

public function testSelectSelectFields()
{
$query = new SelectQuery(fields: ['text', 'number']);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT `text`, `number` FROM `test`", $this->sql->generate($query));
}

public function testSelectOrder()
{
$query = new SelectQuery(order: [
'number' => 'ASC',
'text' => 'DESC',
]);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` ORDER BY `number` ASC, `text` DESC", $this->sql->generate($query));
}

public function testSelectCount()
{
$query = new SelectQuery(fields: [
(new SelectField('number'))->setFunction(SelectField::COUNT),
]);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT COUNT(`number`) FROM `test`", $this->sql->generate($query));
}

public function testSelectCountStar()
{
$query = new SelectQuery(fields: [
(new SelectField('*'))->setFunction(SelectField::COUNT),
]);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT COUNT(*) FROM `test`", $this->sql->generate($query));
}

public function testSelectSum()
{
$query = new SelectQuery(fields: [
(new SelectField('number'))->setFunction(SelectField::SUM),
]);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT SUM(`number`) FROM `test`", $this->sql->generate($query));
}

public function testSelectSumAs()
{
$query = new SelectQuery(fields: [
(new SelectField('number'))->setFunction(SelectField::SUM)->setAlias('sum'),
]);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT SUM(`number`) AS `sum` FROM `test`", $this->sql->generate($query));
}

public function testSelectAVG()
{
$query = new SelectQuery(fields: [
(new SelectField('number'))->setFunction(SelectField::AVERAGE),
]);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT AVG(`number`) FROM `test`", $this->sql->generate($query));
}

public function testSelectLimitNumber()
{
$query = new SelectQuery(limit:100);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` LIMIT 0, 100", $this->sql->generate($query));
}

public function testSelectLimitArray()
{
$query = new SelectQuery(limit:[5, 100]);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` LIMIT 5, 100", $this->sql->generate($query));
}

public function testSelectLimit()
{
$query = new SelectQuery(limit:new Limit(100, 5));
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` LIMIT 5, 100", $this->sql->generate($query));
}

public function testSelectGroup()
{
$query = new SelectQuery(group:['number', 'text']);
$query->modelClassName = TestModel::class;

$this->assertEquals("SELECT * FROM `test` GROUP BY `number`, `text`", $this->sql->generate($query));
}

public function testDelete()
{
$query = new DeleteQuery();
$query->modelClassName = TestModel::class;

/** @noinspection SqlWithoutWhere */
$this->assertEquals("DELETE FROM `test`", $this->sql->generate($query));
}

public function testDeleteLimit()
{
$query = new DeleteQuery(limit: 100);
$query->modelClassName = TestModel::class;

/** @noinspection SqlWithoutWhere */
$this->assertEquals("DELETE FROM `test` LIMIT 100", $this->sql->generate($query));
}

public function testUpdate()
{
$query = new UpdateQuery(['text' => 'value']);
$query->modelClassName = TestModel::class;

/** @noinspection SqlWithoutWhere */
$this->assertEquals("UPDATE `test` SET `text` = 'value'", $this->sql->generate($query));
}
}
Loading