|
| 1 | +var neo4j = require("../../../../lib/v1"); |
| 2 | +var resultSummary = require("../../../../lib/v1/result-summary"); |
| 3 | +var util = require("./util") |
| 4 | + |
| 5 | +module.exports = function () { |
| 6 | + |
| 7 | + this.When(/^the `Statement Result` is consumed a `Result Summary` is returned$/, function (callback) { |
| 8 | + self = this; |
| 9 | + this.rc.then(function(res) { |
| 10 | + self.summary = res.summary; |
| 11 | + callback(); |
| 12 | + }).catch(function(err) {callback(new Error("Rejected Promise: " + err))}); |
| 13 | + }); |
| 14 | + |
| 15 | + this.Then(/^the `Statement Result` is closed$/, function () { |
| 16 | + //No result cursor in JavaScript Driver |
| 17 | + }); |
| 18 | + |
| 19 | + this.When(/^I request a `Statement` from the `Result Summary`$/, function () { |
| 20 | + this.statement = this.summary.statement |
| 21 | + }); |
| 22 | + |
| 23 | + this.Then(/^requesting the `Statement` as text should give: (.*)$/, function (statementText) { |
| 24 | + if (this.statement.text != statementText) { |
| 25 | + throw Error("'" + this.statement.text + "' does not match expected text: '" + statementText + "'") |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + this.Then(/^requesting the `Statement` parameter should give: (.*)$/, function (statementParam) { |
| 30 | + var param = util.literalValueToTestValue(statementParam); |
| 31 | + if (typeof this.statement.parameters === 'undefined') { |
| 32 | + this.statement.parameters = {} |
| 33 | + } |
| 34 | + if (!util.compareValues(this.statement.parameters, param)) { |
| 35 | + throw Error("Params does not match! Got: " + this.statement.parameters + " Expected: " + param); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + this.Then(/^requesting `Counters` from `Result Summary` should give$/, function (table) { |
| 40 | + var updateStatistics = this.summary.updateStatistics |
| 41 | + for ( var i = 0 ; i < table.hashes().length; i++) { |
| 42 | + var statistic = table.hashes()[i].counter; |
| 43 | + var expected = util.literalValueToTestValueNormalIntegers(table.hashes()[i].result); |
| 44 | + var given = getStatistic(statistic, updateStatistics) |
| 45 | + if (!util.compareValues(given, expected)) { |
| 46 | + throw Error("Statistics for: " + statistic + " does not match. Expected: '" + expected + "' Given: '" + given + "'"); |
| 47 | + } |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + this.Then(/^requesting the `Statement Type` should give (.*)$/, function (type) { |
| 52 | + var type = getStatementTypeFromString(type); |
| 53 | + if (this.summary.statementType != type) |
| 54 | + { |
| 55 | + throw Error("statementType does not match. Expected: '" + type + "' Given: '" + this.summary.statementType + "'"); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + this.Then(/^the `Result Summary` has a `Plan`$/, function () { |
| 60 | + if(! this.summary.hasPlan()) { |
| 61 | + throw Error("Expected summary to have a `plan`. It did not..."); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + this.Then(/^the `Result Summary` does not have a `Plan`$/, function () { |
| 66 | + if(this.summary.hasPlan()) { |
| 67 | + throw Error("Expected summary to NOT have a `plan`. It did not..."); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + this.Then(/^the `Result Summary` has a `Profile`$/, function () { |
| 72 | + if(! this.summary.hasProfile()) { |
| 73 | + throw Error("Expected summary to have a `profile plan`. It did not..."); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + this.Then(/^the `Result Summary` does not have a `Profile`$/, function () { |
| 78 | + if( this.summary.hasProfile()) { |
| 79 | + throw Error("Expected summary to NOT have a `profile plan`. It did..."); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + this.Then(/^requesting the `Plan` it contains$/, function (table) { |
| 84 | + checkPlanExact(table, this.summary.plan) |
| 85 | + }); |
| 86 | + |
| 87 | + this.Then(/^the `Plan` also contains method calls for:$/, function (table) { |
| 88 | + checkPlan(table, this.summary.plan) |
| 89 | + }); |
| 90 | + |
| 91 | + this.Then(/^requesting the `Profile` it contains:$/, function (table) { |
| 92 | + checkPlanExact(table, this.summary.profile) |
| 93 | +}); |
| 94 | + |
| 95 | +this.Then(/^the `Profile` also contains method calls for:$/, function (table) { |
| 96 | + checkPlan(table, this.summary.profile) |
| 97 | +}); |
| 98 | + |
| 99 | +this.Then(/^the `Result Summary` `Notifications` is empty$/, function () { |
| 100 | + if (! this.summary.notifications.length == 0) { |
| 101 | + throw Error("Expected no notifications. Got: " + this.summary.notifications.length) |
| 102 | + } |
| 103 | +}); |
| 104 | + |
| 105 | +this.Then(/^the `Result Summary` `Notifications` has one notification with$/, function (table) { |
| 106 | + |
| 107 | + var expected = {}; |
| 108 | + if (this.summary.notifications.length > 1) { |
| 109 | + throw Error("Expected only one notification. Got: " + this.summary.notifications.length) |
| 110 | + } |
| 111 | + var givenNotification = this.summary.notifications[0]; |
| 112 | + var given = {} |
| 113 | + for ( var i = 0 ; i < table.hashes().length; i++) { |
| 114 | + var key = table.hashes()[i]['key'] |
| 115 | + expected[key] = util.literalValueToTestValueNormalIntegers(table.hashes()[i]['value']); |
| 116 | + given[key] = givenNotification[key] |
| 117 | + } |
| 118 | + if (Object.keys(givenNotification).length !== Object.keys(given).length) { |
| 119 | + throw Error("Keys do not match with expected. Got: " + Object.keys(givenNotification) + " Expected: " + Object.keys(given)) |
| 120 | + } |
| 121 | + if (!util.compareValues(expected, given)) { |
| 122 | + throw Error("Summary notifications does not match. Expected: '" + util.printable(expected) + "' Given: '" + util.printable(givenNotification) + "'"); |
| 123 | + } |
| 124 | +}); |
| 125 | + |
| 126 | + function checkPlanExact(table, plan) |
| 127 | + { |
| 128 | + for ( var i = 0 ; i < table.hashes().length; i++) { |
| 129 | + var dataKey = getPlanParam(table.hashes()[i]["plan method"]) |
| 130 | + var given = plan[dataKey]; |
| 131 | + if (given === undefined || given === null || given === NaN) { |
| 132 | + throw Error("Plans `" + dataKey + "` has no content! Got: " + given); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + function checkPlan(table, plan) |
| 138 | + { |
| 139 | + for ( var i = 0 ; i < table.hashes().length; i++) { |
| 140 | + var dataKey = getPlanParam(table.hashes()[i]["plan method"]) |
| 141 | + var given = plan[dataKey]; |
| 142 | + if (given === undefined || given === null || given === NaN) { |
| 143 | + throw Error("Plans `" + dataKey + "` has no content! Got: " + given); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + function getPlanParam(key) { |
| 149 | + if (key == 'operator type') { |
| 150 | + return 'operatorType' |
| 151 | + } |
| 152 | + if (key == 'db hits') { |
| 153 | + return 'dbHits' |
| 154 | + } |
| 155 | + if (key == 'records') { |
| 156 | + return 'rows' |
| 157 | + } |
| 158 | + else { |
| 159 | + return key |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + function getStatementTypeFromString(type) { |
| 164 | + if (type == 'read write') { |
| 165 | + return resultSummary.statementType.READ_WRITE; |
| 166 | + } |
| 167 | + if (type == 'read only') { |
| 168 | + return resultSummary.statementType.READ_ONLY; |
| 169 | + } |
| 170 | + if (type == 'write only') { |
| 171 | + return resultSummary.statementType.WRITE_ONLY; |
| 172 | + } |
| 173 | + if (type == 'schema write') { |
| 174 | + return resultSummary.statementType.SCHEMA_WRITE; |
| 175 | + } |
| 176 | + throw Error("No statement type mapping of: " + type) |
| 177 | + } |
| 178 | + |
| 179 | + function getStatistic(statementString, updateStatistics) { |
| 180 | + if (statementString == 'nodes created') { |
| 181 | + return updateStatistics.nodesCreated(); |
| 182 | + } |
| 183 | + if (statementString == 'nodes deleted') { |
| 184 | + return updateStatistics.nodesDeleted(); |
| 185 | + } |
| 186 | + if (statementString == 'relationships created') { |
| 187 | + return updateStatistics.relationshipsCreated(); |
| 188 | + } |
| 189 | + if (statementString == 'relationships deleted') { |
| 190 | + return updateStatistics.relationshipsDeleted(); |
| 191 | + } |
| 192 | + if (statementString == 'properties set') { |
| 193 | + return updateStatistics.propertiesSet(); |
| 194 | + } |
| 195 | + if (statementString == 'labels added') { |
| 196 | + return updateStatistics.labelsAdded(); |
| 197 | + } |
| 198 | + if (statementString == 'labels removed') { |
| 199 | + return updateStatistics.labelsRemoved(); |
| 200 | + } |
| 201 | + if (statementString == 'indexes added') { |
| 202 | + return updateStatistics.indexesAdded(); |
| 203 | + } |
| 204 | + if (statementString == 'indexes removed') { |
| 205 | + return updateStatistics.indexesRemoved(); |
| 206 | + } |
| 207 | + if (statementString == 'constraints added') { |
| 208 | + return updateStatistics.constraintsAdded(); |
| 209 | + } |
| 210 | + if (statementString == 'constraints removed') { |
| 211 | + return updateStatistics.constraintsRemoved(); |
| 212 | + } |
| 213 | + if (statementString == 'contains updates') { |
| 214 | + return updateStatistics.containsUpdates(); |
| 215 | + } |
| 216 | + throw Error("No statistics mapping of: " + statementString) |
| 217 | + } |
| 218 | + |
| 219 | +} |
0 commit comments