Skip to content

Query params per file #107

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

Merged
merged 16 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from 8 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
Expand Up @@ -217,6 +217,12 @@
icon="/actions/refresh.png"
description="Clean canvas">
</action>
<action id="GraphDatabaseConsoleToolWindowActions.ToggleFileSpecificParametersUsed"
class="com.neueda.jetbrains.plugin.graphdb.jetbrains.actions.ui.console.ToggleFileSpecificParametersUsedAction"
text="Using global parameters"
icon="/actions/edit.svg"
description="Toggle global or file-specific query parameters">
</action>
</group>

<group id="GraphDatabaseFileActions"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.intellij.openapi.fileTypes.FileTypeConsumer;
import com.intellij.openapi.fileTypes.FileTypeFactory;
import com.neueda.jetbrains.plugin.graphdb.language.cypher.util.FileTypeExtensionUtil;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -13,6 +14,6 @@ public class CypherFileTypeFactory extends FileTypeFactory {

@Override
public void createFileTypes(@NotNull FileTypeConsumer fileTypeConsumer) {
fileTypeConsumer.consume(CypherFileType.INSTANCE, "cyp;cypher;cql");
fileTypeConsumer.consume(CypherFileType.INSTANCE, String.join(";", FileTypeExtensionUtil.EXTENSIONS));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.neueda.jetbrains.plugin.graphdb.language.cypher.util;

import java.util.ArrayList;
import java.util.List;

public class FileTypeExtensionUtil {

public static final List<String> EXTENSIONS = new ArrayList<String>() {{
add("cyp");
add("cql");
add("cypher");
}};

public static boolean isCypherFileTypeExtension(String extension) {
return extension != null && EXTENSIONS.contains(extension);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@

import com.fasterxml.jackson.databind.JsonMappingException;
import com.intellij.psi.PsiElement;
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.settings.SettingsComponent;
import com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.console.params.ParametersProvider;
import com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.console.params.ParametersService;
import com.neueda.jetbrains.plugin.graphdb.test.integration.neo4j.util.base.BaseIntegrationTest;

import java.util.Map;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;

public class CypherParametersProviderTest extends BaseIntegrationTest {

private class TestParametersProvider implements ParametersProvider {

private String parametersJson;
private String parametersJson, localParametersJson;

@Override
public String getParametersJson() {
return parametersJson;
}

@Override
public String getLocalParametersJson() {
return localParametersJson;
}

public void setParametersJson(String parametersJson) {
this.parametersJson = parametersJson;
}

public void setLocalParametersJson(String localParametersJson) {
this.localParametersJson = localParametersJson;
}
}

private ParametersService parametersService;
Expand All @@ -38,19 +48,40 @@ public void setUp() throws Exception {
}

public void testParsingEmptyJsonObject() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("{}");
parametersProvider.setLocalParametersJson("{\"param\": \"non-empty\"}");
Map<String, Object> parameters = parametersService.getParameters(getPsiFile("RETURN $param"));
assertThat(parameters).isEmpty();
}

public void testParsingEmptyJsonObjectInLocalParams() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(true);
parametersProvider.setLocalParametersJson("{}");
parametersProvider.setParametersJson("{\"param\": \"non-empty\"}");
// local (file-specific) parameters are returned from getParameters() here, and they must be empty
Map<String, Object> parameters = parametersService.getParameters(getPsiFile("RETURN $param"));
assertThat(parameters).isEmpty();
}

public void testParsingEmptyParameters() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("");
parametersProvider.setLocalParametersJson("{\"param\": \"non-empty\"}");
Map<String, Object> result = parametersService.getParameters(getPsiFile("RETURN $param"));
assertThat(result).isEmpty();
}

public void testParsingEmptyLocalParameters() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(true);
parametersProvider.setLocalParametersJson("");
parametersProvider.setParametersJson("{\"param\": \"non-empty\"}");
Map<String, Object> result = parametersService.getParameters(getPsiFile("RETURN $param"));
assertThat(result).isEmpty();
}

public void testParsingStringParameter() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("{\"name\": \"Anna\"}");
Map<String, Object> result = parametersService
.getParameters(getPsiFile("match (p:Person) where p.name = $name return *"));
Expand All @@ -59,6 +90,7 @@ public void testParsingStringParameter() throws Exception {
}

public void testParsingIntegerParameter() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("{\"p1\": 17}");
Map<String, Object> result = parametersService
.getParameters(getPsiFile("match (p:Person) where p.age = $p1 return *"));
Expand All @@ -67,6 +99,7 @@ public void testParsingIntegerParameter() throws Exception {
}

public void testParsingBooleanParameter() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("{\"p2\": false}");
Map<String, Object> result = parametersService.
getParameters(getPsiFile("match (p:Person) where p.is_citizen = $p2 return *"));
Expand All @@ -75,6 +108,7 @@ public void testParsingBooleanParameter() throws Exception {
}

public void testParsingJsonObjectParameter() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("{\"p3\": {\"name\":\"Alex\"}}");
Map<String, Object> result = parametersService.
getParameters(getPsiFile("match (p:Person) where p.father = $p3 return *"));
Expand All @@ -85,53 +119,74 @@ public void testParsingJsonObjectParameter() throws Exception {

public void testParsingMultipleParameters() throws Exception {
parametersProvider.setParametersJson("{\"firstName\": \"Kaleb\", \"lastName\": \"Johnson\"}");
parametersProvider.setLocalParametersJson(
"{\"country\": \"France\", \"city\": \"Paris\", \"age\": 90, \"lastName\": \"Green\"}"
);

SettingsComponent.getInstance().enableFileSpecificParams(false);
Map<String, Object> result = parametersService
.getParameters(getPsiFile("match (p:Person)\n" +
"where p.first_name = $firstName " +
" and p.last_name = $lastName return *"));

assertThat(result)
.hasSize(2)
.containsEntry("firstName", "Kaleb")
.containsEntry("lastName", "Johnson");

SettingsComponent.getInstance().enableFileSpecificParams(true);
result = parametersService
.getParameters(getPsiFile("match (p:Person)\n" +
"where p.city = $city and p.age = $age " +
" and p.last_name = $lastName return *"));
assertThat(result)
.hasSize(3)
.containsEntry("city", "Paris")
.containsEntry("age", 90)
.containsEntry("lastName", "Green");
}

public void testParsingCommentOnly() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("// Provide query parameters in JSON format here:");
Map<String, Object> result = parametersService.getParameters(getPsiFile("RETURN $param"));

assertThat(result).isEmpty();
}

public void testParsingCommentWithParameter() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("// Provide query parameters in JSON format here:\n{\"name\": \"Eva\"}");
Map<String, Object> result = parametersService.getParameters(getPsiFile("RETURN $name"));

assertThat(result).hasSize(1);
}

public void testParsingNumericParameter() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("{\"0\": \"Tom\"}");
Map<String, Object> result = parametersService.getParameters(getPsiFile("RETURN $0"));

assertThat(result).containsEntry("0", "Tom");
}

public void testParsingOldStyleStringParameter() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("{\"name\": \"Ethan\"}");
Map<String, Object> result = parametersService.getParameters(getPsiFile("RETURN {name}"));

assertThat(result).containsEntry("name", "Ethan");
}

public void testParsingOldStyleNumericParameter() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("{\"0\": \"Simon\"}");
Map<String, Object> result = parametersService.getParameters(getPsiFile("RETURN {0}"));

assertThat(result).containsEntry("0", "Simon");
}

public void testFilteringUsedParameters() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("{\"firstName\": \"Frodo\", \"lastName\": \"Baggins\"}");
Map<String, Object> result = parametersService.getParameters(getPsiFile("RETURN $lastName"));

Expand All @@ -142,6 +197,7 @@ public void testFilteringUsedParameters() throws Exception {

public void testParsingJsonArray() throws Exception {
try {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("// Provide query parameters in JSON format here:\n[\"item1\",\"item2\"]");
parametersService.getParameters(getPsiFile("return 1"));
fail("JsonMappingException expected because of array in parameters json expected");
Expand All @@ -152,6 +208,7 @@ public void testParsingJsonArray() throws Exception {

public void testParsingNumber() throws Exception {
try {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("1");
parametersService.getParameters(getPsiFile("return 1"));
fail("JsonMappingException expected because of number provided instead of parameters map");
Expand All @@ -162,6 +219,7 @@ public void testParsingNumber() throws Exception {

public void testParsingString() throws Exception {
try {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("\"abc\"");
parametersService.getParameters(getPsiFile("return 1"));
fail("JsonMappingException expected because of string provided instead of parameters map");
Expand All @@ -172,6 +230,7 @@ public void testParsingString() throws Exception {

public void testParsingUnwrappedParameter() throws Exception {
try {
SettingsComponent.getInstance().enableFileSpecificParams(false);
parametersProvider.setParametersJson("\"param1\":\"val1\"");
parametersService.getParameters(getPsiFile("return 1"));
fail("JsonMappingException expected because of parameter not wrapped in curly braces");
Expand All @@ -181,6 +240,7 @@ public void testParsingUnwrappedParameter() throws Exception {
}

public void testParametersRetrievalWithNoPsiElement() throws Exception {
SettingsComponent.getInstance().enableFileSpecificParams(false);
Map<String, Object> result = parametersService.getParameters(null);
assertThat(result).isEmpty();
}
Expand Down
Loading