summaryrefslogtreecommitdiff
path: root/jstests/core/catalog/list_collections_no_views.js
blob: b0dc8c777c571c64cadd9360b3e8c5628067828d (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
/**
 * SERVER-25942 Test that views are not validated in the case that only collections are queried.
 *
 * The test runs commands that are not allowed with security token: applyOps.
 * @tags: [
 *   not_allowed_with_security_token,
 *   assumes_against_mongod_not_mongos,
 *   assumes_superuser_permissions,
 *   # applyOps is not retryable.
 *   requires_non_retryable_commands,
 *   # The drop of offending views may not happen on the donor after a committed migration.
 *   tenant_migration_incompatible,
 * ]
 */
(function() {
'use strict';
let mydb = db.getSiblingDB('list_collections_no_views');

assert.commandWorked(mydb.createCollection('foo'));
assert.commandWorked(mydb.createView('bar', 'foo', []));

let all = mydb.runCommand({listCollections: 1});
assert.commandWorked(all);

let allExpected = [
    {
        "name": "bar",
        "type": "view",
    },
    {
        "name": "foo",
        "type": "collection",
    },
    {
        "name": "system.views",
        "type": "collection",
    },
];

assert.eq(allExpected,
          all.cursor.firstBatch
              .map(function(c) {
                  return {name: c.name, type: c.type};
              })
              .sort(function(c1, c2) {
                  if (c1.name > c2.name) {
                      return 1;
                  }

                  if (c1.name < c2.name) {
                      return -1;
                  }

                  return 0;
              }));

// TODO (SERVER-25493): {type: {$exists: false}} is needed for versions <= 3.2
let collOnlyCommand = {
    listCollections: 1,
    filter: {$or: [{type: 'collection'}, {type: {$exists: false}}]}
};

let collOnly = mydb.runCommand(collOnlyCommand);
assert.commandWorked(collOnly);

let collOnlyExpected = [
    {
        "name": "foo",
        "type": "collection",
    },
    {
        "name": "system.views",
        "type": "collection",
    },
];

assert.eq(collOnlyExpected,
          collOnly.cursor.firstBatch
              .map(function(c) {
                  return {name: c.name, type: c.type};
              })
              .sort(function(c1, c2) {
                  if (c1.name > c2.name) {
                      return 1;
                  }

                  if (c1.name < c2.name) {
                      return -1;
                  }

                  return 0;
              }));

let viewOnly = mydb.runCommand({listCollections: 1, filter: {type: 'view'}});
assert.commandWorked(viewOnly);
let viewOnlyExpected = [{
    "name": "bar",
    "type": "view",
}];

assert.eq(viewOnlyExpected,
          viewOnly.cursor.firstBatch
              .map(function(c) {
                  return {name: c.name, type: c.type};
              })
              .sort(function(c1, c2) {
                  if (c1.name > c2.name) {
                      return 1;
                  }

                  if (c1.name < c2.name) {
                      return -1;
                  }

                  return 0;
              }));

assert.commandWorked(db.adminCommand({
    applyOps: [{
        op: "i",
        ns: mydb.getName() + ".system.views",
        o: {_id: "invalid_view_def", invalid: NumberLong(1000)}
    }]
}));

let collOnlyInvalidView = mydb.runCommand(collOnlyCommand);
assert.eq(collOnlyExpected,
          collOnlyInvalidView.cursor.firstBatch
              .map(function(c) {
                  return {name: c.name, type: c.type};
              })
              .sort(function(c1, c2) {
                  if (c1.name > c2.name) {
                      return 1;
                  }

                  if (c1.name < c2.name) {
                      return -1;
                  }

                  return 0;
              }));

assert.commandFailed(mydb.runCommand({listCollections: 1}));
assert.commandFailed(mydb.runCommand({listCollections: 1, filter: {type: 'view'}}));

// Fix database state for end of test validation and burn-in tests
mydb.dropDatabase();
})();