Passed
Push — extract-store ( 38a23e...9fc033 )
by Konrad
05:01
created

DeleteQueryTest::testDelete2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under
5
 * the terms of the GPL-3 license.
6
 *
7
 * (c) Konrad Abicht <[email protected]>
8
 * (c) Benjamin Nowack
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tests\Integration\Store\InMemoryStoreSqlite\Query;
15
16
use sweetrdf\InMemoryStoreSqlite\Store\InMemoryStoreSqlite;
17
use Tests\TestCase;
18
19
/**
20
 * Tests for query method - focus on DELETE queries.
21
 */
22
class DeleteQueryTest extends TestCase
23
{
24
    protected function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->subjectUnderTest = InMemoryStoreSqlite::createInstance();
29
    }
30
31
    protected function runSPOQuery($g = null)
32
    {
33
        return null == $g
34
            ? $this->subjectUnderTest->query('SELECT * WHERE {?s ?p ?o.}')
35
            : $this->subjectUnderTest->query('SELECT * FROM <'.$g.'> WHERE {?s ?p ?o.}');
36
    }
37
38
    public function testDelete()
39
    {
40
        // test data
41
        $this->subjectUnderTest->query('INSERT INTO <http://example.com/1> {
42
            <http://s> <http://p1> "baz" .
43
        }');
44
        $this->subjectUnderTest->query('INSERT INTO <http://example.com/2> {
45
            <http://s> <http://p1> "bar" .
46
        }');
47
48
        $this->assertEquals(2, \count($this->runSPOQuery()['result']['rows']));
49
50
        $this->subjectUnderTest->query('DELETE {<http://s> ?p ?o .}');
51
52
        $this->assertEquals(0, \count($this->runSPOQuery()['result']['rows']));
53
    }
54
55
    public function testDelete2()
56
    {
57
        // test data
58
        $this->subjectUnderTest->query('INSERT INTO <http://example.com/1> {
59
            <http://s> <http://p1> "baz" .
60
        }');
61
        $this->subjectUnderTest->query('INSERT INTO <http://example.com/2> {
62
            <http://s> <http://p2> "bar" .
63
        }');
64
65
        $this->assertEquals(2, \count($this->runSPOQuery()['result']['rows']));
66
67
        $this->subjectUnderTest->query('DELETE {<http://s> <http://p1> ?o .}');
68
69
        $this->assertEquals(1, \count($this->runSPOQuery()['result']['rows']));
70
    }
71
72
    public function testDeleteAGraph()
73
    {
74
        // test data
75
        $this->subjectUnderTest->query('INSERT INTO <http://example.com/1> {
76
            <http://s> <http://p1> "baz" .
77
        }');
78
79
        $this->assertEquals(1, \count($this->runSPOQuery()['result']['rows']));
80
81
        $this->subjectUnderTest->query('DELETE FROM <http://example.com/1>');
82
83
        $this->assertEquals(0, \count($this->runSPOQuery()['result']['rows']));
84
    }
85
86
    public function testDeleteWhere1()
87
    {
88
        // test data
89
        $this->subjectUnderTest->query('INSERT INTO <http://example.com/1> {
90
            <http://s> <http://to-delete> 1, 2 .
91
            <http://s> <http://to-check> 1, 2 .
92
            <http://s> rdf:type <http://Test> .
93
        }');
94
95
        $this->assertEquals(5, \count($this->runSPOQuery()['result']['rows']));
96
97
        $this->subjectUnderTest->query('DELETE {
98
            <http://s> <http://to-delete> 1, 2 .
99
        } WHERE {
100
            <http://s> <http://to-check> 1, 2 .
101
        }');
102
103
        $this->assertEquals(3, \count($this->runSPOQuery()['result']['rows']));
104
    }
105
106
    public function testDeleteWhereWithBlankNode()
107
    {
108
        // test data
109
        $this->subjectUnderTest->query('INSERT INTO <http://example.com/1> {
110
            _:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://Person> ;
111
                <http://foo> <http://bar > .
112
        }');
113
114
        $this->assertEquals(2, \count($this->runSPOQuery()['result']['rows']));
115
116
        $this->subjectUnderTest->query('DELETE {
117
            _:a ?p ?o .
118
        } WHERE {
119
            _:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://Person> .
120
        }');
121
122
        // first we check the expected behavior and afterwards skip to notice the
123
        // developer about it.
124
        $this->assertEquals(2, \count($this->runSPOQuery()['result']['rows']));
125
        $this->markTestSkipped('DELETE queries with blank nodes are not working.');
126
    }
127
128
    public function testDeleteFromWhere()
129
    {
130
        // test data
131
        $this->subjectUnderTest->query('INSERT INTO <http://example.com/1> {
132
            <http://s> <http://to-delete> 1, 2 .
133
            <http://s> <http://to-check> 1, 2 .
134
            <http://s> rdf:type <http://Test> .
135
        }');
136
137
        $this->assertEquals(5, \count($this->runSPOQuery('http://example.com/1')['result']['rows']));
138
139
        $this->subjectUnderTest->query('DELETE FROM <http://example.com/1> {
140
            <http://s> <http://to-delete> 1, 2 .
141
        } WHERE {
142
            <http://s> <http://to-check> 1, 2 .
143
        }');
144
145
        $this->assertEquals(3, \count($this->runSPOQuery('http://example.com/1')['result']['rows']));
146
    }
147
}
148