Passed
Pull Request — master (#63)
by Eugene
03:00
created

InsertTest::testInsertTypeMismatchedValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 10
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Tests\Integration\Requests;
15
16
use Tarantool\Client\Exception\RequestFailed;
17
use Tarantool\Client\Tests\Integration\TestCase;
18
19
final class InsertTest extends TestCase
20
{
21
    /**
22
     * @dataProvider provideInsertData
23
     *
24
     * @eval create_space('request_insert_str'):create_index('primary', {type = 'hash', parts = {1, 'str'}})
25
     * @eval create_space('request_insert_num'):create_index('primary', {type = 'hash', parts = {1, 'unsigned'}})
26
     */
27
    public function testInsert(string $spaceName, array $values) : void
28
    {
29
        $space = $this->client->getSpace($spaceName);
30
        $result = $space->insert($values);
31
32
        self::assertSame([$values], $result);
33
    }
34
35
    public function provideInsertData() : iterable
36
    {
37
        return [
38
            ['request_insert_str', ['']],
39
            ['request_insert_str', ['foo']],
40
            ['request_insert_str', ['null', null, null]],
41
            ['request_insert_str', ['int', 42, -42]],
42
            ['request_insert_str', ['float', 4.2, -4.2]],
43
            ['request_insert_str', ['array', ['foo' => 'bar']]],
44
            ['request_insert_num', [42]],
45
        ];
46
    }
47
48
    /**
49
     * @dataProvider provideInsertDataWithMismatchedTypes
50
     *
51
     * @eval create_space('request_insert_str'):create_index('primary', {type = 'hash', parts = {1, 'str'}})
52
     * @eval create_space('request_insert_num'):create_index('primary', {type = 'hash', parts = {1, 'unsigned'}})
53
     */
54
    public function testInsertTypeMismatchedValues(string $spaceName, array $values) : void
55
    {
56
        $space = $this->client->getSpace($spaceName);
57
58
        $this->expectException(RequestFailed::class);
59
        $this->expectExceptionMessageRegExp('/Tuple field 1 type does not match one required by operation: expected .+/');
60
        $this->expectExceptionCode(23);
61
62
        $space->insert($values);
63
    }
64
65
    public function provideInsertDataWithMismatchedTypes() : iterable
66
    {
67
        return [
68
            ['request_insert_str', [null]],
69
            ['request_insert_str', [42]],
70
            ['request_insert_str', [[]]],
71
            ['request_insert_num', [null]],
72
            ['request_insert_num', [-42]],
73
            ['request_insert_num', [4.2]],
74
            ['request_insert_num', [[]]],
75
        ];
76
    }
77
78
    /**
79
     * @eval space = create_space('request_insert')
80
     * @eval space:create_index('primary', {type = 'hash', parts = {1, 'unsigned'}})
81
     * @eval space:insert{1, 'foobar'}
82
     */
83
    public function testInsertDuplicateKey() : void
84
    {
85
        $space = $this->client->getSpace('request_insert');
86
87
        $this->expectException(RequestFailed::class);
88
        $this->expectExceptionMessage("Duplicate key exists in unique index 'primary' in space 'request_insert'");
89
        $this->expectExceptionCode(3);
90
91
        $space->insert([1, 'bazqux']);
92
    }
93
}
94