1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #include <gtest/gtest.h>
18 
19 #include "result_metadata.hpp"
20 
21 using namespace datastax;
22 using namespace datastax::internal;
23 using namespace datastax::internal::core;
24 
create_metadata(const char * column_names[])25 SharedRefPtr<ResultMetadata> create_metadata(const char* column_names[]) {
26   size_t count = 0;
27   while (column_names[count] != NULL) {
28     count++;
29   }
30 
31   ResultMetadata::Ptr metadata(new ResultMetadata(count, RefBuffer::Ptr()));
32 
33   for (size_t i = 0; column_names[i] != NULL; ++i) {
34     ColumnDefinition def;
35     def.name = StringRef(column_names[i]);
36     def.index = i;
37     metadata->add(def);
38   }
39 
40   return metadata;
41 }
42 
TEST(ResultMetadataUnitTest,Simple)43 TEST(ResultMetadataUnitTest, Simple) {
44   const char* column_names[] = { "abc", "def", "xyz", NULL };
45   SharedRefPtr<ResultMetadata> metadata(create_metadata(column_names));
46 
47   for (size_t i = 0; column_names[i] != NULL; ++i) {
48     IndexVec indices;
49     size_t count = metadata->get_indices(column_names[i], &indices);
50     EXPECT_EQ(count, 1u);
51     EXPECT_GT(indices.size(), 0u);
52     EXPECT_EQ(indices[0], i);
53   }
54 }
55 
TEST(ResultMetadataUnitTest,CaseSensitive)56 TEST(ResultMetadataUnitTest, CaseSensitive) {
57   const char* column_names[] = { "a", "A", "abc", "Abc", "ABc", "ABC", "aBc", "aBC", "abC", NULL };
58   SharedRefPtr<ResultMetadata> metadata(create_metadata(column_names));
59 
60   for (size_t i = 0; column_names[i] != NULL; ++i) {
61     IndexVec indices;
62     String name;
63     name.push_back('"');
64     name.append(column_names[i]);
65     name.push_back('"');
66     size_t count = metadata->get_indices(name.c_str(), &indices);
67     EXPECT_EQ(count, 1u);
68     EXPECT_GT(indices.size(), 0u);
69     EXPECT_EQ(indices[0], i);
70   }
71 
72   {
73     IndexVec indices;
74     size_t count = metadata->get_indices("a", &indices);
75     EXPECT_EQ(count, 2u);
76   }
77 
78   {
79     IndexVec indices;
80     size_t count = metadata->get_indices("abc", &indices);
81     EXPECT_EQ(count, 7u);
82   }
83 }
84