summaryrefslogtreecommitdiff
path: root/test/direct/where-clause-test.vala
blob: e746daf8855cbce4dcfaf28d7e54c58073778891 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
/* where-clause-test.vala
 *
 * Copyright © 2011 Collabora Ltd.
 *             By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

using Zeitgeist;
using Assertions;

int main (string[] args)
{
    Test.init (ref args);

    // Do not abort on warning()s.
    GLib.Log.set_always_fatal (LogLevelFlags.LEVEL_CRITICAL);

    Test.add_func ("/WhereClause/basic", basic_test);
    Test.add_func ("/WhereClause/nested", nested_test);
    Test.add_func ("/WhereClause/nested_negation", nested_negation_test);
    Test.add_func ("/WhereClause/match_condition", match_condition_test);
    Test.add_func ("/WhereClause/glob/right_boundary", right_boundary_test);

    return Test.run ();
}

private class PublicWhereClause : WhereClause
{

    public PublicWhereClause (WhereClause.Type type, bool negate=false)
    {
        base (type, negate);
    }

    public new static string get_right_boundary (string text)
    {
        return WhereClause.get_right_boundary(text);
    }

}

public void basic_test ()
{
    WhereClause where;

    where = new WhereClause (WhereClause.Type.AND);
    where.add ("1st condition");
    where.add ("2nd condition");
    assert_cmpstr (where.get_sql_conditions (), OperatorType.EQUAL, "(1st condition AND 2nd condition)");

    where = new WhereClause (WhereClause.Type.OR);
    where.add ("1st condition");
    where.add ("2nd condition");
    assert_cmpstr (where.get_sql_conditions (), OperatorType.EQUAL, "(1st condition OR 2nd condition)");

    where = new WhereClause (WhereClause.Type.AND, true);
    where.add ("some condition");
    assert_cmpstr (where.get_sql_conditions (), OperatorType.EQUAL, "NOT some condition");
}

public void nested_test ()
{
    var where = new WhereClause (WhereClause.Type.AND);
    where.add ("1st condition", "arg1");

    var subwhere = new WhereClause (WhereClause.Type.OR);
    {
        var args = new GenericArray<string> ();
        args.add ("arg2");
        args.add ("arg3");
        subwhere.add_with_array ("2nd condition", args);
        subwhere.add ("3rd condition");
    }

    where.extend (subwhere);
    where.add ("last condition");

    assert_cmpstr (where.get_sql_conditions (), OperatorType.EQUAL,
        "(1st condition AND (2nd condition OR 3rd condition) AND " +
        "last condition)");

    {
        var args = where.get_bind_arguments ();
        assert_cmpint (args.length, OperatorType.EQUAL, 3);
        assert_cmpstr (args[0], OperatorType.EQUAL, "arg1");
        assert_cmpstr (args[1], OperatorType.EQUAL, "arg2");
        assert_cmpstr (args[2], OperatorType.EQUAL, "arg3");
    }
}

public void nested_negation_test ()
{
    var where = new WhereClause (WhereClause.Type.OR);
    where.add ("cond1", "arg1");

    var subwhere = new WhereClause (WhereClause.Type.OR, true);
    subwhere.add ("cond2");

    where.extend (subwhere);
    where.add ("cond3");

    assert_cmpstr (where.get_sql_conditions (), OperatorType.EQUAL,
        "(cond1 OR NOT cond2 OR cond3)");
}

public void match_condition_test ()
{
    WhereClause where;

    // Plain
    where = new WhereClause (WhereClause.Type.AND);
    where.add_match_condition ("field1", 5);
    assert_cmpstr (where.get_sql_conditions (), OperatorType.EQUAL,
        "field1 = 5");
    assert_cmpint (where.get_bind_arguments ().length, OperatorType.EQUAL, 0);

    // Negation
    where = new WhereClause (WhereClause.Type.AND);
    where.add_match_condition ("f2", 3, true);
    assert_cmpstr (where.get_sql_conditions (), OperatorType.EQUAL, "f2 != 3");

    // FIXME: test LIKE stuff...
}

public void right_boundary_test ()
{
    var clause = new PublicWhereClause (WhereClause.Type.AND);
    assert_cmpstr (clause.get_right_boundary ("a"), OperatorType.EQUAL, "b");
    assert_cmpstr (clause.get_right_boundary ("hello"), OperatorType.EQUAL, "hellp");
    assert_cmpstr (clause.get_right_boundary ("a b"), OperatorType.EQUAL, "a c");
}

// vim:expandtab:ts=4:sw=4