Skip to content

Commit

Permalink
remove saving to file
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed May 28, 2023
1 parent 0205581 commit 9e8c7e7
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 129 deletions.
72 changes: 28 additions & 44 deletions STUBS/Tests/TransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,37 @@

use Mockery;
use Tests\TestCase;
use App\Models\Source;
use App\Models\Document;
use App\Models\Transformer;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use App\Source\Types\[RESOURCE_CLASS_NAME];

it('should create [RESOURCE_CLASS_NAME]', function () {
$user = User::factory()->withPersonalTeam()
->create();
use App\Transformers\TransformerTypeEnum;
use App\Transformers\Types\[RESOURCE_CLASS_NAME];

class [RESOURCE_CLASS_NAME]Test extends TestCase
{
use SharedSetupForPdfFile;

public function test_parses()
{
$document = Document::factory()->html()->create();

$transformerModel = Transformer::factory()->create([
'type' => TransformerTypeEnum::[RESOURCE_CLASS_NAME],
]);

Storage::fake('projects');

$user = $this->createTeam($user);
$transformer = new [RESOURCE_CLASS_NAME]($document);
$this->assertDatabaseCount('document_chunks', 0);
$transformer->handle($transformerModel);
$this->assertDatabaseCount('document_chunks', 1);

$project = Project::factory()->create([
'team_id' => $user->current_team_id,
]);
$document = Document::first();
$content = $document->content;

assertDatabaseCount('transformers', 0);
$this->assertNotNull($content);

$this->actingAs($user)
->get(route('transformers.[RESOURCE_KEY].create', [
'project' => $project->id,
]))
->assertRedirectToRoute('projects.show', [
'project' => $project->id,
]);
assertDatabaseCount('transformers', 1);
});

it('should run transformer [RESOURCE_CLASS_NAME]', function () {
$user = User::factory()->withPersonalTeam()
->create();

$user = $this->createTeam($user);

$project = Project::factory()->create([
'team_id' => $user->current_team_id,
]);

$transformer = Transformer::factory()->create(
['project_id' => $project->id]
);

$this->actingAs($user)
->post(route('transformers.[RESOURCE_KEY].run', [
'project' => $project->id,
'transformer' => $transformer->id,
]))
->assertRedirectToRoute('projects.show', [
'project' => $project->id,
]);
});
}

}
4 changes: 1 addition & 3 deletions STUBS/Transformer/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ class [RESOURCE_CLASS_NAME] extends BaseTransformer
{
public function handle(Transformer $transformer): Document
{
$filePath = $this->document->pathToFile();

if (str($filePath)->endsWith('.html')) {

if (str($this->document->guid)->endsWith('.html')) {
if (! DocumentChunk::query()
->where('document_id', $this->document->id)
->exists()) {
Expand Down
10 changes: 3 additions & 7 deletions app/Source/Types/WebFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace App\Source\Types;

use App\Exceptions\SourceMissingRequiredMetaDataException;
use App\Ingress\StatusEnum;
use App\Models\Document;
use App\Ingress\StatusEnum;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use App\Exceptions\SourceMissingRequiredMetaDataException;

class WebFile extends BaseSourceType
{
Expand All @@ -22,11 +22,6 @@ public function handle(): Document

$fileContents = Http::get($url)->body();

$path = $this->getPath($fileName);

Storage::disk('projects')
->put($path, $fileContents);

return Document::where('guid', $fileName)
->where('source_id', $this->source->id)
->firstOrCreate(
Expand All @@ -36,6 +31,7 @@ public function handle(): Document
],
[
'status' => StatusEnum::Complete,
'content' => $fileContents
]
);

Expand Down
7 changes: 3 additions & 4 deletions app/Transformers/Types/Html2Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
namespace App\Transformers\Types;

use App\Models\Document;
use App\Models\DocumentChunk;
use App\Models\Transformer;
use App\Transformers\BaseTransformer;
use App\Models\DocumentChunk;
use Soundasleep\Html2Text as Helper;
use App\Transformers\BaseTransformer;

class Html2Text extends BaseTransformer
{
public function handle(Transformer $transformer): Document
{
$filePath = $this->document->pathToFile();

if (str($filePath)->endsWith('.html')) {

if (str($this->document->guid)->endsWith(".html")) {
if (! DocumentChunk::query()
->where('document_id', $this->document->id)
->exists()) {
Expand Down
12 changes: 11 additions & 1 deletion database/factories/DocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Database\Factories;

use App\Ingress\StatusEnum;
use App\Models\Source;
use App\Ingress\StatusEnum;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
Expand All @@ -29,4 +29,14 @@ public function definition(): array
'meta_data' => [],
];
}

public function html()
{
return $this->state(function (array $attributes) {
return [
'content' => fake()->randomHtml(),
'guid' => "foo.html"
];
});
}
}
9 changes: 9 additions & 0 deletions database/factories/TransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ public function pdfTranformer()
];
});
}

public function html2text()
{
return $this->state(function (array $attributes) {
return [
'type' => TransformerTypeEnum::Html2Text,
];
});
}
}
80 changes: 21 additions & 59 deletions tests/Feature/Html2TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,39 @@

namespace Tests\Feature;

use App\Models\Source;
use App\Source\Types\Html2Text;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Mockery;
use Tests\TestCase;
use App\Models\Document;
use App\Models\Transformer;
use Illuminate\Support\Facades\Http;
use App\Transformers\Types\Html2Text;
use Illuminate\Support\Facades\Storage;
use App\Transformers\TransformerTypeEnum;

class Html2TextTest extends TestCase
{
public function test_gets_file()
use SharedSetupForPdfFile;

public function test_parses()
{
$source = Source::factory()->create();
$document = Document::factory()->html()->create();

Storage::fake('projects');
$webFileSourceType = new Html2Text($source);

Http::fake([
'wikipedia.com/*' => Http::response('foo', 200),
$transformerModel = Transformer::factory()->create([
'type' => TransformerTypeEnum::Html2Text,
]);

$webFileSourceType->handle();

Http::assertSentCount(1);

$to = sprintf('%d/sources/%d/foo.pdf',
$source->project_id, $source->id);
Storage::disk('projects')->assertExists($to);

}

public function test_makes_document()
{
$source = Source::factory()->create();


Storage::fake('projects');
$webFileSourceType = new WebFile($source);

Http::fake([
'wikipedia.com/*' => Http::response('foo', 200),
]);

$this->assertDatabaseCount('documents', 0);
$webFileSourceType->handle();
$transformer = new Html2Text($document);
$this->assertDatabaseCount('document_chunks', 0);
$transformer->handle($transformerModel);
$this->assertDatabaseCount('document_chunks', 1);

$this->assertDatabaseCount('documents', 1);

}
$document = Document::first();
$content = $document->content;

public function test_makes_document_once()
{
$source = Source::factory()->create();

Storage::fake('projects');
$webFileSourceType = new WebFile($source);
$this->assertNotNull($content);

Http::fake([
'wikipedia.com/*' => Http::response('foo', 200),
]);

$this->assertDatabaseCount('documents', 0);
$webFileSourceType->handle();

$this->assertDatabaseCount('documents', 1);
$webFileSourceType->handle();
$this->assertDatabaseCount('documents', 1);
}

protected function mockFunction($functionName, $returnValue)
{
$mock = Mockery::mock();
$mock->shouldReceive('__invoke')->andReturn($returnValue);
$this->app->instance($functionName, $mock);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use App\Models\User;
use App\Models\Project;
use App\Models\Transformer;
use App\Models\User;
use Illuminate\Support\Facades\Queue;
use function Pest\Laravel\assertDatabaseCount;

Expand Down Expand Up @@ -52,6 +52,4 @@
->assertRedirectToRoute('projects.show', [
'project' => $project->id,
]);

Queue::assertPushed(ProcessTransformerJob::class);
});
4 changes: 2 additions & 2 deletions tests/Feature/PdfTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\Transformer;
use App\Transformers\Types\PdfTransformer;
use Illuminate\Support\Facades\File;
use Tests\TestCase;
use App\Transformers\Types\PdfTransformer;

class PdfTransformerTest extends TestCase
{
Expand Down
15 changes: 9 additions & 6 deletions tests/Feature/WebFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

namespace Tests\Feature;

use App\Models\Document;
use Mockery;
use Tests\TestCase;
use App\Models\Source;
use App\Models\Document;
use App\Source\Types\WebFile;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Mockery;
use Tests\TestCase;

class WebFileTest extends TestCase
{



public function test_gets_file()
{
$source = Source::factory()->webFileMetaData()->create();
Expand All @@ -27,9 +30,9 @@ public function test_gets_file()

Http::assertSentCount(1);

$to = sprintf('%d/sources/%d/foo.pdf',
$source->project_id, $source->id);
Storage::disk('projects')->assertExists($to);
$document = Document::first();

$this->assertEquals("foo", $document->content);

}

Expand Down

0 comments on commit 9e8c7e7

Please sign in to comment.