Skip to content

Commit eed0348

Browse files
Add new test model and add orchestral testbench versions for older laravel versions
1 parent b153709 commit eed0348

File tree

9 files changed

+99
-38
lines changed

9 files changed

+99
-38
lines changed

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"pestphp/pest": "^2.19",
2626
"symfony/var-dumper": "^6.3 |^7.0.1",
2727
"mockery/mockery": "^1.6",
28-
"orchestra/testbench": "^8.20"
28+
"orchestra/testbench": "^7.38 | ^8.20 | ^9.0"
2929
},
3030
"autoload": {
3131
"psr-4": {
@@ -70,7 +70,10 @@
7070
},
7171
"autoload-dev": {
7272
"psr-4": {
73-
"Codewithkyrian\\ChromaDB\\Tests\\": "tests/"
73+
"Codewithkyrian\\ChromaDB\\Tests\\": "tests/",
74+
"Workbench\\App\\": "workbench/app/",
75+
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
76+
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
7477
}
7578
}
7679
}

config/chromadb.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
| this is set to false, then the ChromaDB Sync feature will not be
6363
| enabled.
6464
*/
65-
'enabled' => env('CHROMA_SYNC_ENABLED', false),
65+
'enabled' => env('CHROMA_SYNC_ENABLED', true),
6666

6767
/*
6868
|--------------------------------------------------------------------------

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</source>
1717
<php>
1818
<env name="APP_ENV" value="testing"/>
19+
<env name="DB_CONNECTION" value="testing"/>
1920
<env name="CHROMA_TENANT" value="test_tenant"/>
2021
<env name="CHROMA_DATABASE" value="test_database"/>
2122
</php>

src/Concerns/HasChromaCollection.php

+18-23
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,23 @@
44

55
namespace Codewithkyrian\ChromaDB\Concerns;
66

7+
use Codewithkyrian\ChromaDB\Contracts\ChromaModel;
78
use Codewithkyrian\ChromaDB\Embeddings\EmbeddingFunction;
89
use Codewithkyrian\ChromaDB\Facades\ChromaDB;
910
use Codewithkyrian\ChromaDB\Jobs\UpdateChromaCollectionJob;
1011
use Codewithkyrian\ChromaDB\Resources\CollectionResource;
12+
use Illuminate\Database\Eloquent\Model;
1113
use Illuminate\Support\Facades\DB;
1214
use Illuminate\Database\Eloquent\Builder;
1315

14-
/**
15-
* @method mixed getKey()
16-
* @method string getTable()
17-
* @method mixed getAttribute(string $field)
18-
* @method array getChanges()
19-
* @method self saved(\Illuminate\Events\QueuedClosure|\Closure|string|array $callback)
20-
* @method self deleted(\Illuminate\Events\QueuedClosure|\Closure|string|array $callback)
21-
*/
2216
trait HasChromaCollection
2317
{
2418
private static ?CollectionResource $chromaCollection;
2519

2620
protected static function bootHasChromaCollection(): void
2721
{
28-
$model = new static();
29-
30-
static::$chromaCollection = ChromaDB::getOrCreateCollection(
31-
name: $model->collectionName(),
32-
embeddingFunction: $model->embeddingFunction(),
33-
);
34-
3522
if (config('chromadb.sync.enabled')) {
36-
static::saved(function (self $model) {
23+
static::saved(function (Model&ChromaModel $model) {
3724
$changedFields = array_keys($model->getChanges());
3825

3926
if (!config('chromadb.sync.queue', false)) {
@@ -43,14 +30,22 @@ protected static function bootHasChromaCollection(): void
4330
}
4431
});
4532

46-
static::deleted(function (self $model) {
33+
static::deleted(function (Model&ChromaModel $model) {
4734
self::getChromaCollection()->delete([$model->getKey()]);
4835
});
4936
}
5037
}
5138

5239
public static function getChromaCollection(): CollectionResource
5340
{
41+
$model = new static();
42+
43+
static::$chromaCollection ??= ChromaDB::getOrCreateCollection(
44+
name: $model->collectionName(),
45+
embeddingFunction: $model->embeddingFunction(),
46+
);
47+
48+
5449
return static::$chromaCollection;
5550
}
5651

@@ -83,7 +78,7 @@ public function scopeQueryChromaCollection(Builder $query, string $queryText, in
8378
*
8479
* @return string[]
8580
*/
86-
protected function metadataFields(): array
81+
public function metadataFields(): array
8782
{
8883
return ['id'];
8984
}
@@ -93,29 +88,29 @@ protected function metadataFields(): array
9388
*
9489
* @return string[]
9590
*/
96-
protected function documentFields(): array
91+
public function documentFields(): array
9792
{
9893
return [];
9994
}
10095

10196
/**
10297
* The embedding function to use for the collection.
10398
*/
104-
protected function embeddingFunction(): ?EmbeddingFunction
99+
public function embeddingFunction(): ?EmbeddingFunction
105100
{
106101
return null;
107102
}
108103

109104
/**
110105
* The collection name to use for the model.
111106
*/
112-
protected function collectionName(): string
107+
public function collectionName(): string
113108
{
114109
return $this->getTable();
115110
}
116111

117112

118-
private function generateMetadata(): array
113+
public function generateMetadata(): array
119114
{
120115

121116
return collect($this->metadataFields())
@@ -125,7 +120,7 @@ private function generateMetadata(): array
125120
->toArray();
126121
}
127122

128-
protected function generateChromaDocument(): string
123+
public function generateChromaDocument(): string
129124
{
130125
return collect($this->documentFields())
131126
->map(function (string $field) {

src/Contracts/ChromaModel.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44

55
namespace Codewithkyrian\ChromaDB\Contracts;
66

7+
use Codewithkyrian\ChromaDB\Embeddings\EmbeddingFunction;
78
use Codewithkyrian\ChromaDB\Resources\CollectionResource;
89

910
interface ChromaModel
1011
{
1112

1213
public function collectionName(): string;
1314

14-
public function embeddingFunction(): string;
15+
public function embeddingFunction(): ?EmbeddingFunction;
1516

1617
function metadataFields(): array;
1718

1819
function documentFields(): array;
1920

2021
public function generateMetadata(): array;
2122

22-
public function generateChromaDocument(): array;
23+
public function generateChromaDocument(): string;
2324

2425
public static function getChromaCollection(): CollectionResource;
2526

tests/Concerns/HasChromaCollectionTest.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@
33
declare(strict_types=1);
44

55
use Codewithkyrian\ChromaDB\Concerns\HasChromaCollection;
6+
use Codewithkyrian\ChromaDB\Jobs\UpdateChromaCollectionJob;
67
use Codewithkyrian\ChromaDB\Resources\CollectionResource;
78
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Support\Facades\Event;
10+
use Illuminate\Support\Facades\Queue;
11+
use Workbench\App\Models\TestModel;
812

913
it('creates a chroma collection on boot', function () {
10-
$model = new class extends Model {
11-
use HasChromaCollection;
1214

13-
public function collectionName(): string
14-
{
15-
return 'test_collection';
16-
}
17-
};
18-
19-
$collection = $model->getChromaCollection();
15+
$collection = TestModel::getChromaCollection();
2016
expect($collection)
2117
->toBeInstanceOf(CollectionResource::class)
22-
->and($collection->name)->toBe('test_collection');
18+
->and($collection->name)->toBe('test_models');
19+
});
20+
21+
it('dispatches a job to update the collection on save', function () {
22+
Queue::fake();
23+
24+
$model = TestModel::create([
25+
'title' => 'Test Model',
26+
'document' => 'This is a test model'
27+
]);
28+
29+
Queue::assertPushed(UpdateChromaCollectionJob::class);
2330
});

tests/TestCase.php

+8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace Codewithkyrian\ChromaDB\Tests;
66

77
use Codewithkyrian\ChromaDB\ChromaServiceProvider;
8+
use Orchestra\Testbench\Attributes\WithMigration;
9+
use function Orchestra\Testbench\workbench_path;
810

11+
#[WithMigration('laravel', 'job')]
912
class TestCase extends \Orchestra\Testbench\TestCase
1013
{
1114
protected function getPackageProviders($app): array
@@ -14,4 +17,9 @@ protected function getPackageProviders($app): array
1417
ChromaServiceProvider::class
1518
];
1619
}
20+
21+
protected function defineDatabaseMigrations(): void
22+
{
23+
$this->loadMigrationsFrom(workbench_path('database/migrations'));
24+
}
1725
}

workbench/app/Models/TestModel.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Workbench\App\Models;
4+
5+
use Codewithkyrian\ChromaDB\Concerns\HasChromaCollection;
6+
use Codewithkyrian\ChromaDB\Contracts\ChromaModel;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class TestModel extends Model implements ChromaModel
10+
{
11+
use HasChromaCollection;
12+
13+
protected $fillable = [
14+
'title',
15+
'document',
16+
];
17+
18+
public function documentFields(): array
19+
{
20+
return [
21+
'document',
22+
];
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
public function up(): void
9+
{
10+
Schema::create('test_models', function (Blueprint $table) {
11+
$table->id();
12+
$table->string('title');
13+
$table->text('document');
14+
$table->timestamps();
15+
});
16+
}
17+
18+
public function down(): void
19+
{
20+
Schema::dropIfExists('test_models');
21+
}
22+
};

0 commit comments

Comments
 (0)