|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | 3 | var mysql = require("mysql2"); |
| 4 | +var mysqlPromise = require("mysql2/promise"); |
| 5 | + |
| 6 | +var result = {}; |
4 | 7 |
|
5 | | -var result = { |
6 | | - createConnectionExists: typeof mysql.createConnection === "function", |
7 | | - createPoolExists: typeof mysql.createPool === "function", |
8 | | - createPoolClusterExists: typeof mysql.createPoolCluster === "function", |
9 | | -}; |
| 8 | +// Core factory functions |
| 9 | +result.createConnectionExists = typeof mysql.createConnection === "function"; |
| 10 | +result.createPoolExists = typeof mysql.createPool === "function"; |
| 11 | +result.createPoolClusterExists = typeof mysql.createPoolCluster === "function"; |
10 | 12 |
|
11 | | -// Verify mysql2 internal protocol classes loaded (via public re-exports) |
| 13 | +// Protocol types and charsets |
12 | 14 | var Types = mysql.Types; |
13 | 15 | result.typesExists = typeof Types === "object" && Types !== null; |
14 | | - |
15 | | -// Verify charset constants |
16 | 16 | result.hasCharsets = typeof mysql.Charsets === "object" && mysql.Charsets !== null; |
17 | 17 |
|
18 | | -// Verify escape/format utilities |
| 18 | +// Escape and format utilities — comprehensive coverage |
19 | 19 | result.escapeString = mysql.escape("hello 'world'"); |
| 20 | +result.escapeNumber = mysql.escape(42); |
| 21 | +result.escapeNull = mysql.escape(null); |
| 22 | +result.escapeBool = mysql.escape(true); |
| 23 | +result.escapeArray = mysql.escape([1, "two", null]); |
| 24 | +result.escapeNested = mysql.escape([[1, 2], [3, 4]]); |
20 | 25 | result.escapeId = mysql.escapeId("table name"); |
| 26 | +result.escapeIdQualified = mysql.escapeId("db.table"); |
21 | 27 | result.formatSql = mysql.format("SELECT ? FROM ??", ["value", "table"]); |
| 28 | +result.formatMulti = mysql.format("INSERT INTO ?? SET ?", [ |
| 29 | + "users", |
| 30 | + { name: "test", age: 30 }, |
| 31 | +]); |
22 | 32 |
|
23 | | -// Verify raw() for prepared statements |
| 33 | +// raw() for prepared statement placeholders |
24 | 34 | result.hasRaw = typeof mysql.raw === "function"; |
| 35 | +var rawVal = mysql.raw("NOW()"); |
| 36 | +result.rawEscape = mysql.escape(rawVal); |
25 | 37 |
|
26 | | -// Verify promise wrapper is available |
27 | | -var mysqlPromise = require("mysql2/promise"); |
| 38 | +// Connection pool configuration (no connection needed — exercises config parsing) |
| 39 | +var pool = mysql.createPool({ |
| 40 | + host: "127.0.0.1", |
| 41 | + port: 0, |
| 42 | + user: "root", |
| 43 | + password: "test", |
| 44 | + database: "testdb", |
| 45 | + waitForConnections: true, |
| 46 | + connectionLimit: 5, |
| 47 | + queueLimit: 0, |
| 48 | + enableKeepAlive: true, |
| 49 | + keepAliveInitialDelay: 10000, |
| 50 | +}); |
| 51 | +result.poolCreated = pool !== null && typeof pool === "object"; |
| 52 | +result.poolMethods = [ |
| 53 | + "getConnection", |
| 54 | + "query", |
| 55 | + "execute", |
| 56 | + "end", |
| 57 | + "on", |
| 58 | + "promise", |
| 59 | +].filter(function (m) { |
| 60 | + return typeof pool[m] === "function"; |
| 61 | +}); |
| 62 | + |
| 63 | +// Pool event emitter interface |
| 64 | +result.poolHasOn = typeof pool.on === "function"; |
| 65 | +result.poolHasEmit = typeof pool.emit === "function"; |
| 66 | + |
| 67 | +// Pool cluster configuration |
| 68 | +var cluster = mysql.createPoolCluster({ |
| 69 | + canRetry: true, |
| 70 | + removeNodeErrorCount: 5, |
| 71 | + defaultSelector: "RR", |
| 72 | +}); |
| 73 | +result.clusterCreated = cluster !== null && typeof cluster === "object"; |
| 74 | +result.clusterMethods = ["add", "remove", "getConnection", "of", "end", "on"].filter( |
| 75 | + function (m) { |
| 76 | + return typeof cluster[m] === "function"; |
| 77 | + }, |
| 78 | +); |
| 79 | + |
| 80 | +// Add nodes to cluster (exercises config validation — no connections made) |
| 81 | +cluster.add("MASTER", { |
| 82 | + host: "127.0.0.1", |
| 83 | + port: 0, |
| 84 | + user: "root", |
| 85 | + password: "test", |
| 86 | + database: "testdb", |
| 87 | +}); |
| 88 | +cluster.add("REPLICA1", { |
| 89 | + host: "127.0.0.1", |
| 90 | + port: 0, |
| 91 | + user: "root", |
| 92 | + password: "test", |
| 93 | + database: "testdb", |
| 94 | +}); |
| 95 | + |
| 96 | +// Cluster pattern selector |
| 97 | +var clusterOf = cluster.of("REPLICA*"); |
| 98 | +result.clusterOfCreated = clusterOf !== null && typeof clusterOf === "object"; |
| 99 | + |
| 100 | +// Promise wrapper — deeper coverage |
28 | 101 | result.promiseCreateConnection = typeof mysqlPromise.createConnection === "function"; |
29 | 102 | result.promiseCreatePool = typeof mysqlPromise.createPool === "function"; |
| 103 | +result.promiseCreatePoolCluster = |
| 104 | + typeof mysqlPromise.createPoolCluster === "function"; |
| 105 | + |
| 106 | +// Promise pool with same config shape |
| 107 | +var promisePool = mysqlPromise.createPool({ |
| 108 | + host: "127.0.0.1", |
| 109 | + port: 0, |
| 110 | + user: "root", |
| 111 | + password: "test", |
| 112 | + database: "testdb", |
| 113 | + connectionLimit: 2, |
| 114 | +}); |
| 115 | +result.promisePoolCreated = promisePool !== null && typeof promisePool === "object"; |
| 116 | +result.promisePoolMethods = ["getConnection", "query", "execute", "end"].filter( |
| 117 | + function (m) { |
| 118 | + return typeof promisePool[m] === "function"; |
| 119 | + }, |
| 120 | +); |
| 121 | + |
| 122 | +// Type casting and field metadata |
| 123 | +result.typeNames = [ |
| 124 | + "DECIMAL", |
| 125 | + "TINY", |
| 126 | + "SHORT", |
| 127 | + "LONG", |
| 128 | + "FLOAT", |
| 129 | + "DOUBLE", |
| 130 | + "TIMESTAMP", |
| 131 | + "LONGLONG", |
| 132 | + "INT24", |
| 133 | + "DATE", |
| 134 | + "TIME", |
| 135 | + "DATETIME", |
| 136 | + "YEAR", |
| 137 | + "NEWDATE", |
| 138 | + "VARCHAR", |
| 139 | + "BIT", |
| 140 | + "JSON", |
| 141 | + "NEWDECIMAL", |
| 142 | + "ENUM", |
| 143 | + "SET", |
| 144 | + "TINY_BLOB", |
| 145 | + "MEDIUM_BLOB", |
| 146 | + "LONG_BLOB", |
| 147 | + "BLOB", |
| 148 | + "VAR_STRING", |
| 149 | + "STRING", |
| 150 | + "GEOMETRY", |
| 151 | +].filter(function (t) { |
| 152 | + return typeof Types[t] === "number"; |
| 153 | +}); |
| 154 | + |
| 155 | +// Format with Date objects (use epoch 0 for timezone-stable output) |
| 156 | +var d = new Date(0); |
| 157 | +result.formatDateType = typeof mysql.format("SELECT ?", [d]); |
| 158 | + |
| 159 | +// Format with Buffer |
| 160 | +result.formatBuffer = mysql.format("SELECT ?", [Buffer.from("binary")]); |
| 161 | + |
| 162 | +// Format with nested object (SET clause) |
| 163 | +result.formatObject = mysql.format("UPDATE ?? SET ?", [ |
| 164 | + "tbl", |
| 165 | + { name: "test", active: true, score: null }, |
| 166 | +]); |
| 167 | + |
| 168 | +// Clean up pools (no connections to close — releases internal timers) |
| 169 | +pool.end(function () {}); |
| 170 | +cluster.end(function () {}); |
| 171 | +promisePool.end().catch(function () {}); |
30 | 172 |
|
31 | 173 | console.log(JSON.stringify(result)); |
0 commit comments