summaryrefslogtreecommitdiff
path: root/jstests/core/rename4.js
blob: 185193deaa9d3c5cdc3cffefbfe13c9ef4da8573 (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
t = db.jstests_rename4;
t.drop();

function bad(f) {
    var docsBeforeUpdate = t.find().toArray();
    var res = eval(f);

    // Ensure error
    if (!res.hasWriteError()) {
        print("Error:" + res.toString());
        print("Existing docs (before)");
        printjson(docsBeforeUpdate);
        print("Existing docs (after)");
        printjson(t.find().toArray());
        assert(false, "Expected error but didn't get one for: " + f);
    }
}

bad("t.update( {}, {$rename:{'a':'a'}} )");
bad("t.update( {}, {$rename:{'':'a'}} )");
bad("t.update( {}, {$rename:{'a':''}} )");
bad("t.update( {}, {$rename:{'.a':'b'}} )");
bad("t.update( {}, {$rename:{'a':'.b'}} )");
bad("t.update( {}, {$rename:{'a.':'b'}} )");
bad("t.update( {}, {$rename:{'a':'b.'}} )");
bad("t.update( {}, {$rename:{'a.b':'a'}} )");
bad("t.update( {}, {$rename:{'a.$':'b'}} )");
bad("t.update( {}, {$rename:{'a':'b.$'}} )");

// Only bad if input doc has field resulting in conflict
t.save({_id: 1, a: 2});
bad("t.update( {}, {$rename:{'_id':'a'}} )");
bad("t.update( {}, {$set:{b:1},$rename:{'a':'b'}} )");
bad("t.update( {}, {$rename:{'a':'b'},$set:{b:1}} )");
bad("t.update( {}, {$rename:{'a':'b'},$set:{a:1}} )");
bad("t.update( {}, {$set:{'b.c':1},$rename:{'a':'b'}} )");
bad("t.update( {}, {$set:{b:1},$rename:{'a':'b.c'}} )");
bad("t.update( {}, {$rename:{'a':'b'},$set:{'b.c':1}} )");
bad("t.update( {}, {$rename:{'a':'b.c'},$set:{b:1}} )");

t.remove({});
t.save({a: [1], b: {c: [2]}, d: [{e: 3}], f: 4});
bad("t.update( {}, {$rename:{'a.0':'f'}} )");
bad("t.update( {}, {$rename:{'a.0':'g'}} )");
bad("t.update( {}, {$rename:{'f':'a.0'}} )");
bad("t.update( {}, {$rename:{'b.c.0':'f'}} )");
bad("t.update( {}, {$rename:{'f':'b.c.0'}} )");
bad("t.update( {}, {$rename:{'d.e':'d.f'}} )");
bad("t.update( {}, {$rename:{'d.e':'f'}} )");
bad("t.update( {}, {$rename:{'d.f':'d.e'}} )");
bad("t.update( {}, {$rename:{'f':'d.e'}} )");
bad("t.update( {}, {$rename:{'d.0.e':'d.f'}} )");
bad("t.update( {}, {$rename:{'d.0.e':'f'}} )");
bad("t.update( {}, {$rename:{'d.f':'d.0.e'}} )");
bad("t.update( {}, {$rename:{'f':'d.0.e'}} )");
bad("t.update( {}, {$rename:{'f.g':'a'}} )");
bad("t.update( {}, {$rename:{'a':'f.g'}} )");

function good(start, mod, expected) {
    t.remove({});
    t.save(start);
    var res = t.update({}, mod);
    assert.writeOK(res);
    var got = t.findOne();
    delete got._id;
    assert.docEq(expected, got);
}

good({a: 1}, {$rename: {a: 'b'}}, {b: 1});
good({a: 1}, {$rename: {a: 'bb'}}, {bb: 1});
good({b: 1}, {$rename: {b: 'a'}}, {a: 1});
good({bb: 1}, {$rename: {bb: 'a'}}, {a: 1});
good({a: {y: 1}}, {$rename: {'a.y': 'a.z'}}, {a: {z: 1}});
good({a: {yy: 1}}, {$rename: {'a.yy': 'a.z'}}, {a: {z: 1}});
good({a: {z: 1}}, {$rename: {'a.z': 'a.y'}}, {a: {y: 1}});
good({a: {zz: 1}}, {$rename: {'a.zz': 'a.y'}}, {a: {y: 1}});
good({a: {c: 1}}, {$rename: {a: 'b'}}, {b: {c: 1}});
good({aa: {c: 1}}, {$rename: {aa: 'b'}}, {b: {c: 1}});
good({a: 1, b: 2}, {$rename: {a: 'b'}}, {b: 1});
good({aa: 1, b: 2}, {$rename: {aa: 'b'}}, {b: 1});
good({a: 1, bb: 2}, {$rename: {a: 'bb'}}, {bb: 1});
good({a: 1}, {$rename: {a: 'b.c'}}, {b: {c: 1}});
good({aa: 1}, {$rename: {aa: 'b.c'}}, {b: {c: 1}});
good({a: 1, b: {}}, {$rename: {a: 'b.c'}}, {b: {c: 1}});
good({aa: 1, b: {}}, {$rename: {aa: 'b.c'}}, {b: {c: 1}});
good({a: 1}, {$rename: {b: 'c'}}, {a: 1});
good({aa: 1}, {$rename: {b: 'c'}}, {aa: 1});
good({}, {$rename: {b: 'c'}}, {});
good({a: {b: 1, c: 2}}, {$rename: {'a.b': 'd'}}, {a: {c: 2}, d: 1});
good({a: {bb: 1, c: 2}}, {$rename: {'a.bb': 'd'}}, {a: {c: 2}, d: 1});
good({a: {b: 1}}, {$rename: {'a.b': 'd'}}, {a: {}, d: 1});
good({a: [5]}, {$rename: {a: 'b'}}, {b: [5]});
good({aa: [5]}, {$rename: {aa: 'b'}}, {b: [5]});
good({'0': 1}, {$rename: {'0': '5'}}, {'5': 1});
good({a: 1, b: 2}, {$rename: {a: 'c'}, $set: {b: 5}}, {b: 5, c: 1});
good({aa: 1, b: 2}, {$rename: {aa: 'c'}, $set: {b: 5}}, {b: 5, c: 1});
good({a: 1, b: 2}, {$rename: {z: 'c'}, $set: {b: 5}}, {a: 1, b: 5});
good({aa: 1, b: 2}, {$rename: {z: 'c'}, $set: {b: 5}}, {aa: 1, b: 5});

// (formerly) rewriting single field
good({a: {z: 1, b: 1}}, {$rename: {'a.b': 'a.c'}}, {a: {c: 1, z: 1}});
good({a: {z: 1, tomato: 1}}, {$rename: {'a.tomato': 'a.potato'}}, {a: {potato: 1, z: 1}});
good({a: {z: 1, b: 1, c: 1}}, {$rename: {'a.b': 'a.c'}}, {a: {c: 1, z: 1}});
good(
    {a: {z: 1, tomato: 1, potato: 1}}, {$rename: {'a.tomato': 'a.potato'}}, {a: {potato: 1, z: 1}});
good({a: {z: 1, b: 1}}, {$rename: {'a.b': 'a.cc'}}, {a: {cc: 1, z: 1}});
good({a: {z: 1, b: 1, c: 1}}, {$rename: {'a.b': 'aa.c'}}, {a: {c: 1, z: 1}, aa: {c: 1}});

// invalid target, but missing source
good({a: 1, c: 4}, {$rename: {b: 'c.d'}}, {a: 1, c: 4});

// TODO: This should be supported, and it is with the new update framework, but not with the
// old, and we currently don't have a good way to check which mode we are in. When we do have
// that, add this test guarded under that condition. Or, when we remove the old update path
// just enable this test.

// valid to rename away from an invalid name
// good( {x:1}, {$rename:{'$a.b':'a.b'}}, {x:1} );

// check index
t.drop();
t.ensureIndex({a: 1});

function l(start, mod, query, expected) {
    t.remove({});
    t.save(start);
    var res = t.update({}, mod);
    assert.writeOK(res);
    var got = t.find(query).hint({a: 1}).next();
    delete got._id;
    assert.docEq(expected, got);
}

l({a: 1}, {$rename: {a: 'b'}}, {a: null}, {b: 1});
l({a: 1}, {$rename: {a: 'bb'}}, {a: null}, {bb: 1});
l({b: 1}, {$rename: {b: 'a'}}, {a: 1}, {a: 1});
l({bb: 1}, {$rename: {bb: 'a'}}, {a: 1}, {a: 1});