|
| 1 | +package apoc.nlp.aws |
| 2 | + |
| 3 | +import apoc.util.TestUtil |
| 4 | +import com.amazonaws.SDKGlobalConfiguration.ACCESS_KEY_ENV_VAR |
| 5 | +import com.amazonaws.SDKGlobalConfiguration.SECRET_KEY_ENV_VAR |
| 6 | +import org.junit.Assert.assertTrue |
| 7 | +import org.junit.Assume.assumeNotNull |
| 8 | +import org.junit.BeforeClass |
| 9 | +import org.junit.ClassRule |
| 10 | +import org.junit.Test |
| 11 | +import org.neo4j.graphdb.Result |
| 12 | +import org.neo4j.test.rule.ImpermanentDbmsRule |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * To execute tests, set these environment variables: |
| 17 | + * AWS_ACCESS_KEY_ID=<apiKey>;AWS_SECRET_KEY=<secretKey> |
| 18 | + */ |
| 19 | +class AWSProceduresAPIWithEnvVarsTest { |
| 20 | + companion object { |
| 21 | + private val apiKey: String? = System.getenv(ACCESS_KEY_ENV_VAR) |
| 22 | + private val apiSecret: String? = System.getenv(SECRET_KEY_ENV_VAR) |
| 23 | + |
| 24 | + @ClassRule |
| 25 | + @JvmField |
| 26 | + val neo4j = ImpermanentDbmsRule() |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + @JvmStatic |
| 30 | + fun beforeClass() { |
| 31 | + neo4j.executeTransactionally(""" |
| 32 | + CREATE (:Article { |
| 33 | + uri: "https://neo4j.com/blog/pokegraph-gotta-graph-em-all/", |
| 34 | + body: "These days I’m rarely more than a few feet away from my Nintendo Switch and I play board games, card games and role playing games with friends at least once or twice a week. I’ve even organised lunch-time Mario Kart 8 tournaments between the Neo4j European offices!" |
| 35 | + });""") |
| 36 | + |
| 37 | + neo4j.executeTransactionally(""" |
| 38 | + CREATE (:Article { |
| 39 | + uri: "https://en.wikipedia.org/wiki/Nintendo_Switch", |
| 40 | + body: "The Nintendo Switch is a video game console developed by Nintendo, released worldwide in most regions on March 3, 2017. It is a hybrid console that can be used as a home console and portable device. The Nintendo Switch was unveiled on October 20, 2016. Nintendo offers a Joy-Con Wheel, a small steering wheel-like unit that a Joy-Con can slot into, allowing it to be used for racing games such as Mario Kart 8." |
| 41 | + }); |
| 42 | + """) |
| 43 | + |
| 44 | + assumeNotNull(apiKey, apiSecret) |
| 45 | + TestUtil.registerProcedure(neo4j, AWSProcedures::class.java) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `should extract entities in stream mode`() { |
| 51 | + neo4j.executeTransactionally(""" |
| 52 | + MATCH (a:Article {uri: "https://neo4j.com/blog/pokegraph-gotta-graph-em-all/"}) |
| 53 | + CALL apoc.nlp.aws.entities.stream(a, { |
| 54 | + nodeProperty: "body" |
| 55 | + }) |
| 56 | + YIELD value |
| 57 | + UNWIND value.entities AS result |
| 58 | + RETURN result; |
| 59 | + """, mapOf()) { |
| 60 | + assertStreamWithScoreResult(it) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `should extract entities in graph mode`() { |
| 66 | + neo4j.executeTransactionally(""" |
| 67 | + MATCH (a:Article {uri: "https://neo4j.com/blog/pokegraph-gotta-graph-em-all/"}) |
| 68 | + CALL apoc.nlp.aws.entities.graph(a, { |
| 69 | + nodeProperty: "body", |
| 70 | + writeRelationshipType: "ENTITY" |
| 71 | + }) |
| 72 | + YIELD graph AS g |
| 73 | + RETURN g; |
| 74 | + """, mapOf()) { |
| 75 | + assertGraphResult(it) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `should extract key phrases in stream mode`() { |
| 81 | + neo4j.executeTransactionally(""" |
| 82 | + MATCH (a:Article {uri: "https://neo4j.com/blog/pokegraph-gotta-graph-em-all/"}) |
| 83 | + CALL apoc.nlp.aws.keyPhrases.stream(a, { |
| 84 | + nodeProperty: "body" |
| 85 | + }) |
| 86 | + YIELD value |
| 87 | + UNWIND value.keyPhrases AS result |
| 88 | + RETURN result |
| 89 | + """, mapOf()) { |
| 90 | + assertStreamWithScoreResult(it) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun `should extract key phrases in graph mode`() { |
| 96 | + neo4j.executeTransactionally(""" |
| 97 | + MATCH (a:Article {uri: "https://neo4j.com/blog/pokegraph-gotta-graph-em-all/"}) |
| 98 | + CALL apoc.nlp.aws.keyPhrases.graph(a, { |
| 99 | + nodeProperty: "body", |
| 100 | + writeRelationshipType: "KEY_PHRASE", |
| 101 | + write: true |
| 102 | + }) |
| 103 | + YIELD graph AS g |
| 104 | + RETURN g; |
| 105 | + """, mapOf()) { |
| 106 | + assertGraphResult(it) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `should extract sentiment in stream mode`() { |
| 112 | + neo4j.executeTransactionally(""" |
| 113 | + MATCH (a:Article {uri: "https://neo4j.com/blog/pokegraph-gotta-graph-em-all/"}) |
| 114 | + CALL apoc.nlp.aws.sentiment.stream(a, { |
| 115 | + nodeProperty: "body" |
| 116 | + }) |
| 117 | + YIELD value |
| 118 | + RETURN value AS result; |
| 119 | + """, mapOf()) { |
| 120 | + assertSentimentScoreResult(it) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun `should extract sentiment in graph mode`() { |
| 126 | + neo4j.executeTransactionally(""" |
| 127 | + MATCH (a:Article {uri: "https://neo4j.com/blog/pokegraph-gotta-graph-em-all/"}) |
| 128 | + CALL apoc.nlp.aws.sentiment.graph(a, { |
| 129 | + nodeProperty: "body", |
| 130 | + write: true |
| 131 | + }) |
| 132 | + YIELD graph AS g |
| 133 | + UNWIND g.nodes AS node |
| 134 | + RETURN node {.uri, .sentiment, .sentimentScore} AS result; |
| 135 | + """, mapOf()) { |
| 136 | + assertSentimentScoreResult(it) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private fun assertStreamWithScoreResult(it: Result) { |
| 141 | + val asSequence = it.asSequence().toList() |
| 142 | + assertTrue(asSequence.isNotEmpty()) |
| 143 | + |
| 144 | + asSequence.forEach { |
| 145 | + val entity: Map<String, Any> = it["result"] as Map<String, Any> |
| 146 | + assertTrue(entity.containsKey("score")) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private fun assertGraphResult(it: Result) { |
| 151 | + val asSequence = it.asSequence().toList() |
| 152 | + assertTrue(asSequence.isNotEmpty()) |
| 153 | + |
| 154 | + asSequence.forEach { |
| 155 | + val entity: Map<String, Any> = it["g"] as Map<String, Any> |
| 156 | + assertTrue(entity.containsKey("nodes")) |
| 157 | + assertTrue(entity.containsKey("relationships")) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private fun assertSentimentScoreResult(it: Result) { |
| 162 | + val asSequence = it.asSequence().toList() |
| 163 | + assertTrue(asSequence.isNotEmpty()) |
| 164 | + |
| 165 | + asSequence.forEach { |
| 166 | + val entity: Map<String, Any> = it["result"] as Map<String, Any> |
| 167 | + assertTrue(entity.containsKey("sentimentScore")) |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
0 commit comments