1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2017 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #include "collections/collections_dockey.h"
19 #include "collections/collections_types.h"
20 #include "tests/module_tests/test_helpers.h"
21 
22 #include <gtest/gtest.h>
23 
TEST(CollectionDocKeyTest, make)24 TEST(CollectionDocKeyTest, make) {
25     auto key1 = makeStoredDocKey("beer::bud", DocNamespace::Collections);
26     auto key2 = makeStoredDocKey("beerbud", DocNamespace::Collections);
27     auto key3 = makeStoredDocKey("$collections:beer", DocNamespace::System);
28     auto key4 = makeStoredDocKey("$collections:$default", DocNamespace::System);
29 
30     EXPECT_EQ(strlen("beer"),
31               Collections::DocKey::make(key1, "::").getCollectionLen());
32 
33     EXPECT_EQ(0, Collections::DocKey::make(key2, "::").getCollectionLen());
34 
35     EXPECT_EQ(0, Collections::DocKey::make(key1, "##").getCollectionLen());
36 
37     // This case is if the separator and the key are the same thing
38     // The collection len is 0
39     EXPECT_EQ(0,
40               Collections::DocKey::make(key1, "beer::bud").getCollectionLen());
41 
42     // If a key is beer::brewery and the separator is brewery then the
43     // collection is beer::
44     EXPECT_EQ(strlen("beer::"),
45               Collections::DocKey::make(key1, "bud").getCollectionLen());
46 
47     EXPECT_EQ(0,
48               Collections::DocKey::make(key1, "longerthanthekey")
49                       .getCollectionLen());
50 
51     // Check that system keys can be split up
52     EXPECT_EQ(strlen(Collections::SystemEventPrefix),
53               Collections::DocKey::make(key3).getCollectionLen());
54     EXPECT_EQ(cb::const_char_buffer("beer"),
55               Collections::DocKey::make(key3).getKey());
56     EXPECT_EQ(strlen(Collections::SystemEventPrefix),
57               Collections::DocKey::make(key4).getCollectionLen());
58     EXPECT_EQ(cb::const_char_buffer("$default"),
59               Collections::DocKey::make(key4).getKey());
60 }