Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gremlin cleanup #452

Merged
merged 6 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* It returns a Gremlin template to get all edges labels and their counts
*/
const edgeLabelsTemplate = () => {
export default function edgeLabelsTemplate() {
return "g.E().groupCount().by(label)";
};

export default edgeLabelsTemplate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import { uniq } from "lodash";
* .by(V().bothE("contain").limit(1))
* .limit(1)
*/
const edgesSchemaTemplate = ({ types }: { types: string[] }) => {
export default function edgesSchemaTemplate({ types }: { types: string[] }) {
const labels = uniq(types.flatMap(type => type.split("::")));

return `g.E().project(${labels.map(l => `"${l}"`).join(",")})${labels.map(l => `.by(V().bothE("${l}").limit(1))`).join("")}.limit(1)`;
};

export default edgesSchemaTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import keywordSearchTemplate from "./keywordSearchTemplate";
import { normalizeWithNoSpace as normalize } from "../../../utils/testing";

describe("Gremlin > keywordSearchTemplate", () => {
it("Should return a template only with default range", () => {
const template = keywordSearchTemplate({});

expect(template).toBe("g.V().range(0,10)");
expect(normalize(template)).toBe(normalize("g.V().range(0,10)"));
});

it("Should return a template only for vertex type", () => {
const template = keywordSearchTemplate({
vertexTypes: ["airport"],
});

expect(template).toBe('g.V().hasLabel("airport").range(0,10)');
expect(normalize(template)).toBe(
normalize('g.V().hasLabel("airport").range(0,10)')
);
});

it("Should return a template for searched attributes containing the search term", () => {
Expand All @@ -21,8 +24,10 @@ describe("Gremlin > keywordSearchTemplate", () => {
searchByAttributes: ["city", "code"],
});

expect(template).toBe(
'g.V().or(has("city",containing("JFK")),has("code",containing("JFK"))).range(0,10)'
expect(normalize(template)).toBe(
normalize(
'g.V().or(has("city",containing("JFK")),has("code",containing("JFK"))).range(0,10)'
)
);
});

Expand All @@ -33,8 +38,8 @@ describe("Gremlin > keywordSearchTemplate", () => {
exactMatch: true,
});

expect(template).toBe(
'g.V().or(has("city","JFK"),has("code","JFK")).range(0,10)'
expect(normalize(template)).toBe(
normalize('g.V().or(has("city","JFK"),has("code","JFK")).range(0,10)')
);
});

Expand All @@ -45,7 +50,9 @@ describe("Gremlin > keywordSearchTemplate", () => {
exactMatch: true,
});

expect(template).toBe('g.V().or(has("code","\\"JFK\\"")).range(0,10)');
expect(normalize(template)).toBe(
normalize('g.V().or(has("code","\\"JFK\\"")).range(0,10)')
);
});

it("Should return a template for the ID token attribute exactly matching the search term", () => {
Expand All @@ -57,8 +64,8 @@ describe("Gremlin > keywordSearchTemplate", () => {
searchByAttributes: ["__id"],
});

expect(template).toBe(
'g.V().hasLabel("airport").or(has(id,"JFK")).range(0,10)'
expect(normalize(template)).toBe(
normalize('g.V().hasLabel("airport").or(has(id,"JFK")).range(0,10)')
);
});

Expand All @@ -71,8 +78,10 @@ describe("Gremlin > keywordSearchTemplate", () => {
searchByAttributes: ["__id"],
});

expect(template).toBe(
'g.V().hasLabel("airport").or(has(id,containing("JFK"))).range(0,10)'
expect(normalize(template)).toBe(
normalize(
'g.V().hasLabel("airport").or(has(id,containing("JFK"))).range(0,10)'
)
);
});

Expand All @@ -83,8 +92,16 @@ describe("Gremlin > keywordSearchTemplate", () => {
searchByAttributes: ["city", "code", "__all"],
});

expect(template).toBe(
'g.V().or(has(id,containing("JFK")),has("city",containing("JFK")),has("code",containing("JFK"))).range(0,10)'
expect(normalize(template)).toBe(
normalize(`
g.V()
.or(
has(id, containing("JFK")),
has("city", containing("JFK")),
has("code", containing("JFK"))
)
.range(0,10)
`)
);
});

Expand All @@ -95,7 +112,9 @@ describe("Gremlin > keywordSearchTemplate", () => {
limit: 25,
});

expect(template).toBe('g.V().hasLabel("airport").range(25,50)');
expect(normalize(template)).toBe(
normalize('g.V().hasLabel("airport").range(25,50)')
);
});

it("Should return a template with an offset and limit", () => {
Expand All @@ -107,8 +126,8 @@ describe("Gremlin > keywordSearchTemplate", () => {
limit: 10,
});

expect(template).toBe(
'g.V().or(has("code",containing("JFK"))).range(2,12)'
expect(normalize(template)).toBe(
normalize('g.V().or(has("code",containing("JFK"))).range(2,12)')
);
});

Expand All @@ -122,8 +141,10 @@ describe("Gremlin > keywordSearchTemplate", () => {
offset: 1,
});

expect(template).toBe(
'g.V().hasLabel("airport").or(has("code",containing("JFK"))).range(1,26)'
expect(normalize(template)).toBe(
normalize(
'g.V().hasLabel("airport").or(has("code",containing("JFK"))).range(1,26)'
)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import { escapeString } from "../../../utils";
* )
* .range(0, 100)
*/
const keywordSearchTemplate = ({
export default function keywordSearchTemplate({
searchTerm,
vertexTypes = [],
searchById = true,
searchByAttributes = [],
limit = 10,
offset = 0,
exactMatch = false,
}: KeywordSearchRequest): string => {
}: KeywordSearchRequest): string {
let template = "g.V()";

if (vertexTypes.length !== 0) {
Expand Down Expand Up @@ -67,6 +67,4 @@ const keywordSearchTemplate = ({

template += `.range(${offset},${offset + limit})`;
return template;
};

export default keywordSearchTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import neighborsCountTemplate from "./neighborsCountTemplate";
import { normalizeWithNoSpace as normalize } from "../../../utils/testing";

describe("Gremlin > neighborsCountTemplate", () => {
it("Should return a template for the given vertex id", () => {
Expand All @@ -7,8 +8,10 @@ describe("Gremlin > neighborsCountTemplate", () => {
idType: "string",
});

expect(template).toBe(
'g.V("12").both().dedup().group().by(label).by(count())'
expect(normalize(template)).toBe(
normalize(`
g.V("12").both().dedup().group().by(label).by(count())
`)
);
});

Expand All @@ -18,8 +21,10 @@ describe("Gremlin > neighborsCountTemplate", () => {
idType: "number",
});

expect(template).toBe(
"g.V(12L).both().dedup().group().by(label).by(count())"
expect(normalize(template)).toBe(
normalize(`
g.V(12L).both().dedup().group().by(label).by(count())
`)
);
});

Expand All @@ -30,8 +35,10 @@ describe("Gremlin > neighborsCountTemplate", () => {
limit: 20,
});

expect(template).toBe(
'g.V("12").both().limit(20).dedup().group().by(label).by(count())'
expect(normalize(template)).toBe(
normalize(`
g.V("12").both().limit(20).dedup().group().by(label).by(count())
`)
);
});

Expand All @@ -42,8 +49,10 @@ describe("Gremlin > neighborsCountTemplate", () => {
limit: 0,
});

expect(template).toBe(
'g.V("12").both().dedup().group().by(label).by(count())'
expect(normalize(template)).toBe(
normalize(`
g.V("12").both().dedup().group().by(label).by(count())
`)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import type { NeighborsCountRequest } from "../../useGEFetchTypes";
* g.V("44").both().limit(10).dedup()
* .group().by(label).by(count())
*/
const neighborsCountTemplate = ({
export default function neighborsCountTemplate({
vertexId,
limit = 0,
idType,
}: NeighborsCountRequest) => {
}: NeighborsCountRequest) {
let template = "";
if (idType === "number") {
template = `g.V(${vertexId}L).both()`;
Expand All @@ -29,6 +29,4 @@ const neighborsCountTemplate = ({
template += `.dedup().group().by(label).by(count())`;

return template;
};

export default neighborsCountTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalizeWithNoSpace as normalize } from "../../../utils/testing";
import oneHopTemplate from "./oneHopTemplate";

describe("Gremlin > oneHopTemplate", () => {
Expand All @@ -7,10 +8,12 @@ describe("Gremlin > oneHopTemplate", () => {
idType: "string",
});

expect(template).toBe(
'g.V("12").project("vertices", "edges")' +
".by(both().dedup().fold())" +
".by(bothE().dedup().fold())"
expect(normalize(template)).toBe(
normalize(`
g.V("12").project("vertices", "edges")
.by(both().dedup().fold())
.by(bothE().dedup().fold())
`)
);
});

Expand All @@ -20,10 +23,12 @@ describe("Gremlin > oneHopTemplate", () => {
idType: "number",
});

expect(template).toBe(
'g.V(12L).project("vertices", "edges")' +
".by(both().dedup().fold())" +
".by(bothE().dedup().fold())"
expect(normalize(template)).toBe(
normalize(`
g.V(12L).project("vertices", "edges")
.by(both().dedup().fold())
.by(bothE().dedup().fold())
`)
);
});

Expand All @@ -35,10 +40,12 @@ describe("Gremlin > oneHopTemplate", () => {
limit: 5,
});

expect(template).toBe(
'g.V("12").project("vertices", "edges")' +
".by(both().dedup().range(5, 10).fold())" +
".by(bothE().dedup().fold())"
expect(normalize(template)).toBe(
normalize(`
g.V("12").project("vertices", "edges")
.by(both().dedup().range(5, 10).fold())
.by(bothE().dedup().fold())
`)
);
});

Expand All @@ -51,10 +58,30 @@ describe("Gremlin > oneHopTemplate", () => {
limit: 10,
});

expect(template).toBe(
'g.V("12").project("vertices", "edges")' +
'.by(both().hasLabel("country").dedup().range(5, 15).fold())' +
'.by(bothE().where(otherV().hasLabel("country")).dedup().fold())'
expect(normalize(template)).toBe(
normalize(`
g.V("12").project("vertices", "edges")
.by(both().hasLabel("country").dedup().range(5, 15).fold())
.by(bothE().where(otherV().hasLabel("country")).dedup().fold())
`)
);
});

it("Should return a template for multiple vertex type", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
filterByVertexTypes: ["country", "airport", "continent"],
offset: 5,
limit: 10,
});

expect(normalize(template)).toBe(
normalize(`
g.V("12").project("vertices", "edges")
.by(both().hasLabel("country", "airport", "continent").dedup().range(5, 15).fold())
.by(bothE().where(otherV().hasLabel("country", "airport", "continent")).dedup().fold())
`)
);
});

Expand All @@ -71,12 +98,14 @@ describe("Gremlin > oneHopTemplate", () => {
limit: 10,
});

expect(template).toBe(
'g.V("12").project("vertices", "edges")' +
'.by(both().hasLabel("country")' +
'.and(has("longest",gte(10000)),has("country",containing("ES")))' +
".dedup().range(5, 15).fold())" +
'.by(bothE().where(otherV().hasLabel("country")).dedup().fold())'
expect(normalize(template)).toBe(
normalize(`
g.V("12").project("vertices", "edges")
.by(both().hasLabel("country")
.and(has("longest",gte(10000)),has("country",containing("ES")))
.dedup().range(5, 15).fold())
.by(bothE().where(otherV().hasLabel("country")).dedup().fold())
`)
);
});
});
Loading
Loading