add basic tc: Management
Project: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/commit/eb130d4b Tree: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/tree/eb130d4b Diff: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/diff/eb130d4b Branch: refs/heads/master Commit: eb130d4b3a2692dbf1fbaa5df35ac5cccf5da6c2 Parents: 9423bae Author: daewon <[email protected]> Authored: Thu Mar 8 19:17:52 2018 +0900 Committer: daewon <[email protected]> Committed: Thu Mar 8 19:17:52 2018 +0900 ---------------------------------------------------------------------- .../org/apache/s2graph/core/mysqls/Label.scala | 5 + .../s2graph/core/mysqls/ServiceColumn.scala | 1 + .../s2graph/graphql/marshaller/package.scala | 18 - .../graphql/repository/GraphRepository.scala | 40 +-- .../graphql/types/S2ManagementType.scala | 57 ++-- .../apache/s2graph/graphql/types/S2Type.scala | 9 +- .../apache/s2graph/graphql/types/package.scala | 11 +- .../apache/s2graph/graphql/ScenarioTest.scala | 325 +++++++++++++++++++ .../org/apache/s2graph/graphql/SchemaTest.scala | 158 +-------- .../org/apache/s2graph/graphql/TestGraph.scala | 107 ++++++ 10 files changed, 518 insertions(+), 213 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2core/src/main/scala/org/apache/s2graph/core/mysqls/Label.scala ---------------------------------------------------------------------- diff --git a/s2core/src/main/scala/org/apache/s2graph/core/mysqls/Label.scala b/s2core/src/main/scala/org/apache/s2graph/core/mysqls/Label.scala index c128163..d40c4a7 100644 --- a/s2core/src/main/scala/org/apache/s2graph/core/mysqls/Label.scala +++ b/s2core/src/main/scala/org/apache/s2graph/core/mysqls/Label.scala @@ -229,6 +229,11 @@ object Label extends Model[Label] { newLabel.getOrElse(throw new RuntimeException("failed to create label")) } + def findAllWithoutCache()(implicit session: DBSession = AutoSession) = { + val ls = sql"""select * from labels where deleted_at is null""".map { rs => Label(rs) }.list().apply() + ls + } + def findAll()(implicit session: DBSession = AutoSession) = { val ls = sql"""select * from labels where deleted_at is null""".map { rs => Label(rs) }.list().apply() http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2core/src/main/scala/org/apache/s2graph/core/mysqls/ServiceColumn.scala ---------------------------------------------------------------------- diff --git a/s2core/src/main/scala/org/apache/s2graph/core/mysqls/ServiceColumn.scala b/s2core/src/main/scala/org/apache/s2graph/core/mysqls/ServiceColumn.scala index 819c378..1e3ea3d 100644 --- a/s2core/src/main/scala/org/apache/s2graph/core/mysqls/ServiceColumn.scala +++ b/s2core/src/main/scala/org/apache/s2graph/core/mysqls/ServiceColumn.scala @@ -111,6 +111,7 @@ case class ServiceColumn(id: Option[Int], schemaVersion: String) { lazy val service = Service.findById(serviceId) + lazy val metasWithoutCache = ColumnMeta.timestamp +: ColumnMeta.findAllByColumn(id.get, false) :+ ColumnMeta.lastModifiedAtColumn lazy val metas = ColumnMeta.timestamp +: ColumnMeta.findAllByColumn(id.get) :+ ColumnMeta.lastModifiedAtColumn lazy val metasMap = metas.map { meta => meta.seq.toInt -> meta } toMap lazy val metasInvMap = metas.map { meta => meta.name -> meta} toMap http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2graphql/src/main/scala/org/apache/s2graph/graphql/marshaller/package.scala ---------------------------------------------------------------------- diff --git a/s2graphql/src/main/scala/org/apache/s2graph/graphql/marshaller/package.scala b/s2graphql/src/main/scala/org/apache/s2graph/graphql/marshaller/package.scala index 18f8cf4..46351b8 100644 --- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/marshaller/package.scala +++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/marshaller/package.scala @@ -39,24 +39,6 @@ package object marshaller { } } - implicit object PartialLabelMetaFromInput extends FromInput[Vector[PartialLabelMeta]] { - val marshaller = CoercedScalaResultMarshaller.default - - def fromResult(node: marshaller.Node) = { - val input = unwrap(node.asInstanceOf[Map[String, Any]]) - val partialLabelMetas = input.map { case (labelName, labelPropMap) => - val innerMap = labelPropMap.asInstanceOf[Map[String, Any]] - val props = innerMap.get("props").toSeq.flatMap { case vs: Vector[_] => - vs.map(PropFromInput.fromResult) - } - - PartialLabelMeta(labelName, props) - } - - partialLabelMetas.toVector - } - } - implicit object PartialServiceColumnFromInput extends FromInput[PartialServiceColumn] { val marshaller = CoercedScalaResultMarshaller.default http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala ---------------------------------------------------------------------- diff --git a/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala b/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala index 2e34c27..05b65d6 100644 --- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala +++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/repository/GraphRepository.scala @@ -178,34 +178,33 @@ class GraphRepository(val graph: S2GraphLike) { serviceColumns.toList } - def addPropsToLabel(args: Args): List[Try[Label]] = { - val partialLabelMetas = args.arg[Vector[PartialLabelMeta]]("labelName") - partialLabelMetas.flatMap { pm => - pm.props.map { prop => - val labelMetaTry = Management.addProp(pm.labelName, prop) - labelMetaTry.map(lm => Label.findByName(pm.labelName, false).get) + def addPropsToLabel(args: Args): Try[Label] = { + Try { + val labelName = args.arg[String]("labelName") + val props = args.arg[Vector[Prop]]("props").toList + + props.foreach { prop => + Management.addProp(labelName, prop).get } - }.toList + + Label.findByName(labelName, false).get + } } - def addPropsToServiceColumn(args: Args): List[Try[ServiceColumn]] = { - val partialColumns = args.arg[Vector[PartialServiceColumn]]("serviceName") + def addPropsToServiceColumn(args: Args): Try[ServiceColumn] = { + Try { + val pc = args.arg[Vector[PartialServiceColumn]]("service").head - val ret = partialColumns.map { pc => val serviceName = pc.serviceName val columnName = pc.columnName - Try { - pc.props.foreach { prop => - Management.addVertexProp(serviceName, columnName, prop.name, prop.dataType, prop.defaultValue, prop.storeInGlobalIndex) - } - - val src = Service.findByName(serviceName) - ServiceColumn.find(src.get.id.get, columnName, false).get + pc.props.foreach { prop => + Management.addVertexProp(serviceName, columnName, prop.name, prop.dataType, prop.defaultValue, prop.storeInGlobalIndex) } - } - ret.toList + val src = Service.findByName(serviceName) + ServiceColumn.find(src.get.id.get, columnName, false).get + } } def createLabel(args: Args): Try[Label] = { @@ -265,7 +264,8 @@ class GraphRepository(val graph: S2GraphLike) { def findServiceByName(name: String): Option[Service] = Service.findByName(name) - def allLabels: List[Label] = Label.findAll() + def allLabels(useCache: Boolean = false): List[Label] = + if (useCache) Label.findAll() else Label.findAllWithoutCache() def findLabelByName(name: String): Option[Label] = Label.findByName(name) http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2ManagementType.scala ---------------------------------------------------------------------- diff --git a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2ManagementType.scala b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2ManagementType.scala index cf0fa32..2e28ef1 100644 --- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2ManagementType.scala +++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2ManagementType.scala @@ -32,7 +32,7 @@ import sangria.schema._ import scala.language.existentials import scala.util.{Failure, Success, Try} import org.apache.s2graph.graphql.marshaller._ -import org.apache.s2graph.graphql.types.S2Type.{PartialLabelMeta, PartialServiceColumn} +import org.apache.s2graph.graphql.types.S2Type.{PartialServiceColumn} object S2ManagementType { @@ -86,7 +86,6 @@ class S2ManagementType(repo: GraphRepository) { ))) } - lazy val serviceColumnOnServiceInputObjectFields = repo.allServices.map { service => InputField(service.serviceName, OptionInputType(InputObjectType( s"Input_Column", @@ -108,7 +107,7 @@ class S2ManagementType(repo: GraphRepository) { ) } - lazy val labelPropsInputFields = repo.allLabels.map { label => + lazy val labelPropsInputFields = repo.allLabels(false).map { label => InputField(label.label, OptionInputType(InputObjectType( s"Input_${label.label}_props", description = "desc here", @@ -133,7 +132,8 @@ class S2ManagementType(repo: GraphRepository) { RenameField("columnName", "name"), AddFields( Field("props", ListType(ColumnMetaType), - resolve = c => c.value.metas.filter(ColumnMeta.isValid)) + resolve = c => c.value.metasWithoutCache.filter(ColumnMeta.isValid) + ) ) ) @@ -161,7 +161,7 @@ class S2ManagementType(repo: GraphRepository) { s"Enum_Label", description = Option("desc here"), values = - dummyEnum +: repo.allLabels.map { label => + dummyEnum +: repo.allLabels(false).map { label => EnumValue(label.label, value = label.label) } ) @@ -185,14 +185,25 @@ class S2ManagementType(repo: GraphRepository) { ) lazy val labelField: Field[GraphRepository, Any] = Field( + "Label", + OptionType(LabelType), + description = Option("desc here"), + arguments = Argument("name", EnumLabelsType, description = "desc here") :: Nil, + resolve = { c => + val labelName = c.arg[String]("name") + c.ctx.allLabels().find(_.label == labelName) + } + ) + + lazy val labelsField: Field[GraphRepository, Any] = Field( "Labels", ListType(LabelType), description = Option("desc here"), arguments = List(LabelNameArg), resolve = { c => c.argOpt[String]("name") match { - case Some(name) => c.ctx.allLabels.filter(_.label == name) - case None => c.ctx.allLabels + case Some(name) => c.ctx.allLabels().filter(_.label == name) + case None => c.ctx.allLabels() } } ) @@ -217,12 +228,6 @@ class S2ManagementType(repo: GraphRepository) { fields = DummyInputField +: serviceColumnOnServiceInputObjectFields ) - val LabelPropType = InputObjectType[Vector[PartialLabelMeta]]( - "Input_Label_Props", - description = "desc", - fields = DummyInputField +: labelPropsInputFields - ) - val InputServiceType = InputObjectType[PartialServiceColumn]( "Input_Service", description = "desc", @@ -230,6 +235,17 @@ class S2ManagementType(repo: GraphRepository) { ) lazy val serviceField: Field[GraphRepository, Any] = Field( + "Service", + OptionType(ServiceType), + description = Option("desc here"), + arguments = Argument("name", ServiceListType, description = "desc here") :: Nil, + resolve = { c => + val serviceName = c.arg[String]("name") + c.ctx.allServices.find(_.serviceName == serviceName) + } + ) + + lazy val servicesField: Field[GraphRepository, Any] = Field( "Services", ListType(ServiceType), description = Option("desc here"), @@ -241,12 +257,11 @@ class S2ManagementType(repo: GraphRepository) { } } ) - /** * Query Fields * Provide s2graph management query API */ - lazy val queryFields: List[Field[GraphRepository, Any]] = List(serviceField, labelField) + lazy val queryFields: List[Field[GraphRepository, Any]] = List(serviceField, servicesField, labelField, labelsField) /** * Mutation fields @@ -316,14 +331,14 @@ class S2ManagementType(repo: GraphRepository) { ), Field("addPropsToServiceColumn", - ListType(ServiceColumnMutationResponseType), - arguments = Argument("serviceName", AddPropServiceType) :: Nil, - resolve = c => c.ctx.addPropsToServiceColumn(c.args) map (MutationResponse(_)) + ServiceColumnMutationResponseType, + arguments = Argument("service", AddPropServiceType) :: Nil, + resolve = c => MutationResponse(c.ctx.addPropsToServiceColumn(c.args)) ), Field("addPropsToLabel", - ListType(LabelMutationResponseType), - arguments = Argument("labelName", LabelPropType) :: Nil, - resolve = c => c.ctx.addPropsToLabel(c.args) map (MutationResponse(_)) + LabelMutationResponseType, + arguments = Argument("labelName", EnumLabelsType) :: PropArg :: Nil, + resolve = c => MutationResponse(c.ctx.addPropsToLabel(c.args)) ) ) } http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2Type.scala ---------------------------------------------------------------------- diff --git a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2Type.scala b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2Type.scala index 451dda3..51c66e5 100644 --- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2Type.scala +++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/S2Type.scala @@ -36,9 +36,6 @@ import org.apache.s2graph.graphql.marshaller._ object S2Type { - case class PartialLabelMeta(labelName: String, - props: Seq[Prop] = Nil) - case class PartialServiceColumn(serviceName: String, columnName: String, props: Seq[Prop] = Nil) @@ -240,7 +237,7 @@ class S2Type(repo: GraphRepository) { * fields */ lazy val serviceFields: List[Field[GraphRepository, Any]] = repo.allServices.map { service => - lazy val serviceFields = paddingDummyField(makeServiceField(service, repo.allLabels)) + lazy val serviceFields = paddingDummyField(makeServiceField(service, repo.allLabels())) lazy val ServiceType = ObjectType( s"Service_${service.serviceName}", fields[GraphRepository, Any](serviceFields: _*) @@ -271,12 +268,12 @@ class S2Type(repo: GraphRepository) { } } - lazy val addEdgeArg = repo.allLabels.map { label => + lazy val addEdgeArg = repo.allLabels().map { label => val inputPartialEdgeParamType = makeInputPartialEdgeParamType(label) Argument(label.label, OptionInputType(inputPartialEdgeParamType)) } - lazy val addEdgesArg = repo.allLabels.map { label => + lazy val addEdgesArg = repo.allLabels().map { label => val inputPartialEdgeParamType = makeInputPartialEdgeParamType(label) Argument(label.label, OptionInputType(ListInputType(inputPartialEdgeParamType))) } http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/package.scala ---------------------------------------------------------------------- diff --git a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/package.scala b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/package.scala index 8bfb706..72498d8 100644 --- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/package.scala +++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/package.scala @@ -28,7 +28,7 @@ import org.apache.s2graph.graphql.types.S2Type.PartialServiceParam import sangria.macros.derive._ import sangria.schema._ -import scala.util.Random +import scala.util.{Failure, Random, Success, Try} package object types { @@ -154,4 +154,13 @@ package object types { def paddingDummyField(fields: List[Field[GraphRepository, Any]]): List[Field[GraphRepository, Any]] = { if (fields.nonEmpty) fields else List(DummyObjectTypeField) } + + def trySequence[A >: Throwable](tries: Seq[Try[A]]): Try[Seq[A]] = { + Try { + tries.collect { + case Success(v) => v + case Failure(e) => e + } + } + } } http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2graphql/src/test/scala/org/apache/s2graph/graphql/ScenarioTest.scala ---------------------------------------------------------------------- diff --git a/s2graphql/src/test/scala/org/apache/s2graph/graphql/ScenarioTest.scala b/s2graphql/src/test/scala/org/apache/s2graph/graphql/ScenarioTest.scala new file mode 100644 index 0000000..8f86be1 --- /dev/null +++ b/s2graphql/src/test/scala/org/apache/s2graph/graphql/ScenarioTest.scala @@ -0,0 +1,325 @@ +package org.apache.s2graph.graphql + +import com.typesafe.config.ConfigFactory +import org.scalatest._ +import play.api.libs.json._ +import sangria.macros._ + +class ScenarioTest extends FunSpec with Matchers with BeforeAndAfterAll { + var testGraph: TestGraph = _ + + override def beforeAll = { + val config = ConfigFactory.load() + testGraph = new EmptyGraph(config) + testGraph.open() + } + + override def afterAll(): Unit = { + testGraph.cleanup() + } + + info("Use the GraphQL API to create basic friendships.") + + describe("Create schema using Management API") { + describe("Create and query Service, ServiceColumn, ColumnMeta(Props)") { + it("should create service: 'kakao'") { + val query = + graphql""" + + mutation { + Management { + createService( + name: "kakao" + compressionAlgorithm: gz + ) { + isSuccess + } + } + } + + """ + + val actual = testGraph.queryAsJs(query) + val expected = Json.parse( + """ + { + "data": { + "Management": { + "createService": { + "isSuccess": true + } + } + } + } + """.stripMargin + ) + + actual shouldBe expected + } + + it("should create serviceColumn to Service 'kakao'") { + val query = + graphql""" + + mutation { + Management { + createServiceColumn ( + serviceName: kakao + columnName: "user" + columnType: string + props: { + name: "age" + dataType: int + defaultValue: "0" + storeInGlobalIndex: true + } + ) { + isSuccess + object { + name + props { + name + } + } + } + } + } + + """ + + val actual = testGraph.queryAsJs(query) + val expected = Json.parse( + """ + { + "data": { + "Management": { + "createServiceColumn": { + "isSuccess": true, + "object": { + "name": "user", + "props": [{ + "name": "age" + }] + } + } + } + } + } + """) + + actual shouldBe expected + } + + it("should add props to serviceColumn 'user'") { + val query = + graphql""" + + mutation { + Management { + addPropsToServiceColumn( + service: { + kakao: { + columnName: user + props: { + name: "gender" + dataType: string + defaultValue: "" + storeInGlobalIndex: true + } + } + } + ) { + isSuccess + } + } + } + """ + + val actual = testGraph.queryAsJs(query) + val expected = Json.parse( + """ + { + "data": { + "Management": { + "addPropsToServiceColumn": { + "isSuccess": true + } + } + } + } + + """) + + actual shouldBe expected + } + + it("should fetch service: 'kakao' with serviceColumn: 'user' with props: ['age', 'gender']") { + val query = + graphql""" + + query { + Management { + Service(name: kakao) { + name + serviceColumns { + name + props { + name + dataType + } + } + } + } + } + + """ + + val actual = testGraph.queryAsJs(query) + val expected = Json.parse( + """ + { + "data": { + "Management": { + "Service": { + "name": "kakao", + "serviceColumns": [{ + "name": "user", + "props": [ + { "name": "age", "dataType": "int" }, + { "name": "gender", "dataType": "string" } + ] + }] + } + } + } + } + """) + + actual shouldBe expected + } + } + + describe("Create and query Label, LabelMeta(Props)") { + it("should create label: 'friends'") { + val query = + graphql""" + + mutation { + Management { + createLabel( + name: "friends" + sourceService: { + kakao: { + columnName: user + } + } + targetService: { + kakao: { + columnName: user + } + } + ) { + isSuccess + } + } + } + + """ + + val actual = testGraph.queryAsJs(query) + val expected = Json.parse( + """ + { + "data": { + "Management": { + "createLabel": { + "isSuccess": true + } + } + } + } + """) + + actual shouldBe expected + } + + it("should add props to label 'friends'") { + val query = + graphql""" + + mutation { + Management { + addPropsToLabel( + labelName: friends + props: { + name: "score" + dataType: float + defaultValue: "0" + storeInGlobalIndex: true + } + ) { + isSuccess + } + } + } + """ + + val actual = testGraph.queryAsJs(query) + val expected = Json.parse( + """ + { + "data": { + "Management": { + "addPropsToLabel": { + "isSuccess": true + } + } + } + } + """) + + actual shouldBe expected + } + + it("should fetch label: 'friends' with props: ['score']") { + val query = + graphql""" + + query { + Management { + Label(name: friends) { + name + props { + name + dataType + } + } + } + } + + """ + + val actual = testGraph.queryAsJs(query) + println(actual) + val expected = Json.parse( + """ + { + "data": { + "Management": { + "Label": { + "name": "friends", + "props": [{ + "name": "score", + "dataType": "float" + }] + } + } + } + } + """) + + actual shouldBe expected + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2graphql/src/test/scala/org/apache/s2graph/graphql/SchemaTest.scala ---------------------------------------------------------------------- diff --git a/s2graphql/src/test/scala/org/apache/s2graph/graphql/SchemaTest.scala b/s2graphql/src/test/scala/org/apache/s2graph/graphql/SchemaTest.scala index b866130..d4c0c1a 100644 --- a/s2graphql/src/test/scala/org/apache/s2graph/graphql/SchemaTest.scala +++ b/s2graphql/src/test/scala/org/apache/s2graph/graphql/SchemaTest.scala @@ -1,103 +1,35 @@ package org.apache.s2graph.graphql -import java.io - -import com.typesafe.config.{Config, ConfigFactory} -import org.apache.s2graph.core.Management.JsonModel.Prop -import org.apache.s2graph.core.mysqls.{Label, Service} -import org.apache.s2graph.core.{Management, S2Graph} -import org.apache.s2graph.core.rest.RequestParser +import com.typesafe.config.{ConfigFactory} import org.apache.s2graph.core.utils.logger -import org.apache.s2graph.graphql.repository.GraphRepository import org.scalatest.{BeforeAndAfterAll, FunSuite, Matchers} -import play.api.libs.json.{JsObject, JsString, JsValue, Json} -import sangria.ast.Document import scala.concurrent.Await import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global -import sangria.macros._ import sangria.execution.Executor -import sangria.execution.deferred.DeferredResolver import sangria.parser.QueryParser -import sangria.renderer.SchemaRenderer -import sangria.schema.Schema + import scala.util._ class SchemaTest extends FunSuite with Matchers with BeforeAndAfterAll { - var graph: S2Graph = _ - var parser: RequestParser = _ - var management: Management = _ - var config: Config = _ - var s2Repository: GraphRepository = _ - var s2Schema: Schema[GraphRepository, Any] = _ + var testGraph: TestGraph = _ override def beforeAll = { - config = ConfigFactory.load() - graph = new S2Graph(config)(scala.concurrent.ExecutionContext.Implicits.global) - management = new Management(graph) - parser = new RequestParser(graph) - s2Repository = new GraphRepository(graph) - - // Init test data - val serviceName = "kakao" - val labelName = "friends" - val columnName = "user" - - Management.deleteService(serviceName) - val serviceTry: Try[Service] = - management.createService( - serviceName, - "localhost", - s"${serviceName}_table", - 1, - None - ) - - val serviceColumnTry = serviceTry.map { _ => - management.createServiceColumn( - serviceName, - columnName, - "string", - List( - Prop("age", "0", "int"), - Prop("gender", "", "string") - ) - ) - } - - Management.deleteLabel(labelName) - val labelTry: Try[Label] = - management.createLabel( - labelName, - serviceName, columnName, "string", - serviceName, columnName, "string", - true, serviceName, - Nil, - Seq(Prop("score", "0", "int")), - "strong" - ) + val config = ConfigFactory.load() + testGraph = new BasicGraph(config) - val isPrepared = List(serviceTry, serviceColumnTry, labelTry).forall(_.isSuccess) - - s2Schema = new SchemaDef(s2Repository).S2GraphSchema - - println("-" * 80) - println(SchemaRenderer.renderSchema(s2Schema)) - println("-" * 80) - - - require(isPrepared, "should created metadata") + logger.info("-" * 80) + println(testGraph.showSchema) + logger.info("-" * 80) } override def afterAll(): Unit = { - graph.shutdown(true) + testGraph.cleanup() } test("s2 schema should has types") { - val s2Schema = new SchemaDef(s2Repository).S2GraphSchema - val Success(query) = QueryParser.parse( """ query IntrospectionTypeQuery { @@ -181,7 +113,7 @@ class SchemaTest extends FunSuite with Matchers with BeforeAndAfterAll { ) ) - val actual = Await.result(Executor.execute(s2Schema, query, s2Repository), Duration("10 sec")) + val actual = Await.result(Executor.execute(testGraph.schema, query, testGraph.repository), Duration("10 sec")) val maps = actual.asInstanceOf[Map[String, Map[String, Map[String, Vector[Map[String, String]]]]]]("data")("__schema")("types") val expectedSet = expected("data")("__schema")("types").flatMap(_.values.headOption).toSet @@ -190,74 +122,6 @@ class SchemaTest extends FunSuite with Matchers with BeforeAndAfterAll { logger.info(s"expected only has: ${expectedSet -- actualSet}") logger.info(s"actual only has: ${actualSet -- expectedSet}") - actualSet should be(expectedSet) - } - - test("Management query should have service: 'kakao'") { - val Success(query) = QueryParser.parse( - """ - query GetServiceAndLabel { - Management { - Services(name: kakao) { - serviceColumns { - props { - name - defaultValue - dataType - } - } - } - } - } - """) - - implicit val playJsonMarshaller = sangria.marshalling.playJson.PlayJsonResultMarshaller - - val actual: JsValue = Await.result(Executor.execute(s2Schema, query, s2Repository), Duration("10 sec")) - -// val expected: Map[String, Map[String, Map[String, Vector[Map[String, Vector[Map[String, Vector[Map[String, String]]]]]]]]] = -// Map("data" -> -// Map("Management" -> -// Map("Services" -> -// Vector( -// Map("serviceColumns" -> -// Vector( -// Map( -// "props" -> -// Vector( -// Map("name" -> "age", "defaultValue" -> "0", "dataType" -> "int"), -// Map("name" -> "gender", "defaultValue" -> "", "dataType" -> "string") -// ) -// ) -// ) -// ) -// ) -// ) -// ) -// ) - - true + actualSet shouldBe expectedSet } - - test("Management query should have label: 'friends'") { - val Success(query) = QueryParser.parse( - """ - query Management { - Management { - Labels(name: friends) { - name - props { - name - defaultValue - dataType - } - } - } - } - """) - - val actual = Await.result(Executor.execute(s2Schema, query, s2Repository), Duration("10 sec")) - true - } - } http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/eb130d4b/s2graphql/src/test/scala/org/apache/s2graph/graphql/TestGraph.scala ---------------------------------------------------------------------- diff --git a/s2graphql/src/test/scala/org/apache/s2graph/graphql/TestGraph.scala b/s2graphql/src/test/scala/org/apache/s2graph/graphql/TestGraph.scala new file mode 100644 index 0000000..596891f --- /dev/null +++ b/s2graphql/src/test/scala/org/apache/s2graph/graphql/TestGraph.scala @@ -0,0 +1,107 @@ +package org.apache.s2graph.graphql + +import com.typesafe.config.Config +import org.apache.s2graph.core.Management.JsonModel.Prop +import org.apache.s2graph.core.mysqls.{Label, Model, Service} +import org.apache.s2graph.core.rest.RequestParser +import org.apache.s2graph.core.{Management, S2Graph} +import org.apache.s2graph.graphql.repository.GraphRepository +import play.api.libs.json._ +import sangria.ast.Document +import sangria.execution.Executor +import sangria.renderer.SchemaRenderer +import sangria.schema.Schema + +import scala.concurrent.Await +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration._ +import scala.util._ + +object TestGraph { + +} + +trait TestGraph { + + def open(): Unit + + def cleanup(): Unit + + def repository: GraphRepository + + def schema: Schema[GraphRepository, Any] + + def showSchema: String + + def queryAsJs(query: Document): JsValue = { + implicit val playJsonMarshaller = sangria.marshalling.playJson.PlayJsonResultMarshaller + val js = Await.result(Executor.execute(schema, query, repository), Duration("10 sec")) + js + } + + def queryAsRaw(query: Document, graph: TestGraph): Any = { + Await.result(Executor.execute(schema, query, repository), Duration("10 sec")) + } +} + +class EmptyGraph(config: Config) extends TestGraph { + Model.apply(config) + + lazy val graph = new S2Graph(config)(scala.concurrent.ExecutionContext.Implicits.global) + lazy val management = new Management(graph) + lazy val s2Repository = new GraphRepository(graph) + + override def cleanup(): Unit = graph.shutdown(true) + + override def schema: Schema[GraphRepository, Any] = new SchemaDef(s2Repository).S2GraphSchema + + override def showSchema: String = SchemaRenderer.renderSchema(schema) + + override def repository: GraphRepository = s2Repository + + override def open(): Unit = { + Model.shutdown(true) + } + +} + +class BasicGraph(config: Config) extends EmptyGraph(config) { + // Init test data + val serviceName = "kakao" + val labelName = "friends" + val columnName = "user" + + Management.deleteService(serviceName) + val serviceTry: Try[Service] = + management.createService( + serviceName, + "localhost", + s"${serviceName}_table", + 1, + None + ) + + val serviceColumnTry = serviceTry.map { _ => + management.createServiceColumn( + serviceName, + columnName, + "string", + List( + Prop("age", "0", "int"), + Prop("gender", "", "string") + ) + ) + } + + Management.deleteLabel(labelName) + val labelTry: Try[Label] = + management.createLabel( + labelName, + serviceName, columnName, "string", + serviceName, columnName, "string", + true, serviceName, + Nil, + Seq(Prop("score", "0", "int")), + "strong" + ) +}
