Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 8 | #include "trompeloeil_doctest.hpp" |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 9 | #include <experimental/iterator> |
| 10 | #include <iostream> |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 11 | #include "ast_commands.hpp" |
| 12 | #include "datastoreaccess_mock.hpp" |
| 13 | #include "parser.hpp" |
Václav Kubernát | ed824d0 | 2020-06-09 15:48:30 +0200 | [diff] [blame] | 14 | #include "pretty_printers.hpp" |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 15 | #include "static_schema.hpp" |
| 16 | |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 17 | |
| 18 | TEST_CASE("keyvalue_completion") |
| 19 | { |
| 20 | auto schema = std::make_shared<StaticSchema>(); |
| 21 | schema->addModule("example"); |
| 22 | schema->addContainer("/", "example:a"); |
| 23 | schema->addContainer("/", "example:b"); |
| 24 | schema->addList("/", "example:list", {"number"}); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 25 | schema->addLeaf("/example:list", "example:number", yang::Int32{}); |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 26 | schema->addList("/", "example:twoKeyList", {"number", "name"}); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 27 | schema->addLeaf("/example:twoKeyList", "example:number", yang::Int32{}); |
| 28 | schema->addLeaf("/example:twoKeyList", "example:name", yang::String{}); |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 29 | auto mockDatastore = std::make_shared<MockDatastoreAccess>(); |
| 30 | |
| 31 | // DataQuery gets the schema from DatastoreAccess once |
| 32 | auto expectation = NAMED_REQUIRE_CALL(*mockDatastore, schema()) |
| 33 | .RETURN(schema); |
| 34 | auto dataQuery = std::make_shared<DataQuery>(*mockDatastore); |
| 35 | expectation.reset(); |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 36 | Parser parser(schema, WritableOps::No, dataQuery); |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 37 | std::string input; |
| 38 | std::ostringstream errorStream; |
| 39 | |
| 40 | std::set<std::string> expected; |
Václav Kubernát | f8ce1bc | 2020-06-11 10:33:49 +0200 | [diff] [blame] | 41 | std::vector<std::unique_ptr<trompeloeil::expectation>> queryExpectations; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 42 | std::vector<ListInstance> queryReturn; |
| 43 | |
| 44 | SECTION("get example:list[number=") |
| 45 | { |
| 46 | input = "get example:list[number="; |
| 47 | queryReturn = { |
| 48 | {{"number", 1}}, |
| 49 | {{"number", 7}}, |
| 50 | {{"number", 9}}, |
| 51 | {{"number", 42}} |
| 52 | }; |
| 53 | expected = { |
| 54 | "1", |
| 55 | "7", |
| 56 | "9", |
| 57 | "42" |
| 58 | }; |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 59 | queryExpectations.emplace_back(NAMED_REQUIRE_CALL(*mockDatastore, listInstances("/example:list")).RETURN(queryReturn)); |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | SECTION("get example:twoKeyList[number=") |
| 63 | { |
| 64 | queryReturn = { |
| 65 | {{"number", 1}, {"name", std::string{"Petr"}}}, |
| 66 | {{"number", 7}, {"name", std::string{"Petr"}}}, |
| 67 | {{"number", 10}, {"name", std::string{"Petr"}}}, |
| 68 | {{"number", 10}, {"name", std::string{"Honza"}}}, |
| 69 | {{"number", 100}, {"name", std::string{"Honza"}}}, |
| 70 | }; |
| 71 | input = "get example:twoKeyList["; |
| 72 | SECTION("no keys set") |
| 73 | { |
| 74 | SECTION("number") |
| 75 | { |
| 76 | input += "number="; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 77 | expected = {"1", "7", "10", "100"}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 78 | } |
| 79 | SECTION("name") |
| 80 | { |
| 81 | input += "name="; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 82 | expected = {"'Petr'", "'Honza'"}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 83 | } |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 84 | queryExpectations.emplace_back(NAMED_REQUIRE_CALL(*mockDatastore, listInstances("/example:twoKeyList")).RETURN(queryReturn)); |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | SECTION("name is set") |
| 88 | { |
| 89 | input += "name="; |
| 90 | SECTION("Petr") |
| 91 | { |
| 92 | input += "'Petr'"; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 93 | expected = {"1", "7", "10"}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 94 | } |
| 95 | SECTION("Honza") |
| 96 | { |
| 97 | input += "'Honza'"; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 98 | expected = {"10", "100"}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 99 | } |
| 100 | input += "][number="; |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 101 | queryExpectations.emplace_back(NAMED_REQUIRE_CALL(*mockDatastore, listInstances("/example:twoKeyList")) |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 102 | .TIMES(2) |
| 103 | .RETURN(queryReturn)); |
| 104 | } |
| 105 | |
| 106 | SECTION("number is set") |
| 107 | { |
| 108 | input += "number="; |
| 109 | SECTION("1") |
| 110 | { |
| 111 | input += "1"; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 112 | expected = {"'Petr'"}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 113 | } |
| 114 | SECTION("7") |
| 115 | { |
| 116 | input += "7"; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 117 | expected = {"'Petr'"}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 118 | } |
| 119 | SECTION("10") |
| 120 | { |
| 121 | input += "10"; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 122 | expected = {"'Honza'", "'Petr'"}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 123 | } |
| 124 | SECTION("100") |
| 125 | { |
| 126 | input += "100"; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 127 | expected = {"'Honza'"}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 128 | } |
| 129 | input += "][name="; |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 130 | queryExpectations.emplace_back(NAMED_REQUIRE_CALL(*mockDatastore, listInstances("/example:twoKeyList")) |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 131 | .TIMES(2) |
| 132 | .RETURN(queryReturn)); |
| 133 | } |
| 134 | |
| 135 | SECTION("both keys are set") |
| 136 | { |
| 137 | SECTION("get example:twoKeyList[number=123][name='Petr'") |
| 138 | { |
| 139 | input = "get example:twoKeyList[number=123][name='Petr'"; |
| 140 | expected = {"]/"}; |
| 141 | } |
| 142 | SECTION("get example:twoKeyList[number=123][name='Petr']") |
| 143 | { |
| 144 | input = "get example:twoKeyList[number=123][name='Petr']"; |
| 145 | expected = {"]/"}; |
| 146 | } |
| 147 | SECTION("get example:twoKeyList[number=123][name='Petr'][") |
| 148 | { |
| 149 | input = "get example:twoKeyList[number=123][name='Petr']["; |
| 150 | } |
| 151 | // I use ALLOW_CALL here, all this stuff calls it different number of times |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 152 | queryExpectations.emplace_back(NAMED_ALLOW_CALL(*mockDatastore, listInstances("/example:twoKeyList")) |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 153 | .RETURN(queryReturn)); |
| 154 | } |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | REQUIRE(parser.completeCommand(input, errorStream).m_completions == expected); |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 158 | } |