|
30 | 30 | describe("table", function() |
31 | 31 | local t1, t2 = seed() |
32 | 32 |
|
33 | | - describe(":extend", function() |
34 | | - local t |
35 | | - it("missing db object", function() |
36 | | - ---@type SQLTableExt |
37 | | - t = tbl("tbl_name", { id = true, name = "text" }) |
38 | | - eq(false, pcall(t.insert, { name = "tami" }), "should fail early.") |
39 | | - t.set_db(db) |
40 | | - eq(true, pcall(t.insert, { name = "conni" }), "should work now we have a db object to operate against.") |
41 | | - eq({ { id = 1, name = "conni" } }, t.get(), "only insert conni") |
42 | | - end) |
43 | | - |
44 | | - it("with db object", function() |
45 | | - t = tbl(db, "tansactions", { id = true, amount = "real" }) |
46 | | - eq(1, t.insert { amount = 20.2 }) |
47 | | - eq( |
48 | | - "20.2", |
49 | | - t.map(function(row) |
50 | | - return tostring(row.amount) |
51 | | - end)[1] |
52 | | - ) |
53 | | - end) |
54 | | - |
55 | | - it("overwrite functions and fallback to t.db", function() |
56 | | - t.get = function() |
57 | | - return math.floor(t._get({ where = { id = 1 } })[1].amount) |
58 | | - end |
59 | | - eq(20, t.get()) |
60 | | - end) |
61 | | - end) |
62 | | - |
63 | 33 | describe(":new", function() |
64 | 34 | it("create new object with sql.table methods.", function() |
65 | 35 | eq("table", type(t1)) |
@@ -537,4 +507,75 @@ describe("table", function() |
537 | 507 | eq(true, next(tracks:get { where = { artist = 100 } }) == nil, "shouldn't be any rows referencing Dean Martin") |
538 | 508 | end) |
539 | 509 | end) |
| 510 | + |
| 511 | + describe(":extend", function() |
| 512 | + local t |
| 513 | + it("missing db object", function() |
| 514 | + ---@type SQLTableExt |
| 515 | + t = tbl("tbl_name", { id = true, name = "text" }) |
| 516 | + eq(false, pcall(t.insert, { name = "tami" }), "should fail early.") |
| 517 | + t.set_db(db) |
| 518 | + eq(true, pcall(t.insert, { name = "conni" }), "should work now we have a db object to operate against.") |
| 519 | + eq({ { id = 1, name = "conni" } }, t.get(), "only insert conni") |
| 520 | + end) |
| 521 | + |
| 522 | + it("with db object", function() |
| 523 | + t = tbl(db, "tansactions", { id = true, amount = "real" }) |
| 524 | + eq(1, t.insert { amount = 20.2 }) |
| 525 | + eq( |
| 526 | + "20.2", |
| 527 | + t.map(function(row) |
| 528 | + return tostring(row.amount) |
| 529 | + end)[1] |
| 530 | + ) |
| 531 | + end) |
| 532 | + |
| 533 | + it("overwrite functions and fallback to t.db", function() |
| 534 | + t.get = function() |
| 535 | + return math.floor(t._get({ where = { id = 1 } })[1].amount) |
| 536 | + end |
| 537 | + eq(20, t.get()) |
| 538 | + end) |
| 539 | + |
| 540 | + local some |
| 541 | + |
| 542 | + it("create a new table", function() |
| 543 | + some = tbl:extend(db, "somename", { |
| 544 | + name = "text", |
| 545 | + id = true, |
| 546 | + count = { |
| 547 | + type = "integer", |
| 548 | + default = 0, |
| 549 | + }, |
| 550 | + }) |
| 551 | + end) |
| 552 | + |
| 553 | + it("access function", function() |
| 554 | + eq("function", type(some.get), "should we still have access for") |
| 555 | + eq("function", type(some._get), "should be hard coded.") |
| 556 | + eq("cdata", type(some.db.conn)) |
| 557 | + end) |
| 558 | + |
| 559 | + it("custom function", function() |
| 560 | + function some.insert_or_update(name) |
| 561 | + eq("string", type(name), "name should be a string") |
| 562 | + local entry = some.where { name = name } |
| 563 | + if not entry then |
| 564 | + some.insert { name = name } |
| 565 | + else |
| 566 | + eq("function", type(some.update)) |
| 567 | + some.update { |
| 568 | + where = { id = entry.id }, |
| 569 | + values = { count = entry.count + 2 }, |
| 570 | + } |
| 571 | + end |
| 572 | + end |
| 573 | + -- eq({}, getmetatable(some)) |
| 574 | + eq("function", type(some.insert_or_update), "should be registered as function") |
| 575 | + some.insert_or_update "ff" |
| 576 | + -- eq("ff", some.get({ name = "ff" }).name) |
| 577 | + some.insert_or_update "ff" |
| 578 | + -- eq(2, some.where({ name = "ff" }).count) |
| 579 | + end) |
| 580 | + end) |
540 | 581 | end) |
0 commit comments