1+ var expect = require ( 'chai' ) . expect ;
2+ var jSQL = require ( "../jSQL.js" ) ;
3+
4+ jSQL . onError ( function ( e ) {
5+ console . log ( e . message ) ;
6+ } ) ;
7+
8+ jSQL . load ( function ( ) {
9+ jSQL . reset ( ) ;
10+ describe ( 'Lexer/Parser tests' , function ( ) {
11+
12+ it ( 'create wtf_test table' , function ( ) {
13+ jSQL . query ( "create table wtf_test (id, name, `somekey` varchar(), something enum('hello','goodbye'), primary key(`id`, name), unique key (somekey) )" ) . execute ( ) ;
14+ expect ( ( jSQL . tables . wtf_test !== undefined ) ) . to . be . true ;
15+ } ) ;
16+
17+ it ( 'create typetest table' , function ( ) {
18+ jSQL . query ( "create table typetest (id numeric(3), data json, greet function, active boolean, `number` int(3), code char(4), name varchar(4), created date, anything ambi, something enum('hello','goodbye') )" ) . execute ( ) ;
19+ expect ( ( jSQL . tables . typetest . colmap . number === 4 && jSQL . tables . typetest . types [ 9 ] . args [ 0 ] === 'hello' && jSQL . tables . typetest . types [ 9 ] . args [ 1 ] === 'goodbye' ) ) . to . be . true ;
20+ } ) ;
21+
22+ it ( 'create keytest table' , function ( ) {
23+ jSQL . query ( "create table keytest (id int auto_increment primary key, data, greet, active, `number`, code, name varchar, created date, anything ambi, something enum('hello','goodbye') )" ) . execute ( ) ;
24+ expect ( ( jSQL . tables . keytest . keys . primary . column === 'id' && jSQL . tables . keytest . auto_inc_col === 'id' ) ) . to . be . true ;
25+ } ) ;
26+
27+ it ( 'create keytest2 table' , function ( ) {
28+ jSQL . query ( "create table keytest2 (id int auto_increment, data, greet, active, `number`, code, name varchar, created date, anything ambi, something enum('hello','goodbye'), primary key(`id`, name) )" ) . execute ( ) ;
29+ expect ( ( jSQL . tables . keytest2 . keys . primary . column [ 0 ] === 'id' && jSQL . tables . keytest2 . keys . primary . column [ 1 ] === 'name' && jSQL . tables . keytest2 . auto_inc_col === 'id' ) ) . to . be . true ;
30+ } ) ;
31+
32+ it ( 'create smalltest table' , function ( ) {
33+ jSQL . query ( "create table `smalltest` (id int, str varchar)" ) . execute ( ) ;
34+ expect ( ( ! ! jSQL . tables . smalltest ) ) . to . be . true ;
35+ } ) ;
36+
37+ it ( 'insert into smalltest table' , function ( ) {
38+ jSQL . query ( "insert into smalltest (id, str) values (7, 'testing')" ) . execute ( ) ;
39+ expect ( ( jSQL . tables . smalltest . data . length === 1 ) ) . to . be . true ;
40+ } ) ;
41+
42+ it ( 'insert into smalltest table again' , function ( ) {
43+ jSQL . query ( "insert into `smalltest` values (11, 'testing again')" ) . execute ( ) ;
44+ expect ( ( jSQL . tables . smalltest . data . length === 2 ) ) . to . be . true ;
45+ } ) ;
46+
47+ it ( 'insert into typetest table' , function ( ) {
48+ jSQL . query ( "insert into `typetest` values (11, '{\"greeting\":\"hello\"}', 'function(){console.log(\"hello world\")}', true, 3, '72', 'rob', 'Mon, 25 Dec 1995 13:30:00 +0430', 'scooby doo', \"hello\")" ) . execute ( ) ;
49+ expect ( ( jSQL . tables . typetest . data . length === 1 ) ) . to . be . true ;
50+ } ) ;
51+
52+ it ( 'insert into keytest2 table' , function ( ) {
53+ jSQL . query ( "insert into `keytest2` (id, `greet`) values (11, 'holla')" ) . execute ( ) ;
54+ expect ( ( jSQL . tables . keytest2 . data [ 0 ] [ 0 ] === 11 ) ) . to . be . true ;
55+ } ) ;
56+
57+ it ( 'insert into keytest2 table again' , function ( ) {
58+ jSQL . query ( "insert into `keytest2` (`greet`) values ('poopums')" ) . execute ( ) ;
59+ expect ( ( jSQL . tables . keytest2 . data [ 1 ] [ 0 ] === 12 ) ) . to . be . true ;
60+ } ) ;
61+
62+ it ( 'select from typetest table' , function ( ) {
63+ var res = jSQL . query ( "select * from `typetest`" ) . execute ( ) . fetchAll ( "ARRAY" ) ;
64+ expect ( ( res [ 0 ] [ 7 ] instanceof Date && "function" === typeof res [ 0 ] [ 2 ] ) ) . to . be . true ;
65+ } ) ;
66+
67+ it ( 'select from keytest2 table' , function ( ) {
68+ var q = jSQL . query ( "SELECT id, greet FROM `keytest2` WHERE `id` > 11" ) . execute ( ) ;
69+ expect ( ( q . fetch ( "ASSOC" ) . greet === "poopums" ) ) . to . be . true ;
70+ } ) ;
71+
72+ it ( 'select from keytest2 table again' , function ( ) {
73+ var q = jSQL . query ( "SELECT id, greet FROM `keytest2` WHERE `id` < 12" ) . execute ( ) ;
74+ expect ( ( q . fetch ( "ASSOC" ) . greet === "holla" ) ) . to . be . true ;
75+ } ) ;
76+
77+ it ( 'select from keytest2 table again again' , function ( ) {
78+ var q = jSQL . query ( "SELECT * FROM `keytest2` WHERE `id` < 12 and greet = 'holla'" ) . execute ( ) ;
79+ expect ( ( q . fetch ( "ASSOC" ) . greet === "holla" ) ) . to . be . true ;
80+ } ) ;
81+
82+ it ( 'select from keytest2 table once again' , function ( ) {
83+ var q = jSQL . query ( "SELECT * FROM `keytest2` WHERE `id` < 12 and greet = 'holla' and greet <> \"poo\" or id = 11" ) . execute ( ) ;
84+ expect ( ( q . fetch ( "ASSOC" ) . greet === "holla" ) ) . to . be . true ;
85+ } ) ;
86+
87+ it ( 'select from keytest2 table again once again' , function ( ) {
88+ var q = jSQL . query ( "SELECT * FROM `keytest2` WHERE `greet` LIKE '%olla'" ) . execute ( ) ;
89+ expect ( ( q . fetch ( "ASSOC" ) . greet === "holla" ) ) . to . be . true ;
90+ } ) ;
91+
92+ it ( 'select from keytest2 table again once again once' , function ( ) {
93+ var q = jSQL . query ( "SELECT * FROM `keytest2` WHERE `greet` LIKE '%o%'" ) . execute ( ) ;
94+ expect ( ( q . fetchAll ( "ASSOC" ) . length === 2 ) ) . to . be . true ;
95+ } ) ;
96+
97+ it ( 'select from keytest2 table again once again once more' , function ( ) {
98+ var q = jSQL . query ( "SELECT * FROM `keytest2` WHERE `greet` LIKE 'p%'" ) . execute ( ) ;
99+ expect ( ( q . fetchAll ( "ASSOC" ) . length === 1 ) ) . to . be . true ;
100+ } ) ;
101+
102+ it ( 'update keytest2 table' , function ( ) {
103+ var q = jSQL . query ( "update `keytest2` set `greet` = 'yyyyo' where id = 12" ) . execute ( ) ;
104+ expect ( ( jSQL . tables . keytest2 . data [ 1 ] [ 2 ] === 'yyyyo' ) ) . to . be . true ;
105+ } ) ;
106+
107+ it ( 'update keytest2 table again' , function ( ) {
108+ var q = jSQL . query ( "update`keytest2`set`greet`='waddapp',data=\"data\"where`id`=11" ) . execute ( ) ;
109+ expect ( ( jSQL . tables . keytest2 . data [ 0 ] [ 2 ] === 'waddapp' && jSQL . tables . keytest2 . data [ 0 ] [ 1 ] === 'data' ) ) . to . be . true ;
110+ } ) ;
111+
112+ it ( 'delete from keytest2 table' , function ( ) {
113+ var q = jSQL . query ( "delete from `keytest2` where `greet` = 'waddapp'" ) . execute ( ) ;
114+ expect ( ( jSQL . tables . keytest2 . data . length === 1 ) ) . to . be . true ;
115+ } ) ;
116+
117+ it ( 'drop keytest2 table' , function ( ) {
118+ var q = jSQL . query ( "drop table `keytest2`" ) . execute ( ) ;
119+ expect ( ( jSQL . tables . keytest2 === undefined ) ) . to . be . true ;
120+ } ) ;
121+
122+ } ) ;
123+ } ) ;
0 commit comments