Skip to content

Commit

Permalink
Merge pull request knex#432 from vschoettke/oracle-fix-first-add-tests
Browse files Browse the repository at this point in the history
oracle - fixed first and added more tests
  • Loading branch information
bendrucker committed Aug 21, 2014
2 parents 0e60ca1 + f545c86 commit f4bd7d0
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 86 deletions.
2 changes: 2 additions & 0 deletions lib/dialects/oracle/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ var components = [
// Compiles the `select` statement, or nested sub-selects
// by calling each of the component compilers, trimming out
// the empties, and returning a generated query string.

QueryCompiler_Oracle.prototype.first =
QueryCompiler_Oracle.prototype.select = function() {
var self = this;
var statements = _.map(components, function (component) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/builder/additional.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,4 @@ module.exports = function(knex) {

});

};
};
87 changes: 59 additions & 28 deletions test/integration/builder/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,33 @@ module.exports = function(knex) {
'sum("logins")': 10
}]
);
tester(
'oracle',
'select sum("logins") from "accounts"',
[],
[{
'SUM("LOGINS")': 10
}]
);
});

});

it('has an avg', function() {

return knex('accounts').avg('logins').testSql(function(tester) {
tester('mysql', 'select avg(`logins`) from `accounts`', [], [{
'avg(`logins`)': 1.6667
}]);
tester('sqlite3', 'select avg("logins") from "accounts"', [], [{
'avg("logins")': 1.6666666666666667
}]);
tester('postgresql', 'select avg("logins") from "accounts"', [], [{
avg: '1.6666666666666667'
}]);

function checkResRange(key, resp) {
return Math.abs(10/6 - +(resp[0][key])) < 0.001;
}

// mysql: 1.6667
tester('mysql', 'select avg(`logins`) from `accounts`', [], checkResRange.bind(null, 'avg(`logins`)'));
// sqlite: 1.6666666666666667
tester('sqlite3', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'avg("logins")'));
// postgres: '1.6666666666666667'
tester('postgresql', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'avg'));
// oracle: 1.66666666666667
tester('oracle', 'select avg("logins") from "accounts"', [], checkResRange.bind(null, 'AVG("LOGINS")'));
});

});
Expand Down Expand Up @@ -76,6 +87,14 @@ module.exports = function(knex) {
'count("id")': 6
}]
);
tester(
'oracle',
'select count("id") from "accounts"',
[],
[{
'COUNT("ID")': 6
}]
);
});

});
Expand Down Expand Up @@ -113,6 +132,16 @@ module.exports = function(knex) {
'min("logins")': 1
}]
);
tester(
'oracle',
'select count("id"), max("logins"), min("logins") from "accounts"',
[],
[{
'COUNT("ID")': 6,
'MAX("LOGINS")': 2,
'MIN("LOGINS")': 1
}]
);
});

});
Expand Down Expand Up @@ -150,6 +179,17 @@ module.exports = function(knex) {
'count("id")': 4
}]
);
tester(
'oracle',
'select count("id") from "accounts" group by "logins"',
[],
[{
'COUNT("ID")': 2
},{
'COUNT("ID")': 4
}]
);


}).then(function() {
return knex('accounts').count('id').groupBy('first_name').testSql(function(tester) {
Expand Down Expand Up @@ -177,29 +217,20 @@ module.exports = function(knex) {
'count("id")': 6
}]
);
});
});

});


it('has an avg', function() {

return knex('accounts').avg('logins').testSql(function(tester) {
tester('mysql', 'select avg(`logins`) from `accounts`', [], [{
'avg(`logins`)': 1.6667
}]);
tester('postgresql', 'select avg("logins") from "accounts"', [], [{
avg: '1.6666666666666667'
}]);
tester('sqlite3', 'select avg("logins") from "accounts"', [], [{
'avg("logins")': 1.6666666666666667
}]);
tester(
'oracle',
'select count("id") from "accounts" group by "first_name"',
[],
[{
'COUNT("ID")': 6
}]
);
});
});

});


});


Expand Down
12 changes: 12 additions & 0 deletions test/integration/builder/deletes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ module.exports = function(knex) {
[1],
1
);
tester(
'oracle',
'delete from "accounts" where "id" = ?',
[1],
1
);
});
});

Expand Down Expand Up @@ -61,6 +67,12 @@ module.exports = function(knex) {
[2],
1
);
tester(
'oracle',
'delete from "accounts" where "id" = ?',
[2],
1
);
});
});

Expand Down
62 changes: 61 additions & 1 deletion test/integration/builder/inserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,28 @@ module.exports = function(knex) {
account_id: 3,
details: '',
status: 1
}], 'id').exec(function(err, resp) {
}], 'id')
.testSql(function(tester) {
tester(
'oracle',
"begin execute immediate 'insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (:1, :2, :3) returning ROWID into :4' using ?, ?, ?, out ?; execute immediate 'insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (:1, :2, :3) returning ROWID into :4' using ?, ?, ?, out ?; execute immediate 'insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (:1, :2, :3) returning ROWID into :4' using ?, ?, ?, out ?;end;",
[
1,
'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
0,
function (v) {return v.toString() === '[object ReturningHelper:id]';},
2,
'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
1,
function (v) {return v.toString() === '[object ReturningHelper:id]';},
3,
'',
1,
function (v) {return v.toString() === '[object ReturningHelper:id]';}
],
[1, 2, 3]
);
}).exec(function(err, resp) {
if (err) return ok(err);
ok();
});
Expand Down Expand Up @@ -154,6 +175,29 @@ module.exports = function(knex) {
['Lorem ipsum Dolore labore incididunt enim.', d,'test4@example.com','Test','User',2, d,'Lorem ipsum Dolore labore incididunt enim.', d,'test5@example.com','Test','User',2, d],
[5]
);
tester(
'oracle',
"begin execute immediate 'insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (:1, :2, :3, :4, :5, :6, :7) returning ROWID into :8' using ?, ?, ?, ?, ?, ?, ?, out ?; execute immediate 'insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (:1, :2, :3, :4, :5, :6, :7) returning ROWID into :8' using ?, ?, ?, ?, ?, ?, ?, out ?;end;",
[
'Lorem ipsum Dolore labore incididunt enim.',
d,
'test4@example.com',
'Test',
'User',
2,
d,
function (v) {return v.toString() === '[object ReturningHelper:id]';},
'Lorem ipsum Dolore labore incididunt enim.',
d,
'test5@example.com',
'Test',
'User',
2,
d,
function (v) {return v.toString() === '[object ReturningHelper:id]';}
],
[4, 5]
);
});

});
Expand Down Expand Up @@ -188,6 +232,11 @@ module.exports = function(knex) {
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?)',
['Lorem ipsum Dolore labore incididunt enim.', d, 'test5@example.com','Test','User', 2, d]
);
tester(
'oracle',
"insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) returning ROWID into ?",
['Lorem ipsum Dolore labore incididunt enim.', d, 'test5@example.com','Test','User', 2, d, function (v) {return v.toString() === '[object ReturningHelper:id]';}]
);
})
.then(function() {
throw new Error('There should be a fail when multi-insert are made in unique col.');
Expand Down Expand Up @@ -227,6 +276,12 @@ module.exports = function(knex) {
['Lorem ipsum Dolore labore incididunt enim.', d, 'test6@example.com','Test','User',2, d],
[6]
);
tester(
'oracle',
"insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) returning ROWID into ?",
['Lorem ipsum Dolore labore incididunt enim.', d, 'test6@example.com','Test','User',2, d, function (v) {return v.toString() === '[object ReturningHelper:id]';}],
[7]
);
});

});
Expand All @@ -252,6 +307,11 @@ module.exports = function(knex) {
['d'],
[1]
);
tester(
'oracle',
'insert into "datatype_test" ("enum_value") values (?)',
['d']
);
})
.then(function() {
// No errors happen in sqlite3, which doesn't have native support
Expand Down
Loading

0 comments on commit f4bd7d0

Please sign in to comment.