Skip to content

Commit

Permalink
add more tests for import export
Browse files Browse the repository at this point in the history
  • Loading branch information
do4gr committed Jan 5, 2018
1 parent a6c6696 commit 4940abc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,13 @@ class BulkExport(implicit clientInjector: ClientInjector) {
}

private def serializeArray(in: JsonBundle, identifier: ImportIdentifier, arrayValues: Vector[Any], info: ListInfo, amount: Int = 1000000): ResultFormat = {
val values = arrayValues.slice(info.cursor.array, info.cursor.array + amount)
val result: Map[String, Any] = Map("_typeName" -> identifier.typeName, "id" -> identifier.id, info.currentField -> values)
val values = arrayValues.slice(info.cursor.array, info.cursor.array + amount)
val convertedValues = info.currentTypeIdentifier match {
case TypeIdentifier.Enum => values.map(enum => enum.asInstanceOf[String].replaceAll("\"", ""))
case _ => values
}

val result: Map[String, Any] = Map("_typeName" -> identifier.typeName, "id" -> identifier.id, info.currentField -> convertedValues) // todo this needs to handle enum better
val json = result.toJson
val combinedElements = in.jsonElements :+ json
val combinedSize = in.size + json.toString.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ class BulkImport(implicit injector: ClientInjector) {

private def convertToImportRelation(json: JsValue): ImportRelation = {
val array = json.convertTo[JsArray]
val leftMap = array.elements.head.convertTo[Map[String, String]]
val rightMap = array.elements.reverse.head.convertTo[Map[String, String]]
val left = ImportRelationSide(getImportIdentifier(leftMap), leftMap("fieldName"))
val right = ImportRelationSide(getImportIdentifier(rightMap), rightMap("fieldName"))
val leftMap = array.elements.head.convertTo[Map[String, Option[String]]]
val rightMap = array.elements.reverse.head.convertTo[Map[String, Option[String]]]
val left = ImportRelationSide(ImportIdentifier(leftMap("_typeName").get, leftMap("id").get), leftMap.get("fieldName").flatten)
val right = ImportRelationSide(ImportIdentifier(rightMap("_typeName").get, rightMap("id").get), rightMap.get("fieldName").flatten)

ImportRelation(left, right)
}
Expand Down Expand Up @@ -103,13 +103,19 @@ class BulkImport(implicit injector: ClientInjector) {

private def generateImportRelationsDBActions(project: Project, relations: Vector[ImportRelation]): DBIOAction[Vector[Try[Int]], NoStream, Effect.Write] = {
val x = relations.map { element =>
val fromModel = project.getModelByName_!(element.left.identifier.typeName)
val fromField = fromModel.getFieldByName_!(element.left.fieldName)
val (left, right) = (element.left, element.right) match {
case (l, r) if l.fieldName.isDefined => (l, r)
case (l, r) if r.fieldName.isDefined => (r, l)
case _ => throw sys.error("Invalid ImportRelation at least one fieldName needs to be defined.")
}

val fromModel = project.getModelByName_!(left.identifier.typeName)
val fromField = fromModel.getFieldByName_!(left.fieldName.get)
val relationSide: cool.graph.shared.models.RelationSide.Value = fromField.relationSide.get
val relation: Relation = fromField.relation.get

val aValue: String = if (relationSide == RelationSide.A) element.left.identifier.id else element.right.identifier.id
val bValue: String = if (relationSide == RelationSide.A) element.right.identifier.id else element.left.identifier.id
val aValue: String = if (relationSide == RelationSide.A) left.identifier.id else right.identifier.id
val bValue: String = if (relationSide == RelationSide.A) right.identifier.id else left.identifier.id

val aModel: Model = relation.getModelA_!(project)
val bModel: Model = relation.getModelB_!(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package object ImportExport {
case class ResultFormat(out: JsonBundle, cursor: Cursor, isFull: Boolean)
case class ImportBundle(valueType: String, values: JsArray)
case class ImportIdentifier(typeName: String, id: String)
case class ImportRelationSide(identifier: ImportIdentifier, fieldName: String)
case class ImportRelationSide(identifier: ImportIdentifier, fieldName: Option[String])
case class ImportNode(identifier: ImportIdentifier, values: Map[String, Any])
case class ImportRelation(left: ImportRelationSide, right: ImportRelationSide)
case class ImportList(identifier: ImportIdentifier, values: Map[String, Vector[Any]])
Expand Down

0 comments on commit 4940abc

Please sign in to comment.