/PetaPoco.Tests/Tests.cs

http://github.com/toptensoftware/PetaPoco
C# | 883 lines | 639 code | 155 blank | 89 comment | 9 complexity | 64f22d471a2eaa2972dd01406903e9ec MD5 | raw file

✨ Summary
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using PetaTest;
  6. using PetaPoco;
  7. namespace PetaPoco.Tests
  8. {
  9. [TestFixture("sqlserver")]
  10. [TestFixture("sqlserverce")]
  11. [TestFixture("mysql")]
  12. [TestFixture("postgresql")]
  13. public class Tests
  14. {
  15. public Tests(string connectionStringName)
  16. {
  17. _connectionStringName = connectionStringName;
  18. }
  19. string _connectionStringName;
  20. Random r = new Random();
  21. Database db;
  22. [TestFixtureSetUp]
  23. public void CreateDB()
  24. {
  25. db = new Database(_connectionStringName);
  26. db.OpenSharedConnection(); // <-- Wow, this is crucial to getting SqlCE to perform.
  27. db.Execute(Utils.LoadTextResource(string.Format("PetaPoco.Tests.{0}_init.sql", _connectionStringName)));
  28. }
  29. [TestFixtureTearDown]
  30. public void DeleteDB()
  31. {
  32. db.Execute(Utils.LoadTextResource(string.Format("PetaPoco.Tests.{0}_done.sql", _connectionStringName)));
  33. }
  34. long GetRecordCount()
  35. {
  36. return db.ExecuteScalar<long>("SELECT COUNT(*) FROM petapoco");
  37. }
  38. [TearDown]
  39. public void Teardown()
  40. {
  41. // Delete everything
  42. db.Delete<deco>("");
  43. db.Delete<petapoco2>("");
  44. // Should be clean
  45. Assert.AreEqual(GetRecordCount(), 0);
  46. }
  47. poco CreatePoco()
  48. {
  49. // Need a rounded date as DB can't store millis
  50. var now = DateTime.UtcNow;
  51. now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
  52. // Setup a record
  53. var o = new poco();
  54. o.title = string.Format("insert {0}", r.Next());
  55. o.draft = true;
  56. o.content = string.Format("insert {0}", r.Next());
  57. o.date_created = now;
  58. o.date_edited = now;
  59. o.state = State.Yes;
  60. o.col_w_space = 23;
  61. o.nullreal = 24;
  62. return o;
  63. }
  64. deco CreateDeco()
  65. {
  66. // Need a rounded date as DB can't store millis
  67. var now = DateTime.UtcNow;
  68. now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
  69. // Setup a record
  70. var o = new deco();
  71. o.title = string.Format("insert {0}", r.Next());
  72. o.draft = true;
  73. o.content = string.Format("insert {0}", r.Next());
  74. o.date_created = now;
  75. o.date_edited = now;
  76. o.state = State.Maybe;
  77. o.col_w_space = 23;
  78. o.nullreal = 24;
  79. return o;
  80. }
  81. void AssertPocos(poco a, poco b)
  82. {
  83. Assert.AreEqual(a.id, b.id);
  84. Assert.AreEqual(a.title, b.title);
  85. Assert.AreEqual(a.draft, b.draft);
  86. Assert.AreEqual(a.content, b.content);
  87. Assert.AreEqual(a.date_created, b.date_created);
  88. Assert.AreEqual(a.date_edited, b.date_edited);
  89. Assert.AreEqual(a.state, b.state);
  90. Assert.AreEqual(a.col_w_space, b.col_w_space);
  91. Assert.AreEqual(a.nullreal, b.nullreal);
  92. }
  93. void AssertPocos(deco a, deco b)
  94. {
  95. Assert.AreEqual(a.id, b.id);
  96. Assert.AreEqual(a.title, b.title);
  97. Assert.AreEqual(a.draft, b.draft);
  98. Assert.AreEqual(a.content, b.content);
  99. Assert.AreEqual(a.date_created, b.date_created);
  100. Assert.AreEqual(a.state, b.state);
  101. Assert.AreEqual(a.col_w_space, b.col_w_space);
  102. Assert.AreEqual(a.nullreal, b.nullreal);
  103. }
  104. // Insert some records, return the id of the first
  105. long InsertRecords(int count)
  106. {
  107. long lFirst = 0;
  108. for (int i = 0; i < count; i++)
  109. {
  110. var o=CreatePoco();
  111. db.Insert("petapoco", "id", o);
  112. var lc = db.LastCommand;
  113. if (i == 0)
  114. {
  115. lFirst = o.id;
  116. Assert.AreNotEqual(o.id, 0);
  117. }
  118. }
  119. return lFirst;
  120. }
  121. [Test]
  122. public void poco_Crud()
  123. {
  124. // Create a random record
  125. var o = CreatePoco();
  126. Assert.IsTrue(db.IsNew("id", o));
  127. // Insert it
  128. db.Insert("petapoco", "id", o);
  129. Assert.AreNotEqual(o.id, 0);
  130. Assert.IsFalse(db.IsNew("id", o));
  131. // Retrieve it
  132. var o2 = db.Single<poco>("SELECT * FROM petapoco WHERE id=@0", o.id);
  133. Assert.IsFalse(db.IsNew("id", o2));
  134. // Check it
  135. AssertPocos(o, o2);
  136. // Update it
  137. o2.title = "New Title";
  138. db.Save("petapoco", "id", o2);
  139. // Retrieve itagain
  140. var o3 = db.Single<poco>("SELECT * FROM petapoco WHERE id=@0", o.id);
  141. // Check it
  142. AssertPocos(o2, o3);
  143. // Delete it
  144. db.Delete("petapoco", "id", o3);
  145. // Should be gone!
  146. var o4 = db.SingleOrDefault<poco>("SELECT * FROM petapoco WHERE id=@0", o.id);
  147. Assert.IsNull(o4);
  148. }
  149. [Test]
  150. public void deco_Crud()
  151. {
  152. // Create a random record
  153. var o = CreateDeco();
  154. Assert.IsTrue(db.IsNew(o));
  155. // Insert it
  156. db.Insert(o);
  157. Assert.AreNotEqual(o.id, 0);
  158. Assert.IsFalse(db.IsNew(o));
  159. // Retrieve it
  160. var o2 = db.Single<deco>("SELECT * FROM petapoco WHERE id=@0", o.id);
  161. Assert.IsFalse(db.IsNew(o2));
  162. // Check it
  163. AssertPocos(o, o2);
  164. // Update it
  165. o2.title = "New Title";
  166. db.Save(o2);
  167. // Retrieve itagain
  168. var o3 = db.Single<deco>("SELECT * FROM petapoco WHERE id=@0", o.id);
  169. // Check it
  170. AssertPocos(o2, o3);
  171. // Delete it
  172. db.Delete(o3);
  173. // Should be gone!
  174. var o4 = db.SingleOrDefault<deco>("SELECT * FROM petapoco WHERE id=@0", o.id);
  175. Assert.IsNull(o4);
  176. }
  177. [Test]
  178. public void Fetch()
  179. {
  180. // Create some records
  181. const int count = 5;
  182. long id = InsertRecords(count);
  183. // Fetch em
  184. var r = db.Fetch<poco>("SELECT * from petapoco ORDER BY id");
  185. Assert.AreEqual(r.Count, count);
  186. // Check em
  187. for (int i = 0; i < count; i++)
  188. {
  189. Assert.AreEqual(r[i].id, id + i);
  190. }
  191. }
  192. [Test]
  193. public void Query()
  194. {
  195. // Create some records
  196. const int count = 5;
  197. long id = InsertRecords(count);
  198. // Fetch em
  199. var r = db.Query<poco>("SELECT * from petapoco ORDER BY id");
  200. // Check em
  201. int i = 0;
  202. foreach (var p in r)
  203. {
  204. Assert.AreEqual(p.id, id + i);
  205. i++;
  206. }
  207. Assert.AreEqual(i, count);
  208. }
  209. [Test]
  210. public void Page()
  211. {
  212. // In this test we're checking that the page count is correct when there are
  213. // not-exactly pagesize*N records (ie: a partial page at the end)
  214. // Create some records
  215. const int count = 13;
  216. long id = InsertRecords(count);
  217. // Fetch em
  218. var r = db.Page<poco>(2, 5, "SELECT * from petapoco ORDER BY id");
  219. // Check em
  220. int i = 0;
  221. foreach (var p in r.Items)
  222. {
  223. Assert.AreEqual(p.id, id + i + 5);
  224. i++;
  225. }
  226. // Check other stats
  227. Assert.AreEqual(r.Items.Count, 5);
  228. Assert.AreEqual(r.CurrentPage, 2);
  229. Assert.AreEqual(r.ItemsPerPage, 5);
  230. Assert.AreEqual(r.TotalItems, 13);
  231. Assert.AreEqual(r.TotalPages, 3);
  232. }
  233. [Test]
  234. public void Page_NoOrderBy()
  235. {
  236. // Unordered paging not supported by Compact Edition
  237. if (_connectionStringName == "sqlserverce")
  238. return;
  239. // In this test we're checking that the page count is correct when there are
  240. // not-exactly pagesize*N records (ie: a partial page at the end)
  241. // Create some records
  242. const int count = 13;
  243. long id = InsertRecords(count);
  244. // Fetch em
  245. var r = db.Page<poco>(2, 5, "SELECT * from petapoco");
  246. // Check em
  247. int i = 0;
  248. foreach (var p in r.Items)
  249. {
  250. Assert.AreEqual(p.id, id + i + 5);
  251. i++;
  252. }
  253. // Check other stats
  254. Assert.AreEqual(r.Items.Count, 5);
  255. Assert.AreEqual(r.CurrentPage, 2);
  256. Assert.AreEqual(r.ItemsPerPage, 5);
  257. Assert.AreEqual(r.TotalItems, 13);
  258. Assert.AreEqual(r.TotalPages, 3);
  259. }
  260. [Test]
  261. public void Page_Distinct()
  262. {
  263. // Unordered paging not supported by Compact Edition
  264. if (_connectionStringName == "sqlserverce")
  265. return;
  266. // In this test we're checking that the page count is correct when there are
  267. // not-exactly pagesize*N records (ie: a partial page at the end)
  268. // Create some records
  269. const int count = 13;
  270. long id = InsertRecords(count);
  271. // Fetch em
  272. var r = db.Page<poco>(2, 5, "SELECT DISTINCT id from petapoco ORDER BY id");
  273. // Check em
  274. int i = 0;
  275. foreach (var p in r.Items)
  276. {
  277. Assert.AreEqual(p.id, id + i + 5);
  278. i++;
  279. }
  280. // Check other stats
  281. Assert.AreEqual(r.Items.Count, 5);
  282. Assert.AreEqual(r.CurrentPage, 2);
  283. Assert.AreEqual(r.ItemsPerPage, 5);
  284. Assert.AreEqual(r.TotalItems, 13);
  285. Assert.AreEqual(r.TotalPages, 3);
  286. }
  287. [Test]
  288. public void FetchPage()
  289. {
  290. // Create some records
  291. const int count = 13;
  292. long id = InsertRecords(count);
  293. // Fetch em
  294. var r = db.Fetch<poco>(2, 5, "SELECT * from petapoco ORDER BY id");
  295. // Check em
  296. int i = 0;
  297. foreach (var p in r)
  298. {
  299. Assert.AreEqual(p.id, id + i + 5);
  300. i++;
  301. }
  302. // Check other stats
  303. Assert.AreEqual(r.Count, 5);
  304. }
  305. [Test]
  306. public void Page_boundary()
  307. {
  308. // In this test we're checking that the page count is correct when there are
  309. // exactly pagesize*N records.
  310. // Create some records
  311. const int count = 15;
  312. long id = InsertRecords(count);
  313. // Fetch em
  314. var r = db.Page<poco>(3, 5, "SELECT * from petapoco ORDER BY id");
  315. // Check other stats
  316. Assert.AreEqual(r.Items.Count, 5);
  317. Assert.AreEqual(r.CurrentPage, 3);
  318. Assert.AreEqual(r.ItemsPerPage, 5);
  319. Assert.AreEqual(r.TotalItems, 15);
  320. Assert.AreEqual(r.TotalPages, 3);
  321. }
  322. [Test]
  323. public void deco_Delete()
  324. {
  325. // Create some records
  326. const int count = 15;
  327. long id = InsertRecords(count);
  328. // Delete some
  329. db.Delete<deco>("WHERE id>=@0", id + 5);
  330. // Check they match
  331. Assert.AreEqual(GetRecordCount(), 5);
  332. }
  333. [Test]
  334. public void deco_Update()
  335. {
  336. // Create some records
  337. const int count = 15;
  338. long id = InsertRecords(count);
  339. // Update some
  340. db.Update<deco>("SET title=@0 WHERE id>=@1", "zap", id + 5);
  341. // Check some updated
  342. foreach (var d in db.Query<deco>("ORDER BY Id"))
  343. {
  344. if (d.id >= id + 5)
  345. {
  346. Assert.AreEqual(d.title, "zap");
  347. }
  348. else
  349. {
  350. Assert.AreNotEqual(d.title, "zap");
  351. }
  352. }
  353. }
  354. [Test]
  355. public void deco_ExplicitAttribute()
  356. {
  357. // Create a records
  358. long id = InsertRecords(1);
  359. // Retrieve it in two different ways
  360. var a = db.SingleOrDefault<deco>("WHERE id=@0", id);
  361. var b = db.SingleOrDefault<deco_explicit>("WHERE id=@0", id);
  362. var c = db.SingleOrDefault<deco_explicit>("SELECT * FROM petapoco WHERE id=@0", id);
  363. // b record should have ignored the content
  364. Assert.IsNotNull(a.content);
  365. Assert.IsNull(b.content);
  366. Assert.IsNull(c.content);
  367. }
  368. [Test]
  369. public void deco_IgnoreAttribute()
  370. {
  371. // Create a records
  372. long id = InsertRecords(1);
  373. // Retrieve it in two different ways
  374. var a = db.SingleOrDefault<deco>("WHERE id=@0", id);
  375. var b = db.SingleOrDefault<deco_non_explicit>("WHERE id=@0", id);
  376. var c = db.SingleOrDefault<deco_non_explicit>("SELECT * FROM petapoco WHERE id=@0", id);
  377. // b record should have ignored the content
  378. Assert.IsNotNull(a.content);
  379. Assert.IsNull(b.content);
  380. Assert.IsNull(c.content);
  381. }
  382. [Test]
  383. public void Transaction_complete()
  384. {
  385. using (var scope = db.GetTransaction())
  386. {
  387. InsertRecords(10);
  388. scope.Complete();
  389. }
  390. Assert.AreEqual(GetRecordCount(), 10);
  391. }
  392. [Test]
  393. public void Transaction_cancelled()
  394. {
  395. using (var scope = db.GetTransaction())
  396. {
  397. InsertRecords(10);
  398. }
  399. Assert.AreEqual(GetRecordCount(), 0);
  400. }
  401. [Test]
  402. public void Transaction_nested_nn()
  403. {
  404. using (var scope1 = db.GetTransaction())
  405. {
  406. InsertRecords(10);
  407. using (var scope2 = db.GetTransaction())
  408. {
  409. InsertRecords(10);
  410. }
  411. }
  412. Assert.AreEqual(GetRecordCount(), 0);
  413. }
  414. [Test]
  415. public void Transaction_nested_yn()
  416. {
  417. using (var scope1 = db.GetTransaction())
  418. {
  419. InsertRecords(10);
  420. using (var scope2 = db.GetTransaction())
  421. {
  422. InsertRecords(10);
  423. }
  424. scope1.Complete();
  425. }
  426. Assert.AreEqual(GetRecordCount(), 0);
  427. }
  428. [Test]
  429. public void Transaction_nested_ny()
  430. {
  431. using (var scope1 = db.GetTransaction())
  432. {
  433. InsertRecords(10);
  434. using (var scope2 = db.GetTransaction())
  435. {
  436. InsertRecords(10);
  437. scope2.Complete();
  438. }
  439. }
  440. Assert.AreEqual(GetRecordCount(), 0);
  441. }
  442. [Test]
  443. public void Transaction_nested_yy()
  444. {
  445. using (var scope1 = db.GetTransaction())
  446. {
  447. InsertRecords(10);
  448. using (var scope2 = db.GetTransaction())
  449. {
  450. InsertRecords(10);
  451. scope2.Complete();
  452. }
  453. scope1.Complete();
  454. }
  455. Assert.AreEqual(GetRecordCount(), 20);
  456. }
  457. [Test]
  458. public void Transaction_nested_yny()
  459. {
  460. using (var scope1 = db.GetTransaction())
  461. {
  462. InsertRecords(10);
  463. using (var scope2 = db.GetTransaction())
  464. {
  465. InsertRecords(10);
  466. //scope2.Complete();
  467. }
  468. using (var scope3 = db.GetTransaction())
  469. {
  470. InsertRecords(10);
  471. scope3.Complete();
  472. }
  473. scope1.Complete();
  474. }
  475. Assert.AreEqual(GetRecordCount(), 0);
  476. }
  477. [Test]
  478. public void DateTimesAreUtc()
  479. {
  480. var id = InsertRecords(1);
  481. var a2 = db.SingleOrDefault<deco>("WHERE id=@0", id);
  482. Assert.AreEqual(a2.date_created.Kind, DateTimeKind.Utc);
  483. Assert.AreEqual(a2.date_edited.Value.Kind, DateTimeKind.Utc);
  484. }
  485. [Test]
  486. public void DateTimeNullable()
  487. {
  488. // Need a rounded date as DB can't store millis
  489. var now = DateTime.UtcNow;
  490. now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
  491. // Setup a record
  492. var a = new deco();
  493. a.title = string.Format("insert {0}", r.Next());
  494. a.draft = true;
  495. a.content = string.Format("insert {0}", r.Next());
  496. a.date_created = now;
  497. a.date_edited = null;
  498. db.Insert(a);
  499. // Retrieve it
  500. var b = db.SingleOrDefault<deco>("WHERE id=@0", a.id);
  501. Assert.AreEqual(b.id, a.id);
  502. Assert.AreEqual(b.date_edited.HasValue, false);
  503. // Update it to NULL
  504. b.date_edited = now;
  505. db.Update(b);
  506. var c = db.SingleOrDefault<deco>("WHERE id=@0", a.id);
  507. Assert.AreEqual(c.id, a.id);
  508. Assert.AreEqual(c.date_edited.HasValue, true);
  509. // Update it to not NULL
  510. c.date_edited = null;
  511. db.Update(c);
  512. var d = db.SingleOrDefault<deco>("WHERE id=@0", a.id);
  513. Assert.AreEqual(d.id, a.id);
  514. Assert.AreEqual(d.date_edited.HasValue, false);
  515. }
  516. [Test]
  517. public void NamedArgs()
  518. {
  519. long first=InsertRecords(10);
  520. var items=db.Fetch<deco>("WHERE id >= @min_id AND id <= @max_id",
  521. new
  522. {
  523. min_id = first + 3,
  524. max_id = first + 6
  525. }
  526. );
  527. Assert.AreEqual(items.Count, 4);
  528. }
  529. [Test]
  530. public void SingleOrDefault_Empty()
  531. {
  532. Assert.IsNull(db.SingleOrDefault<deco>("WHERE id=@0", 0));
  533. }
  534. [Test]
  535. public void SingleOrDefault_Single()
  536. {
  537. var id = InsertRecords(1);
  538. Assert.IsNotNull(db.SingleOrDefault<deco>("WHERE id=@0", id));
  539. }
  540. [Test]
  541. public void SingleOrDefault_Multiple()
  542. {
  543. Assert.Throws<InvalidOperationException>(() =>
  544. {
  545. var id = InsertRecords(2);
  546. db.SingleOrDefault<deco>("WHERE id>=@0", id);
  547. });
  548. }
  549. [Test]
  550. public void FirstOrDefault_Empty()
  551. {
  552. Assert.IsNull(db.FirstOrDefault<deco>("WHERE id=@0", 0));
  553. }
  554. [Test]
  555. public void FirstOrDefault_First()
  556. {
  557. var id = InsertRecords(1);
  558. Assert.IsNotNull(db.FirstOrDefault<deco>("WHERE id=@0", id));
  559. }
  560. [Test]
  561. public void FirstOrDefault_Multiple()
  562. {
  563. var id = InsertRecords(2);
  564. Assert.IsNotNull(db.FirstOrDefault<deco>("WHERE id>=@0", id));
  565. }
  566. [Test]
  567. public void Single_Empty()
  568. {
  569. Assert.Throws<InvalidOperationException>(() =>
  570. {
  571. db.Single<deco>("WHERE id=@0", 0);
  572. });
  573. }
  574. [Test]
  575. public void Single_Single()
  576. {
  577. var id = InsertRecords(1);
  578. Assert.IsNotNull(db.Single<deco>("WHERE id=@0", id));
  579. }
  580. [Test]
  581. public void Single_Multiple()
  582. {
  583. Assert.Throws<InvalidOperationException>(() =>
  584. {
  585. var id = InsertRecords(2);
  586. db.Single<deco>("WHERE id>=@0", id);
  587. });
  588. }
  589. [Test]
  590. public void First_Empty()
  591. {
  592. Assert.Throws<InvalidOperationException>(() =>
  593. {
  594. db.First<deco>("WHERE id=@0", 0);
  595. });
  596. }
  597. [Test]
  598. public void First_First()
  599. {
  600. var id = InsertRecords(1);
  601. Assert.IsNotNull(db.First<deco>("WHERE id=@0", id));
  602. }
  603. [Test]
  604. public void First_Multiple()
  605. {
  606. var id = InsertRecords(2);
  607. Assert.IsNotNull(db.First<deco>("WHERE id>=@0", id));
  608. }
  609. [Test]
  610. public void SingleOrDefault_PK_Empty()
  611. {
  612. Assert.IsNull(db.SingleOrDefault<deco>(0));
  613. }
  614. [Test]
  615. public void SingleOrDefault_PK_Single()
  616. {
  617. var id = InsertRecords(1);
  618. Assert.IsNotNull(db.SingleOrDefault<deco>(id));
  619. }
  620. [Test]
  621. public void Single_PK_Empty()
  622. {
  623. Assert.Throws<InvalidOperationException>(() =>
  624. {
  625. db.Single<deco>(0);
  626. });
  627. }
  628. [Test]
  629. public void Single_PK_Single()
  630. {
  631. var id = InsertRecords(1);
  632. Assert.IsNotNull(db.Single<deco>(id));
  633. }
  634. [Test]
  635. public void AutoSelect_SelectPresent()
  636. {
  637. var id = InsertRecords(1);
  638. var a = db.SingleOrDefault<deco>("SELECT * FROM petapoco WHERE id=@0", id);
  639. Assert.IsNotNull(a);
  640. Assert.AreEqual(a.id, id);
  641. }
  642. [Test]
  643. public void AutoSelect_SelectMissingFromMissing()
  644. {
  645. var id = InsertRecords(1);
  646. var a = db.SingleOrDefault<deco>("WHERE id=@0", id);
  647. Assert.IsNotNull(a);
  648. Assert.AreEqual(a.id, id);
  649. }
  650. [Test]
  651. public void AutoSelect_SelectMissingFromPresent()
  652. {
  653. var id = InsertRecords(1);
  654. var a = db.SingleOrDefault<deco>("FROM petapoco WHERE id=@0", id);
  655. Assert.IsNotNull(a);
  656. Assert.AreEqual(a.id, id);
  657. }
  658. void AssertDynamic(dynamic a, dynamic b)
  659. {
  660. Assert.AreEqual(a.id, b.id);
  661. Assert.AreEqual(a.title, b.title);
  662. Assert.AreEqual(a.draft, b.draft);
  663. Assert.AreEqual(a.content, b.content);
  664. Assert.AreEqual(a.date_created, b.date_created);
  665. Assert.AreEqual(a.state, b.state);
  666. }
  667. dynamic CreateExpando()
  668. {
  669. // Need a rounded date as DB can't store millis
  670. var now = DateTime.UtcNow;
  671. now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
  672. // Setup a record
  673. dynamic o = new System.Dynamic.ExpandoObject();
  674. o.title = string.Format("insert {0}", r.Next());
  675. o.draft = true;
  676. o.content = string.Format("insert {0}", r.Next());
  677. o.date_created = now;
  678. o.date_edited = now;
  679. o.state = (int)State.Maybe;
  680. return o;
  681. }
  682. [Test]
  683. public void Dynamic_Query()
  684. {
  685. // Create a random record
  686. var o = CreateExpando();
  687. Assert.IsTrue(db.IsNew("id", o));
  688. // Insert it
  689. db.Insert("petapoco", "id", o);
  690. Assert.AreNotEqual(o.id, 0);
  691. Assert.IsFalse(db.IsNew("id", o));
  692. // Retrieve it
  693. var o2 = db.Single<dynamic>("SELECT * FROM petapoco WHERE id=@0", o.id);
  694. Assert.IsFalse(db.IsNew("id", o2));
  695. // Check it
  696. AssertDynamic(o, o2);
  697. // Update it
  698. o2.title = "New Title";
  699. db.Save("petapoco", "id", o2);
  700. // Retrieve itagain
  701. var o3 = db.Single<dynamic>("SELECT * FROM petapoco WHERE id=@0", o.id);
  702. // Check it
  703. AssertDynamic(o2, o3);
  704. // Delete it
  705. db.Delete("petapoco", "id", o3);
  706. // Should be gone!
  707. var o4 = db.SingleOrDefault<dynamic>("SELECT * FROM petapoco WHERE id=@0", o.id);
  708. Assert.IsNull(o4);
  709. }
  710. [Test]
  711. public void Manual_PrimaryKey()
  712. {
  713. var o=new petapoco2();
  714. o.email="blah@blah.com";
  715. o.name="Mr Blah";
  716. db.Insert(o);
  717. var o2 = db.SingleOrDefault<petapoco2>("WHERE email=@0", "blah@blah.com");
  718. Assert.AreEqual(o2.name, "Mr Blah");
  719. }
  720. [Test]
  721. public void SingleValueRequest()
  722. {
  723. var id = InsertRecords(1);
  724. var id2 = db.SingleOrDefault<long>("SELECT id from petapoco WHERE id=@0", id);
  725. Assert.AreEqual(id, id2);
  726. }
  727. }
  728. }