Skip to content

Commit

Permalink
Add string specific comparision operators
Browse files Browse the repository at this point in the history
  • Loading branch information
robak86 committed Jun 28, 2018
1 parent e1db711 commit 9fa0490
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ examples/node_modules
examples/**/*.js
.DS_Store
neography
drafts.md
12 changes: 12 additions & 0 deletions lib/cypher/builders/WhereAttributeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ export class WhereAttributeBuilder<T> {
in(values:T[]):WhereAttributeQueryPart {
return new WhereAttributeQueryPart(this.propertyName, this._alias, 'in', values);
}

contains(value:string):WhereAttributeQueryPart {
return new WhereAttributeQueryPart(this.propertyName, this._alias, 'CONTAINS', value);
}

startsWith(value:string):WhereAttributeQueryPart {
return new WhereAttributeQueryPart(this.propertyName, this._alias, 'STARTS WITH', value);
}

endsWith(value:string):WhereAttributeQueryPart {
return new WhereAttributeQueryPart(this.propertyName, this._alias, 'ENDS WITH', value);
}
}
4 changes: 2 additions & 2 deletions lib/cypher/builders/WhereBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class WhereBuilder<T = any> {
return new WhereAttributeBuilder(prop, this._alias);
}

//TODO: investigate matching for queries like WHERE NOT (n)-[:SOME_REL]-(b)
path(m:MatchBuilderCallback){}
// //TODO: investigate matching for queries like WHERE NOT (n)-[:SOME_REL]-(b)
// path(m:MatchBuilderCallback){}

aliased(alias:string):WhereBuilder<T>{
return cloned(this, w => w._alias = alias);
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {IQueryPart} from "./cypher/abstract/IQueryPart";
export * from './connection/Connection';
export * from './utils/SimpleDebugLogger';

export const buildQuery = () => new QueryBuilder();
export const buildQuery = (...elements:IQueryPart[]) => new QueryBuilder(elements);

export class Neography {
private config:Config;
Expand Down
36 changes: 36 additions & 0 deletions spec/cypher/builders/WhereAttributeBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,40 @@ describe(`WhereAttributeBuilder`, () => {
expect(cypher.params).to.be.undefined;
});
});

describe(`.contains`, () => {
it('returns correct cypher statement', () => {
let cypher = builder.contains("abc").toCypher(ctx);
expect(cypher.cypherString).to.eq(`node.someProperty CONTAINS { __node_someProperty_prop1 }`)
});

it('returns correct params object', () => {
let cypher = builder.contains('abc').toCypher(ctx);
expect(cypher.params).to.eql({__node_someProperty_prop1: 'abc'})
});
});

describe(`.startsWith`, () => {
it('returns correct cypher statement', () => {
let cypher = builder.startsWith("abc").toCypher(ctx);
expect(cypher.cypherString).to.eq(`node.someProperty STARTS WITH { __node_someProperty_prop1 }`)
});

it('returns correct params object', () => {
let cypher = builder.startsWith('abc').toCypher(ctx);
expect(cypher.params).to.eql({__node_someProperty_prop1: 'abc'})
});
});

describe(`.endsWith`, () => {
it('returns correct cypher statement', () => {
let cypher = builder.endsWith("abc").toCypher(ctx);
expect(cypher.cypherString).to.eq(`node.someProperty ENDS WITH { __node_someProperty_prop1 }`)
});

it('returns correct params object', () => {
let cypher = builder.endsWith('abc').toCypher(ctx);
expect(cypher.params).to.eql({__node_someProperty_prop1: 'abc'})
});
});
});
137 changes: 137 additions & 0 deletions spec/integration/stringSpecificComparisionOperators.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import {cleanDatabase, getDefaultNeography} from "../helpers/ConnectionHelpers";
import {DummyGraphNode} from "../fixtures/DummyGraphNode";
import {buildQuery, Connection} from "../../lib";
import {create} from "../../lib/cypher/create";
import {match} from "../../lib/cypher/match";
import {where} from "../../lib/cypher/where";
import {returns} from "../../lib/cypher/common";
import {expect} from 'chai';
import {orderBy} from "../../lib/cypher/order";

describe(`String specific comparision operators`, () => {
let connection:Connection;

beforeEach(async () => {
const neography = getDefaultNeography();
connection = neography.checkoutConnection();
await cleanDatabase();
});

let nodes:DummyGraphNode[];

beforeEach(async () => {
nodes = [
new DummyGraphNode({attr1: "abc"}),
new DummyGraphNode({attr1: "bcd"}),
new DummyGraphNode({attr1: "efg"}),
new DummyGraphNode({attr1: "bbb"}),
new DummyGraphNode({attr1: "aac"})
];

const query = buildQuery(
create(c => [
c.node(nodes[0]),
c.node(nodes[1]),
c.node(nodes[2]),
c.node(nodes[3]),
c.node(nodes[4])
])
);

await connection.runQuery(query).toArray();
});

describe(`contains`, () => {
it(`ex1. it returns nodes using contains operator`, async () => {
const q1 = buildQuery(
match(m => m.node(DummyGraphNode).as('n')),
where(w => w.aliased('n').attribute('attr1').contains("abc")),
returns('n')
);

const result = await connection.runQuery(q1).pluck('n').toArray();

expect(result.length).to.eq(1);
expect(result[0].attr1).to.eql("abc")
});

it(`ex2. it returns nodes using contains operator`, async () => {
const q1 = buildQuery(
match(m => m.node(DummyGraphNode).as('n')),
where(w => w.aliased('n').attribute('attr1').contains("bc")),
returns('n'),
orderBy(o => o.aliased('n').attribute('attr1').asc())
);

const result = await connection.runQuery(q1).pluck('n').toArray();

expect(result.length).to.eq(2);
expect(result[0].attr1).to.eql("abc");
expect(result[1].attr1).to.eql("bcd");
});
});


describe(`starts with`, () => {
it(`ex1. it returns nodes using contains operator`, async () => {
const q1 = buildQuery(
match(m => m.node(DummyGraphNode).as('n')),
where(w => w.aliased('n').attribute('attr1').startsWith("a")),
returns('n'),
orderBy(o => o.aliased('n').attribute('attr1').asc())
);

const result = await connection.runQuery(q1).pluck('n').toArray();

expect(result.length).to.eq(2);
expect(result[0].attr1).to.eql("aac");
expect(result[1].attr1).to.eql("abc");
});

it(`ex2. it returns nodes using contains operator`, async () => {
const q1 = buildQuery(
match(m => m.node(DummyGraphNode).as('n')),
where(w => w.aliased('n').attribute('attr1').startsWith("b")),
returns('n'),
orderBy(o => o.aliased('n').attribute('attr1').asc())
);

const result = await connection.runQuery(q1).pluck('n').toArray();

expect(result.length).to.eq(2);
expect(result[0].attr1).to.eql("bbb");
expect(result[1].attr1).to.eql("bcd");
});
});

describe(`ends with`, () => {
it(`ex1. it returns nodes using contains operator`, async () => {
const q1 = buildQuery(
match(m => m.node(DummyGraphNode).as('n')),
where(w => w.aliased('n').attribute('attr1').endsWith("c")),
returns('n'),
orderBy(o => o.aliased('n').attribute('attr1').asc())
);

const result = await connection.runQuery(q1).pluck('n').toArray();

expect(result.length).to.eq(2);
expect(result[0].attr1).to.eql("aac");
expect(result[1].attr1).to.eql("abc");
});

it(`ex2. it returns nodes using contains operator`, async () => {
const q1 = buildQuery(
match(m => m.node(DummyGraphNode).as('n')),
where(w => w.aliased('n').attribute('attr1').endsWith("g")),
returns('n'),
orderBy(o => o.aliased('n').attribute('attr1').asc())
);

const result = await connection.runQuery(q1).pluck('n').toArray();

expect(result.length).to.eq(1);
expect(result[0].attr1).to.eql("efg");
});
});
});

0 comments on commit 9fa0490

Please sign in to comment.