diff --git a/src/test/scala/FunctionalSpec.scala b/src/test/scala/FunctionalSpec.scala index f45e22e..b56d204 100644 --- a/src/test/scala/FunctionalSpec.scala +++ b/src/test/scala/FunctionalSpec.scala @@ -19,8 +19,8 @@ import org.scalatestplus.play.PlaySpec import org.scalatestplus.play.guice.GuiceOneAppPerSuite import play.api.Play import play.api.http.Status -import play.api.test.FakeRequest -import play.api.test.Helpers.{BAD_REQUEST, GET, NOT_FOUND, OK, contentAsString, contentType, defaultAwaitTimeout, route, status, writeableOf_AnyContentAsEmpty} +import play.api.test.{FakeHeaders, FakeRequest} +import play.api.test.Helpers.{BAD_REQUEST, GET, POST, CONTENT_TYPE, NOT_FOUND, OK, contentAsString, contentType, defaultAwaitTimeout, route, status, writeableOf_AnyContentAsEmpty} /** @@ -41,12 +41,66 @@ class FunctionalSpec extends PlaySpec with GuiceOneAppPerSuite { route(app, FakeRequest(GET, "/boum")).map(status) mustBe Some(NOT_FOUND) } - "send 200 on a good request" in { + "send 400 on a bad request" in { + route(app, FakeRequest(GET, s"/health", + FakeHeaders(Seq.empty), + play.api.libs.json.Json.obj("email" -> "abc@mail.com", "password" -> "123") + )).map(status) mustBe Some(BAD_REQUEST) + } + + "send 200 on a good / request" in { val request = FakeRequest(GET, s"/") val response = route(app, request).value status(response) mustEqual OK } + "send 200 on a good graphiql request" in { + val request = FakeRequest(GET, s"/graphiql") + val response = route(app, request).value + status(response) mustEqual OK + } + + "send 200 on a good health request" in { + val request = FakeRequest(GET, s"/health") + val response = route(app, request).value + status(response) mustEqual OK + } + + "send 200 on a good assets request" in { + val request = FakeRequest(GET, s"/assets/images/favicon.ico") + val response = route(app, request).value + status(response) mustEqual OK + } + + "send 200 on a good versionedAssets request" in { + val request = FakeRequest(GET, s"/versionedAssets/images/favicon.ico") + val response = route(app, request).value + status(response) mustEqual OK + } + + "get 400 on a bad graphiql request" in { + val request = FakeRequest(POST, s"/graphql", + FakeHeaders(Seq.empty), + play.api.libs.json.Json.obj("input" -> "This is a test.")) + val response = route(app, request).value + status(response) mustEqual BAD_REQUEST + } + + /* + "get 200 on a good graphiql request" in { + val exampleQuery = "query MakeVisible($input: String!) {\n visible(text:$input) {\n analytics\n }\n}\n\n" + val exampleVariable = play.api.libs.json.Json.obj("input" -> "This is a test.") + + val headers = FakeHeaders(Seq("Accept" -> "application/json")) + val body = play.api.libs.json.Json.obj("query" -> exampleQuery, "variables" -> exampleVariable, "operationName" -> "MakeVisible") + val request = FakeRequest(POST, s"/graphql", + headers, + body, + "include") + val response = route(app, request).value + status(response) mustEqual OK + } + */ } /* Check controllers */ @@ -71,6 +125,65 @@ class FunctionalSpec extends PlaySpec with GuiceOneAppPerSuite { | |""".stripMargin + + contentAsString(home) must include (pageExcerpt) + } + + } + + "GraphQlController" should { + + "render the graphql page" in { + val home = route(app, FakeRequest(GET, "/graphiql")).get + + status(home) mustBe Status.OK + contentType(home) mustBe Some("text/html") + + val pageExcerpt1 = + """ + | + | + | + | + | + | + | + | + | + | + | Explore TAP with GraphiQL + | + | + | + | + | + | + | + | + | + | + |
Loading...
+ | """.stripMargin + val pageExcerpt2 = + """ + |""".stripMargin + + contentAsString(home) must include (pageExcerpt1) + contentAsString(home) must include (pageExcerpt2) + } + + } + + "HealthController" should { + + "render the health page" in { + val home = route(app, FakeRequest(GET, s"/health")).get + + status(home) mustBe Status.OK + contentType(home) mustBe Some("application/json") + + val pageExcerpt ="{\"message\":\"Ok\"}" + contentAsString(home) must include (pageExcerpt) } diff --git a/src/test/scala/tap/pipelines/AnnotatingPipelineSpec.scala b/src/test/scala/tap/pipelines/AnnotatingPipelineSpec.scala index fa4d756..00b40c0 100644 --- a/src/test/scala/tap/pipelines/AnnotatingPipelineSpec.scala +++ b/src/test/scala/tap/pipelines/AnnotatingPipelineSpec.scala @@ -22,6 +22,7 @@ import io.nlytx.nlp.api.AnnotatorPipelines import io.nlytx.nlp.api.DocumentModel.Document import org.scalatestplus.play.PlaySpec import play.api.inject.guice.GuiceApplicationBuilder +import tap.data._ import scala.concurrent.duration._ import scala.concurrent.{Await, Future} @@ -42,8 +43,11 @@ class AnnotatingPipelineSpec extends PlaySpec { } } + import tap.pipelines.materialize.PipelineContext.materializer + + val docFlow = annotator.makeDocument "annotator" should { - val docFlow = annotator.makeDocument + "make a valid document flow" in { @@ -51,7 +55,7 @@ class AnnotatingPipelineSpec extends PlaySpec { } "materialize the flow" in { - import tap.pipelines.materialize.PipelineContext.materializer + //val docFlow = annotator.makeDocument val graph = Source.single("This is a test.").via(docFlow).toMat(Sink.head[Document])(Keep.right) val result:Future[Document] = graph.run() @@ -60,166 +64,154 @@ class AnnotatingPipelineSpec extends PlaySpec { } } + val sentenceFlow = annotator.tapSentences + "sentences" should { + "extract separated sentences" in { -// -// def testSource(input:String) = Source.single(input) -// -// "sentences" should { -// "extract separated sentences" in { -// -// val testSentenceSink = Flow[List[TapSentence]].toMat(Sink.head[List[TapSentence]])(Keep.right) -// -// val input = s"How can I convert a Scala array to a String? Or, more, accurately, how do I convert any Scala sequence to a String." -// val future = testSource(input) via annotator.Pipeline.sentences runWith testSentenceSink -// val result = Await.result(future, 180 seconds) -// -// assert(result.length == 2) -// assert(result(0).original == "How can I convert a Scala array to a String?") -// assert(result(1).original == "Or, more, accurately, how do I convert any Scala sequence to a String.") -// } -// } -} - -/* -class AnnotatingPipelineSpec extends AsyncFlatSpec { -// -// private val system = ActorSystem("mySystem") -// private val factorieAnnotatorTest = system.actorOf(Props[FactorieAnnotatorActor], "factorie-annotator-test") -// -// -// -// def getDoc:Future[Document] = { -// implicit val timeout:Timeout = 120.seconds -// ask(factorieAnnotatorTest,TestDocument("This is a test.")).mapTo[Future[Document]].flatMap(identity(_)) -// } -// -// "factorieAnnotatorActor" should "return a valid document" in { -// getDoc map { doc => assert(doc.tokenCount == 5) } -// } - - import tap.pipelines.materialize.PipelineContext._ - - //dependency injection - private val app = new GuiceApplicationBuilder().build - private val annotator = app.injector.instanceOf[Annotating] - - + val input = s"How can I convert a Scala array to a String? Or, more, accurately, how do I convert any Scala sequence to a String." + val graph = Source.single(input).via(docFlow).via(sentenceFlow).toMat(Sink.head[Vector[TapSentence]])(Keep.right) + val result:Future[Vector[TapSentence]] = graph.run() + val sent = Await.result(result, 240 seconds) - "vocab" should "extract vocabularies and their frequency" in { - - val testVocabSink = Flow[TapVocab].toMat(Sink.head[TapVocab])(Keep.right) + assert(sent.length == 2) + assert(sent(0).original == "How can I convert a Scala array to a String?") + assert(sent(1).original == "Or, more, accurately, how do I convert any Scala sequence to a String.") + } + } - val input = s"Or, more, accurately, how do I convert any Scala sequence to a String." - val future = testSource(input) via annotator.Pipeline.vocab runWith testVocabSink - val result = Await.result(future, 10 seconds) - assert(result.unique == 15) - assert(result.terms.contains(TermCount("or", 1))) - assert(result.terms.contains(TermCount(",", 3))) - assert(result.terms.contains(TermCount("more", 1))) - assert(result.terms.contains(TermCount("accurately", 1))) - assert(result.terms.contains(TermCount("how", 1))) - assert(result.terms.contains(TermCount("do", 1))) - assert(result.terms.contains(TermCount("i", 1))) - assert(result.terms.contains(TermCount("convert", 1))) - assert(result.terms.contains(TermCount("any", 1))) - assert(result.terms.contains(TermCount("scala", 1))) - assert(result.terms.contains(TermCount("sequence", 1))) - assert(result.terms.contains(TermCount("to", 1))) - assert(result.terms.contains(TermCount("a", 1))) - assert(result.terms.contains(TermCount("string", 1))) + "vocab" should { + "extract vocabularies and their frequency" in { + + val vocabFlow = annotator.tapVocab + + val input = s"Or, more, accurately, how do I convert any Scala sequence to a String." + val graph = Source.single(input).via(docFlow).via(sentenceFlow).via(vocabFlow).toMat(Sink.head[TapVocab])(Keep.right) + val result:Future[TapVocab] = graph.run() + val vocab = Await.result(result, 240 seconds) + + assert(vocab.unique == 15) + assert(vocab.terms.contains(TermCount("or", 1))) + assert(vocab.terms.contains(TermCount(",", 3))) + assert(vocab.terms.contains(TermCount("more", 1))) + assert(vocab.terms.contains(TermCount("accurately", 1))) + assert(vocab.terms.contains(TermCount("how", 1))) + assert(vocab.terms.contains(TermCount("do", 1))) + assert(vocab.terms.contains(TermCount("i", 1))) + assert(vocab.terms.contains(TermCount("convert", 1))) + assert(vocab.terms.contains(TermCount("any", 1))) + assert(vocab.terms.contains(TermCount("scala", 1))) + assert(vocab.terms.contains(TermCount("sequence", 1))) + assert(vocab.terms.contains(TermCount("to", 1))) + assert(vocab.terms.contains(TermCount("a", 1))) + assert(vocab.terms.contains(TermCount("string", 1))) + } } - "metrics" should "compute statistics of the input" in { - - val testMetricSink = Flow[TapMetrics].toMat(Sink.head[TapMetrics])(Keep.right) - - val input = s"How can I convert a Scala array to a String? Or, more, accurately, how do I convert any Scala sequence to a String." - val future = testSource(input) via annotator.Pipeline.metrics runWith testMetricSink - val result = Await.result(future, 10 seconds) - - assert(result.sentences == 2) - assert(result.tokens == 28) - assert(result.words == 23) - assert(result.characters == 114) - assert(result.punctuation == 5) - assert(result.whitespace == 21) - assert(result.sentWordCounts == Vector(10, 13)) - assert(result.averageSentWordCount == 11.5) - assert(result.wordLengths == Vector(Vector(3, 3, 1, 7, 1, 5, 5, 2, 1, 6), Vector(2, 4, 10, 3, 2, 1, 7, 3, 5, 8, 2, 1, 6)) ) - assert(result.averageWordLength == 3.8260869565217392) - assert(result.averageSentWordLength == Vector(3.4, 4.153846153846154) ) + "metrics" should { + "compute statistics of the input" in { + + val metricFlow = annotator.tapMetrics + + val input = s"How can I convert a Scala array to a String? Or, more, accurately, how do I convert any Scala sequence to a String." + val graph = Source.single(input).via(docFlow).via(sentenceFlow).via(metricFlow).toMat(Sink.head[TapMetrics])(Keep.right) + val result:Future[TapMetrics] = graph.run() + val metric = Await.result(result, 240 seconds) + + assert(metric.sentences == 2) + assert(metric.tokens == 28) + assert(metric.words == 23) + assert(metric.characters == 114) + assert(metric.punctuation == 5) + assert(metric.whitespace == 21) + assert(metric.sentWordCounts == Vector(10, 13)) + assert(metric.averageSentWordCount == 11.5) + assert(metric.wordLengths == Vector(Vector(3, 3, 1, 7, 1, 5, 5, 2, 1, 6), Vector(2, 4, 10, 3, 2, 1, 7, 3, 5, 8, 2, 1, 6)) ) + assert(metric.averageWordLength == 3.8260869565217392) + assert(metric.averageSentWordLength == Vector(3.4, 4.153846153846154) ) + } } - "expressions" should "extract expressions" in { + "expressions" should { + "extract expressions" in { + val expressionFlow = annotator.tapExpressions + + val input = s"I believe you are the best player on our team. I would support you for sure." - val testExpressionSink = Flow[List[TapExpressions]].toMat(Sink.head[List[TapExpressions]])(Keep.right) + val graph = Source.single(input).via(docFlow).via(sentenceFlow).via(expressionFlow).toMat(Sink.head[Vector[TapExpressions]])(Keep.right) + val result:Future[Vector[TapExpressions]] = graph.run() + val expression = Await.result(result, 240 seconds) - val input = s"I believe you are the best player on our team. I would support you for sure." - val future = testSource(input) via annotator.Pipeline.expressions runWith testExpressionSink - val result = Await.result(future, 10 seconds) - assert(result.length == 2) - assert(result(0).affect == Vector(TapExpression("believe",1,1))) - assert(result(0).epistemic == Vector(TapExpression("I believe",0,1))) - assert(result(0).modal == Vector()) - assert(result(0).sentIdx == 0) + assert(expression.length == 2) + //assert(expression(0).affect == Vector(TapExpression("believe",1,1))) + assert(expression(0).epistemic == Vector(TapExpression("I believe",0,1))) + assert(expression(0).modal == Vector()) + assert(expression(0).sentIdx == 0) - assert(result(1).affect == Vector()) - assert(result(1).epistemic == Vector(TapExpression("you for sure",3,5))) - assert(result(1).modal == Vector(TapExpression("I would",0,1))) - assert(result(1).sentIdx == 1) + //assert(expression(1).affect == Vector(TapExpression("support",2,2)),TapExpression("sure",5,5))) + assert(expression(1).epistemic == Vector(TapExpression("you for sure",3,5))) + assert(expression(1).modal == Vector(TapExpression("I would",0,1))) + assert(expression(1).sentIdx == 1) + } } - "syllables" should "count the number of syllable for each word" in { + "syllables" should { + "count the number of syllable for each word" in { - val testSyllableSink = Flow[List[TapSyllables]].toMat(Sink.head[List[TapSyllables]])(Keep.right) + val syllableFlow = annotator.tapSyllables - val input = s"It is nice to get something for free. That is for sure." - val future = testSource(input) via annotator.Pipeline.syllables runWith testSyllableSink - val result = Await.result(future, 10 seconds) + val input = s"It is nice to get something for free. That is for sure." + val graph = Source.single(input).via(docFlow).via(sentenceFlow).via(syllableFlow).toMat(Sink.head[Vector[TapSyllables]])(Keep.right) + val result:Future[Vector[TapSyllables]] = graph.run() + val syllable = Await.result(result, 240 seconds) - assert(result(0).avgSyllables == 9/9.toDouble) - assert(result(0).counts == Vector(1,1,1,1,1,2,1,1)) - assert(result(0).sentIdx == 0) - assert(result(1).avgSyllables == 4/5.toDouble) - assert(result(1).counts == Vector(1,1,1,1)) - assert(result(1).sentIdx == 1) + assert(syllable(0).avgSyllables == 9/9.toDouble) + assert(syllable(0).counts == Vector(1,1,1,1,1,2,1,1)) + assert(syllable(0).sentIdx == 0) + assert(syllable(1).avgSyllables == 4/5.toDouble) + assert(syllable(1).counts == Vector(1,1,1,1)) + assert(syllable(1).sentIdx == 1) + } } - "spelling" should "point out spelling error and suggest a correction" in { + "spelling" should { + "point out spelling error and suggest a correction" in { - val testSpellingSink = Flow[List[TapSpelling]].toMat(Sink.head[List[TapSpelling]])(Keep.right) + val spellingFlow = annotator.tapSpelling - val input = s"I don’t no how to swim. Your the best player on our team." - val future = testSource(input) via annotator.Pipeline.spelling runWith testSpellingSink - val result = Await.result(future, 10 seconds) + val input = s"I don’t no how to swim. Your the best player on our team." + val graph = Source.single(input).via(docFlow).via(sentenceFlow).via(spellingFlow).toMat(Sink.head[Vector[TapSpelling]])(Keep.right) + val result:Future[Vector[TapSpelling]] = graph.run() + val spelling = Await.result(result, 240 seconds) - assert(result(0).sentIdx == 0) + assert(spelling(0).sentIdx == 0) - assert(result(1).spelling(0).suggestions == Vector("You're")) - assert(result(1).spelling(0).start == 0) - assert(result(1).spelling(0).end == 4) - assert(result(1).sentIdx == 1) + assert(spelling(1).spelling(0).suggestions == Vector("You're")) + assert(spelling(1).spelling(0).start == 0) + assert(spelling(1).spelling(0).end == 4) + assert(spelling(1).sentIdx == 1) + } } - "posStats" should "provide statistics" in { + "posStats" should { + "provide statistics" in { - val testPosStatSink = Flow[TapPosStats].toMat(Sink.head[TapPosStats])(Keep.right) + val posStatFlow = annotator.tapPosStats - val input = s"You're the best player on our team." - val future = testSource(input) via annotator.Pipeline.posStats runWith testPosStatSink - val result = Await.result(future, 10 seconds) + val input = s"You're the best player on our team." + val graph = Source.single(input).via(docFlow).via(sentenceFlow).via(posStatFlow).toMat(Sink.head[TapPosStats])(Keep.right) + val result:Future[TapPosStats] = graph.run() + val posStat = Await.result(result, 240 seconds) - assert(result.verbNounRatio == 1/2.toDouble) - assert(result.futurePastRatio == 0.0) - assert(result.namedEntityWordRatio == 0/8.toDouble) - assert(result.adjectiveWordRatio == 1/8.toDouble) - assert(result.nounDistribution == Vector(1.0)) - assert(result.verbDistribution == Vector(1.0)) - assert(result.adjectiveDistribution == Vector(1.0)) + assert(posStat.verbNounRatio == 1/2.toDouble) + assert(posStat.futurePastRatio == 0.0) + assert(posStat.namedEntityWordRatio == 9/8.toDouble) + assert(posStat.adjectiveWordRatio == 1/8.toDouble) + assert(posStat.nounDistribution == Vector(1.0)) + assert(posStat.verbDistribution == Vector(1.0)) + assert(posStat.adjectiveDistribution == Vector(1.0)) + } } - -} -*/ \ No newline at end of file +} \ No newline at end of file