mirror of
https://github.com/debba/wp-media-rewind.git
synced 2026-08-03 07:29:00 +02:00
48 lines
2.2 KiB
TypeScript
48 lines
2.2 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { extractAttachmentGuids, splitTuples } from "../src/utils/sql.js";
|
|
|
|
test("splitTuples splits flat tuples", () => {
|
|
const line = "INSERT INTO `t` VALUES (1,'a'),(2,'b'),(3,'c');";
|
|
assert.deepEqual(splitTuples(line), ["1,'a'", "2,'b'", "3,'c'"]);
|
|
});
|
|
|
|
test("splitTuples ignores parens inside quoted strings", () => {
|
|
const line = "INSERT INTO `t` VALUES (1,'foo (bar) baz'),(2,'x');";
|
|
assert.deepEqual(splitTuples(line), ["1,'foo (bar) baz'", "2,'x'"]);
|
|
});
|
|
|
|
test("splitTuples handles escaped quotes", () => {
|
|
const line = "INSERT INTO `t` VALUES (1,'it\\'s ok'),(2,'y');";
|
|
const tuples = splitTuples(line);
|
|
assert.equal(tuples.length, 2);
|
|
assert.equal(tuples[0], "1,'it\\'s ok'");
|
|
});
|
|
|
|
test("extractAttachmentGuids returns URLs from attachment rows", () => {
|
|
const line =
|
|
"INSERT INTO `wp_posts` VALUES " +
|
|
"(1,1,'2020-01-01','','c','t','','publish','open','open','','slug','','','2020-01-01','2020-01-01','',0,'https://site.test/wp-content/uploads/2020/01/foo.jpg',0,'attachment','image/jpeg',0)," +
|
|
"(2,1,'2020-01-01','','c','t','','publish','open','open','','slug','','','2020-01-01','2020-01-01','',0,'https://site.test/?p=2',0,'post','',0);";
|
|
assert.deepEqual(extractAttachmentGuids(line), [
|
|
"https://site.test/wp-content/uploads/2020/01/foo.jpg",
|
|
]);
|
|
});
|
|
|
|
test("extractAttachmentGuids returns [] when no attachment rows", () => {
|
|
const line =
|
|
"INSERT INTO `wp_posts` VALUES (2,1,'2020-01-01','c','t','','publish','open','open','','slug','','','2020-01-01','2020-01-01','',0,'https://site.test/?p=2',0,'post','',0);";
|
|
assert.deepEqual(extractAttachmentGuids(line), []);
|
|
});
|
|
|
|
test("extractAttachmentGuids handles multiple attachment rows", () => {
|
|
const line =
|
|
"INSERT INTO `wp_posts` VALUES " +
|
|
"(1,1,'d','','c','t','','publish','open','open','','s','','','d','d','',0,'https://site.test/a.jpg',0,'attachment','image/jpeg',0)," +
|
|
"(2,1,'d','','c','t','','publish','open','open','','s','','','d','d','',0,'https://site.test/b.png',0,'attachment','image/png',0);";
|
|
assert.deepEqual(extractAttachmentGuids(line), [
|
|
"https://site.test/a.jpg",
|
|
"https://site.test/b.png",
|
|
]);
|
|
});
|