Permalink
Browse files

Final testing round... think might be alpha ready

  • Loading branch information...
subsonic committed May 5, 2012
1 parent 7b524ae commit 53029b36fe08f8b87db2ca2c648b7dd5c76d2843
Showing with 244 additions and 49 deletions.
  1. +4 −4 lib/mysql.js
  2. +1 −1 lib/postgres.js
  3. +157 −0 test/mysql_queries_spec.coffee
  4. +53 −0 test/mysql_schema_spec.coffee
  5. +29 −0 test/mysql_spec.coffee
  6. +0 −44 test/pg_query_spec.coffee
View
@@ -24,7 +24,7 @@ var parseWhere = function(conditions) {
} else if (_.isArray(value)) {
var arrayConditions = [];
_.each(value, function(v) {
- arrayConditions.push('$' + ++seed);
+ arrayConditions.push("?");
params.push(v);
});
_conditions.push(util.format('"%s" %s (%s)', property, operation == '!=' || operation == '<>' ? 'NOT IN' : 'IN', arrayConditions.join(', ')));
@@ -202,10 +202,11 @@ var Table = function(tableName, pk, _db) {
});
var sql = util.format("UPDATE %s SET %s", this.name, f.join(', '));
if(where){
+
if(_.isNumber(where)){
sql+= util.format(" \nWHERE \"%s\" = %d", this.pk, where);
}else if (_.isString(where)){
- sql+= util.format(" \nWHERE \"%s\" = $%d", this.pk, parameters.length+1);
+ sql+= util.format(" \nWHERE \"%s\" = ?", this.pk);
parameters.push(where);
}else{
where = parseWhere(where);
@@ -222,7 +223,6 @@ var Table = function(tableName, pk, _db) {
var MySQL = function(credentials){
- console.log("Well hello MySQL")
events.EventEmitter.call(this);
var self = this;
this.dbType = "MySQL";
@@ -243,7 +243,7 @@ var MySQL = function(credentials){
this.execute = function(sql, params, callback) {
- console.log("Executing " + sql)
+
var client = mysql.createClient(credentials);
self.emit("beforeExecute", self);
View
@@ -244,7 +244,7 @@ var Postgres = function(connection){
where table_schema NOT IN ('pg_catalog', 'information_schema')";
this.execute = function(sql, params, callback) {
- console.log("Executing " + sql)
+
pg.connect(self.connectionString, function(err,db){
self.emit("beforeExecute", self);
db.query(sql, params,function(err, results){
@@ -0,0 +1,157 @@
+massive = require("../index");
+should = require("should");
+util = require("util");
+
+describe "MySQL Queries", ->
+ db = null
+ before (done) ->
+ massive.connect {user : "root", password : "", database : "test"}, (err,_db) ->
+ db = _db
+ done()
+
+ describe "initialization", ->
+
+ it "sets the table name", ->
+ db.products.name.should.equal("products")
+
+ it "defaults the pk to id", ->
+ db.products.pk.should.equal("id")
+
+
+ describe "SELECT queries", ->
+ it "runs a select *", ->
+ query = db.products.find()
+ query.sql.should.equal "SELECT * FROM products"
+ query.params.length.should.equal 0
+
+ it "adds columns when specified", ->
+ query = db.products.find ["name"]
+ query.sql.should.equal "SELECT name FROM products"
+
+
+ it "adds columns when specified in criteria", ->
+ query = db.products.find {}, {columns : "name"}
+ query.sql.should.equal "SELECT name FROM products"
+ query.params.length.should.equal 0
+
+ it "adds a where when id is a number", ->
+ query = db.products.find {id : 1}
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 1")
+ query.params.length.should.equal 0
+
+ it "adds a where as a primary key", ->
+ query = db.products.find(5)
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5")
+ query.params.length.should.equal 0
+
+
+ it "adds a LIMIT if specified", ->
+ query = db.products.find(5).limit(1);
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5 \nLIMIT 1")
+
+ it "adds a LIMIT with SKIP if specified", ->
+ query = db.products.find(5).limit(10,1);
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5 \nLIMIT(10,1)")
+
+ it "adds an ORDER if specified", ->
+ query = db.products.find(5).order("name")
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" = 5 \nORDER BY name")
+
+ it "handles greater than", ->
+ query = db.products.find({"id >" : "steve"})
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" > ?")
+ query.params.length.should.equal 1
+
+ it "handles less than", ->
+ query = db.products.find({"id <" : "steve"})
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" < ?")
+ query.params.length.should.equal 1
+
+ it "handles bang equal", ->
+ query = db.products.find({"id !=" : "steve"})
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" <> ?")
+ query.params.length.should.equal 1
+
+ it "handles ineqaulity", ->
+ query = db.products.find({"id <>" : "steve"})
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" <> ?")
+ query.params.length.should.equal 1
+
+ it "handles IN", ->
+ query = db.products.find({id : ["steve","juice","pete"]})
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" IN (?, ?, ?)")
+ query.params.length.should.equal 3
+
+ it "handles NOT IN", ->
+ query = db.products.find({"id != ": ["steve","juice","pete"]})
+ query.sql.should.equal("SELECT * FROM products \nWHERE \"id\" NOT IN (?, ?, ?)")
+ query.params.length.should.equal 3
+
+ it "handles inline goodness", ->
+ query = db.run("select * from crazytown where id = ?", [1]);
+ query.sql.should.equal("select * from crazytown where id = ?")
+ query.params.length.should.equal 1
+
+ describe "destroy", ->
+ it "creates a delete everything", ->
+ query = db.products.destroy()
+ query.sql.should.equal "DELETE FROM products"
+ query.params.length.should.equal 0
+
+ it "uses where when specified as an argument", ->
+ query = db.products.destroy({id : 1})
+ query.sql.should.equal "DELETE FROM products \nWHERE \"id\" = 1"
+ query.params.length.should.equal 0
+
+
+ it "adds a where as a primary key", ->
+ query = db.products.destroy(6)
+ query.sql.should.equal("DELETE FROM products \nWHERE \"id\" = 6")
+ query.params.length.should.equal 0
+
+ describe "insert", ->
+ it "creates a basic insert with returning", ->
+ query = db.products.insert({name : "steve", price : 12.00})
+ query.sql.should.equal "INSERT INTO products (name, price) VALUES\n(?, ?)"
+ query.params.length.should.equal 2
+
+ it "creates a batch for item arrays", ->
+ items = [{title:"stuffy stuff", price: 12.00, desc : "bubble"},{title:"poofy poof", price: 24.00, desc : "glurp"}];
+ query = db.products.insert(items)
+ query.sql.should.equal "INSERT INTO products (title, price, desc) VALUES\n(?, ?, ?),\n(?, ?, ?)"
+ query.params.length.should.equal 6
+
+ it "throws an error if no data was supplied", ->
+ (-> db.products.insert().execute()).should.throw
+
+ describe "updates", ->
+ it "creates a basic update", ->
+ query = db.products.update({name:"pumpkin", price:1000}, 12)
+ query.sql.should.equal("UPDATE products SET name = ?, price = ? \nWHERE \"id\" = 12")
+ query.params.length.should.equal 2
+
+ it "creates a basic update with a string key", ->
+ query = db.products.update({name:"pumpkin", price:1000}, "12")
+ query.sql.should.equal("UPDATE products SET name = ?, price = ? \nWHERE \"id\" = ?")
+ query.params.length.should.equal 3
+
+ it "creates a basic update with multi result", ->
+ query = db.products.update({name:"pumpkin", price:1000}, {"id >": 12})
+ query.sql.should.equal("UPDATE products SET name = ?, price = ? \nWHERE \"id\" > 12")
+ query.params.length.should.equal 2
+
+ it "updates all rows", ->
+ query = db.products.update({name:"leto", sand: true})
+ query.sql.should.equal("UPDATE products SET name = ?, sand = ?")
+ query.params.length.should.equal 2
+
+
+ describe "aggregates", ->
+ it "counts with SELECT COUNT", ->
+ query = db.products.count()
+ query.sql.should.equal("SELECT COUNT(1) FROM products");
+ it "counts with SELECT COUNT and a WHERE", ->
+ query = db.products.count({"id > " : 1})
+ query.sql.should.equal("SELECT COUNT(1) FROM products \nWHERE \"id\" > 1");
+
+
@@ -0,0 +1,53 @@
+massive = require("../index");
+should = require("should");
+util = require("util");
+
+
+describe "schema queries", ->
+ db = null
+ before (done) ->
+ massive.connect {user : "root", password : "", database : "test"}, (err,_db) ->
+ db = _db
+ done()
+
+ it "should exist", ->
+ should.exist db
+
+ describe "dropTable", ->
+ it "creates a drop statement", ->
+ db.dropTable("cheese").sql.should.equal("DROP TABLE IF EXISTS cheese;");
+
+ describe "create table", ->
+
+ it "adds a column with NULL declaration", ->
+ query = db.createTable "cheese", {bucket:"beef NOT NULL PLEASE"}
+ query.sql.indexOf("bucket beef NOT NULL PLEASE").should.be.greaterThan(0)
+
+ it "adds primary key by default", ->
+ query = db.createTable "cheese",{title:"string"}
+ query.sql.indexOf("INT NOT NULL PRIMARY KEY AUTO_INCREMENT").should.be.greaterThan(0)
+
+ it "adds a varchar 255 for string", ->
+ query = db.createTable "cheese",{title:"string"}
+ query.sql.indexOf("title varchar(255)").should.be.greaterThan(0)
+
+ it "adds a varchar 255 for string with null", ->
+ query = db.createTable "cheese",{title:"string null"}
+ query.sql.indexOf("title varchar(255) null").should.be.greaterThan(0)
+
+ it "adds a decimal(8,2) for money", ->
+ query = db.createTable "cheese",{price:"money"}
+ query.sql.indexOf("price decimal(8,2)").should.be.greaterThan(0)
+
+ it "adds a datetime for date", ->
+ query = db.createTable "cheese",{created:"date"}
+ console.log(query.sql)
+ query.sql.indexOf("created datetime").should.be.greaterThan(0)
+
+ it "adds a bool for... bool", ->
+ query = db.createTable "cheese",{stuff:"bool"}
+ query.sql.indexOf("stuff bool").should.be.greaterThan(0)
+
+ it "adds created and updated for timestamps", ->
+ query = db.createTable "cheese",{timestamps:true}
+ query.sql.indexOf("created_at datetime,updated_at timestamp").should.be.greaterThan(0)
View
@@ -0,0 +1,29 @@
+massive = require("../index");
+should = require("should");
+util = require("util");
+
+describe "Connections", ->
+ db = null
+ before (done) ->
+ massive.connect {user : "root", password : "", database : "test"}, (err,_db) ->
+ db = _db
+ done()
+
+ it "returns a new postgres db", ->
+ should.exist db
+
+ it "has a tables array", ->
+ should.exist db.tables
+
+ it "sets dbType to PostgreSQL", ->
+ db.dbType.should.equal("MySQL")
+
+ it "loads tables with name and pk", ->
+ db.tables.length.should.be.greaterThan 0
+
+ #this requires a products table in the test db
+ it "grafts the tables onto the query", ->
+ should.exist db.products
+
+ it "returns the pk", ->
+ db.products.pk.should.equal("id")
View
@@ -17,11 +17,6 @@ describe "Postgres Queries", ->
it "defaults the pk to id", ->
db.products.pk.should.equal("id")
- describe "Inline SQL", ->
- it "runs a simple query", ->
- db.execute "SELECT * FROM products", (err,result) ->
- should.exist result.rows
-
describe "SELECT queries", ->
it "runs a select *", ->
query = db.products.find()
@@ -179,44 +174,5 @@ describe "Postgres Queries", ->
it "tables should have an each method", ->
should.exist db.products.each
- #this is a stupid test
- it "iterates on the table", ->
- count = 0
- db.products.each (err,result) ->
- count++
- count.should.be.greaterThan 0
-
- describe "singles on queries and tables", ->
- it "query has a first method", ->
- query = db.products.find()
- should.exist query.first
-
- it "query returns a single item", (done)->
- query = db.products.find()
- query.first (err,result) ->
- should.exist result
- done()
-
- it "table has a first method", ->
- should.exist db.products.first
-
- it "returns the first item in table", ->
- db.products.first (err,result) ->
- should.exist(result)
-
- it "has a last method", ->
- query = db.products.find()
- should.exist query.last
-
- it "returns a single item", (done)->
- query = db.products.find()
- query.last (err,result) ->
- should.exist result
- done()
- it "table has a first method", ->
- should.exist db.products.last
- it "returns the first item in table", ->
- db.products.last (err,result) ->
- should.exist(result)

0 comments on commit 53029b3

Please sign in to comment.