UNPKG

5.95 kBJavaScriptView Raw
1'use strict';
2
3var test = require('tape');
4var config = require('./test.config.json');
5var cartodb = require('./')(config.username, config.apikey);
6var TABLE_NAME = 'test_table';
7test('basic', function (t) {
8 t.test('clear', function (t) {
9 t.plan(1);
10 cartodb(TABLE_NAME).delete()
11 .exec(function (err) {
12 t.error(err);
13 console.log('err', err && err.toString());
14 });
15 });
16 t.test('clear check', function (t) {
17 t.plan(2);
18 cartodb(TABLE_NAME).count()
19 .exec(function (err, resp) {
20 t.error(err);
21 t.deepEquals([{count: 0}], resp);
22 });
23 });
24 t.test('insert', function (t) {
25 t.plan(1);
26 cartodb.insert([
27 {
28 ammount: 1,
29 description: 'one',
30 ifso: true
31 },
32 {
33 ammount: 2,
34 description: 'two',
35 ifso: false,
36 name: 'even'
37 },
38 {
39 ammount: 3,
40 description: 'three',
41 ifso: true,
42 updated_at2: new Date()
43 },
44 {
45 description: 'try to use $cartodb$ to escape',
46 ifso: false,
47 the_geom: {"type":"Point","coordinates":[-98.19805569,29.49655938]}
48 }
49 ]).into(TABLE_NAME).exec(function (err) {
50 t.error(err, err && err.stack);
51 });
52 });
53 t.test('insert check', function (t) {
54 t.plan(2);
55 cartodb.from(TABLE_NAME).count()
56 .exec(function (err, resp) {
57 t.error(err);
58 t.deepEquals([{count: 4}], resp);
59 });
60 });
61 t.test('select 1', function (t) {
62 t.plan(1);
63 cartodb.select('ammount').from(TABLE_NAME).where('ifso', true).then(function (resp) {
64 if (!resp) {
65 return t.ok(resp);
66 }
67 t.deepEquals(resp.sort(function (a, b) {
68 return a.ammount - b.ammount;
69 }), [ { ammount: 1 }, { ammount: 3 } ]);
70 }).catch(function () {
71 t.notOk(true);
72 });
73 });
74 t.test('select 2', function (t) {
75 t.plan(1);
76 cartodb.select('ammount').from(TABLE_NAME).where('updated_at2', '<', new Date()).then(function (resp) {
77 if (!resp) {
78 return t.ok(resp);
79 }
80 t.deepEquals(resp, [ { ammount: 3 } ]);
81 }).catch(function () {
82 t.notOk(true);
83 });
84 });
85 t.test('update', function (t) {
86 t.plan(1);
87 cartodb(TABLE_NAME).update({
88 ifso: true
89 }).where('name', 'even').exec(function (err) {
90 t.error(err, err && err.stack);
91 });
92 });
93 t.test('update 2', function (t) {
94 t.plan(1);
95 cartodb(TABLE_NAME).update({
96 the_geom: {"type":"MultiPoint","coordinates":[[-98.19805569,29.49655938],[-97,28]]}
97 }).where({the_geom: {"type":"Point","coordinates":[-98.19805569,29.49655938]}}).exec(function (err) {
98 t.error(err, err && err.stack);
99 });
100 });
101 t.test('select 2', function (t) {
102 t.plan(2);
103 cartodb.select('ammount').from(TABLE_NAME).where('ifso', true).exec(function (err, resp) {
104 t.error(err, err && err.stack);
105 if (!resp) {
106 return t.ok(resp);
107 }
108 t.deepEquals(resp.sort(function (a, b) {
109 return a.ammount - b.ammount;
110 }), [ { ammount: 1 }, { ammount: 2 }, { ammount: 3 } ]);
111 });
112 });
113 t.test('check version', function (t) {
114 t.plan(1);
115 cartodb.raw('select version();').then(function () {
116 t.ok(true);//, JSON.stringify(resp, false, 2));
117 }).catch(function (e) {
118 t.error(e);
119 });
120 });
121 t.test('table info', function (t) {
122 t.plan(1);
123 cartodb(cartodb.raw('INFORMATION_SCHEMA.COLUMNS')).select('column_name', 'data_type')
124 .where({
125 table_name: TABLE_NAME
126 }).then(function (resp) {
127 t.ok(true, JSON.stringify(resp, false, 2));
128 }).catch(function (e) {
129 t.error(e);
130 });
131 });
132 t.test('select 2', function (t) {
133 t.plan(2);
134 cartodb.select('ammount').from(TABLE_NAME).whereRaw('ifso = ?', [true]).limit(1).exec(function (err, resp) {
135 t.error(err, err && err.stack);
136 if (!resp) {
137 return t.ok(resp);
138 }
139 t.deepEquals(resp.sort(function (a, b) {
140 return a.ammount - b.ammount;
141 }), [ { ammount: 1 } ]);
142 });
143 });
144 t.test('create table', function (t) {
145 t.plan(1);
146 cartodb.schema.createTable('cartodb_tools_test_fin', function (table) {
147 table.boolean('bool');
148 table.text('some_text');
149 }).then(function (resp) {
150 t.ok(true, JSON.stringify(resp, false, 2));
151 }).catch(function (e) {
152 t.error(e);
153 });
154 });
155 t.test('created table info', function (t) {
156 t.plan(1);
157 cartodb(cartodb.raw('INFORMATION_SCHEMA.COLUMNS')).select('column_name', 'data_type')
158 .where({
159 table_name: 'cartodb_tools_test_fin'
160 }).then(function (resp) {
161 t.equals(resp.length, 5);
162 }).catch(function (e) {
163 t.error(e);
164 });
165 });
166 t.test('drop table', function (t) {
167 t.plan(1);
168 cartodb.schema.dropTableIfExists('cartodb_tools_test_fin').then(function (resp) {
169 t.ok(true, JSON.stringify(resp, false, 2));
170 }).catch(function (e) {
171 t.error(e);
172 });
173 });
174});
175test('advanced', function (t) {
176 t.test('control', function (t) {
177 t.plan(1);
178 cartodb.raw('select true').then(function (r){
179 console.log(r);
180 t.ok(true, 'works');
181 }).catch(function (e) {
182 t.error(e);
183 })
184 });
185 t.test('new way', function (t) {
186 t.plan(1);
187 cartodb.raw('select true').batch().then(function (r){
188 t.ok(true, 'works');
189 }).catch(function (e) {
190 t.error(e, e.stack);
191 })
192 });
193 t.test('fallback', function (t) {
194 t.plan(1);
195 cartodb.raw('INSERT INTO errors_log (job_id, error_message, date) VALUES (\'first part!!!!\', \'no error\', NOW())')
196 .onSuccess(cartodb.raw('INSERT INTO errors_log (job_id, error_message, date) VALUES (\'success part!!!!\', \'no error\', NOW())'))
197 .onError(cartodb.raw('INSERT INTO errors_log (job_id, error_message, date) VALUES (\'<%= job_id %>\', \'<%= error_message %>\', NOW())')).batch().then(function (r){
198 t.ok(true, 'works');
199 }).catch(function (e) {
200 t.error(e, e.stack);
201 })
202 });
203})