Skip to content

Commit 5dba814

Browse files
committed
Added test for CodeIgniter
1 parent 7270fb3 commit 5dba814

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"php": ">= 8.1"
2424
},
2525
"require-dev": {
26+
"codeigniter4/framework": "^4",
2627
"doctrine/dbal": "^4",
2728
"doctrine/orm": "^3",
2829
"phpunit/phpunit": "^10",

tests/CodeIgniterTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/codeigniter4/framework/system/Test/bootstrap.php';
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use CodeIgniter\Config\BaseConfig;
8+
use CodeIgniter\Config\BaseService;
9+
use CodeIgniter\Config\Factories;
10+
use CodeIgniter\Database\Config;
11+
use CodeIgniter\Model;
12+
use CodeIgniter\Settings\Settings;
13+
use CodeIgniter\Test\DatabaseTestTrait;
14+
use Config\Database;
15+
use Pgvector\Vector;
16+
17+
class ItemModel extends Model
18+
{
19+
protected $table = 'ci_items';
20+
protected $allowedFields = ['embedding'];
21+
}
22+
23+
final class CodeIgniterTest extends TestCase
24+
{
25+
public function testWorks()
26+
{
27+
$config = [
28+
'DSN' => 'Postgre://localhost/pgvector_php_test?charset=utf8',
29+
];
30+
$db = Database::connect($config);
31+
32+
$db->query('CREATE EXTENSION IF NOT EXISTS vector');
33+
$db->query('DROP TABLE IF EXISTS ci_items');
34+
$db->query('CREATE TABLE IF NOT EXISTS ci_items (id bigserial PRIMARY KEY, embedding vector(3))');
35+
36+
$itemModel = new ItemModel($db);
37+
$itemModel->insert(['embedding' => new Vector([1, 1, 1])], false);
38+
$itemModel->insert(['embedding' => new Vector([2, 2, 2])], false);
39+
$itemModel->insert(['embedding' => new Vector([1, 1, 2])], false);
40+
41+
$items = $itemModel->orderBy("embedding <-> '[1,1,1]'")->findAll();
42+
$this->assertEquals([1, 3, 2], array_map(fn ($v) => $v['id'], $items));
43+
}
44+
}

0 commit comments

Comments
 (0)