|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | + |
| 5 | +use Doctrine\DBAL\DriverManager; |
| 6 | +use Doctrine\DBAL\Exception\TableNotFoundException; |
| 7 | +use Doctrine\DBAL\Types\Type; |
| 8 | +use Doctrine\ORM\EntityManager; |
| 9 | +use Doctrine\ORM\ORMSetup; |
| 10 | +use Doctrine\ORM\Tools\SchemaTool; |
| 11 | +use Pgvector\HalfVector; |
| 12 | +use Pgvector\SparseVector; |
| 13 | +use Pgvector\Vector; |
| 14 | + |
| 15 | +require_once __DIR__ . '/models/DoctrineItem.php'; |
| 16 | + |
| 17 | +final class DoctrineTest extends TestCase |
| 18 | +{ |
| 19 | + public function testTypes() |
| 20 | + { |
| 21 | + $config = ORMSetup::createAttributeMetadataConfiguration( |
| 22 | + paths: [__DIR__ . '/models'], |
| 23 | + isDevMode: true |
| 24 | + ); |
| 25 | + |
| 26 | + $connection = DriverManager::getConnection([ |
| 27 | + 'driver' => 'pgsql', |
| 28 | + 'dbname' => 'pgvector_php_test' |
| 29 | + ], $config); |
| 30 | + |
| 31 | + $entityManager = new EntityManager($connection, $config); |
| 32 | + |
| 33 | + Type::addType('vector', 'Pgvector\Doctrine\VectorType'); |
| 34 | + Type::addType('halfvec', 'Pgvector\Doctrine\HalfVectorType'); |
| 35 | + Type::addType('sparsevec', 'Pgvector\Doctrine\SparseVectorType'); |
| 36 | + |
| 37 | + $schemaManager = $entityManager->getConnection()->createSchemaManager(); |
| 38 | + try { |
| 39 | + $schemaManager->dropTable('doctrine_items'); |
| 40 | + } catch (TableNotFoundException $e) { |
| 41 | + // do nothing |
| 42 | + } |
| 43 | + |
| 44 | + $schemaTool = new SchemaTool($entityManager); |
| 45 | + $schemaTool->createSchema([$entityManager->getClassMetadata('DoctrineItem')]); |
| 46 | + |
| 47 | + $item = new DoctrineItem(); |
| 48 | + $item->setEmbedding(new Vector([1, 2, 3])); |
| 49 | + $item->setHalfEmbedding(new HalfVector([4, 5, 6])); |
| 50 | + $item->setSparseEmbedding(new SparseVector([7, 8, 9])); |
| 51 | + $entityManager->persist($item); |
| 52 | + $entityManager->flush(); |
| 53 | + |
| 54 | + $itemRepository = $entityManager->getRepository('DoctrineItem'); |
| 55 | + $item = $itemRepository->find(1); |
| 56 | + $this->assertEquals([1, 2, 3], $item->getEmbedding()->toArray()); |
| 57 | + $this->assertEquals([4, 5, 6], $item->getHalfEmbedding()->toArray()); |
| 58 | + $this->assertEquals([7, 8, 9], $item->getSparseEmbedding()->toArray()); |
| 59 | + } |
| 60 | +} |
0 commit comments