/test/schema.js
JavaScript | 404 lines | 353 code | 51 blank | 0 comment | 0 complexity | 817bd3588bc1d9b6cdb67cc6ccda5810 MD5 | raw file
- var JSRel = require('../lib/jsrel.js');
- var vows = require('vows');
- var assert = require('assert');
- vows.describe('== TESTING SCHEMA ==').addBatch({
- "JSRel.use() with no schema in creation": {
- topic: function() {
- try { return JSRel.use("tmp/schema01", {user: { name: true } }) }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /options\.schema is required/);
- }
- },
- "JSRel.use() with no schema in loading": {
- topic: null,
- "succeeds": function() {
- var id = "tmp/use_twice"
- var db = JSRel.create(id, {schema: {user: { xxx: 1, name: true }}});
- var db2 = JSRel.use(id);
- assert.equal(db, db2);
- }
- },
- "JSRel.create() with already existing uniqId": {
- topic: function() {
- var id = "tmp/xxx"
- var db = JSRel.create(id, {schema: {user: { xxx: 1, name: true }}});
- try {
- var db2 = JSRel.create(id, {schema: {user: { xxx: 1, name: true }}});
- }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /already exists/);
- }
- },
- "JSRel.createIfNotExists() with already existing uniqId": {
- topic: null,
- "succeeds": function() {
- var id = "tmp/yyy"
- var db = JSRel.create(id, {schema: {user: { xxx: 1, name: true }}});
- var db2 = JSRel.createIfNotExists(id, {schema: {user: { xxx: 1, name: true }}});
- assert.equal(db, db2);
- }
- },
- "A schema that has 'id' as a column name": {
- topic: function() {
- try { return JSRel.use("tmp/schema02", { schema: {user: { id: 1, name: true } }}) }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /id is not allowed/);
- }
- },
- "A schema that has 'upd_at' as a column name": {
- topic: function() {
- try { return JSRel.use("tmp/schema03", { schema: {user: { upd_at: 1, name: true } }}) }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /upd_at is not allowed/);
- }
- },
- "A schema that has 'bool' as a column name": {
- topic: function() {
- try { return JSRel.use("tmp/schema04", { schema: {user: { bool: false, name: true } }}) }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /bool is not allowed/);
- }
- },
- "A schema that has 'join' as a column name": {
- topic: function() {
- try { return JSRel.use("tmp/schema100", { schema: {user: { join: false, name: true } }}) }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /join is not allowed/);
- }
- },
- "A schema that contains ',' in a column name": {
- topic: function() {
- try { return JSRel.use("tmp/schema101", { schema: {user: { "A,B": false, name: true } }}) }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /cannot be included in a column name/);
- }
- },
- "A schema that contains '.' in a column name": {
- topic: function() {
- try { return JSRel.use("tmp/schema102", { schema: {user: { "A.B": false, name: true } }}) }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /cannot be included in a column name/);
- }
- },
- "A schema with no tables": {
- topic: function() {
- try { return JSRel.use("tmp/schema05", { schema: {}}) }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /schema must contain at least one table/);
- }
- },
- "A table with no columns": {
- topic: function() {
- try { return JSRel.use("tmp/schema06", { schema: {user: {}}}) }
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /table "user" must contain at least one column/);
- }
- },
- "A schema that has unregistered indexes": {
- topic: function() {
- try { return JSRel.use("tmp/schema07", { schema: {
- user: {
- name : true,
- $indexes: "xxxx"
- }
- }})}
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /"xxxx" is unregistered column. in "user"/);
- }
- },
- "A schema that has unregistered classes": {
- topic: function() {
- try { return JSRel.use("tmp/schema08", { schema: {
- user: {
- name : true,
- $classes: "xxxx"
- }
- }})}
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /"xxxx" is unregistered column. in "user"/);
- }
- },
- "A schema that has invalid index": {
- topic: function() {
- try { return JSRel.use("tmp/schema09", { schema: {
- user: {
- name : true,
- $indexes : {name: true}
- }
- }})}
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /is unregistered column. in "user"/);
- }
- },
- "setting classes to string columns": {
- topic: function() {
- try { return JSRel.use("tmp/schema10", { schema: {
- user: {
- name : true,
- $classes : "name"
- }
- }})}
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /Cannot set class index to string columns "name"/);
- }
- },
- "setting xxx and xxx_id": {
- topic: function() {
- try { return JSRel.use("tmp/schema11", { schema: {
- user: { a : "a", a_id : 1 },
- rel : { a : true }
- }})}
- catch (e) { return e.message }
- },
- " is not allowed" : function(topic) {
- assert.match(topic, /"a_id" is already registered/);
- }
- },
- "A schema": {
- topic: function() {
- return JSRel.use("tmp/tiny", { schema: {
- user : {
- name: true,
- mail: true,
- age : 0,
- is_activated: "on",
- $indexes: "name",
- $uniques: [["name", "mail"]],
- $classes: "is_activated"
- },
- book : {
- title: true,
- ISBN : true,
- code : 1,
- $indexes: "title",
- $uniques: ["ISBN", "code"]
- },
- user_book: {
- u : "user",
- b : "book"
- }
- }})
- },
- " generates _tblInfos" : function(jsrel) {
- assert.ok(jsrel._tblInfos);
- },
- " generates two tables" : function(jsrel) {
- assert.equal(jsrel.tables.length, 3);
- },
- " has table 'user'" : function(jsrel) {
- assert.instanceOf(jsrel.table('user'), JSRel.Table);
- },
- " has table 'user_book'" : function(jsrel) {
- assert.instanceOf(jsrel.table('user_book'), JSRel.Table);
- },
- " And book has six columns" : function(jsrel) {
- assert.equal(jsrel.table('book').columns.length, 6);
- },
- "typeof column 'ISBN' is string" : function(jsrel) {
- assert.equal(jsrel.table('book')._colInfos.ISBN.type, JSRel.Table._STR);
- },
- "column 'ISBN' is required" : function(jsrel) {
- assert.equal(jsrel.table('book')._colInfos.ISBN.required, true);
- },
- "typeof column 'age' is number" : function(jsrel) {
- assert.equal(jsrel.table('user')._colInfos.age.type, JSRel.Table._NUM);
- },
- "column 'age' is not required" : function(jsrel) {
- assert.equal(jsrel.table('user')._colInfos.age.required, false);
- },
- "typeof 'is_activated' is boolean" : function(jsrel) {
- assert.equal(jsrel.table('user')._colInfos.is_activated.type, JSRel.Table._BOOL);
- },
- "typeof 'ins_at' is number" : function(jsrel) {
- assert.equal(jsrel.table('user')._colInfos.ins_at.type, JSRel.Table._NUM);
- },
- "typeof 'id' is number" : function(jsrel) {
- assert.equal(jsrel.table('user_book')._colInfos.id.type, JSRel.Table._NUM);
- },
- "'id' is a unique column" : function(jsrel) {
- assert.isTrue(jsrel.table('user_book')._indexes.id._unique);
- },
- "'ISBN' is a unique column" : function(jsrel) {
- assert.isTrue(jsrel.table('book')._indexes.ISBN._unique);
- },
- "'name' is not a unique index" : function(jsrel) {
- assert.isFalse(jsrel.table('user')._indexes.name._unique);
- },
- "'is_activated' is a class index" : function(jsrel) {
- assert.equal(jsrel.table('user')._classes.is_activated.cols[0], "is_activated");
- },
- "'u' is referring external table 'user'" : function(jsrel) {
- assert.equal(jsrel.table('user_book')._rels.u, 'user');
- },
- "'b_id' is not a unique index" : function(jsrel) {
- assert.isFalse(jsrel.table('user_book')._indexes.b_id._unique);
- },
- "'book' is referred by 'user_book.b'" : function(jsrel) {
- assert.isTrue(jsrel.table('book')._referreds.user_book.hasOwnProperty("b"));
- },
- "'book' is referred by 'user_book.b, and required'" : function(jsrel) {
- assert.isTrue(jsrel.table('book')._referreds.user_book.b);
- },
- "data is empty" : function(jsrel) {
- assert.lengthOf(Object.keys(jsrel.table('book')._data), 0);
- },
- "'name,mail' is a unique complex index" : function(jsrel) {
- assert.isTrue(jsrel.table('user')._indexes["name,mail"]._unique);
- },
- "'name' has two indexes" : function(jsrel) {
- assert.lengthOf(Object.keys(jsrel.table('user')._idxKeys.name), 2);
- }
- },
- "deletion of table": {
- topic: function() {
- var schema = {
- user : {
- name: true,
- mail: true,
- age : 0,
- is_activated: "on",
- $indexes: "name",
- $uniques: [["name", "mail"]],
- $classes: "is_activated"
- },
- book : {
- title: true,
- ISBN : true,
- code : 1,
- $indexes: "title",
- $uniques: ["ISBN", "code"]
- },
- user_book: {
- u : "user",
- b : "book"
- },
- user_info: {
- info: true,
- uuuu: {type: "user", required: false}
- }
- };
- return schema;
- },
- "dropping table which is referred from other tables(should fail)" : function(schema) {
- var db = JSRel.use("tst1", { schema: schema });
- try {
- db.drop("book");
- assert.fail();
- }
- catch (e) {
- assert.match(e.message, /"user_book"/);
- }
- },
- "dropping table with referring table (should succeed)" : function(schema) {
- var db = JSRel.use("tst2", { schema: schema });
- var initialLnegth = db.tables.length;
- db.drop("book", "user_book");
- assert.lengthOf(db.tables, initialLnegth - 2);
- assert.include(db.tables, "user");
- assert.include(db.tables, "user_info");
- },
- "dropping table which is referred from other tables but not required (should succeed)" : function(schema) {
- var db = JSRel.use("tst3", { schema: schema });
- var shinout = db.ins("user", {name: "shinout", mail: "shinout@shinout.net"});
- var info1 = db.ins("user_info", {info: "medical doctor", uuuu: shinout });
- var info2 = db.ins("user_info", {info: "wanna be a scientist", uuuu: shinout });
- var info3 = db.ins("user_info", {info: "plays Alto sax", uuuu: shinout });
- var info4 = db.ins("user_info", {info: "plays the keyboard with transposing", uuuu: shinout });
- var info5 = db.ins("user_info", {info: "begins playing the electric bass", uuuu: shinout });
- assert.isNotNull(db.one("user_info", info1.uuuu_id));
- assert.isNotNull(db.one("user_info", info2.uuuu_id));
- assert.isNotNull(db.one("user_info", info3.uuuu_id));
- assert.isNotNull(db.one("user_info", info4.uuuu_id));
- assert.isNotNull(db.one("user_info", info5.uuuu_id));
- var initialLnegth = db.tables.length;
- db.drop("user", "user_book");
- assert.lengthOf(db.tables, initialLnegth - 2);
- assert.include(db.tables, "book");
- assert.include(db.tables, "user_info");
- assert.isNull(db.one("user_info", info1.id).uuuu_id);
- assert.isNull(db.one("user_info", info2.id).uuuu_id);
- assert.isNull(db.one("user_info", info3.id).uuuu_id);
- assert.isNull(db.one("user_info", info4.id).uuuu_id);
- assert.isNull(db.one("user_info", info5.id).uuuu_id);
- }
- }
- }).export(module);