-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClientV2Test.php
More file actions
135 lines (107 loc) · 4.88 KB
/
ClientV2Test.php
File metadata and controls
135 lines (107 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace V2;
use Mindee\ClientV2;
use Mindee\Http\MindeeApiV2;
use Mindee\Input\InferenceParameters;
use Mindee\Input\LocalInputSource;
use Mindee\Input\LocalResponse;
use Mindee\Input\PathInput;
use Mindee\Parsing\V2\InferenceResponse;
use Mindee\Parsing\V2\JobResponse;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class ClientV2Test extends TestCase
{
private static function makeClientWithMockedApi(MindeeApiV2 $mockedApi): ClientV2
{
$client = new ClientV2("dummy");
$reflection = new \ReflectionClass($client);
$property = $reflection->getProperty('mindeeApi');
$property->setAccessible(true);
$property->setValue($client, $mockedApi);
return $client;
}
public function testEnqueuePostAsync(): void
{
$predictable = $this->createMock(MindeeApiV2::class);
$syntheticResponse = file_get_contents(\TestingUtilities::getV2DataDir() . '/job/ok_processing.json');
$predictable->expects($this->once())
->method('reqPostInferenceEnqueue')
->with(
$this->isInstanceOf(LocalInputSource::class),
$this->isInstanceOf(InferenceParameters::class)
)
->willReturn(new JobResponse(json_decode($syntheticResponse, true)));
$mindeeClient = self::makeClientWithMockedApi($predictable);
$input = new PathInput(\TestingUtilities::getFileTypesDir() . '/pdf/blank_1.pdf');
$params = new InferenceParameters('dummy-model-id', textContext: 'dummy text context');
$response = $mindeeClient->enqueueInference($input, $params);
$this->assertNotNull($response, 'enqueue() must return a response');
$this->assertInstanceOf(JobResponse::class, $response);
}
public function testDocumentGetJobAsync(): void
{
/** @var MindeeApiV2&MockObject $predictable */
$predictable = $this->createMock(MindeeApiV2::class);
$syntheticResponse = file_get_contents(\TestingUtilities::getV2DataDir() . '/job/ok_processing.json');
$processing = new JobResponse(json_decode($syntheticResponse, true));
$predictable->expects($this->once())
->method('reqGetJob')
->with($this->equalTo('dummy-id'))
->willReturn($processing);
$mindeeClient = self::makeClientWithMockedApi($predictable);
$response = $mindeeClient->getJob('dummy-id');
$this->assertNotNull($response, 'must return a response');
$this->assertNotNull($response->job, 'job must return a response');
}
public function testDocumentGetInferenceAsync(): void
{
/** @var MindeeApiV2&MockObject $predictable */
$predictable = $this->createMock(MindeeApiV2::class);
$jsonFile = \TestingUtilities::getV2DataDir() . '/products/financial_document/complete.json';
$this->assertFileExists($jsonFile, 'Test resource file must exist');
$json = json_decode(file_get_contents($jsonFile), true);
$processing = new InferenceResponse($json);
$predictable->expects($this->once())
->method('reqGetInference')
->with($this->equalTo('12345678-1234-1234-1234-123456789abc'))
->willReturn($processing);
$mindeeClient = self::makeClientWithMockedApi($predictable);
$response = $mindeeClient->getInference('12345678-1234-1234-1234-123456789abc');
$this->assertNotNull($response, 'must have a response');
$this->assertNotNull($response->inference, 'inference must have a response');
$fields = $response->inference->result->fields ?? [];
$this->assertCount(
21,
$fields,
'Result must have 21 fields'
);
$supplierName = $fields['supplier_name']->value ?? null;
$this->assertEquals(
'John Smith',
$supplierName,
'Result "' . $supplierName . '" must deserialize fields properly.'
);
}
public function testInferenceLoadsLocally(): void
{
$jsonFile = \TestingUtilities::getV2DataDir() . '/products/financial_document/complete.json';
$this->assertFileExists($jsonFile, 'Test resource file must exist');
$localResponse = new LocalResponse($jsonFile);
$loaded = $localResponse->deserializeResponse(InferenceResponse::class);
$this->assertNotNull($loaded, 'Loaded InferenceResponse must not be null');
$this->assertInstanceOf(InferenceResponse::class, $loaded);
$modelId = $loaded->inference->model->id ?? null;
$this->assertEquals(
'12345678-1234-1234-1234-123456789abc',
$modelId,
'Model Id mismatch'
);
$supplierName = $loaded->inference->result->fields['supplier_name']->value ?? null;
$this->assertEquals(
'John Smith',
$supplierName,
'Supplier name mismatch'
);
}
}