CustomConverterBuilderTest.scala 6.2 KB
Newer Older
梦境迷离's avatar
梦境迷离 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/*
 * Copyright (c) 2022 bitlap
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.bitlap.csv.core.test

import org.bitlap.csv.core.{ CsvableBuilder, ScalableBuilder }
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

/**
 * @author 梦境迷离
 * @version 1.0,2022/4/29
 */
class CustomConverterBuilderTest extends AnyFlatSpec with Matchers {

  "CustomConverterBuilder1" should "ok" in {
    val line = "abc,cdf,d,12,2,false,0.1,0.23333"
    val dimension = ScalableBuilder[Dimension2].build(line, ',').toScala
    assert(dimension.toString == "Some(Dimension2(abc,Some(cdf),d,12,2,false,0.1,0.23333))")
    val csv = CsvableBuilder[Dimension2].build(dimension.get, ',').toCsvString
    println(csv)
    assert(csv == line)
  }

  "CustomConverterBuilder2" should "ok when using json value" in {
    val line = """abc,"{""a"":""b"",""c"":""d""}",d,12,2,false,0.1,0.23333"""
    val dimension1 = ScalableBuilder[Dimension2]
      .setField(_.c, _ => 12L)
      .build(line, ',')
      .toScala

    println(dimension1)
    assert(dimension1.toString == "Some(Dimension2(abc,Some({\"a\":\"b\",\"c\":\"d\"}),d,12,2,false,0.1,0.23333))")

    val csv = CsvableBuilder[Dimension2]
      .setField[Char](_.d, _ => "????????")
      .setField[Option[String]](_.value, js => s"""\"${js.get.replace("\"", "\"\"")}\"""")
      .build(dimension1.get, ',')
      .toCsvString

    println(csv)
    assert(csv == "abc,\"{\"\"a\"\":\"\"b\"\",\"\"c\"\":\"\"d\"\"}\",????????,12,2,false,0.1,0.23333")
  }

  "CustomConverterBuilder3" should "ok when using json value" in {
    val line = """abc,"{""a"":""b"",""c"":""d""}",d,12,2,false,0.1,0.23333"""
    val d = ScalableBuilder[Dimension2]
      .setField(_.value, _ => None)
      .build(line, ',')
      .toScala
    assert(d.toString == "Some(Dimension2(abc,None,d,12,2,false,0.1,0.23333))")

    val d2 = ScalableBuilder[Dimension2]
      .setField(_.value, _ => None)
      .build("""abc,"{""a"":""b"",""c"":""d""}",d,12,2,false,0.1,0.23333""", ',')
      .toScala
    assert(d2.toString == "Some(Dimension2(abc,None,d,12,2,false,0.1,0.23333))")

    val e = ScalableBuilder[Dimension2]
      .build(line, ',')
      .toScala
    println(e)

    assert(e.toString == "Some(Dimension2(abc,Some({\"a\":\"b\",\"c\":\"d\"}),d,12,2,false,0.1,0.23333))")
  }

  "CustomConverterBuilder4" should "ok when using toCsvString" in {
    val e = Dimension2("1", Some("hello"), 'c', 1L, 1, false, 0.1f, 0.2)
    val dimension1 = CsvableBuilder[Dimension2]
      .build(e, ',')
      .toCsvString
    assert(dimension1 == "1,hello,c,1,1,false,0.1,0.2")

    val dimension2 = CsvableBuilder[Dimension2]
      .setField[Option[String]](_.value, _ => "hello world")
      .build(e, '*')
      .toCsvString
    assert(dimension2 == "1*hello world*c*1*1*false*0.1*0.2")

    val dimension3 = CsvableBuilder[Dimension2]
      .setField[Option[String]](_.value, _ => "hello world")
      .build(Dimension2("1", Some("hello"), 'c', 1L, 1, false, 0.1f, 0.2), ',')
      .toCsvString
    assert(dimension3 == "1,hello world,c,1,1,false,0.1,0.2")
  }

  "CustomConverterBuilder5" should "ok when using list" in {
    val es = List(
      Dimension2("1", Some("hello"), 'c', 1L, 1, true, 0.1f, 0.2),
      Dimension2("2", Some("hello bitlap"), 'c', 1L, 1, false, 0.1f, 0.2)
    )

    val dimension1 = es.map(e => CsvableBuilder[Dimension2].build(e, ',').toCsvString)
    assert(dimension1 == List("1,hello,c,1,1,true,0.1,0.2", "2,hello bitlap,c,1,1,false,0.1,0.2"))

    val csv = List("1,hello,c,1,1,true,0.1,0.2", "2,hello bitlap,c,1,1,false,0.1,0.2")
    val scala = csv.map(f => ScalableBuilder[Dimension2].build(f, ',').toScala)
    assert(
      scala.toString() == "List(Some(Dimension2(1,Some(hello),c,1,1,true,0.1,0.2)), Some(Dimension2(2,Some(hello bitlap),c,1,1,false,0.1,0.2)))"
    )

  }

  private val csv = """100,1,"{""city"":""北京"",""os"":""Mac""}",vv,1"""
  private val metric = Metric(1, 2, Nil, "name", 2)
  private val metric2 = Metric2(1, 2, Nil, "name", 2)

  "CustomConverterBuilder6" should "fail when find List or Seq but without using setFiled" in {
    """
      |ScalableBuilder[Metric2].build(csv, ',').toScala
      |""".stripMargin shouldNot compile

    """
      |CsvableBuilder[Metric2].build(metric, ',').toCsvString
      |""".stripMargin shouldNot compile

  }

  "CustomConverterBuilder7" should "fail when find List or Seq but without using setFiled" in {
    """
      |ScalableBuilder[Metric2].build(csv, ',').toScala
      |""".stripMargin shouldNot compile

    """
      |CsvableBuilder[Metric2].build(metric2, ',').toCsvString
      |""".stripMargin shouldNot compile
  }

  "CustomConverterBuilder8" should "ok when not pass columnSeparator" in {
    val e = Dimension2("1", Some("hello"), 'c', 1L, 1, false, 0.1f, 0.2)
    val csv = CsvableBuilder[Dimension2].build(e).toCsvString
    println(csv)
    assert(csv == "1,hello,c,1,1,false,0.1,0.2")

    val scala = ScalableBuilder[Dimension2].build(csv).toScala
    println(scala)
    assert(scala.get == e)
  }
梦境迷离's avatar
梦境迷离 已提交
157 158 159 160 161 162 163 164

  "CustomConverterBuilder9" should "fail if case class has currying" in {
    """
      |case class Test(i:Int)(j:String)
      |    val t = Test(1)("hello")
      |    CsvableBuilder[Test].build(t).toCsvString
      |""".stripMargin shouldNot compile
  }
梦境迷离's avatar
梦境迷离 已提交
165
}