Skip to content

Commit

Permalink
andWhere and equals as default
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Oct 15, 2015
1 parent eb63441 commit 1566437
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
10 changes: 9 additions & 1 deletion es5/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ var Query = (function () {
var theirLink = theirChain[theirIndex];
var theirArguments = theirLink.options;

if (ourLink.name === theirLink.name) {
//so andWhere and where are treated both as the same
var theirLinkName = theirLink.name.replace("andWhere", "where");
var ourLinkName = ourLink.name.replace("andWhere", "where");

if (ourLinkName === theirLinkName) {
if (this[argumentsEqual](ourArguments, theirArguments)) {
hasMatchingLink = true;
break;
Expand All @@ -293,6 +297,10 @@ var Query = (function () {
}, {
key: addChain,
value: function value(chainName, options) {
var chainNameId = chainName.replace("andWhere", "where"); //treat where equals to andWhere
if (chainNameId === "where" && options.length === 2) {
options = [options[0], "=", options[1]];
}
(0, _incognito2["default"])(this).chain.push({
name: chainName,
options: options
Expand Down
33 changes: 32 additions & 1 deletion es5/spec/database/query/equalTo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ var databaseConfig = require("../../../../database.json").testing;

describe(".equalTo(query)", function () {

var database = undefined;
var database = undefined,
mockUsers = undefined;

beforeEach(function () {
database = new _libDatabaseJs2["default"](databaseConfig);
mockUsers = [{ id: 1, name: "Bob" }];
});

it("should compare simple queries that are equal and return true", function () {
Expand Down Expand Up @@ -71,6 +73,7 @@ describe(".equalTo(query)", function () {

queryA.equalTo(queryB).should.be["true"];
});

it("should compare simple queries with regex object parameters that do not match and return false", function () {
var data = {
name: "Bob",
Expand All @@ -86,6 +89,34 @@ describe(".equalTo(query)", function () {
queryA.equalTo(queryB).should.be["false"];
});

describe("(where)", function () {
it("should treat where equals to andWhere", function (done) {
var name = "aName";

database.mock.select(/.*/).from(/u.*rs/).where("created_at", ">", /.*/).where("name", name).results(mockUsers);

database.select("id").from("users").where("created_at", ">", "2015-10-02 21:39:14").andWhere("name", name).results(function (error, rows) {
if (error) {
throw error;
}
rows.should.eql(mockUsers);
done();
});
});

it("should use equals as the default operator", function (done) {
database.mock.select(/.*/).from(/u.*rs/).where("id", 1).results(mockUsers);

database.select("id").from("users").where("id", "=", 1).results(function (error, rows) {
if (error) {
throw error;
}
rows.should.eql(mockUsers);
done();
});
});
});

it("should compare complex queries that are equal and return true");
it("should compare complex queries that are not equal and return false");
});
10 changes: 9 additions & 1 deletion es6/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ export default class Query {
const theirLink = theirChain[theirIndex];
const theirArguments = theirLink.options;

if (ourLink.name === theirLink.name) {
//so andWhere and where are treated both as the same
const theirLinkName = theirLink.name.replace("andWhere", "where");
const ourLinkName = ourLink.name.replace("andWhere", "where");

if (ourLinkName === theirLinkName) {
if (this[argumentsEqual](ourArguments, theirArguments)) {
hasMatchingLink = true;
break;
Expand All @@ -206,6 +210,10 @@ export default class Query {
}

[addChain](chainName, options) {
const chainNameId = chainName.replace("andWhere", "where"); //treat where equals to andWhere
if (chainNameId === "where" && options.length === 2) {
options = [options[0], "=", options[1]];
}
privateData(this).chain.push({
name: chainName,
options: options
Expand Down
31 changes: 30 additions & 1 deletion es6/spec/database/query/equalTo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ const databaseConfig = require("../../../../database.json").testing;

describe(".equalTo(query)", () => {

let database;
let database,
mockUsers;

beforeEach(() => {
database = new Database(databaseConfig);
mockUsers = [
{id: 1, name: "Bob"}
];
});

it("should compare simple queries that are equal and return true", () => {
Expand Down Expand Up @@ -65,6 +69,7 @@ describe(".equalTo(query)", () => {

queryA.equalTo(queryB).should.be.true;
});

it("should compare simple queries with regex object parameters that do not match and return false", () => {
const data = {
name: "Bob",
Expand All @@ -80,6 +85,30 @@ describe(".equalTo(query)", () => {
queryA.equalTo(queryB).should.be.false;
});

describe("(where)", () => {
it("should treat where equals to andWhere", done => {
const name = "aName";

database.mock.select(/.*/).from(/u.*rs/).where("created_at", ">", /.*/).where("name", name).results(mockUsers);

database.select("id").from("users").where("created_at", ">", "2015-10-02 21:39:14").andWhere("name", name).results((error, rows) => {
if (error) { throw error; }
rows.should.eql(mockUsers);
done();
});
});

it("should use equals as the default operator", done => {
database.mock.select(/.*/).from(/u.*rs/).where("id", 1).results(mockUsers);

database.select("id").from("users").where("id", "=", 1).results((error, rows) => {
if (error) { throw error; }
rows.should.eql(mockUsers);
done();
});
});
});

it("should compare complex queries that are equal and return true");
it("should compare complex queries that are not equal and return false");
});

0 comments on commit 1566437

Please sign in to comment.