PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/test/schema.js

http://github.com/shinout/jsrel
JavaScript | 404 lines | 353 code | 51 blank | 0 comment | 0 complexity | 817bd3588bc1d9b6cdb67cc6ccda5810 MD5 | raw file
  1. var JSRel = require('../lib/jsrel.js');
  2. var vows = require('vows');
  3. var assert = require('assert');
  4. vows.describe('== TESTING SCHEMA ==').addBatch({
  5. "JSRel.use() with no schema in creation": {
  6. topic: function() {
  7. try { return JSRel.use("tmp/schema01", {user: { name: true } }) }
  8. catch (e) { return e.message }
  9. },
  10. " is not allowed" : function(topic) {
  11. assert.match(topic, /options\.schema is required/);
  12. }
  13. },
  14. "JSRel.use() with no schema in loading": {
  15. topic: null,
  16. "succeeds": function() {
  17. var id = "tmp/use_twice"
  18. var db = JSRel.create(id, {schema: {user: { xxx: 1, name: true }}});
  19. var db2 = JSRel.use(id);
  20. assert.equal(db, db2);
  21. }
  22. },
  23. "JSRel.create() with already existing uniqId": {
  24. topic: function() {
  25. var id = "tmp/xxx"
  26. var db = JSRel.create(id, {schema: {user: { xxx: 1, name: true }}});
  27. try {
  28. var db2 = JSRel.create(id, {schema: {user: { xxx: 1, name: true }}});
  29. }
  30. catch (e) { return e.message }
  31. },
  32. " is not allowed" : function(topic) {
  33. assert.match(topic, /already exists/);
  34. }
  35. },
  36. "JSRel.createIfNotExists() with already existing uniqId": {
  37. topic: null,
  38. "succeeds": function() {
  39. var id = "tmp/yyy"
  40. var db = JSRel.create(id, {schema: {user: { xxx: 1, name: true }}});
  41. var db2 = JSRel.createIfNotExists(id, {schema: {user: { xxx: 1, name: true }}});
  42. assert.equal(db, db2);
  43. }
  44. },
  45. "A schema that has 'id' as a column name": {
  46. topic: function() {
  47. try { return JSRel.use("tmp/schema02", { schema: {user: { id: 1, name: true } }}) }
  48. catch (e) { return e.message }
  49. },
  50. " is not allowed" : function(topic) {
  51. assert.match(topic, /id is not allowed/);
  52. }
  53. },
  54. "A schema that has 'upd_at' as a column name": {
  55. topic: function() {
  56. try { return JSRel.use("tmp/schema03", { schema: {user: { upd_at: 1, name: true } }}) }
  57. catch (e) { return e.message }
  58. },
  59. " is not allowed" : function(topic) {
  60. assert.match(topic, /upd_at is not allowed/);
  61. }
  62. },
  63. "A schema that has 'bool' as a column name": {
  64. topic: function() {
  65. try { return JSRel.use("tmp/schema04", { schema: {user: { bool: false, name: true } }}) }
  66. catch (e) { return e.message }
  67. },
  68. " is not allowed" : function(topic) {
  69. assert.match(topic, /bool is not allowed/);
  70. }
  71. },
  72. "A schema that has 'join' as a column name": {
  73. topic: function() {
  74. try { return JSRel.use("tmp/schema100", { schema: {user: { join: false, name: true } }}) }
  75. catch (e) { return e.message }
  76. },
  77. " is not allowed" : function(topic) {
  78. assert.match(topic, /join is not allowed/);
  79. }
  80. },
  81. "A schema that contains ',' in a column name": {
  82. topic: function() {
  83. try { return JSRel.use("tmp/schema101", { schema: {user: { "A,B": false, name: true } }}) }
  84. catch (e) { return e.message }
  85. },
  86. " is not allowed" : function(topic) {
  87. assert.match(topic, /cannot be included in a column name/);
  88. }
  89. },
  90. "A schema that contains '.' in a column name": {
  91. topic: function() {
  92. try { return JSRel.use("tmp/schema102", { schema: {user: { "A.B": false, name: true } }}) }
  93. catch (e) { return e.message }
  94. },
  95. " is not allowed" : function(topic) {
  96. assert.match(topic, /cannot be included in a column name/);
  97. }
  98. },
  99. "A schema with no tables": {
  100. topic: function() {
  101. try { return JSRel.use("tmp/schema05", { schema: {}}) }
  102. catch (e) { return e.message }
  103. },
  104. " is not allowed" : function(topic) {
  105. assert.match(topic, /schema must contain at least one table/);
  106. }
  107. },
  108. "A table with no columns": {
  109. topic: function() {
  110. try { return JSRel.use("tmp/schema06", { schema: {user: {}}}) }
  111. catch (e) { return e.message }
  112. },
  113. " is not allowed" : function(topic) {
  114. assert.match(topic, /table "user" must contain at least one column/);
  115. }
  116. },
  117. "A schema that has unregistered indexes": {
  118. topic: function() {
  119. try { return JSRel.use("tmp/schema07", { schema: {
  120. user: {
  121. name : true,
  122. $indexes: "xxxx"
  123. }
  124. }})}
  125. catch (e) { return e.message }
  126. },
  127. " is not allowed" : function(topic) {
  128. assert.match(topic, /"xxxx" is unregistered column. in "user"/);
  129. }
  130. },
  131. "A schema that has unregistered classes": {
  132. topic: function() {
  133. try { return JSRel.use("tmp/schema08", { schema: {
  134. user: {
  135. name : true,
  136. $classes: "xxxx"
  137. }
  138. }})}
  139. catch (e) { return e.message }
  140. },
  141. " is not allowed" : function(topic) {
  142. assert.match(topic, /"xxxx" is unregistered column. in "user"/);
  143. }
  144. },
  145. "A schema that has invalid index": {
  146. topic: function() {
  147. try { return JSRel.use("tmp/schema09", { schema: {
  148. user: {
  149. name : true,
  150. $indexes : {name: true}
  151. }
  152. }})}
  153. catch (e) { return e.message }
  154. },
  155. " is not allowed" : function(topic) {
  156. assert.match(topic, /is unregistered column. in "user"/);
  157. }
  158. },
  159. "setting classes to string columns": {
  160. topic: function() {
  161. try { return JSRel.use("tmp/schema10", { schema: {
  162. user: {
  163. name : true,
  164. $classes : "name"
  165. }
  166. }})}
  167. catch (e) { return e.message }
  168. },
  169. " is not allowed" : function(topic) {
  170. assert.match(topic, /Cannot set class index to string columns "name"/);
  171. }
  172. },
  173. "setting xxx and xxx_id": {
  174. topic: function() {
  175. try { return JSRel.use("tmp/schema11", { schema: {
  176. user: { a : "a", a_id : 1 },
  177. rel : { a : true }
  178. }})}
  179. catch (e) { return e.message }
  180. },
  181. " is not allowed" : function(topic) {
  182. assert.match(topic, /"a_id" is already registered/);
  183. }
  184. },
  185. "A schema": {
  186. topic: function() {
  187. return JSRel.use("tmp/tiny", { schema: {
  188. user : {
  189. name: true,
  190. mail: true,
  191. age : 0,
  192. is_activated: "on",
  193. $indexes: "name",
  194. $uniques: [["name", "mail"]],
  195. $classes: "is_activated"
  196. },
  197. book : {
  198. title: true,
  199. ISBN : true,
  200. code : 1,
  201. $indexes: "title",
  202. $uniques: ["ISBN", "code"]
  203. },
  204. user_book: {
  205. u : "user",
  206. b : "book"
  207. }
  208. }})
  209. },
  210. " generates _tblInfos" : function(jsrel) {
  211. assert.ok(jsrel._tblInfos);
  212. },
  213. " generates two tables" : function(jsrel) {
  214. assert.equal(jsrel.tables.length, 3);
  215. },
  216. " has table 'user'" : function(jsrel) {
  217. assert.instanceOf(jsrel.table('user'), JSRel.Table);
  218. },
  219. " has table 'user_book'" : function(jsrel) {
  220. assert.instanceOf(jsrel.table('user_book'), JSRel.Table);
  221. },
  222. " And book has six columns" : function(jsrel) {
  223. assert.equal(jsrel.table('book').columns.length, 6);
  224. },
  225. "typeof column 'ISBN' is string" : function(jsrel) {
  226. assert.equal(jsrel.table('book')._colInfos.ISBN.type, JSRel.Table._STR);
  227. },
  228. "column 'ISBN' is required" : function(jsrel) {
  229. assert.equal(jsrel.table('book')._colInfos.ISBN.required, true);
  230. },
  231. "typeof column 'age' is number" : function(jsrel) {
  232. assert.equal(jsrel.table('user')._colInfos.age.type, JSRel.Table._NUM);
  233. },
  234. "column 'age' is not required" : function(jsrel) {
  235. assert.equal(jsrel.table('user')._colInfos.age.required, false);
  236. },
  237. "typeof 'is_activated' is boolean" : function(jsrel) {
  238. assert.equal(jsrel.table('user')._colInfos.is_activated.type, JSRel.Table._BOOL);
  239. },
  240. "typeof 'ins_at' is number" : function(jsrel) {
  241. assert.equal(jsrel.table('user')._colInfos.ins_at.type, JSRel.Table._NUM);
  242. },
  243. "typeof 'id' is number" : function(jsrel) {
  244. assert.equal(jsrel.table('user_book')._colInfos.id.type, JSRel.Table._NUM);
  245. },
  246. "'id' is a unique column" : function(jsrel) {
  247. assert.isTrue(jsrel.table('user_book')._indexes.id._unique);
  248. },
  249. "'ISBN' is a unique column" : function(jsrel) {
  250. assert.isTrue(jsrel.table('book')._indexes.ISBN._unique);
  251. },
  252. "'name' is not a unique index" : function(jsrel) {
  253. assert.isFalse(jsrel.table('user')._indexes.name._unique);
  254. },
  255. "'is_activated' is a class index" : function(jsrel) {
  256. assert.equal(jsrel.table('user')._classes.is_activated.cols[0], "is_activated");
  257. },
  258. "'u' is referring external table 'user'" : function(jsrel) {
  259. assert.equal(jsrel.table('user_book')._rels.u, 'user');
  260. },
  261. "'b_id' is not a unique index" : function(jsrel) {
  262. assert.isFalse(jsrel.table('user_book')._indexes.b_id._unique);
  263. },
  264. "'book' is referred by 'user_book.b'" : function(jsrel) {
  265. assert.isTrue(jsrel.table('book')._referreds.user_book.hasOwnProperty("b"));
  266. },
  267. "'book' is referred by 'user_book.b, and required'" : function(jsrel) {
  268. assert.isTrue(jsrel.table('book')._referreds.user_book.b);
  269. },
  270. "data is empty" : function(jsrel) {
  271. assert.lengthOf(Object.keys(jsrel.table('book')._data), 0);
  272. },
  273. "'name,mail' is a unique complex index" : function(jsrel) {
  274. assert.isTrue(jsrel.table('user')._indexes["name,mail"]._unique);
  275. },
  276. "'name' has two indexes" : function(jsrel) {
  277. assert.lengthOf(Object.keys(jsrel.table('user')._idxKeys.name), 2);
  278. }
  279. },
  280. "deletion of table": {
  281. topic: function() {
  282. var schema = {
  283. user : {
  284. name: true,
  285. mail: true,
  286. age : 0,
  287. is_activated: "on",
  288. $indexes: "name",
  289. $uniques: [["name", "mail"]],
  290. $classes: "is_activated"
  291. },
  292. book : {
  293. title: true,
  294. ISBN : true,
  295. code : 1,
  296. $indexes: "title",
  297. $uniques: ["ISBN", "code"]
  298. },
  299. user_book: {
  300. u : "user",
  301. b : "book"
  302. },
  303. user_info: {
  304. info: true,
  305. uuuu: {type: "user", required: false}
  306. }
  307. };
  308. return schema;
  309. },
  310. "dropping table which is referred from other tables(should fail)" : function(schema) {
  311. var db = JSRel.use("tst1", { schema: schema });
  312. try {
  313. db.drop("book");
  314. assert.fail();
  315. }
  316. catch (e) {
  317. assert.match(e.message, /"user_book"/);
  318. }
  319. },
  320. "dropping table with referring table (should succeed)" : function(schema) {
  321. var db = JSRel.use("tst2", { schema: schema });
  322. var initialLnegth = db.tables.length;
  323. db.drop("book", "user_book");
  324. assert.lengthOf(db.tables, initialLnegth - 2);
  325. assert.include(db.tables, "user");
  326. assert.include(db.tables, "user_info");
  327. },
  328. "dropping table which is referred from other tables but not required (should succeed)" : function(schema) {
  329. var db = JSRel.use("tst3", { schema: schema });
  330. var shinout = db.ins("user", {name: "shinout", mail: "shinout@shinout.net"});
  331. var info1 = db.ins("user_info", {info: "medical doctor", uuuu: shinout });
  332. var info2 = db.ins("user_info", {info: "wanna be a scientist", uuuu: shinout });
  333. var info3 = db.ins("user_info", {info: "plays Alto sax", uuuu: shinout });
  334. var info4 = db.ins("user_info", {info: "plays the keyboard with transposing", uuuu: shinout });
  335. var info5 = db.ins("user_info", {info: "begins playing the electric bass", uuuu: shinout });
  336. assert.isNotNull(db.one("user_info", info1.uuuu_id));
  337. assert.isNotNull(db.one("user_info", info2.uuuu_id));
  338. assert.isNotNull(db.one("user_info", info3.uuuu_id));
  339. assert.isNotNull(db.one("user_info", info4.uuuu_id));
  340. assert.isNotNull(db.one("user_info", info5.uuuu_id));
  341. var initialLnegth = db.tables.length;
  342. db.drop("user", "user_book");
  343. assert.lengthOf(db.tables, initialLnegth - 2);
  344. assert.include(db.tables, "book");
  345. assert.include(db.tables, "user_info");
  346. assert.isNull(db.one("user_info", info1.id).uuuu_id);
  347. assert.isNull(db.one("user_info", info2.id).uuuu_id);
  348. assert.isNull(db.one("user_info", info3.id).uuuu_id);
  349. assert.isNull(db.one("user_info", info4.id).uuuu_id);
  350. assert.isNull(db.one("user_info", info5.id).uuuu_id);
  351. }
  352. }
  353. }).export(module);