<commit_before>package controllers.graphql.counter

import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.model.MediaTypes.`application/json`
import akka.http.scaladsl.model.StatusCodes.OK
import akka.util.ByteString
import controllers.graphql.TestHelper
import controllers.graphql.jsonProtocols.GraphQLMessage
import controllers.graphql.jsonProtocols.GraphQLMessageJsonProtocol._
import models.counter.Counter
import spray.json._

class QuerySpec extends TestHelper {

  val addServerCounterMutation = "mutation Increment { addServerCounter(amount: 1) { amount } }"
  val addServerCounterGraphQLMessage = ByteString(GraphQLMessage(addServerCounterMutation).toJson.compactPrint)
  val addServerCounterEntity = HttpEntity(`application/json`, addServerCounterGraphQLMessage)

  val serverCounter = "query IncrementAndGet { serverCounter { amount } }"
  val serverCounterGraphQLMessage = ByteString(GraphQLMessage(serverCounter).toJson.compactPrint)
  val serverCounterEntity = HttpEntity(`application/json`, serverCounterGraphQLMessage)

  "GraphQLController" must {

    implicit val counterJsonReader = CounterJsonReader

    "increment amount of counter" in {
      Post("/graphql", addServerCounterEntity) ~> routes ~> check {
        val counter = responseAs[String].parseJson.convertTo[Counter]

        status shouldBe OK
        contentType.mediaType shouldBe `application/json`
        counter.amount shouldBe 1
      }
    }

    "increment several times and then get an amount of counter" in {
      Post("/graphql", addServerCounterEntity) ~> routes ~> check {

        Post("/graphql", addServerCounterEntity) ~> routes ~> check {
          val counter = responseAs[String].parseJson.convertTo[Counter]

          status shouldBe OK
          contentType.mediaType shouldBe `application/json`
          counter.amount shouldBe 2
        }

        Post("/graphql", serverCounterEntity) ~> routes ~> check {
          val counter = responseAs[String].parseJson.convertTo[Counter]

          status shouldBe OK
          contentType.mediaType shouldBe `application/json`
          counter.amount shouldBe 2
        }
      }
    }
  }
}

object CounterJsonReader extends JsonReader[Counter] with DefaultJsonProtocol {
  override def read(json: JsValue): Counter = {
    val data = json.asJsObject.fields("data").asJsObject
    data.fields.head._2.convertTo[Counter](jsonFormat1(Counter.apply))
  }
}<commit_msg>Create a test for batch query execution<commit_after>package controllers.graphql.counter

import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.model.MediaTypes.`application/json`
import akka.http.scaladsl.model.StatusCodes.OK
import akka.util.ByteString
import controllers.graphql.TestHelper
import controllers.graphql.jsonProtocols.GraphQLMessage
import controllers.graphql.jsonProtocols.GraphQLMessageJsonProtocol._
import models.counter.Counter
import spray.json._

class QuerySpec extends TestHelper {

  val addServerCounterMutation = "mutation Increment { addServerCounter(amount: 1) { amount } }"
  val addServerCounterGraphQLMessage = ByteString(GraphQLMessage(addServerCounterMutation).toJson.compactPrint)
  val addServerCounterEntity = HttpEntity(`application/json`, addServerCounterGraphQLMessage)

  val serverCounter = "query IncrementAndGet { serverCounter { amount } }"
  val serverCounterGraphQLMessage = ByteString(GraphQLMessage(serverCounter).toJson.compactPrint)
  val serverCounterEntity = HttpEntity(`application/json`, serverCounterGraphQLMessage)

  val batchQueries = Seq(GraphQLMessage(addServerCounterMutation, Some("Increment")), GraphQLMessage(serverCounter, Some("IncrementAndGet")))
  val batchEntity = HttpEntity(`application/json`, batchQueries.toJson.compactPrint)
  "GraphQLController" must {

    implicit val counterJsonReader = CounterJsonReader

    "increment amount of counter" in {
      Post(endpoint, addServerCounterEntity) ~> routes ~> check {
        val counter = responseAs[String].parseJson.convertTo[Counter]

        status shouldBe OK
        contentType.mediaType shouldBe `application/json`
        counter.amount shouldBe 1
      }
    }

    "increment several times and then get an amount of counter" in {
      Post(endpoint, addServerCounterEntity) ~> routes ~> check {

        Post(endpoint, addServerCounterEntity) ~> routes ~> check {
          val counter = responseAs[String].parseJson.convertTo[Counter]

          status shouldBe OK
          contentType.mediaType shouldBe `application/json`
          counter.amount shouldBe 2
        }

        Post(endpoint, serverCounterEntity) ~> routes ~> check {
          val counter = responseAs[String].parseJson.convertTo[Counter]

          status shouldBe OK
          contentType.mediaType shouldBe `application/json`
          counter.amount shouldBe 2
        }
      }
    }

    "execute batch query" in {
      Post(endpoint, batchEntity) ~> routes ~> check {
        status shouldBe OK
        contentType.mediaType shouldBe `application/json`
      }
    }
  }
}

object CounterJsonReader extends JsonReader[Counter] with DefaultJsonProtocol {
  override def read(json: JsValue): Counter = {
    val data = json.asJsObject.fields("data").asJsObject
    data.fields.head._2.convertTo[Counter](jsonFormat1(Counter.apply))
  }
}
{'fraction_non_alphanumeric': 0.08564421807206188, 'fraction_numerical': 0.0022637238256932655, 'mean_word_length': 4.073684210526316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3312866', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1412, 'n_words': 310}