summaryrefslogtreecommitdiff
path: root/src/mongo/db/commands/write_commands/batch_test.cpp
blob: 758ad628d4e60f5333c21f0c9374ea51047b7e6c (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
 *    Copyright (C) 2013 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    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 Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mongo/db/commands/write_commands/batch.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/json.h"
#include "mongo/unittest/unittest.h"

namespace mongo {

    TEST(InsertItemParsing, Normal) {
        // Tests that the document {foo: "bar"} is valid to be inserted.

        WriteBatch::WriteItem w1(WriteBatch::WRITE_INSERT,
                                 fromjson("{foo:\"bar\"}"));
        ASSERT(w1.isValid());
    }

    TEST(UpdateItemParsing, Normal) {
        WriteBatch::WriteItem w1(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:{}, u:{}, multi:true, upsert:true}"));
        ASSERT(w1.isValid());
    }

    TEST(UpdateItemParsing, MissingOptionalFields) {
        // Omitting fields "multi" and "upsert" is acceptable.

        WriteBatch::WriteItem w1(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:{}, u:{}, multi:true}"));
        ASSERT(w1.isValid());

        WriteBatch::WriteItem w2(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:{}, u:{}, upsert:true}"));
        ASSERT(w2.isValid());
    }

    TEST(UpdateItemParsing, MissingRequiredFields) {
        // Omitting fields "q" or "u" is not acceptable.

        WriteBatch::WriteItem w1(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:{}}"));
        ASSERT_FALSE(w1.isValid());

        WriteBatch::WriteItem w2(WriteBatch::WRITE_UPDATE,
                                 fromjson("{u:{}}"));
        ASSERT_FALSE(w2.isValid());
    }

    TEST(UpdateItemParsing, InvalidTypes) {
        // Fields "q" and "u" must be subdocuments; fields "multi" and "upsert" must be bools.

        WriteBatch::WriteItem w1(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:{}, u:1}"));
        ASSERT_FALSE(w1.isValid());

        WriteBatch::WriteItem w2(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:{}, u:[]}"));
        ASSERT_FALSE(w2.isValid());

        WriteBatch::WriteItem w3(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:1, u:{}}"));
        ASSERT_FALSE(w3.isValid());

        WriteBatch::WriteItem w4(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:[], u:{}}"));
        ASSERT_FALSE(w4.isValid());

        WriteBatch::WriteItem w5(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:{}, u:{}, multi:1}"));
        ASSERT_FALSE(w5.isValid());

        WriteBatch::WriteItem w6(WriteBatch::WRITE_UPDATE,
                                 fromjson("{q:{}, u:{}, upsert:1}"));
        ASSERT_FALSE(w6.isValid());
    }

    //
    // TODO: Reject ambiguous/invalid input.
    //

    // TEST(UpdateItemParsing, ExtraFields) {
    //     WriteBatch::WriteItem w1(WriteBatch::WRITE_UPDATE,
    //                              fromjson("{q:{}, u:{}, extraField:1}"));
    //     ASSERT_FALSE(w1.isValid());
    // }

    // TEST(UpdateItemParsing, RepeatedFields) {
    //     WriteBatch::WriteItem w1(WriteBatch::WRITE_UPDATE,
    //                              fromjson("{q:{}, u:{}, q:1}"));
    //     ASSERT_FALSE(w1.isValid());
    //
    //     WriteBatch::WriteItem w2(WriteBatch::WRITE_UPDATE,
    //                              fromjson("{q:{}, u:{}, multi:true, multi:1}"));
    //     ASSERT_FALSE(w2.isValid());
    // }

    TEST(DeleteItemParsing, Normal) {
        // Field "q" will accept a subdocument.

        WriteBatch::WriteItem w1(WriteBatch::WRITE_DELETE,
                                 fromjson("{q:{}}"));
        ASSERT(w1.isValid());
    }

    TEST(DeleteItemParsing, MissingRequiredFields) {
        // Omitting required field "q" is not acceptable.

        WriteBatch::WriteItem w1(WriteBatch::WRITE_DELETE,
                                 fromjson("{}"));
        ASSERT_FALSE(w1.isValid());
    }

    TEST(DeleteItemParsing, InvalidTypes) {
        // Field "q" will accept a subdocument.

        WriteBatch::WriteItem w1(WriteBatch::WRITE_DELETE,
                                 fromjson("{q:1}"));
        ASSERT_FALSE(w1.isValid());
    }

    //
    // TODO: Reject ambiguous/invalid input.
    //

    // TEST(DeleteItemParsing, ExtraFields) {
    //     WriteBatch::WriteItem w1(WriteBatch::WRITE_DELETE,
    //                              fromjson("{q:{}, extraField:1}"));
    //     ASSERT_FALSE(w1.isValid());
    // }

    // TEST(DeleteItemParsing, RepeatedFields) {
    //     WriteBatch::WriteItem w1(WriteBatch::WRITE_DELETE,
    //                              fromjson("{q:{}, q:1}"));
    //     ASSERT_FALSE(w1.isValid());
    // }

    TEST(BatchParsing, Normal) {
        string errMsg;

        WriteBatch wb1("test.c", WriteBatch::WRITE_INSERT);
        ASSERT(wb1.parse(fromjson("{insert:\"c\", documents:[{}]}"), &errMsg));

        WriteBatch wb2("test.c", WriteBatch::WRITE_UPDATE);
        ASSERT(wb2.parse(fromjson("{update:\"c\", updates:[{q:{}, u:{}}]}"), &errMsg));

        WriteBatch wb3("test.c", WriteBatch::WRITE_DELETE);
        ASSERT(wb3.parse(fromjson("{delete:\"c\", deletes:[{q:{}}]}"), &errMsg));
    }

    TEST(BatchParsing, MissingRequiredFields) {
        // Omitting the batch's "container field" (e.g. "documents") is not acceptable.

        string errMsg;

        WriteBatch wb1("test.c", WriteBatch::WRITE_INSERT);
        ASSERT_FALSE(wb1.parse(fromjson("{insert:\"c\"}"), &errMsg));

        WriteBatch wb2("test.c", WriteBatch::WRITE_UPDATE);
        ASSERT_FALSE(wb2.parse(fromjson("{update:\"c\"}"), &errMsg));

        WriteBatch wb3("test.c", WriteBatch::WRITE_DELETE);
        ASSERT_FALSE(wb3.parse(fromjson("{delete:\"c\"}"), &errMsg));
    }

    TEST(BatchParsing, WithOptionalFields) {
        string errMsg;

        WriteBatch wb1("test.c", WriteBatch::WRITE_INSERT);
        ASSERT(wb1.parse(fromjson("{insert:\"c\","
                                  " documents:[{}],"
                                  " continueOnError:false,"
                                  " writeConcern:{}}"),
                         &errMsg));
    }

    TEST(BatchParsing, InvalidTypes) {
        // The batch container field accepts a list of subdocuments.
        // Field "continueOnErrror" accepts a bool.
        // Field "writeConcern" accepts a subdocument.

        string errMsg;

        WriteBatch wb1("test.c", WriteBatch::WRITE_INSERT);
        ASSERT_FALSE(wb1.parse(fromjson("{insert:\"c\","
                                        " documents:{}}"),
                               &errMsg));

        WriteBatch wb2("test.c", WriteBatch::WRITE_INSERT);
        ASSERT_FALSE(wb2.parse(fromjson("{insert:\"c\","
                                        " documents:[4]}"),
                               &errMsg));

        WriteBatch wb3("test.c", WriteBatch::WRITE_INSERT);
        ASSERT_FALSE(wb3.parse(fromjson("{insert:\"c\","
                                        " documents:[{}],"
                                        " continueOnError:1}"),
                               &errMsg));

        WriteBatch wb4("test.c", WriteBatch::WRITE_INSERT);
        ASSERT_FALSE(wb4.parse(fromjson("{insert:\"c\","
                                        " documents:[{}],"
                                        " writeConcern:1}"),
                               &errMsg));
    }

}