org.scalatest.FreeSpec Scala Examples
The following examples show how to use org.scalatest.FreeSpec.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: VariablesTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.model import org.rogach.scallop.{ScallopConf, Subcommand} import org.scalatest.{FreeSpec, Matchers} class VariablesTest extends FreeSpec with Matchers{ "replaceInText should work" in { val variables = new Variables variables += ("TO_REPLACE" -> "REPLACED") val text = "TO_REPLACE has been ${TO_REPLACE}" val expected = "TO_REPLACE has been REPLACED" assert(variables.replaceInText(text)===expected) } "subsetInText should work" in { val variables = new Variables variables += ("IN_KEY" -> "IN_VALUE") variables += ("OUT_KEY" -> "OUT_VALUE") val text = "this text contains ${IN_KEY} but does not contains OUT_KEY" val expectedVariables = new Variables expectedVariables += ("IN_KEY" -> "IN_VALUE") assert(variables.subsetInText(text, Nil) === expectedVariables) } "replaceInText should preserve partition variables" in { val text: String = """INSERT OVERWRITE TABLE db1.dest PARTITION(part=${partition:toto}) SELECT ${partition:toto} as num FROM db2.source""" val vars = new Variables() vars += "partition:toto" -> "${partition:toto}0" val expected: String = """INSERT OVERWRITE TABLE db1.dest PARTITION(part="${partition:toto}") SELECT "${partition:toto}" as num FROM db2.source""" assert(vars.replaceInText(text) == expected) } "the scallopConverter should work" in { object Conf extends ScallopConf(Seq("--variables", "HELLO=yes", "LIST=(1,2,3)", "sub")) { val variables = opt[Variables](name = "variables")(Variables.scallopConverter) val sub = new Subcommand("sub") { banner("Print version information of this program") override def toString = "sub" } } assert(Conf.variables() === Variables("HELLO" -> "yes", "LIST" -> "(1,2,3)")) assert(Conf.subcommand.get.toString === "sub") } }
Example 2
Source File: DiffTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.commands import com.flaminem.flamy.conf.FlamyContext import org.scalatest.{FreeSpec, Matchers} class DiffTest extends FreeSpec with Matchers { "diffColumns should work" in { val leftContext = new FlamyContext("flamy.model.dir.paths" -> "src/test/resources/LeftDiff") val rightContext = new FlamyContext("flamy.model.dir.paths" -> "src/test/resources/RightDiff") val diff = new Diff val expected = """+-----------------------+-----------------------+ || model | model | |+-----------------------+-----------------------+ || db_source.left_table | | || | db_source.right_table | || db_source.source | db_source.source | || col2 INT | | || * partcol2 STRING | * partcol3 STRING | |+-----------------------+-----------------------+""".stripMargin assert(diff.diffColumns(leftContext, rightContext) === expected) } "diffTables should work" in { val leftContext = new FlamyContext("flamy.model.dir.paths" -> "src/test/resources/LeftDiff") val rightContext = new FlamyContext("flamy.model.dir.paths" -> "src/test/resources/RightDiff") val diff = new Diff val expected = """+----------------------+-----------------------+ || model | model | |+----------------------+-----------------------+ || db_source.left_table | | || | db_source.right_table | |+----------------------+-----------------------+""".stripMargin assert(diff.diffTables(leftContext, rightContext) === expected) } }
Example 3
Source File: RemoteHiveRunnerTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.exec.hive import com.flaminem.flamy.conf.{Environment, FlamyContext, FlamyGlobalOptions} import com.flaminem.flamy.model.{Column, Variables} import com.flaminem.flamy.utils.sql.{MetaData, ResultRow} import org.scalatest.{FreeSpec, Matchers} class RemoteHiveRunnerTest extends FreeSpec with Matchers { val context = new FlamyContext( new FlamyGlobalOptions( conf = Map( "flamy.environments" -> "remote", "flamy.model.dir.paths" -> "src/test/resources/test", "flamy.variables.path" -> "${flamy.model.dir.paths}/VARIABLES.properties", "flamy.env.model.hive.presets.path" -> "${flamy.model.dir.paths}/PRESETS.hql", "flamy.env.remote.hive.server.uri" -> "\"hmaster02.dc3.dataminem.net:10000\"" ) ) , Some(Environment("remote")) ) context.dryRun = true val runner = new RemoteHiveRunner(context) "testRemote1" ignore { val createDB: String = "CREATE DATABASE IF NOT EXISTS test" val createSource: String = "CREATE TABLE IF NOT EXISTS test.source (a INT, b INT, c INT, d INT)" val populate: String = "SELECT to_date(truncate_date(from_unixtime(unix_timestamp()),'WEEK')) FROM test.source" val runner: HiveRunner = new RemoteHiveRunner(context) runner.runCreate(createDB, new Variables) runner.runCreate(createSource, new Variables) runner.runText(populate, None, new Variables, explainIfDryRun = true) } "testRemote2" ignore { try { val context: FlamyContext = new FlamyContext("test", Environment("prod")) val runner: RemoteHiveRunner = new RemoteHiveRunner(context) // val con: Connection = runner.con // val st: Statement = con.createStatement // st.execute("USE dev_client_value") // st.execute("MSCK REPAIR TABLE url_semantic_data_s3") } catch { case e: Exception => e.printStackTrace() } } "getConfiguration" - { "should work with hive config result" in { val key = "hive.support.concurrency" val value = "true" val metaData = new MetaData(new Column("set")::Nil) val res = new ResultRow(metaData, Seq(s"$key=$value")) assert(runner.readConfigurationResult(key, res) === Some(value)) } "should work with undefined hive config result " in { val key = "hive.support.concurrency" val col = new Column("set") val res = new ResultRow(new MetaData(new Column("set")::Nil), Seq(s"$key is undefined")) assert(runner.readConfigurationResult(key, res) === None) } "should work with spark config result" in { val key = "hive.support.concurrency" val value = "true" val metaData = new MetaData(new Column("key")::new Column("value")::Nil) val res = new ResultRow(metaData, Seq(key, value)) assert(runner.readConfigurationResult(key, res) === Some(value)) } "should work with undefined spark config result " in { val key = "hive.support.concurrency" val metaData = new MetaData(new Column("key")::new Column("value")::Nil) val res = new ResultRow(metaData, Seq(key, "<undefined>")) assert(runner.readConfigurationResult(key, res) === None) } } }
Example 4
Source File: MultilineActionFeedbackTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.exec.utils import org.scalatest.FreeSpec class MultilineActionFeedbackTest extends FreeSpec { "IncrementalIndexer should work" in { val index = new MultilineActionFeedback.IncrementalIndexer[Int] assert(index(0) == 0) assert(index(1) == 1) assert(index(2) == 2) assert(index(0) == 0) assert(index(1) == 1) assert(index(2) == 2) } }
Example 5
Source File: TableGraphRunnerTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.exec.run import com.flaminem.flamy.conf.FlamyContext import com.flaminem.flamy.conf.hive.ModelHiveContext import com.flaminem.flamy.exec.FlamyRunner import com.flaminem.flamy.exec.actions.RunAction import com.flaminem.flamy.exec.utils.Workflow.Status import com.flaminem.flamy.exec.utils.{HasWorkflowHistory, Workflow} import com.flaminem.flamy.graph.TableGraph import com.flaminem.flamy.model.ItemRange import com.flaminem.flamy.model.names.TableName import org.scalatest.FreeSpec class TableGraphRunnerTest extends FreeSpec { val context = new FlamyContext( "flamy.model.dir.paths" -> "src/test/resources/GraphRunner", "flamy.env.model.hive.presets.path" -> "src/test/resources/GraphRunner/PRESETS.hql" ) context.dryRun = true "a GraphRunner should run tables in the correct order" in { ModelHiveContext.reset() val itemArgs = ItemRange(Seq("db_dest.dest"), Seq("db_dest.dest1", "db_dest.dest2")) val runGraph: TableGraph = TableGraph.getCompleteModelGraph(context, itemArgs, checkNoMissingTable = true).subGraph(itemArgs).withRemovedViews() val flamyRunner: FlamyRunner = FlamyRunner(context) val graphRunner = new GraphRunner(flamyRunner, context, runGraph) with HasWorkflowHistory[RunAction] graphRunner.run() val actual : Seq[(TableName, Status)] = graphRunner .getHistory .collect{ case (action, Workflow.Status.RUNNING) => action.tableName -> Workflow.Status.RUNNING case (action, Workflow.Status.SUCCESSFUL) => action.tableName -> Workflow.Status.SUCCESSFUL } val expected = Seq( TableName("db_dest.dest") -> Workflow.Status.RUNNING, TableName("db_dest.dest") -> Workflow.Status.SUCCESSFUL, TableName("db_dest.dest2") -> Workflow.Status.RUNNING, TableName("db_dest.dest2") -> Workflow.Status.SUCCESSFUL, TableName("db_dest.dest1") -> Workflow.Status.RUNNING, TableName("db_dest.dest1") -> Workflow.Status.SUCCESSFUL ) assert(actual === expected) } "a GraphRunner should run tables in the correct order bis" in { ModelHiveContext.reset() val itemArgs = ItemRange(Seq("db_dest2.dest"), Seq("db_dest2.dest1", "db_dest2.dest2", "db_dest2.dest3")) val runGraph: TableGraph = TableGraph.getCompleteModelGraph(context, itemArgs, checkNoMissingTable = true).subGraph(itemArgs).withRemovedViews() val flamyRunner: FlamyRunner = FlamyRunner(context) val graphRunner = new GraphRunner(flamyRunner, context, runGraph) with HasWorkflowHistory[RunAction] graphRunner.run() val actual : Seq[(TableName, Status)] = graphRunner .getHistory .collect{ case (action, Workflow.Status.RUNNING) => action.tableName -> Workflow.Status.RUNNING case (action, Workflow.Status.SUCCESSFUL) => action.tableName -> Workflow.Status.SUCCESSFUL } val expected = Seq( TableName("db_dest2.dest") -> Workflow.Status.RUNNING, TableName("db_dest2.dest") -> Workflow.Status.SUCCESSFUL, TableName("db_dest2.dest2") -> Workflow.Status.RUNNING, TableName("db_dest2.dest2") -> Workflow.Status.SUCCESSFUL, TableName("db_dest2.dest3") -> Workflow.Status.RUNNING, TableName("db_dest2.dest3") -> Workflow.Status.SUCCESSFUL, TableName("db_dest2.dest1") -> Workflow.Status.RUNNING, TableName("db_dest2.dest1") -> Workflow.Status.SUCCESSFUL ) assert(actual === expected) } }
Example 6
Source File: UniqueListTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.utils.collection.immutable import org.scalatest.FreeSpec class UniqueListTest extends FreeSpec { "a UniqueList should throw an exception if the same element is inserted twice" in { intercept[UnicityViolationException[_]] { UniqueList(1, 2, 3, 1) } intercept[UnicityViolationException[_]] { UniqueList(1, 2, 3) :+ 1 } intercept[UnicityViolationException[_]] { UniqueList(1, 2, 3) ++ Seq(4, 5, 6, 1) } intercept[UnicityViolationException[_]] { UniqueList(1, 2, 3) ++ Seq(4, 5, 6, 6) } intercept[UnicityViolationException[_]] { var l = UniqueList(1, 2, 3) l :+= 1 } } "a UniqueList should work as long as distinct elements are inserted " in { assert (UniqueList(1, 2, 3).toSeq === (1 to 3)) assert ((UniqueList(1, 2, 3) :+ 4).toSeq === (1 to 4)) assert ((UniqueList(1, 2, 3) ++ Seq(4,5,6)).toSeq === (1 to 6)) var l = UniqueList(1, 2, 3) l :+= 4 l ++= Seq(5, 6) assert(l.toSeq === (1 to 6)) } }
Example 7
Source File: UniqueSeqMapTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.utils.collection.immutable import org.scalatest.FreeSpec class UniqueSeqMapTest extends FreeSpec { "a UniqueSeqMap should throw an exception if the same element is inserted twice" in { intercept[UnicityViolationException[_]] { UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c", 1 -> "d") } intercept[UnicityViolationException[_]] { UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c") + (1 -> "d") } intercept[UnicityViolationException[_]] { UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c") ++ Seq(4 -> "d", 5 -> "e", 6 -> "f", 1 -> "g") } intercept[UnicityViolationException[_]] { UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c") ++ Seq(4 -> "d", 5 -> "e", 6 -> "f", 6 -> "g") } intercept[UnicityViolationException[_]] { var l = UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c") l += 1 -> "d" } } "a UniqueSeqMap should work as long as distinct keys are inserted with distinct values" in { assert(UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c").toSeq === Seq(1 -> "a", 2 -> "b", 3 -> "c")) assert((UniqueSeqMap(1 -> "a", 2 -> "b") + (3 -> "c")).toSeq === Seq(1 -> "a", 2 -> "b", 3 -> "c")) assert((UniqueSeqMap(1 -> "a", 2 -> "b") ++ Seq(3 -> "c", 4 -> "d")).toSeq === Seq(1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d")) var l = UniqueSeqMap(1 -> "a", 2 -> "b") l += (3 -> "c") l ++= Seq(4 -> "d", 5 -> "e") assert(l.toSeq === Seq(1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e")) } "a UniqueSeqMap should work as long as distinct keys are inserted with identical values" in { assert(UniqueSeqMap(1 -> "a", 2 -> "a", 3 -> "a").toSeq === Seq(1 -> "a", 2 -> "a", 3 -> "a")) assert((UniqueSeqMap(1 -> "a", 2 -> "a") + (3 -> "a")).toSeq === Seq(1 -> "a", 2 -> "a", 3 -> "a")) assert((UniqueSeqMap(1 -> "a", 2 -> "a") ++ Seq(3 -> "a", 4 -> "a")).toSeq === Seq(1 -> "a", 2 -> "a", 3 -> "a", 4 -> "a")) var l = UniqueSeqMap(1 -> "a", 2 -> "a") l += (3 -> "a") l ++= Seq(4 -> "a", 5 -> "a") assert(l.toSeq === Seq(1 -> "a", 2 -> "a", 3 -> "a", 4 -> "a", 5 -> "a")) } "a UniqueSeqMap should preserve ordering" in { assert(UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c").toSeq === Seq(1 -> "a", 2 -> "b", 3 -> "c")) assert(UniqueSeqMap(3 -> "a", 2 -> "b", 1 -> "c").toSeq === Seq(3 -> "a", 2 -> "b", 1 -> "c")) assert(UniqueSeqMap(3 -> "c", 2 -> "b", 1 -> "a").toSeq === Seq(3 -> "c", 2 -> "b", 1 -> "a")) assert(UniqueSeqMap(1 -> "c", 2 -> "b", 3 -> "a").toSeq === Seq(1 -> "c", 2 -> "b", 3 -> "a")) } "get should work" in { val m = UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c") assert(m.get(1) === Some("a")) assert(m.get(2) === Some("b")) assert(m.get(3) === Some("c")) assert(m.get(4) === None) } "getOrElse should work" in { val m = UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c") assert(m.getOrElse(1, "") === "a") assert(m.getOrElse(2, "") === "b") assert(m.getOrElse(3, "") === "c") assert(m.getOrElse(4, "") === "") } "filter should work" in { val m: Traversable[(Int, String)] = UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c").filter{ case (i, c) => i % 2 == 1 } assert(m === UniqueSeqMap(1 -> "a", 3 -> "c").toSeq) } "collect should work" in { val m: Traversable[Int] = UniqueSeqMap(1 -> "a", 2 -> "b", 3 -> "c").collect{ case (i, c) if i % 2 == 1 => i } assert(m === Seq(1, 3 )) } }
Example 8
Source File: NamedCollectionTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.utils.collection.immutable import com.flaminem.flamy.utils.Named import org.scalatest.FreeSpec class NamedCollectionTest extends FreeSpec { case class Person(name: String) extends Named { override def getName: String = name } "NamedCollection" - { "should be sorted" in { val c: NamedCollection[Person] = NamedCollection[Person](Person("Joe"), Person("Jack"), Person("William"), Person("Averell")) assert(c.toSeq === Seq(Person("Averell"), Person("Jack"), Person("Joe"), Person("William"))) } "+ should work" in { val c: NamedCollection[Person] = NamedCollection[Person]() val c2: NamedCollection[Person] = c + Person("john") assert(c2.size === 1) } "++ should work" in { val c: NamedCollection[Person] = NamedCollection[Person]() val c2: NamedCollection[Person] = c ++ Seq(Person("Joe"), Person("Jack"), Person("William"), Person("Averell")) assert(c2.size === 4) } "- should work" in { val c: NamedCollection[Person] = NamedCollection[Person](Person("Joe"), Person("Jack"), Person("William"), Person("Averell")) val c2: NamedCollection[Person] = c - Person("Joe") assert(c2.toSeq === Seq(Person("Averell"), Person("Jack"), Person("William"))) } "map should work" in { val c: NamedCollection[Person] = NamedCollection[Person](Person("Joe"), Person("Jack")) def daltonify(p:Person): Person = p match {case Person(name) => Person(s"$name Dalton")} val c2: NamedCollection[Person] = c.map{daltonify} assert(c2.toSeq === Seq(Person("Jack Dalton"), Person("Joe Dalton"))) } } }
Example 9
Source File: TraversableLikeExtensionTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.utils.collection import org.scalatest.FreeSpec import scala.collection.immutable.Seq class TraversableLikeExtensionTest extends FreeSpec { "splitBy should correctly work with sorted groups" in { val l: Seq[Int] = 1 to 10 val res = l.splitBy{x => x / 6} assert(res===Seq((0,Vector(1, 2, 3, 4, 5)), (1, Vector(6, 7, 8, 9, 10)))) } "splitBy should correctly work with unsorted groups" in { val l: Seq[Int] = 1 to 10 val res = l.splitBy{x => x % 2} assert(res === Seq( (1, Vector(1)), (0, Vector(2)), (1, Vector(3)), (0, Vector(4)), (1, Vector(5)), (0, Vector(6)), (1, Vector(7)), (0, Vector(8)), (1, Vector(9)), (0, Vector(10)) ) ) } }
Example 10
Source File: ConfigurablePatchJsonSpec.scala From patchless with Apache License 2.0 | 5 votes |
package patchless.circe.extras import cats.syntax.either._ import io.circe.generic.extras.Configuration import io.circe.generic.extras.auto._ import io.circe.parser.parse import org.scalatest.{FreeSpec, Matchers} import patchless.Patch import shapeless.record.Record class ConfigurablePatchJsonSpec extends FreeSpec with Matchers { "Configurable decoder" - { "Normal" in { import io.circe.generic.extras.defaults._ case class Foo(aString: String, bInt: Int, cBoolean: Boolean) def parsePatch(str: String) = parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _) val aPatched = parsePatch("""{"aString": "patched"}""") val bPatched = parsePatch("""{"bInt": 22}""") val cPatched = parsePatch("""{"cBoolean": false}""") aPatched.updates shouldEqual Record(aString = Some("patched"), bInt = None, cBoolean = None) bPatched.updates shouldEqual Record(aString = None, bInt = Some(22), cBoolean = None) cPatched.updates shouldEqual Record(aString = None, bInt = None, cBoolean = Some(false)) } "Snake case" in { implicit val configuration = Configuration.default.withSnakeCaseMemberNames case class Foo(aString: String, bInt: Int, cBoolean: Boolean) def parsePatch(str: String) = parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _) val aPatched = parsePatch("""{"a_string": "patched"}""") val bPatched = parsePatch("""{"b_int": 22}""") val cPatched = parsePatch("""{"c_boolean": false}""") aPatched.updates shouldEqual Record(aString = Some("patched"), bInt = None, cBoolean = None) bPatched.updates shouldEqual Record(aString = None, bInt = Some(22), cBoolean = None) cPatched.updates shouldEqual Record(aString = None, bInt = None, cBoolean = Some(false)) } "Options" in { import io.circe.generic.extras.defaults._ case class Foo(aString: Option[String]) def parsePatch(str: String) = parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _) val aPatchedSome = parsePatch("""{"aString": "patched"}""") val aPatchedNone = parsePatch("""{"aString": null}""") aPatchedSome.updates shouldEqual Record(aString = Some(Some("patched"))) aPatchedNone.updates shouldEqual Record(aString = Some(None)) } } }
Example 11
Source File: CreateTableParser$Test.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.parsing.hive import com.flaminem.flamy.conf.FlamyContext import org.scalatest.FreeSpec class CreateTableParser$Test extends FreeSpec { implicit val context = new FlamyContext("flamy.model.dir.paths" -> "src/test/resources/test") "a correct CREATE TABLE query should be correctly parsed" in { val query = "CREATE TABLE toto.test_table (id INT) PARTITIONED BY (week STRING)" val expectedResult = """Table(type=TABLE, name=test_table, schema=toto, columns[Column(name=id, type=int)], partitions[Column(name=week, type=string)])""" val actualResult = CreateTableParser.parseQuery(query) assert(actualResult.toString === expectedResult ) } "a correct CREATE TABLE IF NOT EXISTS query should be correctly parsed" in { val query = "CREATE TABLE IF NOT EXISTS toto.test_table (id INT) PARTITIONED BY (week STRING)" val expectedResult = """Table(type=TABLE, name=test_table, schema=toto, columns[Column(name=id, type=int)], partitions[Column(name=week, type=string)])""" val actualResult = CreateTableParser.parseQuery(query) assert(actualResult.toString === expectedResult ) } "a correct text of multiple queries should be correctly parsed" in { val text = """ -- DROP TABLE IF EXISTS DBM_reports.report ; | CREATE TABLE IF NOT EXISTS DBM_reports.report | (device_type INT, | mobile_make_id INT, | mobile_model_id INT | ) | PARTITIONED BY (day STRING) | STORED AS SEQUENCEFILE ; | """.stripMargin val expectedResult = """Table(type=TABLE, name=report, schema=dbm_reports, columns[Column(name=device_type, type=int), Column(name=mobile_make_id, type=int), Column(name=mobile_model_id, type=int)], partitions[Column(name=day, type=string)])""" val actualResult = CreateTableParser.parseText(text) assert(actualResult.toString === expectedResult ) } }
Example 12
Source File: AnnotationParser$Test.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.parsing.hive import com.flaminem.flamy.conf.FlamyContext import com.flaminem.flamy.model.Variables import com.flaminem.flamy.model.names.TableName import com.flaminem.flamy.model.partitions.transformation._ import org.scalatest.FreeSpec class AnnotationParser$Test extends FreeSpec { implicit val context = new FlamyContext("flamy.model.dir.paths" -> "src/test/resources/empty_test") "a text with IGNORE partition transformation should be OK" in { val text = """ |@regen( | IGNORE db1.source | ; |) |INSERT OVERWRITE TABLE db2.dest PARTITION(part2) |SELECT id, part1 |FROM db1.source |; """.stripMargin val Seq(actual: Annotation) = AnnotationParser.parseText(text, new Variables(), isView = false) assert( actual.isInstanceOf[Ignore] ) assert( actual.table === TableName("db1.source") ) } "a text with IGNORE TIMESTAMP partition transformation should be OK" in { val text = """ |@regen( | IGNORE TIMESTAMP db1.source | ; |) |INSERT OVERWRITE TABLE db2.dest PARTITION(part2) |SELECT id, part1 |FROM db1.source |; """.stripMargin val Seq(actual: Annotation) = AnnotationParser.parseText(text, new Variables(), isView = false) assert( actual.isInstanceOf[IgnoreTimestamp] ) assert( actual.table === TableName("db1.source") ) } }
Example 13
Source File: WhereASTTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.parsing.hive.ast import com.flaminem.flamy.conf.FlamyContext import com.flaminem.flamy.parsing.hive.FlamyParsingException import com.flaminem.flamy.parsing.model.ColumnDependency import org.scalatest.FreeSpec class WhereASTTest extends FreeSpec { implicit val flamyContext = new FlamyContext("flamy.model.dir.paths" -> "src/test/resources/test") "Methods tests" - { "toAST" in { val clause = """c1 = "A" AND source.c2 = 2 AND db.source.c3 = 3""" val whereAST = WhereAST(clause) assert (SQLFormatter.treeToSQL(whereAST.tree) === clause) } "getUsedColumns should work" in { val whereAST = WhereAST("""c1 = "A" AND source.c2 = 2 AND db.source.c3 = 3""") val expected = Seq( new ColumnDependency("c1"), new ColumnDependency("c2", "source"), new ColumnDependency("c3", "source", "db") ) assert(whereAST.usedColumns === expected) } "getUsedColumns should work with quoted names" in { val whereAST = WhereAST("""1 = `db.source`.c3""") val expected = Seq( new ColumnDependency("c3", "db.source") ) assert(whereAST.usedColumns === expected) } "getUsedColumns should throw an exception when table name is incorrect" in { intercept[FlamyParsingException]{ WhereAST("""1 = db.source.c3.truc""") } } } }
Example 14
Source File: ColumnDependencyTest.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.parsing.model import org.scalatest.FreeSpec class ColumnDependencyTest extends FreeSpec { "matches should work" in { assert (new ColumnDependency("c").matches(new ColumnDependency("c","t","s"))) assert (new ColumnDependency("c").matches(new ColumnDependency("c","t"))) assert (new ColumnDependency("c").matches(new ColumnDependency("c","t","s"))) assert (new ColumnDependency("c","t").matches(new ColumnDependency("c","t"))) assert (new ColumnDependency("c","t").matches(new ColumnDependency("c","t","s"))) assert (new ColumnDependency("c","t","s").matches(new ColumnDependency("c","t","s"))) assert (! new ColumnDependency("c").matches(new ColumnDependency("!","t","s"))) assert (! new ColumnDependency("c").matches(new ColumnDependency("!","t"))) assert (! new ColumnDependency("c").matches(new ColumnDependency("!","t","s"))) assert (! new ColumnDependency("c","t").matches(new ColumnDependency("c"))) assert (! new ColumnDependency("c","t").matches(new ColumnDependency("!","t"))) assert (! new ColumnDependency("c","t").matches(new ColumnDependency("c","!"))) assert (! new ColumnDependency("c","t").matches(new ColumnDependency("!","t","s"))) assert (! new ColumnDependency("c","t").matches(new ColumnDependency("c","!","s"))) assert (! new ColumnDependency("c","t","s").matches(new ColumnDependency("c"))) assert (! new ColumnDependency("c","t","s").matches(new ColumnDependency("c","t"))) assert (! new ColumnDependency("c","t","s").matches(new ColumnDependency("!","t","s"))) assert (! new ColumnDependency("c","t","s").matches(new ColumnDependency("c","!","s"))) assert (! new ColumnDependency("c","t","s").matches(new ColumnDependency("c","t","!"))) } }
Example 15
Source File: ParsingUtils$Test.scala From flamy with Apache License 2.0 | 5 votes |
package com.flaminem.flamy.parsing import org.scalatest.{FreeSpec, Matchers} class ParsingUtils$Test extends FreeSpec with Matchers { "skipStrings" - { "should work with string 1" in { val s = """''abc""" assert( ParsingUtils.skipString(s.toCharArray, 0) === s.length - 3 ) } "should work with string 2" in { val s = """'""'abc""" assert( ParsingUtils.skipString(s.toCharArray, 0) === s.length - 3 ) } "should work with string 3" in { val s = """'"\'"'abc""" assert( ParsingUtils.skipString(s.toCharArray, 0) === s.length - 3 ) } } "skipParentheses" - { "should work with parentheses" in { val s = "((()())(()))" assert( ParsingUtils.skipParentheses(s.toCharArray, 0) === s.length ) } "should work with parentheses and string" in { val s = """((()("'\"'"))(('"\'"')))()""" assert( ParsingUtils.skipParentheses(s.toCharArray, 0) === s.length - 2 ) } } "noSpaceTableNamePattern should correctly recognise table names" in { val regex = ParsingUtils.noSpaceTableNamePattern.r assert(regex.findFirstMatchIn("").isEmpty) assert(regex.findFirstMatchIn(",T").nonEmpty) assert(regex.findFirstMatchIn(",db.T").nonEmpty) assert(regex.findFirstMatchIn(",db.T`T2`").nonEmpty) assert(regex.findFirstMatchIn("FROM T").nonEmpty) assert(regex.findFirstMatchIn("FROM db.t").nonEmpty) assert(regex.findFirstMatchIn("FROM`T`").nonEmpty) assert(regex.findFirstMatchIn("FROM`db`.`t`").nonEmpty) assert(regex.findFirstMatchIn("FROM`db`.t").nonEmpty) assert(regex.findFirstMatchIn("FROM db.`t`").nonEmpty) assert(regex.findFirstMatchIn("FROM `db`.`t`").nonEmpty) val r = ParsingUtils.noSpaceTableNamePattern.r assert(r.findFirstMatchIn(",db4.toto`t2`,db5.toto)").get.group(0) === "db4.toto") } "tableNamePattern should correctly recognise table names" in { val regex = ParsingUtils.tableNamePattern.r assert(regex.findFirstMatchIn("").isEmpty) assert(regex.findFirstMatchIn("FROM T").nonEmpty) assert(regex.findFirstMatchIn("FROM db.t").nonEmpty) assert(regex.findFirstMatchIn("FROM`T`").nonEmpty) assert(regex.findFirstMatchIn("FROM`db`.`t`").nonEmpty) assert(regex.findFirstMatchIn("FROM`db`.t").nonEmpty) assert(regex.findFirstMatchIn("FROM db.`t`").nonEmpty) assert(regex.findFirstMatchIn("FROM `db`.`t`").nonEmpty) assert(regex.findFirstMatchIn(" db1.test").get.group(0) === " db1.test") assert(regex.findFirstMatchIn(",T").isEmpty) } }
Example 16
Source File: DataWeaveCLITest.scala From data-weave-native with Apache License 2.0 | 5 votes |
package org.mule.weave.dwnative.cli import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.PrintStream import org.scalatest.FreeSpec import org.scalatest.Matchers import scala.io.Source class DataWeaveCLITest extends FreeSpec with Matchers { "should work with output application/json" in { val out = System.out try { val stream = new ByteArrayOutputStream() System.setOut(new PrintStream(stream, true)) new DataWeaveCLIRunner().run(Array("output application/json --- (1 to 3)[0]")) val source = Source.fromBytes(stream.toByteArray, "UTF-8") val result = source.mkString result.trim shouldBe "1" } finally { System.setOut(out) println("Finish OK 3") } } "should work with simple script and not output" in { val defaultOut = System.out try { val stream = new ByteArrayOutputStream() System.setOut(new PrintStream(stream, true)) new DataWeaveCLIRunner().run(Array("(1 to 3)[0]")) val source = Source.fromBytes(stream.toByteArray, "UTF-8") val result = source.mkString result.trim shouldBe "1" } finally { System.setOut(defaultOut) } } "should work ok when sending payload from stdin" in { val out = System.out val in = System.in try { val input = """[ | 1, | 2, | 3 |] """.stripMargin.trim val stream = new ByteArrayOutputStream() System.setOut(new PrintStream(stream, true)) System.setIn(new ByteArrayInputStream(input.getBytes("UTF-8"))) new DataWeaveCLIRunner().run(Array("payload[0]")) val source = Source.fromBytes(stream.toByteArray, "UTF-8") val result = source.mkString.trim source.close() result.trim shouldBe "1" } finally { System.setOut(out) System.setIn(in) println("Finish OK 2") } } "should work with light formats" in { val out = System.out val in = System.in try { val input = """[{ | "a" : 1, | "b" : 2, | "c" : 3 |}] """.stripMargin.trim val stream = new ByteArrayOutputStream() System.setOut(new PrintStream(stream, true)) System.setIn(new ByteArrayInputStream(input.getBytes("UTF-8"))) new DataWeaveCLIRunner().run(Array("input payload json output csv header=false ---payload")) val source = Source.fromBytes(stream.toByteArray, "UTF-8") val result = source.mkString.trim source.close() result.trim shouldBe "1,2,3" } finally { System.setOut(out) System.setIn(in) println("Finish OK 2") } } }
Example 17
Source File: ReadDatasourceIntegSpec.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.deeplang.doperations import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FreeSpec} import io.deepsense.deeplang.{LocalExecutionContext, TestDataSources, TestFiles} class ReadDatasourceIntegSpec extends FreeSpec with BeforeAndAfter with BeforeAndAfterAll with LocalExecutionContext with TestDataSources with TestFiles { for (ds <- someDatasourcesForReading) { s"ReadDatasource should work with datasource ${ds.getParams.getName}" in { val rds = ReadDatasource().setDatasourceId(ds.getId) rds.execute()(LocalExecutionContext.createExecutionContext(datasourceClient)) } } }
Example 18
Source File: DeserializationTests.scala From Waves with MIT License | 5 votes |
package com.wavesplatform import com.wavesplatform.serialization.Deser import org.scalatest.{FreeSpec, Matchers} class DeserializationTests extends FreeSpec with Matchers { "serializeArray" - { "works with arrays < 32k" in { val byteArray = Array.fill(Short.MaxValue)(0.toByte) Deser.serializeArrayWithLength(byteArray) should not be empty } "IllegalArgumentException thrown with arrays > 32k" in { val byteArray = Array.fill(Short.MaxValue + 1)(0.toByte) an[IllegalArgumentException] should be thrownBy Deser.serializeArrayWithLength(byteArray) } } }
Example 19
Source File: ClientSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network import java.util.concurrent.ConcurrentHashMap import com.wavesplatform.{TransactionGen, Version} import io.netty.buffer.{ByteBuf, Unpooled} import io.netty.channel.Channel import io.netty.channel.embedded.EmbeddedChannel import io.netty.channel.group.ChannelGroup import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import scala.concurrent.duration.DurationInt import scala.util.Random class ClientSpec extends FreeSpec with Matchers with MockFactory with TransactionGen { private val clientHandshake = new Handshake( applicationName = "wavesI", applicationVersion = Version.VersionTuple, nodeName = "test", nodeNonce = Random.nextInt(), declaredAddress = None ) private val serverHandshake = clientHandshake.copy(nodeNonce = Random.nextInt()) "should send only a local handshake on connection" in { val channel = createEmbeddedChannel(mock[ChannelGroup]) val sentClientHandshakeBuff = channel.readOutbound[ByteBuf]() Handshake.decode(sentClientHandshakeBuff) shouldBe clientHandshake channel.outboundMessages() shouldBe empty } "should add a server's channel to all channels after the handshake only" in { var channelWasAdded = false val allChannels = mock[ChannelGroup] (allChannels.add _).expects(*).onCall { _: Channel => channelWasAdded = true true } val channel = createEmbeddedChannel(allChannels) // skip the client's handshake channel.readOutbound[ByteBuf]() channelWasAdded shouldBe false val replyServerHandshakeBuff = Unpooled.buffer() serverHandshake.encode(replyServerHandshakeBuff) channel.writeInbound(replyServerHandshakeBuff) channelWasAdded shouldBe true } private def createEmbeddedChannel(allChannels: ChannelGroup) = new EmbeddedChannel( new HandshakeDecoder(PeerDatabase.NoOp), new HandshakeTimeoutHandler(1.minute), new HandshakeHandler.Client( handshake = clientHandshake, establishedConnections = new ConcurrentHashMap(), peerConnections = new ConcurrentHashMap(), peerDatabase = PeerDatabase.NoOp, allChannels = allChannels ) ) }
Example 20
Source File: MicroBlockInvSpecSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network import com.wavesplatform.common.state.ByteStr import com.wavesplatform.crypto._ import com.wavesplatform.{EitherMatchers, TransactionGen} import org.scalacheck.Gen import org.scalatest.concurrent.Eventually import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks => PropertyChecks} class MicroBlockInvSpecSpec extends FreeSpec with Matchers with EitherMatchers with PropertyChecks with Eventually with TransactionGen { private val microBlockInvGen: Gen[MicroBlockInv] = for { acc <- accountGen totalSig <- byteArrayGen(SignatureLength) prevBlockSig <- byteArrayGen(SignatureLength) } yield MicroBlockInv(acc, ByteStr(totalSig), ByteStr(prevBlockSig)) "MicroBlockInvMessageSpec" - { import MicroBlockInvSpec._ "deserializeData(serializedData(data)) == data" in forAll(microBlockInvGen) { inv => inv.signaturesValid() should beRight val restoredInv = deserializeData(serializeData(inv)).get restoredInv.signaturesValid() should beRight restoredInv shouldBe inv } } }
Example 21
Source File: HandshakeDecoderSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network import java.nio.charset.StandardCharsets import com.google.common.primitives.{Ints, Longs} import com.wavesplatform.{NoShrink, TransactionGen} import io.netty.buffer.Unpooled import io.netty.channel.embedded.EmbeddedChannel import io.netty.channel.{ChannelHandlerContext, ChannelInboundHandlerAdapter} import org.scalacheck.{Arbitrary, Gen} import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks => PropertyChecks} class HandshakeDecoderSpec extends FreeSpec with Matchers with MockFactory with PropertyChecks with TransactionGen with NoShrink { "should read a handshake and remove itself from the pipeline" in { var mayBeDecodedHandshake: Option[Handshake] = None val channel = new EmbeddedChannel( new HandshakeDecoder(PeerDatabase.NoOp), new ChannelInboundHandlerAdapter { override def channelRead(ctx: ChannelHandlerContext, msg: Any): Unit = msg match { case x: Handshake => mayBeDecodedHandshake = Some(x) case _ => } } ) val origHandshake = new Handshake( applicationName = "wavesI", applicationVersion = (1, 2, 3), nodeName = "test", nodeNonce = 4, declaredAddress = None ) val buff = Unpooled.buffer origHandshake.encode(buff) buff.writeCharSequence("foo", StandardCharsets.UTF_8) channel.writeInbound(buff) mayBeDecodedHandshake should contain(origHandshake) } private val invalidHandshakeBytes: Gen[Array[Byte]] = { // To bypass situations where the appNameLength > whole buffer and HandshakeDecoder waits for next bytes val appName = "x" * Byte.MaxValue val nodeName = "y" * Byte.MaxValue val appNameBytes = appName.getBytes(StandardCharsets.UTF_8) val versionBytes = Array(1, 2, 3).flatMap(Ints.toByteArray) val nodeNameBytes = nodeName.getBytes(StandardCharsets.UTF_8) val nonceBytes = Longs.toByteArray(1) val timestampBytes = Longs.toByteArray(System.currentTimeMillis() / 1000) val validDeclaredAddressLen = Set(0, 8, 20) val invalidBytesGen = Gen.listOfN(3, Arbitrary.arbByte.arbitrary).filter { case List(appNameLen, nodeNameLen, declaredAddressLen) => !(appNameLen == appNameBytes.size || nodeNameLen == nodeNameBytes.size || validDeclaredAddressLen.contains(declaredAddressLen)) case _ => false } invalidBytesGen.map { case List(appNameLen, nodeNameLen, declaredAddressLen) => Array(appNameLen) ++ appNameBytes ++ versionBytes ++ Array(nodeNameLen) ++ nodeNameBytes ++ nonceBytes ++ Array(declaredAddressLen) ++ timestampBytes } } "should blacklist a node sends an invalid handshake" in forAll(invalidHandshakeBytes) { bytes: Array[Byte] => val decoder = new SpiedHandshakeDecoder val channel = new EmbeddedChannel(decoder) val buff = Unpooled.buffer buff.writeBytes(bytes) channel.writeInbound(buff) decoder.blockCalls shouldBe >(0) } private class SpiedHandshakeDecoder extends HandshakeDecoder(PeerDatabase.NoOp) { var blockCalls = 0 override protected def block(ctx: ChannelHandlerContext, e: Throwable): Unit = { blockCalls += 1 } } }
Example 22
Source File: MessageCodecSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network import java.nio.charset.StandardCharsets import com.wavesplatform.TransactionGen import com.wavesplatform.transaction.assets.IssueTransaction import com.wavesplatform.transaction.{ProvenTransaction, Transaction} import io.netty.channel.ChannelHandlerContext import io.netty.channel.embedded.EmbeddedChannel import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks => PropertyChecks} class MessageCodecSpec extends FreeSpec with Matchers with MockFactory with PropertyChecks with TransactionGen { "should block a sender of invalid messages" in { val codec = new SpiedMessageCodec val ch = new EmbeddedChannel(codec) ch.writeInbound(RawBytes(TransactionSpec.messageCode, "foo".getBytes(StandardCharsets.UTF_8))) ch.readInbound[IssueTransaction]() codec.blockCalls shouldBe 1 } "should not block a sender of valid messages" in forAll(randomTransactionGen) { origTx: ProvenTransaction => val codec = new SpiedMessageCodec val ch = new EmbeddedChannel(codec) ch.writeInbound(RawBytes.fromTransaction(origTx)) val decodedTx = ch.readInbound[Transaction]() decodedTx shouldBe origTx codec.blockCalls shouldBe 0 } private class SpiedMessageCodec extends MessageCodec(PeerDatabase.NoOp) { var blockCalls = 0 override def block(ctx: ChannelHandlerContext, e: Throwable): Unit = { blockCalls += 1 } } }
Example 23
Source File: BrokenConnectionDetectorSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network import com.wavesplatform.TransactionGen import io.netty.channel.embedded.EmbeddedChannel import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks => PropertyChecks} import scala.concurrent.duration.DurationInt class BrokenConnectionDetectorSpec extends FreeSpec with Matchers with MockFactory with PropertyChecks with TransactionGen { "should not close an active connection until the timeout" in { val handler = new BrokenConnectionDetector(400.millis) val ch = new EmbeddedChannel(handler) ch.writeInbound("foo") Thread.sleep(200) ch.runPendingTasks() ch.isActive shouldBe true } "should not close a connection when messages are keep going" in { val handler = new BrokenConnectionDetector(100.millis) val ch = new EmbeddedChannel(handler) (1 to 3).foreach { _ => ch.writeInbound("bar") Thread.sleep(50) ch.runPendingTasks() } ch.isActive shouldBe true } "should close a broken connection" in { val handler = new BrokenConnectionDetector(200.millis) val ch = new EmbeddedChannel(handler) ch.writeInbound("bar") Thread.sleep(250) ch.runPendingTasks() ch.isActive shouldBe false } }
Example 24
Source File: ChannelGroupExtSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network import java.util.concurrent.ConcurrentHashMap import io.netty.channel._ import io.netty.channel.embedded.EmbeddedChannel import io.netty.channel.group.DefaultChannelGroup import io.netty.util.concurrent.GlobalEventExecutor import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import scala.jdk.CollectionConverters._ import scala.util.Random class ChannelGroupExtSpec extends FreeSpec with Matchers with MockFactory { "broadcast" - { "should not send a message to the excluded channels" in { val message = "test" val channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE) val received = ConcurrentHashMap.newKeySet[Int]() def receiver(id: Int): Channel = new EmbeddedChannel( new ChannelId { override def asShortText(): String = asLongText() override def asLongText(): String = id.toString override def compareTo(o: ChannelId): Int = o.asLongText().toInt - id }, new ChannelOutboundHandlerAdapter { override def write(ctx: ChannelHandlerContext, msg: scala.Any, promise: ChannelPromise): Unit = { received.add(id) super.write(ctx, msg, promise) } } ) val allIds = (0 to 5).toSet val allChannels = allIds.map(receiver) val excludedChannels = allChannels.filter(_ => Random.nextBoolean) val excludedIds = excludedChannels.map(_.id.asLongText().toInt) allChannels.foreach(channelGroup.add) channelGroup.broadcast(message, excludedChannels).syncUninterruptibly() received.asScala shouldBe (allIds -- excludedIds) } } }
Example 25
Source File: RequestsSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.api.http.requests import com.wavesplatform.account.KeyPair import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.transaction.transfer.TransferTransaction import com.wavesplatform.{NoShrink, TransactionGen} import org.scalacheck.Gen import org.scalatest.{FreeSpec, Matchers, OptionValues} import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import play.api.libs.json._ class RequestsSpec extends FreeSpec with Matchers with OptionValues with ScalaCheckPropertyChecks with TransactionGen with NoShrink { private def transferRequestGen(version: Int): Gen[(KeyPair, JsObject)] = (for { sender <- accountGen recipient <- accountGen proofs <- proofsGen } yield ( sender, Json.obj( "type" -> 4, "version" -> version, "senderPublicKey" -> sender.publicKey.toString, "assetId" -> JsNull, "attachment" -> "", "feeAssetId" -> JsNull, "timestamp" -> System.currentTimeMillis(), "fee" -> 100000, "amount" -> 10000, "recipient" -> recipient.publicKey.toAddress.stringRepr, "proofs" -> JsArray(proofs.proofs.map(p => JsString(p.toString))) ) )).label(s"Transfer Request v$version") "TransferRequest" - { "accepts proofs for version >= 2" in { TransferTransaction.supportedVersions.filter(_ >= 2).foreach { version => forAll(transferRequestGen(version)) { case (sender, json) => val request = json.as[TransferRequest] val tx = request.toTxFrom(sender.publicKey).explicitGet() request.proofs.value should be(tx.proofs) } } } } }
Example 26
Source File: CommonAccountApiSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.api.common import org.scalatest.{FreeSpec, Matchers} class CommonAccountApiSpec extends FreeSpec with Matchers { "Waves distribution" - { "height is taken into account" in pending "balances only present in diff are not ignored" in pending "ignores zero balances" in pending "diff is ignored when height is less than blockchain height" in pending "diff is applied when height matches blockchain height" in pending } "Asset distribution" - { "works for NFT" in pending "balances only present in diff are not ignored" in pending "ignores zero balances" in pending "returns balances only for requested asset" in pending "height is taken into account" in pending "diff is ignored when height is less than blockchain height" in pending "diff is applied when height matches blockchain height" in pending } "Active leases for address" - { "does not return leases which were cancelled in diff" in pending "includes new leases from diff" in pending } "Portfolio" - { "includes NFT balances when ReducedNFTFee feature is inactive" in pending "excludes NFT balances when ReducedNFTFee feature is active" - { "from diff" in pending "from leveldb" in pending } } "NFT list" - { "does not include NFTs which were spent in diff" in pending "includes NFTs which were received in diff" in pending } }
Example 27
Source File: UtxPoolSynchronizerSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network import java.util.concurrent.CountDownLatch import com.wavesplatform.account.PublicKey import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.lang.ValidationError import com.wavesplatform.settings.SynchronizationSettings.UtxSynchronizerSettings import com.wavesplatform.transaction.smart.script.trace.TracedResult import com.wavesplatform.transaction.{GenesisTransaction, Transaction} import com.wavesplatform.utils.Schedulers import io.netty.util.HashedWheelTimer import monix.execution.atomic.AtomicInt import monix.reactive.Observable import org.scalatest.{BeforeAndAfterAll, FreeSpec, Matchers} import scala.concurrent.duration._ class UtxPoolSynchronizerSpec extends FreeSpec with Matchers with BeforeAndAfterAll { private[this] val timer = new HashedWheelTimer private[this] val scheduler = Schedulers.timeBoundedFixedPool(timer, 1.second, 2, "test-utx-sync") "UtxPoolSynchronizer" - { val latch = new CountDownLatch(5) val counter = AtomicInt(10) def countTransactions(tx: Transaction): TracedResult[ValidationError, Boolean] = { if (counter.getAndDecrement() > 5) while (!Thread.currentThread().isInterrupted) {} else latch.countDown() TracedResult(Right(true)) } "accepts only those transactions from network which can be validated quickly" in withUPS(countTransactions) { ups => 1 to 10 foreach { i => ups.publish(GenesisTransaction.create(PublicKey(new Array[Byte](32)).toAddress, i * 10L, 0L).explicitGet()) } latch.await() counter.get() shouldEqual 0 } } private def withUPS(putIfNew: Transaction => TracedResult[ValidationError, Boolean])(f: UtxPoolSynchronizer => Unit): Unit = { val ups = new UtxPoolSynchronizerImpl(UtxSynchronizerSettings(1000, 2, 1000, true), putIfNew, (_, _) => (), Observable.empty, scheduler) f(ups) ups.close() } override protected def afterAll(): Unit = { super.afterAll() scheduler.shutdown() timer.stop() } }
Example 28
Source File: UtilsSpecification.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.utils import cats.Id import com.wavesplatform.lang.directives.DirectiveSet import com.wavesplatform.lang.directives.values.V3 import com.wavesplatform.lang.utils._ import com.wavesplatform.lang.v1.compiler.Terms.{FUNCTION_CALL, TRUE} import com.wavesplatform.lang.v1.compiler.Types.BOOLEAN import com.wavesplatform.lang.v1.evaluator.ctx.{EvaluationContext, UserFunction} import com.wavesplatform.lang.v1.traits.Environment import com.wavesplatform.state.diffs.smart.predef.chainId import com.wavesplatform.common.state.ByteStr import com.wavesplatform.transaction.smart.WavesEnvironment import monix.eval.Coeval import org.scalatest.{FreeSpec, Matchers} class UtilsSpecification extends FreeSpec with Matchers { private val environment = new WavesEnvironment(chainId, Coeval(???), null, EmptyBlockchain, null, DirectiveSet.contractDirectiveSet, ByteStr.empty) "estimate()" - { "handles functions that depend on each other" in { val callee = UserFunction[Environment]("callee", 0, BOOLEAN)(TRUE) val caller = UserFunction[Environment]("caller", 0, BOOLEAN)(FUNCTION_CALL(callee.header, List.empty)) val ctx = EvaluationContext.build[Id, Environment]( environment, typeDefs = Map.empty, letDefs = Map.empty, functions = Seq(caller, callee) ) estimate(V3, ctx).size shouldBe 2 } } }
Example 29
Source File: ObservedLoadingCacheSpecification.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.utils import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicLong import com.google.common.base.Ticker import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache} import com.wavesplatform.utils.ObservedLoadingCacheSpecification.FakeTicker import monix.execution.Ack import monix.reactive.Observer import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import scala.jdk.CollectionConverters._ import scala.concurrent.Future import scala.concurrent.duration.DurationInt class ObservedLoadingCacheSpecification extends FreeSpec with Matchers with MockFactory { private val ExpiringTime = 10.minutes "notifies" - { "on refresh" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() loadingCache.refresh("foo") } "on put" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() loadingCache.put("foo", 10) } "on putAll" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() (changes.onNext _).expects("bar").returning(Future.successful(Ack.Continue)).once() loadingCache.putAll(Map[String, Integer]("foo" -> 10, "bar" -> 11).asJava) } "on invalidate" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() loadingCache.invalidate("foo") } "on invalidateAll" in test { (loadingCache, changes, _) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() (changes.onNext _).expects("bar").returning(Future.successful(Ack.Continue)).once() loadingCache.invalidateAll(Seq("foo", "bar").asJava) } } "don't notify" - { "on cache expiration" in test { (loadingCache, changes, ticker) => (changes.onNext _).expects("foo").returning(Future.successful(Ack.Continue)).once() loadingCache.put("foo", 1) ticker.advance(ExpiringTime.toMillis + 100, TimeUnit.MILLISECONDS) } } private def test(f: (LoadingCache[String, Integer], Observer[String], FakeTicker) => Unit): Unit = { val changes = mock[Observer[String]] val ticker = new FakeTicker() val delegate = CacheBuilder .newBuilder() .expireAfterWrite(ExpiringTime.toMillis, TimeUnit.MILLISECONDS) .ticker(ticker) .build(new CacheLoader[String, Integer] { override def load(key: String): Integer = key.length }) val loadingCache = new ObservedLoadingCache(delegate, changes) f(loadingCache, changes, ticker) } } private object ObservedLoadingCacheSpecification { // see https://github.com/google/guava/blob/master/guava-testlib/src/com/google/common/testing/FakeTicker.java class FakeTicker extends Ticker { private val nanos = new AtomicLong() private var autoIncrementStepNanos = 0L def advance(time: Long, timeUnit: TimeUnit): FakeTicker = advance(timeUnit.toNanos(time)) def advance(nanoseconds: Long): FakeTicker = { nanos.addAndGet(nanoseconds) this } def setAutoIncrementStep(autoIncrementStep: Long, timeUnit: TimeUnit): FakeTicker = { require(autoIncrementStep >= 0, "May not auto-increment by a negative amount") this.autoIncrementStepNanos = timeUnit.toNanos(autoIncrementStep) this } override def read: Long = nanos.getAndAdd(autoIncrementStepNanos) } }
Example 30
Source File: CommonSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.state import com.wavesplatform.account.Address import com.wavesplatform.common.state.ByteStr import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.crypto.SignatureLength import com.wavesplatform.db.WithDomain import com.wavesplatform.lagonaki.mocks.TestBlock import com.wavesplatform.transaction.Asset.IssuedAsset import com.wavesplatform.transaction.GenesisTransaction import com.wavesplatform.{NoShrink, TestTime, TransactionGen} import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks => PropertyChecks} class CommonSpec extends FreeSpec with Matchers with WithDomain with TransactionGen with PropertyChecks with NoShrink { private val time = new TestTime private def nextTs = time.getTimestamp() private val AssetIdLength = 32 private def genesisBlock(genesisTs: Long, address: Address, initialBalance: Long) = TestBlock.create( genesisTs, ByteStr(Array.fill[Byte](SignatureLength)(0)), Seq(GenesisTransaction.create(address, initialBalance, genesisTs).explicitGet()) ) "Common Conditions" - { "Zero balance of absent asset" in forAll(accountGen, positiveLongGen, byteArrayGen(AssetIdLength)) { case (sender, initialBalance, assetId) => withDomain() { d => d.appendBlock(genesisBlock(nextTs, sender.toAddress, initialBalance)) d.balance(sender.toAddress, IssuedAsset(ByteStr(assetId))) shouldEqual 0L } } } }
Example 31
Source File: CommonAccountsApiSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.http import com.wavesplatform.api.common.CommonAccountsApi import com.wavesplatform.common.utils._ import com.wavesplatform.db.WithDomain import com.wavesplatform.features.BlockchainFeatures import com.wavesplatform.settings.TestFunctionalitySettings import com.wavesplatform.state.{DataEntry, Diff, StringDataEntry, diffs} import com.wavesplatform.transaction.{DataTransaction, GenesisTransaction} import com.wavesplatform.{BlocksTransactionsHelpers, TransactionGen, history} import monix.execution.Scheduler.Implicits.global import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks class CommonAccountsApiSpec extends FreeSpec with Matchers with WithDomain with TransactionGen with BlocksTransactionsHelpers with ScalaCheckDrivenPropertyChecks { "Data stream" - { "handles non-existent address" in { val entry1 = StringDataEntry("test", "test") val entry2 = StringDataEntry("test1", "test") val entry3 = StringDataEntry("test2", "test") val preconditions = for { acc <- accountGen ts = System.currentTimeMillis() fee <- smallFeeGen genesis = GenesisTransaction.create(acc.toAddress, diffs.ENOUGH_AMT, ts).explicitGet() data1 = DataTransaction.selfSigned(1.toByte, acc, Seq(entry1), fee, ts).explicitGet() data2 = DataTransaction.selfSigned(1.toByte, acc, Seq(entry2), fee, ts).explicitGet() data3 = DataTransaction.selfSigned(1.toByte, acc, Seq(entry3), fee, ts).explicitGet() (block1, mbs1) = UnsafeBlocks.unsafeChainBaseAndMicro(history.randomSig, Seq(genesis), Seq(Seq(data1)), acc, 3, ts) (block2, mbs2) = UnsafeBlocks.unsafeChainBaseAndMicro(mbs1.last.totalResBlockSig, Seq(data2), Seq(Seq(data3)), acc, 3, ts) } yield (acc, block1, mbs1.head, block2, mbs2.head) forAll(preconditions) { case (acc, block1, mb1, block2, mb2) => withDomain(domainSettingsWithFS(TestFunctionalitySettings.withFeatures(BlockchainFeatures.NG, BlockchainFeatures.DataTransaction))) { d => val commonAccountsApi = CommonAccountsApi(d.blockchainUpdater.bestLiquidDiff.getOrElse(Diff.empty), d.db, d.blockchainUpdater) def dataList(): Set[DataEntry[_]] = commonAccountsApi.dataStream(acc.toAddress, None).toListL.runSyncUnsafe().toSet d.appendBlock(block1) dataList() shouldBe empty d.appendMicroBlock(mb1) dataList() shouldBe Set(entry1) d.appendBlock(block2) dataList() shouldBe Set(entry1, entry2) d.appendMicroBlock(mb2) dataList() shouldBe Set(entry1, entry2, entry3) } } } } }
Example 32
Source File: OneDimensionalMiningConstraintSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.mining import com.wavesplatform.state.{Blockchain, Diff} import com.wavesplatform.transaction.Transaction import com.wavesplatform.{NoShrink, TransactionGen} import org.scalacheck.Gen import org.scalamock.scalatest.PathMockFactory import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks => PropertyChecks} class OneDimensionalMiningConstraintSuite extends FreeSpec with Matchers with PropertyChecks with PathMockFactory with TransactionGen with NoShrink { "OneDimensionalMiningConstraint" - { "should be full if the limit is 0, but not overfilled" in { val tank = createConstConstraint(0, 1, "const") tank.isFull shouldBe true tank.isOverfilled shouldBe false } "put(transaction)" - tests { (maxTxs, txs) => val constraint = createConstConstraint(maxTxs, transactionSize = 1, "txSize") txs.foldLeft(constraint)(_.put(stub[Blockchain], _, Diff.empty)) } } private def tests(toConstraint: (Int, List[Transaction]) => MiningConstraint): Unit = { val dontReachLimitGen: Gen[MiningConstraint] = for { maxTxs <- Gen.chooseNum(1, Int.MaxValue) txNumber <- Gen.chooseNum(0, maxTxs - 1) txs <- Gen.listOfN(math.min(txNumber, 15), randomTransactionGen) } yield toConstraint(maxTxs, txs) "multiple items don't reach the limit" in forAll(dontReachLimitGen) { updatedConstraint => updatedConstraint.isFull shouldBe false updatedConstraint.isOverfilled shouldBe false } val reachSoftLimitGen: Gen[MiningConstraint] = for { maxTxs <- Gen.chooseNum(1, 10) txs <- Gen.listOfN(maxTxs, randomTransactionGen) } yield toConstraint(maxTxs, txs) "multiple items reach the limit softly" in forAll(reachSoftLimitGen) { updatedConstraint => updatedConstraint.isFull shouldBe true updatedConstraint.isOverfilled shouldBe false } val reachHardLimitGen: Gen[MiningConstraint] = for { maxTxs <- Gen.chooseNum(1, 10) txNumber <- Gen.chooseNum(maxTxs + 1, maxTxs + 10) txs <- Gen.listOfN(txNumber, randomTransactionGen) } yield toConstraint(maxTxs, txs) "multiple items reach the limit with gap" in forAll(reachHardLimitGen) { updatedConstraint => updatedConstraint.isFull shouldBe true updatedConstraint.isOverfilled shouldBe true } } }
Example 33
Source File: GenesisBlockUpdateSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.events import com.wavesplatform.settings.WavesSettings import com.wavesplatform.state.diffs.ENOUGH_AMT import com.wavesplatform.transaction.GenesisTransaction import com.wavesplatform.{BlockGen, TestHelpers} import org.scalacheck.Gen import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import com.wavesplatform.common.utils.EitherExt2 class GenesisBlockUpdateSpec extends FreeSpec with Matchers with BlockGen with ScalaCheckPropertyChecks with EventsHelpers { override protected def settings: WavesSettings = TestHelpers.enableNG(super.settings) val genesisAppendWithWavesAmountGen: Gen[(BlockAppended, Long)] = for { master <- accountGen wavesAmount <- Gen.choose(1L, ENOUGH_AMT) gt = GenesisTransaction.create(master.toAddress, wavesAmount, 0).explicitGet() b <- blockGen(Seq(gt), master) ba = appendBlock(b) } yield (ba, wavesAmount) "on genesis block append" - { "master address balance gets correctly updated" in forAll(genesisAppendWithWavesAmountGen) { case (BlockAppended(_, _, _, _, _, upds), wavesAmount) => upds.head.balances.head._3 shouldBe wavesAmount } "updated Waves amount is calculated correctly" in forAll(genesisAppendWithWavesAmountGen) { case (BlockAppended(_, _, _, updatedWavesAmount, _, _), wavesAmount) => updatedWavesAmount shouldBe wavesAmount } } }
Example 34
Source File: PatchJsonSpec.scala From patchless with Apache License 2.0 | 5 votes |
package patchless.circe import cats.syntax.either._ import org.scalatest.{FreeSpec, Matchers} import io.circe.parser.parse import patchless.Patch import shapeless.record.Record import io.circe.generic.auto._ class PatchJsonSpec extends FreeSpec with Matchers { "Decoder" - { "Auto" in { case class Foo(aString: String, bInt: Int, cBoolean: Boolean) def parsePatch(str: String) = parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _) val aPatched = parsePatch("""{"aString": "patched"}""") val bPatched = parsePatch("""{"bInt": 22}""") val cPatched = parsePatch("""{"cBoolean": false}""") aPatched.updates shouldEqual Record(aString = Some("patched"), bInt = None, cBoolean = None) bPatched.updates shouldEqual Record(aString = None, bInt = Some(22), cBoolean = None) cPatched.updates shouldEqual Record(aString = None, bInt = None, cBoolean = Some(false)) } "Options" in { case class Foo(aString: Option[String]) def parsePatch(str: String) = parse(str).valueOr(throw _).as[Patch[Foo]].valueOr(throw _) val aPatchedSome = parsePatch("""{"aString": "patched"}""") val aPatchedNone = parsePatch("""{"aString": null}""") aPatchedSome.updates shouldEqual Record(aString = Some(Some("patched"))) aPatchedNone.updates shouldEqual Record(aString = Some(None)) } } }
Example 35
Source File: PortFilesSpec.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ports import java.nio.file.{Path, Paths} import java.util.UUID import com.daml.ports.PortFiles.FileAlreadyExists import org.scalatest.{FreeSpec, Inside, Matchers} import scalaz.{-\/, \/-} class PortFilesSpec extends FreeSpec with Matchers with Inside { "Can create a port file with a unique file name" in { val path = uniquePath() inside(PortFiles.write(path, Port(1024))) { case \/-(()) => } path.toFile.exists() shouldBe true } "Cannot create a port file with a nonunique file name" in { val path = uniquePath() inside(PortFiles.write(path, Port(1024))) { case \/-(()) => } inside(PortFiles.write(path, Port(1024))) { case -\/(FileAlreadyExists(p)) => p shouldBe path } } private def uniquePath(): Path = { val fileName = s"${this.getClass.getSimpleName}-${UUID.randomUUID().toString}.dummy" Paths.get(fileName) } }
Example 36
Source File: SnappyTest.scala From mantis with Apache License 2.0 | 5 votes |
package io.iohk.ethereum.snappy import io.iohk.ethereum.domain.{Block, Blockchain, Receipt} import io.iohk.ethereum.utils.Logger import org.scalatest.{FreeSpec, Matchers} import scala.concurrent.duration._ class SnappyTest extends FreeSpec with Matchers with Logger { val config = Config() val pre = new Prerequisites(config) import pre._ "Blockchain regression test" in { val startN = targetBlockchain match { case Some(tb) => config.startBlock.getOrElse(findHighestBlockNumber(tb) - 1).max(1) case None => BigInt(1) } val targetN = config.targetBlock.getOrElse(findHighestBlockNumber(sourceBlockchain)).max(1) val progLog = new ProgressLogger(startN, targetN, 5.seconds) progLog.start() for (n <- startN to targetN) { val block: Block = sourceBlockchain.getBlockByNumber(n) .getOrElse(fail(s"Failed to retrieve block by number: $n")) val expectedReceipts = sourceBlockchain.getReceiptsByHash(block.header.hash) .getOrElse(fail(s"Failed to retrieve receipts for block number: $n")) val result = executeBlock(block) result match { case Left(error) => fail(s"Failed to execute block $n: $error") case Right(receipts) => if (receipts == expectedReceipts) { targetBlockchain.foreach(_.save(block)) targetBlockchain.foreach(_.save(block.header.hash, receipts)) } else { fail(s"Block $n did not execute correctly.\n$receipts did not equal $expectedReceipts") } } progLog.update(n) } } private def executeBlock(block: Block): Either[Any, Seq[Receipt]] = targetBlockchain match { case Some(_) => ledger.executeBlock(block) case None => // this seems to discard failures, for better errors messages we might want to implement a different method (simulateBlock?) val result = ledger.prepareBlock(block) Right(result.blockResult.receipts) } // TODO: replace with blockchain.getBestBlockNumber() - not implemented yet private def findHighestBlockNumber(blockchain: Blockchain, n: BigInt = 1000000, bottom: BigInt = 0, top: BigInt = -1): BigInt = { if (top - bottom == 1) n else if (top < 0) { def candidates(n: BigInt): Stream[BigInt] = n #:: candidates(n + 1000000) val newTop = candidates(1).find(n => blockchain.getBlockByNumber(n).isEmpty).get findHighestBlockNumber(blockchain, newTop / 2, 0, newTop) } else { val (newBottom, newTop) = blockchain.getBlockByNumber(n) match { case Some(_) => (n, top) case None => (bottom, n) } findHighestBlockNumber(blockchain, (bottom + top) / 2, newBottom, newTop) } } }
Example 37
Source File: CirceJSONSerializerTest.scala From scala-json-rpc with MIT License | 5 votes |
package io.github.shogowada.scala.jsonrpc.serializers import org.scalatest.{FreeSpec, Matchers, OneInstancePerTest} class CirceJSONSerializerTest extends FreeSpec with OneInstancePerTest with Matchers { override def newInstance = new CirceJSONSerializerTest val jsonSerializer = CirceJSONSerializer() "given I have a plain case class" - { case class Test(helloWorld: String) val expectedDeserialized = Test("hello world") val expectedSerialized = """{"helloWorld":"hello world"}""" s"when I serialize $expectedDeserialized" - { val serialized = jsonSerializer.serialize(expectedDeserialized) "then it should serialize into JSON" in { serialized should equal(Some(expectedSerialized)) } } s"when I deserialize $expectedSerialized" - { val deserialized = jsonSerializer.deserialize[Test](expectedSerialized) "then it should deserialize into the case class" in { deserialized should equal(Some(expectedDeserialized)) } } } }
Example 38
Source File: Stackoverflow58206168.scala From Binding.scala with MIT License | 5 votes |
package com.thoughtworks.binding package regression import org.scalatest.{FreeSpec, Matchers} import Binding._ import scala.collection.mutable import Binding.BindingInstances.functorSyntax._ import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should.Matchers final class Stackoverflow58206168 extends AnyFreeSpec with Matchers { // See https://stackoverflow.com/questions//binding-scala-vars-bind-seems-to-not-work-correctly "Binding.scala: Vars.bind seems to not work correctly" in { val events = mutable.Buffer.empty[List[Int]] val test: Vars[Int] = Vars(1, 2, 3, 4) test.all.map { events += _.toList }.watch() test.value.append(1111) assert(events == mutable.Buffer(List(1, 2, 3, 4), List(1, 2, 3, 4, 1111))) } }
Example 39
Source File: ParquetWriterItSpec.scala From parquet4s with MIT License | 5 votes |
package com.github.mjakubowski84.parquet4s import java.nio.file.Files import org.apache.parquet.hadoop.ParquetFileWriter import org.scalatest.{BeforeAndAfter, FreeSpec, Matchers} import scala.util.Random class ParquetWriterItSpec extends FreeSpec with Matchers with BeforeAndAfter { case class Record(i: Int, d: Double, s: String) object Record { def random(n: Int): Seq[Record] = (1 to n).map(_ => Record(Random.nextInt(), Random.nextDouble(), Random.nextString(10))) } private val tempDir = com.google.common.io.Files.createTempDir().toPath.toAbsolutePath private val writePath = tempDir.resolve("file.parquet") // Generate records and do a single batch write. private val records = Record.random(5000) private def readRecords: Seq[Record] = { val iter = ParquetReader.read[Record](writePath.toString) try iter.toSeq finally iter.close() } after { // Delete written files Files.deleteIfExists(writePath) } "Batch write should result in proper number of records in the file" in { ParquetWriter.writeAndClose(writePath.toString, records) readRecords should be(records) } "Multiple incremental writes produce same result as a single batch write" in { val w = ParquetWriter.writer[Record](writePath.toString) try records.grouped(5).foreach(w.write) finally w.close() readRecords shouldBe records } "Writing record by record works as well" in { val w = ParquetWriter.writer[Record](writePath.toString) try records.foreach(record => w.write(record)) finally w.close() readRecords shouldBe records } "Incremental writes work with write mode OVERWRITE" in { val w = ParquetWriter.writer[Record]( writePath.toString, ParquetWriter.Options(ParquetFileWriter.Mode.OVERWRITE)) try records.grouped(5).foreach(w.write) finally w.close() readRecords shouldBe records } "Writing to closed writer throws an exception" in { val w = ParquetWriter.writer[Record](writePath.toString) w.close() an[IllegalStateException] should be thrownBy records .grouped(2) .foreach(w.write) } "Closing writer without writing anything to it throws no exception" in { val w = ParquetWriter.writer[Record](writePath.toString) noException should be thrownBy w.close() } "Closing writer twice throws no exception" in { val w = ParquetWriter.writer[Record](writePath.toString) noException should be thrownBy w.close() noException should be thrownBy w.close() } }
Example 40
Source File: ParquetWriterAndParquetReaderCompatibilityItSpec.scala From parquet4s with MIT License | 5 votes |
package com.github.mjakubowski84.parquet4s import com.github.mjakubowski84.parquet4s.Case.CaseDef import com.github.mjakubowski84.parquet4s.CompatibilityParty._ import org.scalatest.{BeforeAndAfter, FreeSpec, Matchers} class ParquetWriterAndParquetReaderCompatibilityItSpec extends FreeSpec with Matchers with BeforeAndAfter with TestUtils { before { clearTemp() } private def runTestCase(testCase: CaseDef): Unit = { testCase.description in { ParquetWriter.writeAndClose(tempPathString, testCase.data)(testCase.writerFactory) val parquetIterable = ParquetReader.read(tempPathString)(testCase.reader) try { parquetIterable should contain theSameElementsAs testCase.data } finally { parquetIterable.close() } } } "Spark should be able to read file saved by ParquetWriter if the file contains" - { CompatibilityTestCases.cases(Writer, Reader).foreach(runTestCase) } }
Example 41
Source File: ParquetWriterAndSparkCompatibilityItSpec.scala From parquet4s with MIT License | 5 votes |
package com.github.mjakubowski84.parquet4s import com.github.mjakubowski84.parquet4s.Case.CaseDef import com.github.mjakubowski84.parquet4s.CompatibilityParty._ import org.scalatest.{BeforeAndAfter, FreeSpec, Matchers} class ParquetWriterAndSparkCompatibilityItSpec extends FreeSpec with Matchers with BeforeAndAfter with SparkHelper { before { clearTemp() } private def runTestCase(testCase: CaseDef): Unit = testCase.description in { ParquetWriter.writeAndClose(tempPathString, testCase.data)(testCase.writerFactory) readFromTemp(testCase.typeTag) should contain theSameElementsAs testCase.data } "Spark should be able to read file saved by ParquetWriter if the file contains" - { CompatibilityTestCases.cases(Writer, Spark).foreach(runTestCase) } }
Example 42
Source File: SparkAndParquetReaderCompatibilityItSpec.scala From parquet4s with MIT License | 5 votes |
package com.github.mjakubowski84.parquet4s import com.github.mjakubowski84.parquet4s.CompatibilityParty._ import org.scalatest.{BeforeAndAfter, FreeSpec, Matchers} class SparkAndParquetReaderCompatibilityItSpec extends FreeSpec with Matchers with BeforeAndAfter with SparkHelper { before { clearTemp() } private def runTestCase(testCase: Case.CaseDef): Unit = testCase.description in { writeToTemp(testCase.data)(testCase.typeTag) val parquetIterable = ParquetReader.read(tempPathString)(testCase.reader) try { parquetIterable should contain theSameElementsAs testCase.data } finally { parquetIterable.close() } } "ParquetReader should be able to read file saved by Spark if the file contains" - { CompatibilityTestCases.cases(Spark, Reader).foreach(runTestCase) } }
Example 43
Source File: ParquetWriterAndSparkCompatibilityItSpec.scala From parquet4s with MIT License | 5 votes |
package com.github.mjakubowski84.parquet4s import com.github.mjakubowski84.parquet4s.Case.CaseDef import com.github.mjakubowski84.parquet4s.CompatibilityParty._ import org.scalatest.{BeforeAndAfter, FreeSpec, Matchers} class ParquetWriterAndSparkCompatibilityItSpec extends FreeSpec with Matchers with BeforeAndAfter with SparkHelper { before { clearTemp() } private def runTestCase(testCase: CaseDef): Unit = testCase.description in { ParquetWriter.writeAndClose(tempPathString, testCase.data)(testCase.writerFactory) readFromTemp(testCase.typeTag) should contain theSameElementsAs testCase.data } "Spark should be able to read file saved by ParquetWriter if the file contains" - { CompatibilityTestCases.cases(Writer, Spark).foreach(runTestCase) } }
Example 44
Source File: SparkAndParquetReaderCompatibilityItSpec.scala From parquet4s with MIT License | 5 votes |
package com.github.mjakubowski84.parquet4s import com.github.mjakubowski84.parquet4s.CompatibilityParty._ import org.scalatest.{BeforeAndAfter, FreeSpec, Matchers} class SparkAndParquetReaderCompatibilityItSpec extends FreeSpec with Matchers with BeforeAndAfter with SparkHelper { before { clearTemp() } private def runTestCase(testCase: Case.CaseDef): Unit = testCase.description in { writeToTemp(testCase.data)(testCase.typeTag) val parquetIterable = ParquetReader.read(tempPathString)(testCase.reader) try { parquetIterable should contain theSameElementsAs testCase.data } finally { parquetIterable.close() } } "ParquetReader should be able to read file saved by Spark if the file contains" - { CompatibilityTestCases.cases(Spark, Reader).foreach(runTestCase) } }
Example 45
Source File: PathGroupTest.scala From docless with MIT License | 5 votes |
package com.timeout.docless.swagger import org.scalatest.{FreeSpec, Inside, Matchers} import cats.data.NonEmptyList import cats.data.Validated import SchemaError._ import com.timeout.docless.schema.JsonSchema import com.timeout.docless.schema.JsonSchema._ import com.timeout.docless.swagger.Method._ class PathGroupTest extends FreeSpec with Matchers { "PathGroup" - { val petstore = PetstoreSchema() val pet = PetstoreSchema.Schemas.pet val paths = Path("/example") :: petstore.paths.get.toList val defs = petstore.definitions.get.toList val defsNoPet = defs.filterNot(_.id === pet.id) val params = petstore.parameters.get.toList val group1 = PathGroup(paths, defs, params) val group2 = PathGroup(List(Path("/extra")), Nil, Nil) val groupMissingErr = PathGroup(paths, defsNoPet, params) def err(path: String, m: Method, f: Definition => Ref): SchemaError = missingDefinition(RefWithContext.response(f(pet.definition), m, path)) "aggregate" - { "when some top level definitions are missing" - { "returns the missing refs" in { PathGroup.aggregate(petstore.info, List(groupMissingErr)) should ===( Validated.invalid[NonEmptyList[SchemaError], APISchema]( NonEmptyList.of( err("/pets", Get, ArrayRef.apply), err("/pets", Post, TypeRef.apply), err("/pets/{id}", Get, TypeRef.apply), err("/pets/{id}", Delete, TypeRef.apply) ) ) ) } } "when some nested definitions are missing" - { val info = Info("example") case class Nested(name: String) case class TopLevel(nested: Nested) val schema = JsonSchema.deriveFor[TopLevel] val nested = schema.relatedDefinitions.head val paths = List( "/example".Post( Operation('_, "...") .withParams(BodyParameter(schema = Some(schema.asRef))) ) ) val withNested = PathGroup(paths, schema.definitions.toList, Nil) val withoutNested = PathGroup(paths, List(schema.definition), Nil) "returns the missing refs" in { PathGroup.aggregate(info, List(withNested)).isValid shouldBe true PathGroup.aggregate(info, List(withoutNested)) should ===( Validated.invalid[NonEmptyList[SchemaError], APISchema]( NonEmptyList.of( MissingDefinition( RefWithContext.definition(nested.asRef, schema.definition) ) ) ) ) } } "when no definition is missing" - { "returns a valid api schema" in new Inside { inside(PathGroup.aggregate(petstore.info, List(group1, group2))) { case Validated.Valid(schema) => schema.info should ===(petstore.info) schema.paths.get should ===(group1.paths ++ group2.paths) schema.definitions.get should ===( group1.definitions ++ group2.definitions ) schema.parameters.get should ===(group1.params ++ group2.params) } } } } } }
Example 46
Source File: SwaggerTest.scala From docless with MIT License | 5 votes |
package com.timeout.docless.swagger import com.github.fge.jackson.JsonLoader import com.github.fge.jsonschema.main.JsonSchemaFactory import io.circe._ import io.circe.syntax._ import org.scalatest.FreeSpec import scala.collection.JavaConverters._ class SwaggerTest extends FreeSpec { "Can build and serialise a swagger object" in { val petstoreSchema = PetstoreSchema() val json = JsonLoader.fromResource("/swagger-schema.json") val schema = JsonSchemaFactory.byDefault().getJsonSchema(json) val printer = Printer.spaces2.copy(dropNullKeys = true) val jsonS = printer.pretty(petstoreSchema.asJson) val report = schema.validate(JsonLoader.fromString(jsonS)) val err = System.err if (!report.isSuccess) { err.println(jsonS) err.println( "============== Validation errors ================================" ) val errors = report.asScala.toList errors.foreach(err.println) fail() } } }
Example 47
Source File: PlainEnumTest.scala From docless with MIT License | 5 votes |
package com.timeout.docless.schema.derive import com.timeout.docless.schema.PlainEnum import org.scalatest.{FreeSpec, Matchers} import PlainEnum.IdFormat class PlainEnumTest extends FreeSpec with Matchers { sealed trait TheEnum case object EnumA extends TheEnum case object EnumB extends TheEnum sealed trait TheADT case class X(a: Int) extends TheADT case object Y extends TheADT "Enum" - { "handles sealed traits of case objects only" in { val enum = PlainEnum[TheEnum] enum.ids should ===(List("EnumA", "EnumB")) } "allows to override format to lowercase" in { implicit val format: IdFormat = IdFormat.LowerCase val enum = PlainEnum[TheEnum] enum.ids should ===(List("enuma", "enumb")) } "allows to override format to uppercase" in { implicit val format: IdFormat = IdFormat.UpperCase val enum = PlainEnum[TheEnum] enum.ids should ===(List("ENUMA", "ENUMB")) } "allows to override format to snake case" in { implicit val format: IdFormat = IdFormat.SnakeCase val enum = PlainEnum[TheEnum] enum.ids should ===(List("enum_a", "enum_b")) } "allows to override format to upper snake case" in { implicit val format: IdFormat = IdFormat.UpperSnakeCase val enum = PlainEnum[TheEnum] enum.ids should ===(List("ENUM_A", "ENUM_B")) } "doesn't typecheck on ADTs" in { """ PlainEnum[TheADT] """.stripMargin shouldNot typeCheck } } }
Example 48
Source File: ResetInverterSpec.scala From barstools with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See LICENSE for license details. package barstools.tapeout.transforms import chisel3._ import firrtl._ import org.scalatest.{FreeSpec, Matchers} class ExampleModuleNeedsResetInverted extends Module with ResetInverter { val io = IO(new Bundle { val out = Output(UInt(32.W)) }) val r = RegInit(0.U) io.out := r invert(this) } class ResetNSpec extends FreeSpec with Matchers { "Inverting reset needs to be done throughout module" in { val optionsManager = new ExecutionOptionsManager("dsptools") with HasChiselExecutionOptions with HasFirrtlOptions { firrtlOptions = firrtlOptions.copy(compilerName = "low", customTransforms = List(new ResetInverterTransform)), } chisel3.Driver.execute(optionsManager, () => new ExampleModuleNeedsResetInverted) match { case ChiselExecutionSuccess(_, chirrtl, Some(FirrtlExecutionSuccess(_, firrtl))) => chirrtl should include ("input reset :") chirrtl should not include "input reset_n :" chirrtl should not include "node reset = not(reset_n)" firrtl should include ("input reset_n :") firrtl should include ("node reset = not(reset_n)") firrtl should not include "input reset :" case _ => // bad } } }
Example 49
Source File: KinesisSinkGraphStageIntegrationSpec.scala From reactive-kinesis with Apache License 2.0 | 5 votes |
package com.weightwatchers.reactive.kinesis.stream import akka.stream.scaladsl.Source import com.weightwatchers.reactive.kinesis.common.{ AkkaUnitTestLike, KinesisConfiguration, KinesisSuite } import com.weightwatchers.reactive.kinesis.models.ProducerEvent import org.scalatest.{FreeSpec, Matchers} import scala.concurrent.duration._ class KinesisSinkGraphStageIntegrationSpec extends FreeSpec with KinesisSuite with KinesisConfiguration with AkkaUnitTestLike with Matchers { "KinesisSinkGraph" - { "produced messages are written to the stream" in new withKinesisConfForApp("sink_produce") { val messageCount = 100 val elements = 1.to(messageCount).map(_.toString) Source(elements) .map(num => ProducerEvent(num, num)) .runWith(Kinesis.sink(producerConf())) .futureValue val list = testConsumer.retrieveRecords(TestStreamName, messageCount) list should contain allElementsOf elements testConsumer.shutdown() } "upstream fail should fail the materialized value of the sink" in new withKinesisConfForApp( "sink_fail" ) { Source .failed(new IllegalStateException("Boom")) .runWith(Kinesis.sink(producerConf())) .failed .futureValue shouldBe a[IllegalStateException] } } // do not create messages in setup, we will create messages inside the test override def TestStreamNrOfMessagesPerShard: Long = 0 override implicit def patienceConfig: PatienceConfig = PatienceConfig(60.seconds, 1.second) }
Example 50
Source File: KinesisProducerIntegrationSpec.scala From reactive-kinesis with Apache License 2.0 | 5 votes |
package com.weightwatchers.reactive.kinesis import java.io.File import com.amazonaws.services.kinesis.producer.{KinesisProducer => AWSKinesisProducer} import com.typesafe.config.ConfigFactory import com.weightwatchers.reactive.kinesis.common.{ KinesisSuite, KinesisTestConsumer, TestCredentials } import com.weightwatchers.reactive.kinesis.consumer.KinesisConsumer.ConsumerConf import com.weightwatchers.reactive.kinesis.models.ProducerEvent import com.weightwatchers.reactive.kinesis.producer.{KinesisProducer, ProducerConf} import org.scalatest.concurrent.Eventually import org.scalatest.mockito.MockitoSugar import org.scalatest.time.{Millis, Seconds, Span} import org.scalatest.{BeforeAndAfterAll, FreeSpec, Matchers} import scala.concurrent.duration._ import scala.language.postfixOps import scala.util.Random //scalastyle:off magic.number class KinesisProducerIntegrationSpec extends FreeSpec with Matchers with MockitoSugar with BeforeAndAfterAll with Eventually with KinesisSuite { implicit val ece = scala.concurrent.ExecutionContext.global val TestStreamNrOfMessagesPerShard: Long = 0 implicit override val patienceConfig: PatienceConfig = PatienceConfig(timeout = Span(5, Seconds), interval = Span(100, Millis)) "The KinesisProducer" - { "Should publish a message to a stream" in new withKinesisConfForApp( "int-test-stream-producer-1" ) { val conf = producerConf() val producer = KinesisProducer(conf) val existingRecordCount = testConsumer.retrieveRecords(conf.streamName, 10).size val event = ProducerEvent("1234", Random.alphanumeric.take(10).mkString) producer.addUserRecord(event) eventually { val records: Seq[String] = testConsumer.retrieveRecords(conf.streamName, 10) records.size shouldBe (existingRecordCount + 1) records should contain( new String(event.payload.array(), java.nio.charset.StandardCharsets.UTF_8) ) } } } } //scalastyle:on
Example 51
Source File: TypesafeConfigExtensionsSpec.scala From reactive-kinesis with Apache License 2.0 | 5 votes |
package com.weightwatchers.reactive.kinesis.utils import com.typesafe.config.ConfigFactory import org.scalatest.mockito.MockitoSugar import org.scalatest.{BeforeAndAfterAll, FreeSpec, Matchers} class TypesafeConfigExtensionsSpec extends FreeSpec with Matchers with MockitoSugar with BeforeAndAfterAll { val kplConfig = ConfigFactory.parseString(""" |kpl { | AggregationEnabled = true | AggregationMaxCount = 4294967295 | AggregationMaxSize = 51200 | CollectionMaxCount = 500 |} | """.stripMargin).getConfig("kpl") //scalastyle:off magic.number "The RichConfig" - { "Should convert typesafe config key values into Java Properties" in { import TypesafeConfigExtensions._ val javaProperties = kplConfig.toProperties javaProperties.size() should equal(4) javaProperties.getProperty("AggregationEnabled") should equal("true") javaProperties.getProperty("AggregationMaxCount") should equal("4294967295") javaProperties.getProperty("AggregationMaxSize") should equal("51200") javaProperties.getProperty("CollectionMaxCount") should equal("500") } } //scalastyle:on }
Example 52
Source File: ReadsRecoverOpsSpec.scala From play-json-ops with MIT License | 5 votes |
package play.api.libs.json.ops.v4 import org.scalatest.FreeSpec import org.scalatest.Matchers._ import play.api.data.validation.ValidationError import play.api.libs.json._ class ReadsRecoverOpsSpec extends FreeSpec { private val it = classOf[ReadsRecoverOps[_]].getSimpleName s"$it.recoverJsError should recover from exceptions with a JsError" in { val readStringAsInt = Reads.of[String].map(_.toInt).recoverJsError readStringAsInt.reads(JsString("not a number")) match { case JsError(Seq((JsPath, Seq(ValidationError(Seq(message), ex))))) => assertResult("error.expected.int")(message) ex shouldBe a[NumberFormatException] case o => fail(s"Expected a single error message with a single exception, not $o") } } s"$it.recoverWith should throw any exception not caught by the partial function" in { val readStringAsIntInverted = Reads.of[String].map(1 / _.toInt).recoverWith { case ex: NumberFormatException => RecoverOps.expectedTypeError(classOf[Int], ex) } // it should catch format exceptions assert(readStringAsIntInverted.reads(JsString("not a number")).isError) // but math exceptions are uncaught an[ArithmeticException] shouldBe thrownBy { readStringAsIntInverted.reads(JsString("0")) } } s"$it.recoverTotal should call the recover function" in { val readStringAsIntInverted = Reads.of[String].map(_.toInt.abs).recoverTotal(_ => -1) assertResult(JsSuccess(-1))(readStringAsIntInverted.reads(JsString("not a number"))) } }
Example 53
Source File: JsonImplicitsSpec.scala From play-json-ops with MIT License | 5 votes |
package play.api.libs.json.ops.v4 import org.scalacheck.{Arbitrary, Gen, Shrink} import org.scalacheck.ops._ import org.scalatest.FreeSpec import play.api.libs.json.{Format, Json} import org.scalatest.prop.GeneratorDrivenPropertyChecks._ case class KeyWrapper(key: String) object KeyWrapper { implicit val arbKeyWrapper: Arbitrary[KeyWrapper] = Arbitrary(Gen.string.map(KeyWrapper(_))) implicit val shrinkKeyWrapper: Shrink[KeyWrapper] = Shrink { k => Shrink.shrinkString.shrink(k.key).map(KeyWrapper(_)) } implicit lazy val format: Format[KeyWrapper] = Format.asString(convertFromString, _.key) implicit val writeKey: WritesKey[KeyWrapper] = WritesKey(_.key) implicit val readKey: ReadsKey[KeyWrapper] = ReadsKey.of[String].map(KeyWrapper(_)) implicit lazy val convertFromString: String => KeyWrapper = KeyWrapper(_) } class JsonImplicitsSpec extends FreeSpec { private val exampleJson = Json.obj( "A" -> "value", "B" -> "other" ) private val exampleMap = Map( KeyWrapper("A") -> "value", KeyWrapper("B") -> "other" ) private val exampleMapFormat = Format.of[Map[KeyWrapper, String]] "explicit call to write should format the Json correctly" in { assertResult(exampleJson) { exampleMapFormat.writes(exampleMap) } } "explicit call to read should read correctly formatted Json" in { assertResult(exampleMap) { exampleMapFormat.reads(exampleJson).recoverTotal { err => throw InvalidJsonException[Map[KeyWrapper, String]](exampleJson, err) } } } "formatter should read every value it writes and write it out the same way" in { forAll { value: Map[String, String] => val keyWrappedMap = value.map { case (k, v) => (KeyWrapper(k), v) } val json = exampleMapFormat.writes(keyWrappedMap) val parsedMap = exampleMapFormat.reads(json).recoverTotal { err => throw InvalidJsonException[Map[KeyWrapper, String]](json, err) } assertResult(keyWrappedMap)(parsedMap) } } }
Example 54
Source File: ReadsRecoverOpsSpec.scala From play-json-ops with MIT License | 5 votes |
package play.api.libs.json.ops.v4 import org.scalatest.FreeSpec import org.scalatest.Matchers._ import play.api.libs.json._ class ReadsRecoverOpsSpec extends FreeSpec { private val it = classOf[ReadsRecoverOps[_]].getSimpleName s"$it.recoverJsError should recover from exceptions with a JsError" in { val readStringAsInt = Reads.of[String].map(_.toInt).recoverJsError readStringAsInt.reads(JsString("not a number")) match { case JsError(Seq((JsPath, Seq(JsonValidationError(Seq(message), ex))))) => assertResult("error.expected.int")(message) ex shouldBe a[NumberFormatException] case o => fail(s"Expected a single error message with a single exception, not $o") } } s"$it.recoverWith should throw any exception not caught by the partial function" in { val readStringAsIntInverted = Reads.of[String].map(1 / _.toInt).recoverWith { case ex: NumberFormatException => RecoverOps.expectedTypeError(classOf[Int], ex) } // it should catch format exceptions assert(readStringAsIntInverted.reads(JsString("not a number")).isError) // but math exceptions are uncaught an[ArithmeticException] shouldBe thrownBy { readStringAsIntInverted.reads(JsString("0")) } } s"$it.recoverTotal should call the recover function" in { val readStringAsIntInverted = Reads.of[String].map(_.toInt.abs).recoverTotal(_ => -1) assertResult(JsSuccess(-1))(readStringAsIntInverted.reads(JsString("not a number"))) } }
Example 55
Source File: BenchmarksCheck.scala From arrows with Apache License 2.0 | 5 votes |
package benchmarks import org.scalatest.FreeSpec class BenchmarksCheck extends FreeSpec { "all benchmarks should return the same result" - { "SyncSuccessOnlyBenchmarks" in { (new SyncSuccessOnlyBenchmarks).checkImpls() } "SyncWithFailuresBenchmarks" in { (new SyncWithFailuresBenchmarks).checkImpls() } "AsyncSuccessOnlyBenchmarks" in { (new SyncWithFailuresBenchmarks).checkImpls() } "AsyncWithFailuresBenchmarks" in { (new SyncWithFailuresBenchmarks).checkImpls() } } }
Example 56
Source File: FixedPrecisionChangerSpec.scala From dsptools with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See LICENSE for license details. package dsptools.numbers import chisel3._ import chisel3.experimental.FixedPoint import dsptools.DspTester import org.scalatest.{FreeSpec, Matchers} //scalastyle:off magic.number class FixedPrecisionChanger(inWidth: Int, inBinaryPoint: Int, outWidth: Int, outBinaryPoint: Int) extends Module { val io = IO(new Bundle { val in = Input(FixedPoint(inWidth.W, inBinaryPoint.BP)) val out = Output(FixedPoint(outWidth.W, outBinaryPoint.BP)) }) val reg = Reg(FixedPoint()) reg := io.in io.out := reg } class FixedPointTruncatorTester(c: FixedPrecisionChanger, inValue: Double, outValue: Double) extends DspTester(c) { poke(c.io.in, inValue) step(1) expect(c.io.out, outValue, s"got ${peek(c.io.out)} should have $outValue") } class RemoveMantissa(inWidth: Int, inBinaryPoint: Int, outWidth: Int, outBinaryPoint: Int) extends Module { val io = IO(new Bundle { val in = Input(FixedPoint(inWidth.W, inBinaryPoint.BP)) val out = Output(FixedPoint(outWidth.W, 0.BP)) }) val reg = Reg(FixedPoint()) reg := io.in io.out := reg//.setBinaryPoint(0) } class RemoveMantissaTester(c: RemoveMantissa, inValue: Double, outValue: Double) extends DspTester(c) { poke(c.io.in, inValue) step(1) expect(c.io.out, outValue, s"got ${peek(c.io.out)} should have $outValue") } class FixedPrecisionChangerSpec extends FreeSpec with Matchers { "assignment of numbers with differing binary points seems to work as I would expect" - { "here we assign to a F8.1 from a F8.3" in { dsptools.Driver.execute(() => new FixedPrecisionChanger(8, 3, 8, 1)) { c => new FixedPointTruncatorTester(c, 6.875, 6.5) } should be (true) } "here we assign to a F8.1 from a F8.1" - { "conversion to fixed point with less precision than poked value rounds up to 7, IS THIS RIGHT?" in { dsptools.Driver.execute(() => new FixedPrecisionChanger(8, 1, 8, 1)) { c => new FixedPointTruncatorTester(c, 6.875, 7.0) } should be(true) } } "here we assign to a F10.6 from a F10.3" in { dsptools.Driver.execute(() => new FixedPrecisionChanger(10, 3, 10, 6)) { c => new FixedPointTruncatorTester(c, 6.875, 6.875) } should be (true) } "let's try 1/3 just for fun with a big mantissa" - { "oops, this works because I built in a fudge factor for double comparison, how should this be done" in { dsptools.Driver.execute(() => new FixedPrecisionChanger(64, 58, 64, 16)) { c => new FixedPointTruncatorTester(c, 1.0 / 3.0, 0.3333282470703125) } should be(true) } } } "removing mantissa can be done" - { "by using the setBinaryPoint Method" in { dsptools.Driver.execute(() => new RemoveMantissa(12, 4, 8, 0)) { c => new RemoveMantissaTester(c, 3.75, 3.0) } should be(true) } } }
Example 57
Source File: LnSpec.scala From dsptools with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See LICENSE for license details. package dsptools.numbers import chisel3._ import chisel3.util._ import chisel3.testers.BasicTester import chisel3.iotesters.{ChiselFlatSpec, PeekPokeTester, TesterOptionsManager} import dsptools.{DspTester, ReplOptionsManager} import org.scalatest.FreeSpec class LnModule extends Module { val io = IO(new Bundle { val num = Input(DspReal()) val ln = Output(DspReal()) }) io.ln := io.num.ln() } class LnTester(c: LnModule) extends DspTester(c) { poke(c.io.num,11.0) private val x = peek(c.io.ln) println(s"poked 1.0 got $x expected ${math.log(11.0)}") } class LnSpec extends FreeSpec { "ln should work" in { dsptools.Driver.execute(() => new LnModule) { c => new LnTester(c) } } } object LnTester extends App { val manager = new ReplOptionsManager if(manager.parse(args)) { dsptools.Driver.executeFirrtlRepl(() => new LnModule, manager) } }
Example 58
Source File: AbsSpec.scala From dsptools with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See LICENSE for license details. package dsptools.numbers import chisel3._ import chisel3.experimental._ import dsptools.{DspContext, DspTester, Grow, Wrap} import org.scalatest.{FreeSpec, Matchers} class AbsSpec extends FreeSpec with Matchers { "absolute value should work for all types" - { "abs should be obvious when not at extreme negative" - { "but returns a negative number for extreme value at max negative for SInt and FixedPoint" - { "with interpreter" in { dsptools.Driver.execute(() => new DoesAbs(UInt(4.W), SInt(4.W), FixedPoint(5.W, 2.BP))) { c => new DoesAbsTester(c) } should be(true) } "and with verilator" in { dsptools.Driver.execute(() => new DoesAbs(UInt(4.W), SInt(4.W), FixedPoint(5.W, 2.BP)), Array("--backend-name", "verilator")) { c => new DoesAbsTester(c) } should be(true) } } } } } class DoesAbsTester(c: DoesAbs[UInt, SInt, FixedPoint]) extends DspTester(c) { for(i <- 0.0 to 15.0 by 1.0) { poke(c.io.uIn, i) expect(c.io.uAbsGrow, i) expect(c.io.uAbsWrap, i) step(1) } for(i <- -7.0 to 7.0 by 1.0) { poke(c.io.sIn, i) expect(c.io.sAbsGrow, i.abs) expect(c.io.sAbsWrap, i.abs) step(1) } poke(c.io.sIn, -8.0) expect(c.io.sAbsGrow, 8.0) expect(c.io.sAbsWrap, -8.0) val increment = 0.25 for(i <- -3.75 to 3.75 by increment) { poke(c.io.fIn, i) expect(c.io.fAbsGrow, i.abs) expect(c.io.fAbsWrap, i.abs) step(1) } poke(c.io.fIn, -4.0) expect(c.io.fAbsGrow, 4.0) expect(c.io.fAbsWrap, -4.0) } class DoesAbs[TU <: Data: Signed : Ring, TS <: Data : Signed : Ring, TF <: Data : Signed : Ring] (uGen: TU, sGen: TS, fGen: TF) extends Module { val io = IO(new Bundle { val uIn = Input(uGen) val sIn = Input(sGen) val fIn = Input(fGen) val uAbsGrow = Output(uGen) val uAbsWrap = Output(uGen) val sAbsGrow = Output(SInt(5.W)) val sAbsWrap = Output(SInt(4.W)) val fAbsGrow = Output(FixedPoint(6.W, 2.BP)) val fAbsWrap = Output(FixedPoint(5.W, 2.BP)) }) io.uAbsGrow := DspContext.withOverflowType(Grow) { io.uIn.context_abs() } io.uAbsWrap := DspContext.withOverflowType(Wrap) { io.uIn.context_abs() } io.sAbsGrow := DspContext.withOverflowType(Grow) { io.sIn.context_abs() } io.sAbsWrap := DspContext.withOverflowType(Wrap) { io.sIn.context_abs() } io.fAbsGrow := DspContext.withOverflowType(Grow) { io.fIn.context_abs() } io.fAbsWrap := DspContext.withOverflowType(Wrap) { io.fIn.context_abs() } }
Example 59
Source File: LoggingSpec.scala From dsptools with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See LICENSE for license details. package dsptools import chisel3._ import logger.{LazyLogging, LogLevel, Logger} import org.scalatest.{FreeSpec, Matchers} class DutWithLogging extends Module with LazyLogging { val io = IO(new Bundle {}) logger.error("error level message") logger.warn("warn level message") logger.info("info level message") logger.debug("debug level message") logger.trace("trace level message") } class DutWithLoggingTester(c: DutWithLogging) extends DspTester(c) class LoggingSpec extends FreeSpec with Matchers { "logging can be emitted during hardware generation" - { "level defaults to warn" in { Logger.makeScope() { val captor = new Logger.OutputCaptor Logger.setOutput(captor.printStream) dsptools.Driver.execute(() => new DutWithLogging, Array.empty[String]) { c => new DutWithLoggingTester(c) } captor.getOutputAsString should include("error level message") captor.getOutputAsString should include("warn level message") captor.getOutputAsString should not include ("info level message") captor.getOutputAsString should not include ("debug level message") captor.getOutputAsString should not include ("trace level message") } } "logging level can be set via command line args" in { Logger.makeScope() { val captor = new Logger.OutputCaptor Logger.setOutput(captor.printStream) dsptools.Driver.execute(() => new DutWithLogging, Array("--log-level", "info")) { c => new DutWithLoggingTester(c) } captor.getOutputAsString should include("error level message") captor.getOutputAsString should include ("warn level message") captor.getOutputAsString should include ("info level message") captor.getOutputAsString should not include ("debug level message") captor.getOutputAsString should not include ("trace level message") } } "logging level can be set for a specific package" in { Logger.makeScope() { val captor = new Logger.OutputCaptor Logger.setOutput(captor.printStream) dsptools.Driver.execute(() => new DutWithLogging, Array("--class-log-level", "dsptools:warn")) { c => new DutWithLoggingTester(c) } captor.getOutputAsString should include("error level message") captor.getOutputAsString should include ("warn level message") captor.getOutputAsString should not include ("info level message") captor.getOutputAsString should not include ("debug level message") captor.getOutputAsString should not include ("trace level message") } } } }
Example 60
Source File: RunTest.scala From ZparkIO with MIT License | 5 votes |
package com.leobenkel.zparkioProjectExample import com.leobenkel.zparkiotest.SparkExecution import org.scalatest.FreeSpec class RunTest extends FreeSpec { "Run" - { "Should run" ignore { val REBUILD_JAR = true import sys.process._ val version = "cat VERSION".!!.replaceAll("v", "").replaceAll("\n", "") println(s"Version: '$version'") val rootPath = System.getProperty("user.dir") println(rootPath) val jarPath = s"$rootPath/ProjectExample/target/docker/0/zparkio_testProject-$version-all.jar" println(jarPath) if (REBUILD_JAR || s"ls $jarPath".! != 0) { val out: Int = ("pwd" #&& ("sbt" :: "; project projectExample" :: "; set test in assembly := {}" :: s"""; set version := "$version" """ :: "; docker" :: Nil)) .!(new ProcessLogger { override def out(s: => String): Unit = System.out.println(s) override def err(s: => String): Unit = System.err.println(s) override def buffer[T](f: => T): T = f }) println(out) assert(out == 0) } assert(s"ls $jarPath".! == 0) val s = SparkExecution( sparkConf = Map.empty, sparkFile = Map.empty, pathToJar = jarPath, mainClassPath = "com.leobenkel.zparkioProjectExample.Main", jarArgument = Map.empty ) val exitCode = s.execute assert(exitCode == 0) } } }
Example 61
Source File: ApplicationTest.scala From ZparkIO with MIT License | 5 votes |
package com.leobenkel.zparkioProjectExample import com.leobenkel.zparkiotest.TestWithSpark import org.scalatest.FreeSpec import zio.Exit.{Failure, Success} import zio.{BootstrapRuntime, ZIO} class ApplicationTest extends FreeSpec with TestWithSpark { "Full application" - { "Run" in { TestApp.makeRuntime.unsafeRunSync(TestApp.runTest("--spark-foo" :: "abc" :: Nil)) match { case Success(value) => println(s"Read: $value") assertResult(0)(value) case Failure(cause) => fail(cause.prettyPrint) } } "Wrong argument" in { TestApp.makeRuntime.unsafeRunSync(TestApp.runTest("--bar" :: "foo" :: Nil)) match { case Success(value) => println(s"Read: $value") assertResult(1)(value) case Failure(cause) => fail(cause.prettyPrint) } } "Help" in { TestApp.makeRuntime.unsafeRunSync(TestApp.runTest("--help" :: Nil)) match { case Success(value) => println(s"Read: $value") assertResult(0)(value) case Failure(cause) => fail(cause.prettyPrint) } } } } object TestApp extends Application { def runTest(args: List[String]): ZIO[zio.ZEnv, Throwable, Int] = { super.run(args) } lazy final override val makeRuntime: BootstrapRuntime = super.makeRuntime }
Example 62
Source File: DatasetZTest.scala From ZparkIO with MIT License | 5 votes |
package com.leobenkel.zparkio import com.leobenkel.zparkio.Services.SparkModule import com.leobenkel.zparkio.Services.SparkModule.SparkModule import com.leobenkel.zparkio.implicits.{ZDS, ZDS_R} import com.leobenkel.zparkiotest.TestWithSpark import org.apache.spark.sql.SparkSession import org.scalatest.FreeSpec import zio.{BootstrapRuntime, Task, ZLayer} // https://stackoverflow.com/a/16990806/3357831 case class TestClass( a: Int, b: String ) case class TestClassAfter(a: Int) class DatasetZTest extends FreeSpec with TestWithSpark { "DatasetZ" - { import implicits.DatasetZ "Test with dataset A " in { val s = spark val d: ZDS_R[SparkModule, TestClassAfter] = ZDS( TestClass(a = 1, b = "one"), TestClass(a = 2, b = "two"), TestClass(a = 3, b = "three") ).zMap { case TestClass(a, b) => Task(TestClassAfter(a + b.length)) } val r = new BootstrapRuntime {} assert( List(TestClassAfter(4), TestClassAfter(5), TestClassAfter(8)) == r .unsafeRun(d.provideLayer(ZLayer.succeed(new SparkModule.Service { override def spark: SparkSession = s }))).collect().sortBy(_.a).toList ) } } }
Example 63
Source File: ModuleFailZIOTest.scala From ZparkIO with MIT License | 5 votes |
package com.leobenkel.zparkio.Services import org.scalatest.FreeSpec import zio.{BootstrapRuntime, Exit, Task, ZIO} import scala.util.Try class ModuleFailZIOTest extends FreeSpec { "Module" ignore { trait Module { def service: Module.Service } object Module { trait Service { def foo(bar: String): String } def apply(b: String): ZIO[Module, Throwable, String] = { ZIO.access[Module](_.service.foo(b)) } } case class ModuleServiceIpml(dead: Boolean) extends Module.Service { println(s"SERVICE IS CREATED !!!") if (dead) { throw new RuntimeException("It failed !") } override def foo(bar: String): String = { println(bar) bar } } object ModuleServiceBuilder { def create(dead: Boolean): Task[Module.Service] = { Task(ModuleServiceIpml(dead)) } } case class ModuleIpml(s: Module.Service) extends Module { lazy final override val service: Module.Service = s } "Might fail" in { val runtime = new BootstrapRuntime {} def jobRun: ZIO[Module, Throwable, String] = { (for { a <- Module("bar") b <- Module("foo") } yield { s"$a - $b" }) } Try { runtime.unsafeRunSync { for { s <- ModuleServiceBuilder.create(true) a <- jobRun.provide(ModuleIpml(s)) } yield { a } } match { case a @ Exit.Success(value) => println(s"Intern: $value") a case Exit.Failure(cause) => println(s"Failed inside with: $cause") fail(cause.prettyPrint) } } match { case scala.util.Success(value) => println(s"Outside: $value") case scala.util.Failure(exception) => println(s"Failed outside with : $exception") fail(exception) } } } }
Example 64
Source File: ModuleFailTest.scala From ZparkIO with MIT License | 5 votes |
package com.leobenkel.zparkio.Services import org.scalatest.FreeSpec import zio.{BootstrapRuntime, Exit, ZIO} import scala.util.Try class ModuleFailTest extends FreeSpec { "Module" ignore { trait Module { def service: Module.Service } object Module { trait Service { def foo(bar: String): String } def apply(b: String): ZIO[Module, Nothing, String] = { ZIO.access[Module](_.service.foo(b)) } } case class ModuleServiceIpml(dead: Boolean) extends Module.Service { if (dead) { throw new RuntimeException("It failed !") } override def foo(bar: String): String = { println(bar) bar } } case class ModuleIpml(dead: Boolean) extends Module { lazy final override val service: Module.Service = ModuleServiceIpml(dead) } "Might fail" in { val runtime = new BootstrapRuntime {} Try { runtime.unsafeRunSync { (for { a <- Module("bar") b <- Module("foo") } yield { s"$a - $b" }).provide(ModuleIpml(false)) } match { case a @ Exit.Success(value) => println(s"Intern: $value") a case Exit.Failure(cause) => fail(cause.prettyPrint) } } match { case scala.util.Success(value) => println(s"Outside: $value") case scala.util.Failure(exception) => fail(exception) } } } }
Example 65
Source File: ScalazSyntaxTest.scala From better-monadic-for with MIT License | 5 votes |
package com.olegpy.bm4 import org.scalatest.FreeSpec import scalaz._, Scalaz._ class ScalazSyntaxTest extends FreeSpec { implicit val scalazInstance: Bind[MapCheck] = new Bind[MapCheck] { def bind[A, B](fa: MapCheck[A])(f: A => MapCheck[B]): MapCheck[B] = fa.flatMap(f) def map[A, B](fa: MapCheck[A])(f: A => B): MapCheck[B] = fa.map(f) } "works in generic context with extension methods of scalaz 7" in { def sillyTest[F[_]: Bind](fa: F[Int], fb: F[Int]) = for { _ <- fa b <- fb } yield b sillyTest(new MapCheck(11), new MapCheck(42)) } }
Example 66
Source File: CatsSyntaxTest.scala From better-monadic-for with MIT License | 5 votes |
package com.olegpy.bm4 import cats.Monad import org.scalatest.FreeSpec import cats.implicits._ class CatsSyntaxTest extends FreeSpec { implicit val mcCatsInstance: cats.FlatMap[MapCheck] = new cats.FlatMap[MapCheck] { def flatMap[A, B](fa: MapCheck[A])(f: A => MapCheck[B]): MapCheck[B] = { fa.flatMap(f) } def tailRecM[A, B](a: A)(f: A => MapCheck[Either[A, B]]): MapCheck[B] = { fail() } def map[A, B](fa: MapCheck[A])(f: A => B): MapCheck[B] = fa.map(f) } case class ImplicitTest(id: String) def typed[A](a: A) = () "works in generic context with extension methods of cats" in { import cats.syntax.all._ def sillyTest[F[_]: cats.FlatMap](fa: F[Int], fb: F[Int]) = for { _ <- fa b <- fb } yield b sillyTest(new MapCheck(11), new MapCheck(42)) } "supports implicit0 in F-parametric methods" - { "in = bindings" in { def f[F[_]: Monad] = for { x <- 42.pure[F] _ <- "dummy".pure[F] implicit0(it: ImplicitTest) = ImplicitTest("eggs") str = "dummy" _ = typed[String](str) } yield assert(it eq implicitly[ImplicitTest]) f[Option] } } }
Example 67
Source File: TestNoMap.scala From better-monadic-for with MIT License | 5 votes |
package com.olegpy.bm4 import org.scalatest.FreeSpec case class MapCalled() extends Exception class MapCheck[+A](a: A) { def map[B](f: A => B): MapCheck[B] = throw MapCalled() def flatMap[B](f: A => MapCheck[B]): MapCheck[B] = f(a) } class TestNoMap extends FreeSpec { "emits no map(b => b) in for-comprehension" in { for { _ <- new MapCheck(42) b <- new MapCheck("foo") } yield b } "emits no map(_ => ()) if prev. operation resulted in Unit" in { for { _ <- new MapCheck(42) _ <- new MapCheck(()) } yield () } "preserves map(_ => ()) if prev.operation did not result in Unit" in { intercept[MapCalled] { for { _ <- new MapCheck(42) _ <- new MapCheck("Foo") } yield () } } "removes map(b: Ascribed => b) if it's provable identity" in { for { _ <- new MapCheck(42) b: Long <- new MapCheck(0L) } yield b } "preserves map(b: Ascribed => b) if widening" in { intercept[MapCalled] { for { _ <- new MapCheck(42) b: Any <- new MapCheck("foo") } yield b } } "preserves map if called manually" in { intercept[MapCalled] { new MapCheck(()).flatMap(y => new MapCheck(()).map(x => x)) } } }
Example 68
Source File: PresentationCompiler.scala From better-monadic-for with MIT License | 5 votes |
package com.olegpy.bm4 import org.ensime.pcplod._ import org.scalatest.FreeSpec class PresentationCompiler extends FreeSpec { "PC should have no errors" in { withMrPlod("Arrows.scala") { pc => assert(pc.messages.isEmpty) } } "PC should be able to read values & types properly" in { withMrPlod("Arrows.scala") { pc => assert(pc.symbolAtPoint('flatmap).contains("PcTest.xx")) assert(pc.typeAtPoint('flatmap).contains("Int")) assert(pc.symbolAtPoint('map).contains("PcTest.yy")) assert(pc.typeAtPoint('map).contains("Int")) } } "PC should yield errors" - { "when implicit0(_: Tpe) is used" in { withMrPlod("Wildcards.scala") { pc => val firstError = pc.messages.head assert(firstError.severity == PcMessageSeverity.Error) assert(firstError.message == "implicit pattern requires an identifier, but a wildcard was used: `implicit0((_: Int))`. " + "This doesn't introduce anything into the implicit scope. You might want to remove the implicit0 pattern and type.") } } "when implicit0(_) is used" in { withMrPlod("Wildcards.scala") { pc => val secondError = pc.messages(1) assert(secondError.severity == PcMessageSeverity.Error) assert(secondError.message == "implicit pattern requires an identifier, but a wildcard was used: `implicit0(_)`. " + "This doesn't introduce anything into the implicit scope. You might want to remove the implicit0 pattern.") } } } }
Example 69
Source File: UserAccountRepositoryOnMemorySpec.scala From scala-ddd-base with MIT License | 5 votes |
package com.github.j5ik2o.dddbase.example.repository.memory import java.time.ZonedDateTime import com.github.j5ik2o.dddbase.AggregateNotFoundException import com.github.j5ik2o.dddbase.example.model._ import com.github.j5ik2o.dddbase.example.repository.{ IdGenerator, SpecSupport, UserAccountRepository } import com.github.j5ik2o.dddbase.example.repository.util.ScalaFuturesSupportSpec import monix.eval.Task import monix.execution.Scheduler.Implicits.global import org.scalatest.concurrent.ScalaFutures import org.scalatest.{ FreeSpec, Matchers } import scala.concurrent.duration._ import scala.concurrent.{ Await, Future } class UserAccountRepositoryOnMemorySpec extends FreeSpec with ScalaFutures with ScalaFuturesSupportSpec with Matchers with SpecSupport { val userAccount = UserAccount( id = UserAccountId(IdGenerator.generateIdValue), status = Status.Active, emailAddress = EmailAddress("[email protected]"), password = HashedPassword("aaa"), firstName = "Junichi", lastName = "Kato", createdAt = ZonedDateTime.now, updatedAt = None ) val userAccounts = for (idx <- 1L to 10L) yield UserAccount( id = UserAccountId(IdGenerator.generateIdValue), status = Status.Active, emailAddress = EmailAddress(s"user${idx}@gmail.com"), password = HashedPassword("aaa"), firstName = "Junichi", lastName = "Kato", createdAt = ZonedDateTime.now, updatedAt = None ) "UserAccountRepositoryOnMemory" - { "store" in { val repository = UserAccountRepository.onMemory() val result: UserAccount = (for { _ <- repository.store(userAccount) r <- repository.resolveById(userAccount.id) } yield r).runToFuture.futureValue result shouldBe userAccount } "storeMulti" in { val repository = UserAccountRepository.onMemory() val result: Seq[UserAccount] = (for { _ <- repository.storeMulti(userAccounts) r <- repository.resolveMulti(userAccounts.map(_.id)) } yield r).runToFuture.futureValue sameAs(result, userAccounts) shouldBe true } "store then expired" in { val repository = UserAccountRepository.onMemory(expireAfterWrite = Some(1 seconds)) val resultFuture: Future[UserAccount] = (for { _ <- repository.store(userAccount) _ <- Task.pure(Thread.sleep(1000)) r <- repository.resolveById(userAccount.id) } yield r).runToFuture an[AggregateNotFoundException] should be thrownBy { Await.result(resultFuture, Duration.Inf) } } } }
Example 70
Source File: AnsibleModuleTest.scala From sansible with MIT License | 5 votes |
package ansible import org.scalatest.{Matchers, FreeSpec} import argonaut._ import Argonaut._ import scalaz.Success class AnsibleModuleTest extends FreeSpec with Matchers { import AnsibleModule._ "ModuleOption can be decoded" in { val input = """ |{ | "option1": { | "description": [ | "desc" | ], | "required": true, | "default": null | }, | "option2": { | "required": false, | "default": "yes", | "choices": [ "yes", "no" ], | "description": [ "desc" ] | }, | "option3": { | "description": ["desc"], | "choices": ["a", "b", "c"] | } | |} |""".stripMargin // S => T val option1 = StringOption( "option1", "desc", required = true ) val option2 = BooleanOption( "option2", "desc", required = false ) val option3 = EnumOption( "option3", "desc", choices = List("a", "b", "c"), required = false) input.decodeValidation[List[ModuleOption]] should ===(Success(List( option1, option2, option3 ))) } }
Example 71
Source File: CommonOptionsSpec.scala From sansible with MIT License | 5 votes |
package ansible.dsl import ansible.CommonOptions._ import org.scalatest.FreeSpec import ansible.{Task, Playbook} import ansible.Modules.Ping import ansible.std._ class CommonOptionsSpec extends FreeSpec { trait TestData { val task = Task("test task", Ping()) val playbook = Playbook(Nil, task :: Nil, Nil) } "CommonOptions syntax" - { "withTags" in new TestData { val expected = Some(Set(Tag("a"), Tag("b"))) assertResult(expected)(task.withTags("a", "b").tags) assertResult(expected)(playbook.withTags("a", "b").tags) } "addTags" in new TestData { assertResult(Some(Set(Tag("a"), Tag("b"), Tag("c"))))( task.addingTags("a", "b").addingTags("c").tags ) } "withEnv" in new TestData { assertResult(Some(Map("A" -> "B")))( task.withEnv(Map("A" -> "B")).env ) } "mergeEnv" in new TestData { assertResult(Some(Map("A" -> "B", "C" -> "D")))( task.withEnv(Map("A" -> "B")).mergeEnv(Map("C" -> "D")).env ) } "becoming" in new TestData { assertResult(Some(Become(None, Some(Sudo))))(task.usingSudo.become) assertResult(Some(Become(Some("userX"), Some(Su))))(task.becoming("userX", Su).become) } "withOptions" in new TestData { assertResult(Some("local"))( playbook.modifyOptions(_.copy(connection = Some("local"))) .options.connection ) } } }
Example 72
Source File: IniEncodersSpec.scala From sansible with MIT License | 5 votes |
package ansible import ansible.Inventory._ import ansible.IniEncode._ import ansible.IniEncoders._ import org.scalatest.FreeSpec class IniEncodersSpec extends FreeSpec { "an inventory" - { val host1 = Hostname(id = "host1", port = Some(4444)) val host2 = Hostname(id = "host2", hostVars = Map( "ansible_user" -> "user", "ansible_ssh_pass" -> "secret")) val host3 = HostPattern(id = "host*") val group1 = Group("group1", List(host3), Map( "group_var1" -> "var1" )) val group2 = Group( name = "group2", hosts = Nil, children = List(group1)) "when it contains hostIds only" - { "will not contain headings" in { assertResult( """host1:4444 |host2 ansible_user=user ansible_ssh_pass=secret""".stripMargin)( Inventory(host1 :: host2 :: Nil).iniEncode) } } "when it contains hostids and groups" - { "will include the group name and vars" in { assertResult( """host1:4444 | |[group1] |host* | |[group1:vars] |group_var1=var1""".stripMargin)( Inventory(List( host1, Group("group1", List(host3), Map( "group_var1" -> "var1" )) )).iniEncode) } "will include the group children" in { assertResult( """host1:4444 | |[group1] |host* | |[group1:vars] |group_var1=var1 | |[group2:children] |group1""".stripMargin)(Inventory(List(host1, group1, group2)).iniEncode) } } } }
Example 73
Source File: JsonEncodersSpec.scala From sansible with MIT License | 5 votes |
package ansible import org.scalatest.FreeSpec import argonaut._ import Argonaut._ import Modules.{File, Command, Ping} import Inventory._ import ansible.CommonOptions._ import ansible.std._ import ansible.dsl._ class JsonEncodersSpec extends FreeSpec { "json encoders" - { "can serialise a playbook" in { val f = File(path="/foo/bar", state=Some(File.State.file)) val t1 = Task("create foo/bar", f).withTags("tag1", "tag2") val t3 = Task("reboot system", Command("reboot")) val t2 = Task("ping host", Ping(), Task.Options(notifyTask = Some(t3))) val pb = Playbook( hosts = List(HostPattern("example-host")), tasks = t1 :: t2 :: Nil, handlers = t3 :: Nil, options = Playbook.Options( connection = Some("local") ) ).usingSudo val json = Parse.parse("""{ | "hosts": ["example-host"], | "connection": "local", | "become": true, | "become_method": "sudo", | "tasks": [ | {"name": "create foo/bar", "file": {"state": "file", "path": "/foo/bar"}, "tags": ["tag1", "tag2"]}, | {"name": "ping host", "ping": {}, "notify": "reboot system"} | ], | "handlers": [ | {"name": "reboot system", "command": "reboot", "args": {}} | ] |}""".stripMargin) assertResult(json.toOption.get)(pb.asJson) } } }
Example 74
Source File: InventorySpec.scala From sansible with MIT License | 5 votes |
package ansible import org.scalatest.FreeSpec import Inventory._ class InventorySpec extends FreeSpec { trait TestInventory { def hosts: List[HostId] = Nil def inv = Inventory(List( Group("g", hosts, Map("k1" -> "v1")) )) } "Inventory" - { "groupHosts" - { "gets the list of hosts in a group" in new TestInventory { override val hosts = List( Hostname("h1"), Hostname("h2") ) assertResult(hosts)(inv.groupHosts("g")) } } "withGroupVar" - { "sets a group vars" in new TestInventory { assertResult(Some(Map("k1" -> "v1", "k2" -> "v2")))( inv.withGroupVar("g", "k2" -> "v2").groupVars("g") ) } } "withHostVar" - { "sets a var within a group's host" in new TestInventory { override val hosts = List( Hostname("h1", Map("hk" -> "hv1")), Hostname("h2", Map("hk" -> "hv2")) ) assertResult(List("hv1", "hv2-updated"))( inv.withHostVar("g", "h2", ("hk", "hv2-updated")) .hostVarValues("g", "hk") ) } } } }
Example 75
Source File: AndableTest.scala From testcontainers-scala with MIT License | 5 votes |
package com.dimafeng.testcontainers.lifecycle import org.scalatest.FreeSpec import scala.collection.mutable class AndableTest extends FreeSpec { case class Cont1(i: Int, buffer: mutable.Buffer[Int] = mutable.Buffer.empty[Int]) extends Stoppable { override def stop(): Unit = buffer.append(i) } case class Cont2(i: Int, buffer: mutable.Buffer[Int] = mutable.Buffer.empty[Int]) extends Stoppable { override def stop(): Unit = buffer.append(i) } "Andable" - { "foreach" - { "should iterate Andable in a correct order" in { val andable = Cont1(1) and Cont1(2) and Cont2(3) and Cont2(4) val ixs = mutable.Buffer.empty[Int] andable.foreach { case Cont1(i, _) => ixs.append(i) case Cont2(i, _) => ixs.append(i) } assert(ixs.toSeq === Seq(1,2,3,4)) } } "stop" - { "should stop all Andable in a reverse order" in { val ixs = mutable.Buffer.empty[Int] val andable = Cont1(1, ixs) and Cont1(2, ixs) and Cont2(3, ixs) and Cont2(4, ixs) andable.stop() assert(ixs.toSeq === Seq(4,3,2,1)) } } } }
Example 76
Source File: WeatherForecastSpec2.scala From Scala-Reactive-Programming with MIT License | 5 votes |
package com.packt.publishing.caseclass import org.scalatest.{FreeSpec, MustMatchers} class WeatherForecastSpec2 extends FreeSpec with MustMatchers { private def createWeatherForecast(city: String = "London", date: String = "01/01/2018", hour: String = "10 AM", temperature: String = "6") = WeatherForecast(city, date, hour, temperature) "default values" in { val wf = createWeatherForecast() wf.city mustBe ("London") wf.date mustBe "01/01/2018" wf.hour mustBe "10 AM" wf.temperature mustBe "6" } "check city value" in { val wf = createWeatherForecast(city = "Hyderabad") wf.city must not be "London" wf.city mustBe "Hyderabad" } "check date value" in { val wf = createWeatherForecast(date = "06/04/2018") wf.date mustBe "06/04/2018" } "check hour value" in { val wf = createWeatherForecast(hour = "14") wf.hour mustBe "14" } "check city and temperature values" in { val wf = createWeatherForecast(city = "Hyderabad", temperature = "30") wf.city mustBe "Hyderabad" wf.temperature mustBe "30" } }
Example 77
Source File: WeatherForecastSpec2.scala From Scala-Reactive-Programming with MIT License | 5 votes |
package com.packt.publishing.caseclass import org.scalatest.{FreeSpec, MustMatchers} class WeatherForecastSpec2 extends FreeSpec with MustMatchers { private def createWeatherForecast(city: String = "London", date: String = "01/01/2018", hour: String = "10 AM", temperature: String = "6") = WeatherForecast(city, date, hour, temperature) "default values" in { val wf = createWeatherForecast() wf.city mustBe ("London") wf.date mustBe "01/01/2018" wf.hour mustBe "10 AM" wf.temperature mustBe "6" } "check city value" in { val wf = createWeatherForecast(city = "Hyderabad") wf.city must not be "London" wf.city mustBe "Hyderabad" } "check date value" in { val wf = createWeatherForecast(date = "06/04/2018") wf.date mustBe "06/04/2018" } "check hour value" in { val wf = createWeatherForecast(hour = "14") wf.hour mustBe "14" } "check city and temperature values" in { val wf = createWeatherForecast(city = "Hyderabad", temperature = "30") wf.city mustBe "Hyderabad" wf.temperature mustBe "30" } }
Example 78
Source File: S3AsyncClientImplSpec.scala From reactive-aws-clients with MIT License | 5 votes |
package com.github.j5ik2o.reactive.aws.s3 import org.scalatest.concurrent.ScalaFutures import org.scalatest.{ FreeSpec, Matchers } import software.amazon.awssdk.core.async.AsyncRequestBody import software.amazon.awssdk.services.s3.model._ import scala.concurrent.duration._ class S3AsyncClientImplSpec extends FreeSpec with Matchers with ScalaFutures with S3ContainerSpecSupport { implicit val pc: PatienceConfig = PatienceConfig(20 seconds, 1 seconds) lazy val s3Client = S3AsyncClient(javaS3Client) "S3AsyncClientImpl" - { "create bucket, put object, get object" ignore { val value = "abc" s3Client.createBucket(CreateBucketRequest.builder().bucket("test").build()).futureValue s3Client .putObject(PutObjectRequest.builder().bucket("test").key("test").build(), AsyncRequestBody.fromString(value)).futureValue val response = s3Client.getObjectAsBytes(GetObjectRequest.builder().bucket("test").key("test").build()).futureValue response.asUtf8String() shouldBe value } } }
Example 79
Source File: RowComparerTest.scala From spark-fast-tests with MIT License | 5 votes |
package com.github.mrpowers.spark.fast.tests import org.scalatest.FreeSpec import org.apache.spark.sql.Row class RowComparerTest extends FreeSpec { "areRowsEqual" - { "returns true for rows that contain the same elements" in { val r1 = Row("a", "b") val r2 = Row("a", "b") assert( RowComparer.areRowsEqual(r1, r2, 0.0) == true ) } "returns false for rows that don't contain the same elements" - { val r1 = Row("a", 3) val r2 = Row("a", 4) assert( RowComparer.areRowsEqual(r1, r2, 0.0) == false ) } } }
Example 80
Source File: ArrayPrettyPrintTest.scala From spark-fast-tests with MIT License | 5 votes |
package com.github.mrpowers.spark.fast.tests import org.scalatest.FreeSpec class ArrayPrettyPrintTest extends FreeSpec { "blah" in { val arr: Array[(Any, Any)] = Array(("hi", "there"), ("fun", "train")) val res = ArrayPrettyPrint.weirdTypesToStrings(arr, 10) assert(res sameElements Array(List("hi", "there"), List("fun", "train"))) } "showTwoColumnString" in { val arr: Array[(Any, Any)] = Array(("word1", "word2"), ("hi", "there"), ("fun", "train")) println(ArrayPrettyPrint.showTwoColumnString(arr, 10)) } "dumbshowTwoColumnString" in { val arr: Array[(Any, Any)] = Array(("word1", "word2"), ("hi", "there"), ("fun", "train")) val rowEqual = Array(true, false) println(ArrayPrettyPrint.showTwoColumnStringColorCustomizable(arr, rowEqual)) } }
Example 81
Source File: RDDComparerTest.scala From spark-fast-tests with MIT License | 5 votes |
package com.github.mrpowers.spark.fast.tests import org.scalatest.FreeSpec class RDDComparerTest extends FreeSpec with RDDComparer with SparkSessionTestWrapper { "contentMismatchMessage" - { "returns a string string to compare the expected and actual RDDs" in { val sourceData = List( ("cat"), ("dog"), ("frog") ) val actualRDD = spark.sparkContext.parallelize(sourceData) val expectedData = List( ("man"), ("can"), ("pan") ) val expectedRDD = spark.sparkContext.parallelize(expectedData) val expected = """ Actual RDD Content: cat dog frog Expected RDD Content: man can pan """ assert( contentMismatchMessage(actualRDD, expectedRDD) == expected ) } } "assertSmallRDDEquality" - { "does nothing if the RDDs have the same content" in { val sourceData = List( ("cat"), ("dog"), ("frog") ) val sourceRDD = spark.sparkContext.parallelize(sourceData) val expectedData = List( ("cat"), ("dog"), ("frog") ) val expectedRDD = spark.sparkContext.parallelize(expectedData) assertSmallRDDEquality(sourceRDD, expectedRDD) } "throws an error if the RDDs have different content" in { val sourceData = List( ("cat"), ("dog"), ("frog") ) val sourceRDD = spark.sparkContext.parallelize(sourceData) val expectedData = List( ("mouse"), ("pig"), ("frog") ) val expectedRDD = spark.sparkContext.parallelize(expectedData) val e = intercept[RDDContentMismatch] { assertSmallRDDEquality(sourceRDD, expectedRDD) } } } }
Example 82
Source File: AppConfigSpec.scala From kafka-lag-exporter with Apache License 2.0 | 5 votes |
package com.lightbend.kafkalagexporter import com.typesafe.config.{Config, ConfigFactory} import org.scalatest.{FreeSpec, Matchers} class AppConfigSpec extends FreeSpec with Matchers { val configString = s""" |kafka-lag-exporter { | clusters = [ | { | name = "clusterA" | bootstrap-brokers = "b-1.cluster-a.xyzcorp.com:9092,b-2.cluster-a.xyzcorp.com:9092" | group-whitelist = ["group-a", "group-b"] | topic-whitelist = ["topic-a", "topic-b"] | consumer-properties = { | client.id = "consumer-client-id" | } | admin-client-properties = { | client.id = "admin-client-id" | } | labels = { | environment= "integration" | location = "ny" | } | } | { | name = "clusterB" | bootstrap-brokers = "b-1.cluster-b.xyzcorp.com:9092,b-2.cluster-b.xyzcorp.com:9092" | labels = { | environment= "production" | } | } | { | name = "clusterC" | bootstrap-brokers = "c-1.cluster-b.xyzcorp.com:9092,c-2.cluster-b.xyzcorp.com:9092" | } | ] |}""".stripMargin "AppConfig" - { "should parse static clusters" in { val config: Config = loadConfig(configString) val appConfig = AppConfig(config) appConfig.clusters.length shouldBe 3 appConfig.clusters(0).name shouldBe "clusterA" appConfig.clusters(0).bootstrapBrokers shouldBe "b-1.cluster-a.xyzcorp.com:9092,b-2.cluster-a.xyzcorp.com:9092" appConfig.clusters(0).groupWhitelist shouldBe List("group-a", "group-b") appConfig.clusters(0).topicWhitelist shouldBe List("topic-a", "topic-b") appConfig.clusters(0).consumerProperties("client.id") shouldBe "consumer-client-id" appConfig.clusters(0).adminClientProperties("client.id") shouldBe "admin-client-id" appConfig.clusters(0).labels("environment") shouldBe "integration" appConfig.clusters(0).labels("location") shouldBe "ny" appConfig.clusters(1).name shouldBe "clusterB" appConfig.clusters(1).bootstrapBrokers shouldBe "b-1.cluster-b.xyzcorp.com:9092,b-2.cluster-b.xyzcorp.com:9092" appConfig.clusters(1).consumerProperties shouldBe Map.empty appConfig.clusters(1).adminClientProperties shouldBe Map.empty appConfig.clusters(1).labels("environment") shouldBe "production" appConfig.clusters(2).name shouldBe "clusterC" appConfig.clusters(2).bootstrapBrokers shouldBe "c-1.cluster-b.xyzcorp.com:9092,c-2.cluster-b.xyzcorp.com:9092" appConfig.clusters(2).consumerProperties shouldBe Map.empty appConfig.clusters(2).adminClientProperties shouldBe Map.empty appConfig.clusters(2).labels shouldBe Map.empty } "should set blank string for the clusters if label value is absent" in { val appConfig = AppConfig(loadConfig(configString)) appConfig.clustersGlobalLabels() should contain theSameElementsAs Map( "clusterA" -> Map("environment" -> "integration", "location" -> "ny"), "clusterB" -> Map("environment" -> "production"), "clusterC" -> Map.empty ) } "should handle the empty config case" in { val appConfig = AppConfig(loadConfig("")) appConfig.clustersGlobalLabels() should equal(Map.empty) } } private def loadConfig(configStr: String): Config = { ConfigFactory .parseString(configStr) .withFallback(ConfigFactory.load()) } }
Example 83
Source File: AliasSpecs.scala From guardrail with MIT License | 5 votes |
package alias.server.springMvc.foo import java.util.concurrent.CompletableFuture import org.junit.runner.RunWith import org.mockito.{ArgumentMatchersSugar, MockitoSugar} import org.scalatest.{BeforeAndAfterAll, FreeSpec, Matchers} import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc import org.springframework.boot.test.context.SpringBootTest import org.springframework.context.annotation.ComponentScan import org.springframework.http.MediaType import org.springframework.test.context.TestContextManager import org.springframework.test.context.junit4.SpringRunner import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.{asyncDispatch, get, post} import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print import org.springframework.test.web.servlet.result.MockMvcResultMatchers.{request, status} import spring.test.TestApplication @RunWith(classOf[SpringRunner]) @SpringBootTest(classes = Array(classOf[TestApplication])) @AutoConfigureMockMvc @ComponentScan @EnableAutoConfiguration class AliasSpecs extends FreeSpec with Matchers with BeforeAndAfterAll with MockitoSugar with ArgumentMatchersSugar { @Autowired var mvc: MockMvc = _ @Autowired var handlerMock: FooHandler = _ new TestContextManager(this.getClass).prepareTestInstance(this) "test alias.foo" - { "optional body handled with alias param" in { when(handlerMock.doFoo(java.util.Optional.of(1L), java.util.Optional.of(42L))) .thenReturn(CompletableFuture.completedFuture(FooHandler.DoFooResponse.Created(42L))) val mvcResult = mvc .perform( post("/foo?long=1") .contentType(MediaType.APPLICATION_JSON) .content("42") ) .andExpect(request.asyncStarted) .andReturn mvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status.isCreated) } } }
Example 84
Source File: DateTimeSpecs.scala From guardrail with MIT License | 5 votes |
package dateTime.server.springMvc.dateTime import java.time.{ LocalDate, OffsetDateTime } import java.util.concurrent.CompletableFuture import org.junit.runner.RunWith import org.mockito.{ ArgumentMatchersSugar, MockitoSugar } import org.scalatest.{ BeforeAndAfterAll, FreeSpec, Matchers } import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc import org.springframework.boot.test.context.SpringBootTest import org.springframework.context.annotation.ComponentScan import org.springframework.http.MediaType import org.springframework.test.context.TestContextManager import org.springframework.test.context.junit4.SpringRunner import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.{ asyncDispatch, post, get} import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print import org.springframework.test.web.servlet.result.MockMvcResultMatchers.{ request, status } import spring.test.TestApplication @RunWith(classOf[SpringRunner]) @SpringBootTest(classes = Array(classOf[TestApplication])) @AutoConfigureMockMvc @ComponentScan @EnableAutoConfiguration class DateTimeSpecs extends FreeSpec with Matchers with BeforeAndAfterAll with MockitoSugar with ArgumentMatchersSugar { @Autowired var mvc: MockMvc = _ @Autowired var handlerMock: DateTimeHandler = _ new TestContextManager(this.getClass).prepareTestInstance(this) "test jsre310 stuff" - { "dates everywhere" in { val offsetDtNow = OffsetDateTime.now val localDtNow = LocalDate.now when( handlerMock.getSomething( eqTo(offsetDtNow), eqTo(java.util.Optional.of(offsetDtNow)), eqTo(localDtNow), eqTo(java.util.Optional.of(localDtNow)) ) ).thenReturn(CompletableFuture.completedFuture(DateTimeHandler.GetSomethingResponse.NoContent)) val mvcResult = mvc .perform( get("/foo") .param("dateTime", offsetDtNow.toString) .param("optionalDateTime", offsetDtNow.toString) .param("date", localDtNow.toString) .param("optionalDate", localDtNow.toString) .contentType(MediaType.APPLICATION_JSON) ) .andExpect(request.asyncStarted) .andReturn mvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status.isNoContent) } } }
Example 85
Source File: JacksonBuilderParamTest.scala From guardrail with MIT License | 5 votes |
package core.Jackson import examples.client.dropwizard.definitions.{ Category, Pet } import java.util.Optional import org.scalatest.{ FreeSpec, Matchers } class JacksonBuilderParamTest extends FreeSpec with Matchers { "POJO builders should not accept nulls for required params" in { assertThrows[NullPointerException] { new Pet.Builder(null: String) .build() } } "POJO builders should accept nulls for the value variant for optional params" in { val pet = new Pet.Builder("fluffy") .withCategory(null: Category) .build() pet.getCategory shouldBe Optional.empty } "POJO builders should not accept nulls for the Optional<> variant for optional params" in { assertThrows[NullPointerException] { new Pet.Builder("fluffy") .withCategory(null: Optional[Category]) .build() } } }
Example 86
Source File: JacksonPolyMappingTest.scala From guardrail with MIT License | 5 votes |
package core.Jackson import com.fasterxml.jackson.databind.ObjectMapper import org.scalatest.{ FreeSpec, Matchers } import polymorphismMapped.client.dropwizard.definitions.{ A, B, Base, C, DiscrimEnum, EnumA, EnumB, EnumBase, EnumC } import scala.reflect.ClassTag class JacksonPolyMappingTest extends FreeSpec with Matchers { private val mapper = new ObjectMapper "Polymorphic definitions with discriminator mappings" - { "should have their discriminator initialized properly" in { val a = new A.Builder(42).build() a.getPolytype shouldBe "this_is_a" val b = new B.Builder("foo").build() b.getPolytype shouldBe "this_is_b" val c = new C.Builder(42.42).build() c.getPolytype shouldBe "C" } "should deserialize properly" in { def verify[T](json: String, discriminatorValue: String)(implicit cls: ClassTag[T]): Unit = { val pojo = mapper.readValue(json, classOf[Base]) pojo shouldNot be(null) pojo.getClass shouldBe cls.runtimeClass pojo.getPolytype shouldBe discriminatorValue } verify[A]("""{"polytype": "this_is_a", "some_a": 42}""", "this_is_a") verify[B]("""{"polytype": "this_is_b", "some_b": "foo"}""", "this_is_b") verify[C]("""{"polytype": "C", "some_c": 42.42}""", "C") } } "Polymorphic definitions with enum discriminator mappings" - { "should have their discriminator initialized properly" in { val a = new EnumA.Builder(42).build() a.getPolytype shouldBe DiscrimEnum.SOME_VALUE_ONE val b = new EnumB.Builder("foo").build() b.getPolytype shouldBe DiscrimEnum.ANOTHER_VALUE val c = new EnumC.Builder(42.42).build() c.getPolytype shouldBe DiscrimEnum.YET_ANOTHER_VALUE } "should deserialize properly" in { def verify[T](json: String, discriminatorValue: DiscrimEnum)(implicit cls: ClassTag[T]): Unit = { val pojo = mapper.readValue(json, classOf[EnumBase]) pojo shouldNot be(null) pojo.getClass shouldBe cls.runtimeClass pojo.getPolytype shouldBe discriminatorValue } verify[EnumA]("""{"polytype": "some-value-one", "some_a": 42}""", DiscrimEnum.SOME_VALUE_ONE) verify[EnumB]("""{"polytype": "another-value", "some_b": "foo"}""", DiscrimEnum.ANOTHER_VALUE) verify[EnumC]("""{"polytype": "yet-another-value", "some_c": 42.42}""", DiscrimEnum.YET_ANOTHER_VALUE) } } }
Example 87
Source File: Issue389.scala From guardrail with MIT License | 5 votes |
package core.Jackson import com.fasterxml.jackson.databind.ObjectMapper import issues.issue389.client.dropwizard.definitions.{ Bar, Foo } import java.util import org.scalatest.{ FreeSpec, Matchers } import scala.collection.JavaConverters._ import com.fasterxml.jackson.datatype.jdk8.Jdk8Module class Issue389 extends FreeSpec with Matchers { "x-jvm-type in Array should use the correct type in the array" in { val mapper = new ObjectMapper().registerModule(new Jdk8Module()) val foo = new Foo.Builder() .withCustomArray( (1 until 5) .map(new Bar.Builder().withA(_).build()) .toList .asJava ) .build() val expected = """{"customArray":[{"a":1},{"a":2},{"a":3},{"a":4}],"customMap":null}""" assertResult(expected)(mapper.writeValueAsString(foo)) } "x-jvm-type in Map should use the correct type in the map" in { val mapper = new ObjectMapper().registerModule(new Jdk8Module()) val foo = new Foo.Builder() .withCustomMap( (1 until 5) .map(x => x.toString() -> new Bar.Builder().withA(x).build()) .toMap .asJava ) .build() val expected = """{"customArray":null,"customMap":{"1":{"a":1},"2":{"a":2},"3":{"a":3},"4":{"a":4}}}""" assertResult(expected)(mapper.writeValueAsString(foo)) } }
Example 88
Source File: AsyncHttpClientContentHeaderTest.scala From guardrail with MIT License | 5 votes |
package core.Dropwizard import com.github.tomakehurst.wiremock.client.WireMock._ import helpers.WireMockSupport import org.scalatest.{ FreeSpec, Matchers } import tests.contentTypes.textPlain.client.dropwizard.foo.FooClient class AsyncHttpClientContentHeaderTest extends FreeSpec with Matchers with WireMockSupport { private val MOCK_REQUEST_BODY = "abcd" private val MOCK_RESPONSE_BODY = "efgh" "AsyncHttpClient with text/plain" - { "in both req/resp bodies has Content-Type and Accept headers set" in { wireMockServer.stubFor( post(urlPathEqualTo("/baz")) .withHeader("Content-Type", equalTo("text/plain; charset=utf-8")) .withHeader("Accept", equalTo("text/plain; q=1.0, */*; q=0.1")) .withRequestBody(equalTo(MOCK_REQUEST_BODY)) .willReturn( aResponse() .withStatus(201) .withHeader("Content-Type", "text/plain; charset=utf-8") .withBody(MOCK_RESPONSE_BODY) ) ) val client = new FooClient.Builder().withBaseUrl(wireMockBaseUrl).build() client .doBaz() .withBody(MOCK_REQUEST_BODY) .call() .toCompletableFuture .get() .fold( { body => body shouldBe MOCK_RESPONSE_BODY; () }, { () => fail("Should have gotten 2xx response"); () } ) } "in req body has Content-Type header set" in { wireMockServer.stubFor( post(urlPathEqualTo("/foo")) .withHeader("Content-Type", equalTo("text/plain; charset=utf-8")) .withRequestBody(equalTo(MOCK_REQUEST_BODY)) .willReturn( aResponse() .withStatus(201) ) ) val client = new FooClient.Builder().withBaseUrl(wireMockBaseUrl).build() client .doFoo(MOCK_REQUEST_BODY) .call() .toCompletableFuture .get() .fold( () => (), { () => fail("Should have gotten 201 response"); () } ) } } }
Example 89
Source File: DropwizardRoundTripTest.scala From guardrail with MIT License | 5 votes |
package core.Dropwizard import com.fasterxml.jackson.databind.ObjectMapper import examples.client.dropwizard.user.{ UserClient, GetUserByNameResponse => GetUserByNameClientResponse } import examples.server.dropwizard.definitions.User import examples.server.dropwizard.user.UserHandler._ import examples.server.dropwizard.user._ import helpers.MockHelpers._ import java.util import java.util.Optional import java.util.concurrent.{ CompletableFuture, CompletionStage } import org.asynchttpclient.{ Request, Response } import org.mockito.{ ArgumentMatchersSugar, MockitoSugar } import org.scalatest.concurrent.Waiters import org.scalatest.{ FreeSpec, Matchers } import scala.compat.java8.FunctionConverters._ class DropwizardRoundTripTest extends FreeSpec with Matchers with Waiters with MockitoSugar with ArgumentMatchersSugar { private implicit val mapper = new ObjectMapper "Test server" in { val USERNAME = "foobar" val serverFuture = new CompletableFuture[GetUserByNameResponse] val asyncResponse = mockAsyncResponse(serverFuture) val resource = new UserResource(new UserHandler { override def createUser(body: User): CompletionStage[CreateUserResponse] = ??? override def createUsersWithArrayInput(body: util.List[User]): CompletionStage[CreateUsersWithArrayInputResponse] = ??? override def createUsersWithListInput(body: util.List[User]): CompletionStage[CreateUsersWithListInputResponse] = ??? override def loginUser(username: String, password: String): CompletionStage[LoginUserResponse] = ??? override def logoutUser(): CompletionStage[LogoutUserResponse] = ??? override def updateUser(username: String, body: User): CompletionStage[UpdateUserResponse] = ??? override def deleteUser(username: String): CompletionStage[DeleteUserResponse] = ??? override def getUserByName(username: String): CompletionStage[GetUserByNameResponse] = { username match { case USERNAME => serverFuture.complete( GetUserByNameResponse.Ok( new User.Builder() .withEmail("[email protected]") .withFirstName("Foo") .withLastName("Bar") .withId(1) .withUsername(USERNAME) .build() ) ) case "" => serverFuture.complete(GetUserByNameResponse.BadRequest) case _ => serverFuture.complete(GetUserByNameResponse.NotFound) } serverFuture } }) val httpClient: Request => CompletionStage[Response] = { request => val userPath = "^/v2/user/([^/]*)$".r request.getUri.getPath match { case userPath(username) => resource.getUserByName(username, asyncResponse) serverFuture.thenApply({ response => val entityBody = response match { case r: GetUserByNameResponse.Ok => Some(r.getEntityBody) case _ => None } mockAHCResponse(request.getUrl, response.getStatusCode, entityBody) }) case _ => CompletableFuture.completedFuture(mockAHCResponse(request.getUrl, 404)) } } val client = new UserClient.Builder() .withHttpClient(httpClient.asJava) .withObjectMapper(mapper) .build() val w = new Waiter client .getUserByName(USERNAME) .call() .whenComplete({ (response, t) => w { t shouldBe null } response match { case r: GetUserByNameClientResponse.Ok => w { r.getValue.getUsername.get shouldBe USERNAME r.getValue.getPassword shouldBe Optional.empty } case _: GetUserByNameClientResponse.BadRequest => w { fail("Got BadRequest") } case _: GetUserByNameClientResponse.NotFound => w { fail("Got NotFound") } } w.dismiss() }) w.await(dismissals(1)) } }
Example 90
Source File: ReadDatasourceIntegSpec.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.deeplang.doperations import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FreeSpec} import ai.deepsense.deeplang.{LocalExecutionContext, TestDataSources, TestFiles} class ReadDatasourceIntegSpec extends FreeSpec with BeforeAndAfter with BeforeAndAfterAll with LocalExecutionContext with TestDataSources with TestFiles { for (ds <- someDatasourcesForReading) { s"ReadDatasource should work with datasource ${ds.getParams.getName}" in { val rds = ReadDatasource().setDatasourceId(ds.getId) rds.execute()(LocalExecutionContext.createExecutionContext(datasourceClient)) } } }
Example 91
Source File: DatasourcesApiSpec.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.seahorse.datasource.server import java.util.UUID import org.scalatest.{FreeSpec, Matchers} import spray.http.StatusCodes import ai.deepsense.seahorse.datasource.api.{ApiException, ApiExceptionWithJsonBody} import ai.deepsense.seahorse.datasource.db.dbio.Get import ai.deepsense.seahorse.datasource.model.{Error, Visibility} class DatasourcesApiSpec extends FreeSpec with Matchers { private implicit lazy val api = ApiForTests.api "Api consumer" - { val userId = UUID.randomUUID() val userName = "Alice" "cannot see private datasources that don't belong to him" in { val otherUserId = UUID.randomUUID() for { dsParams <- TestData.someDatasources(Some(Visibility.privateVisibility)) } { val id = UUID.randomUUID() api.putDatasourceImpl(otherUserId, userName, id, dsParams) (the[ApiException] thrownBy api.getDatasourceImpl(userId, id)).errorCode shouldBe StatusCodes.Forbidden.intValue } } "can manage his datasources" in { for (dsParams <- TestData.someDatasources()) { val id = UUID.randomUUID() info("User can add datasource") api.putDatasourceImpl(userId, userName, id, dsParams) api.getDatasourcesImpl(userId).find(_.id == id).get.params shouldEqual dsParams api.getDatasourceImpl(userId, id).params shouldEqual dsParams info("Add operation is idempotent") api.putDatasourceImpl(userId, userName, id, dsParams) api.getDatasourcesImpl(userId).find(_.id == id).get.params shouldEqual dsParams api.getDatasourceImpl(userId, id).params shouldEqual dsParams info("User can also delete datasource") api.deleteDatasourceImpl(userId, id) api.getDatasourcesImpl(userId).find(_.id == id) shouldBe empty info("Once datasource not exists all operations yield 404") the[ApiException].thrownBy( api.getDatasourceImpl(userId, id) ).errorCode shouldBe 404 the[ApiException].thrownBy( api.deleteDatasourceImpl(userId, id) ).errorCode shouldBe 404 } } "cannot add datasource with multichar separator" in { val id = UUID.randomUUID() val putDatasourceExpectedError = Error(code = 400, message = "CSV custom separator should be single character") val putDatasourceException = intercept[ApiExceptionWithJsonBody] { api.putDatasourceImpl(userId, userName, id, TestData.multicharSeparatorLibraryCsvDatasource) } putDatasourceException.error shouldBe putDatasourceExpectedError info("Operation yields 404 for wrong datasource") the[ApiException].thrownBy( api.getDatasourceImpl(userId, id) ).errorCode shouldBe 404 } } "Privileged api consumer" - { val userId = UUID.randomUUID() val userName = "Alice" val privilegedUserId = Get.privilegedUsers.head "can Get private data sources not owned by him" in { for (dsParams <- TestData.someDatasources(Some(Visibility.privateVisibility))) { val id = UUID.randomUUID() api.putDatasourceImpl(userId, userName, id, dsParams) api.getDatasourceImpl(privilegedUserId, id) } } } }
Example 92
Source File: DatasourcesAccessControlSpec.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.seahorse.datasource.server import java.util.UUID import org.scalatest.{FreeSpec, Matchers} import ai.deepsense.seahorse.datasource.api.{ApiException, DatasourceManagerApi} import ai.deepsense.seahorse.datasource.db.FlywayMigration import ai.deepsense.seahorse.datasource.model.{AccessLevel, Visibility} class DatasourcesAccessControlSpec extends FreeSpec with Matchers { lazy val api = ApiForTests.api "Api consumer is a subject to access control" in { val testEnvironment = createTestEnviroment() import testEnvironment._ { info("Alice can access all her datasources") val datasources = api.getDatasourcesImpl(alice) val datasourcesIds = datasources.map(_.id) datasourcesIds should contain(alicesPublicDs) datasourcesIds should contain(alicesPrivateDs) info("Alice has READ_WRITE access to her datasources") datasources.find(_.id == alicesPublicDs).get.accessLevel shouldEqual AccessLevel.writeRead datasources.find(_.id == alicesPrivateDs).get.accessLevel shouldEqual AccessLevel.writeRead } { info("Bob can see only public datasources of other users") val datasources = api.getDatasourcesImpl(bob) val datasourcesIds = datasources.map(_.id) datasourcesIds should contain(alicesPublicDs) datasourcesIds shouldNot contain(alicesPrivateDs) info("Bob has only READ access to other users datasources") datasources.find(_.id == alicesPublicDs).get.accessLevel shouldEqual AccessLevel.read api.getDatasourceImpl(bob, alicesPublicDs).accessLevel shouldEqual AccessLevel.read api.getDatasourceImpl(bob, alicesPublicDs).ownerName shouldEqual aliceName } { info("Bob cannot hack Alices datasources") info("Bob cannot get private datasource") the[ApiException].thrownBy( api.getDatasourceImpl(bob, alicesPrivateDs) ).errorCode shouldBe forbiddenErrorCode info("Bob cannot edit Alices datasource") the[ApiException].thrownBy( api.putDatasourceImpl(bob, bobName, alicesPublicDs, TestData.someDatasource()) ).errorCode shouldBe forbiddenErrorCode the[ApiException].thrownBy( api.putDatasourceImpl(bob, bobName, alicesPrivateDs, TestData.someDatasource()) ).errorCode shouldBe forbiddenErrorCode info("Bob cannot delete Alices datasources") the[ApiException].thrownBy( api.deleteDatasourceImpl(bob, alicesPublicDs) ).errorCode shouldBe forbiddenErrorCode the[ApiException].thrownBy( api.deleteDatasourceImpl(bob, alicesPrivateDs) ).errorCode shouldBe forbiddenErrorCode } } private def createTestEnviroment() = new { val alice = UUID.randomUUID() val aliceName = "Alice" val alicesPublicDs = UUID.randomUUID() val alicesPrivateDs = UUID.randomUUID() api.putDatasourceImpl(alice, aliceName, alicesPublicDs, TestData.someDatasource().copy( visibility = Visibility.publicVisibility )) api.putDatasourceImpl(alice, aliceName, alicesPrivateDs, TestData.someDatasource().copy( visibility = Visibility.privateVisibility )) val bobName = "Bob" val bob = UUID.randomUUID() } private val forbiddenErrorCode = 403 }
Example 93
Source File: ApiSpec.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.seahorse.scheduling.server import java.util.UUID import org.scalatest.{FreeSpec, Matchers} import ai.deepsense.seahorse.scheduling.api.{ApiException, SchedulingManagerApi} import ai.deepsense.seahorse.scheduling.db.FlywayMigration class ApiSpec extends FreeSpec with Matchers { // TODO Copy pasta test private lazy val api = { FlywayMigration.run() new SchedulingManagerApi() } "Api consumer" - { "can add new workflow schedules" in { val workflowId = UUID.randomUUID() val scheduling = TestData.someScheduleFor(workflowId) api.putWorkflowScheduleImpl(scheduling.id, scheduling) api.getWorkflowSchedulesImpl() should contain(scheduling) api.getWorkflowScheduleImpl(scheduling.id) shouldEqual scheduling info("Add operation is idempotent") api.putWorkflowScheduleImpl(scheduling.id, scheduling) api.getWorkflowSchedulesImpl() should contain(scheduling) api.getWorkflowScheduleImpl(scheduling.id) shouldEqual scheduling info("Schedulings might be fetched by workflow id") val scheduleForOtherWorkflow = TestData.someScheduleFor(UUID.randomUUID()) api.putWorkflowScheduleImpl(scheduleForOtherWorkflow.id, scheduleForOtherWorkflow) val anotherSchedulingForSameWorkflow = TestData.someScheduleFor(workflowId) api.putWorkflowScheduleImpl(anotherSchedulingForSameWorkflow.id, anotherSchedulingForSameWorkflow) api.getSchedulesForWorkflowImpl(workflowId) should contain allOf( scheduling, anotherSchedulingForSameWorkflow ) api.getSchedulesForWorkflowImpl(workflowId) should not contain scheduleForOtherWorkflow info("Workflow schedule can also be deleted") api.deleteWorkflowScheduleImpl(scheduling.id) api.getWorkflowSchedulesImpl() shouldNot contain(scheduling) the[ApiException].thrownBy( api.getWorkflowScheduleImpl(scheduling.id) ).errorCode shouldBe 404 } } }
Example 94
Source File: SampleUsageSpec.scala From bloom-filter-scala with MIT License | 5 votes |
package endToEnd.bloomfilter.mutable import bloomfilter.mutable.BloomFilter import org.scalatest.{FreeSpec, Matchers} class SampleUsageSpec extends FreeSpec with Matchers { "Create, put and check " in { val bloomFilter = BloomFilter[String](1000, 0.01) bloomFilter.add("") bloomFilter.add("Hello!") bloomFilter.add("8f16c986824e40e7885a032ddd29a7d3") bloomFilter.mightContain("") shouldBe true bloomFilter.mightContain("Hello!") shouldBe true bloomFilter.mightContain("8f16c986824e40e7885a032ddd29a7d3") shouldBe true bloomFilter.dispose() } }
Example 95
Source File: SampleUsageSpec.scala From bloom-filter-scala with MIT License | 5 votes |
package endToEnd.bloomfilter.mutable._128bit import bloomfilter.mutable._128bit.BloomFilter import org.scalatest.{FreeSpec, Matchers} class SampleUsageSpec extends FreeSpec with Matchers { "Create, put and check " in { val bloomFilter = BloomFilter[String](1000, 0.01) bloomFilter.add("") bloomFilter.add("Hello!") bloomFilter.add("8f16c986824e40e7885a032ddd29a7d3") bloomFilter.mightContain("") shouldBe true bloomFilter.mightContain("Hello!") shouldBe true bloomFilter.mightContain("8f16c986824e40e7885a032ddd29a7d3") shouldBe true bloomFilter.dispose() } }
Example 96
Source File: CanGetDataFromSpec.scala From bloom-filter-scala with MIT License | 5 votes |
package tests.bloomfilter import bloomfilter.CanGetDataFrom.CanGetDataFromArrayChar import org.scalatest.{FreeSpec, Matchers} class CanGetDataFromSpec extends FreeSpec with Matchers { "CanGetDataFromArrayChar" in { CanGetDataFromArrayChar.getByte(Array[Char]('a'), 0) shouldEqual 97.toByte CanGetDataFromArrayChar.getByte(Array[Char]('a'), 1) shouldEqual 0.toByte CanGetDataFromArrayChar.getByte(Array[Char]('a', 'b'), 0) shouldEqual 97.toByte CanGetDataFromArrayChar.getByte(Array[Char]('a', 'b'), 1) shouldEqual 0.toByte CanGetDataFromArrayChar.getByte(Array[Char]('a', 'b'), 2) shouldEqual 98.toByte CanGetDataFromArrayChar.getByte(Array[Char]('a', 'b'), 3) shouldEqual 0.toByte CanGetDataFromArrayChar.getLong(Array[Char]('a', 'b', 'c', 'd'), 0) shouldEqual (0.toLong << 56) | (('d'.toByte & 0xffL) << 48) | ((0 & 0xffL) << 40) | (('c'.toByte & 0xffL) << 32) | ((0 & 0xffL) << 24) | (('b' & 0xffL) << 16) | ((0 & 0xffL) << 8) | 'a' & 0xffL } }
Example 97
Source File: QualityValueSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.http import org.scalatest.FreeSpec import scala.util.Failure import scala.util.Success class QualityValueSpec extends FreeSpec { "QualityValue.parse should" - { "error when input that is not a double in base10" in { val Failure(err) = QualityValue.parse("asd") assertResult("Unexpected q value 'asd'. Expected 0.0 <= q <= 1.0 with no more than 3 decimal places")(err.getMessage) } "error when the provide value is a double and is outside the valid range" in { val Failure(err) = QualityValue.parse("1.1") assertResult("Unexpected q value '1.1'. Expected 0.0 <= q <= 1.0 with no more than 3 decimal places")(err.getMessage) } "error when more than 3 decimal places" in { val Failure(err) = QualityValue.parse("0.1234") assertResult("Unexpected q value '0.1234'. Expected 0.0 <= q <= 1.0 with no more than 3 decimal places")(err.getMessage) } "error when does not start with 0 or 1" in { val Failure(err) = QualityValue.parse("7.1234") assertResult("Unexpected q value '7.1234'. Expected 0.0 <= q <= 1.0 with no more than 3 decimal places")(err.getMessage) } "succeed when input is" - { "0.0" in { assertResult(Success(QualityValue(0.0)))(QualityValue.parse("0.0")) } "0.25" in { assertResult(Success(QualityValue(0.25)))(QualityValue.parse("0.25")) } "1.0" in { assertResult(Success(QualityValue(1.0)))(QualityValue.parse("1.0")) } } } }
Example 98
Source File: MediaTypeSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.http import com.mesosphere.Generators.Implicits._ import org.scalatest.FreeSpec import org.scalatest.prop.PropertyChecks import scala.util.Success final class MediaTypeSpec extends FreeSpec with PropertyChecks { private val testMediaType = MediaType( "application", MediaTypeSubType("vnd.dcos.test", Some("json")), Map("charset" -> "utf-8", "version" -> "v1") ) "MediaType.parse(string) should" - { "parse basic type" in { val Success(t) = MediaType.parse("text/html") assertResult("text/html")(t.show) } "parse with suffix" in { val Success(t) = MediaType.parse("""image/svg+xml""") assertResult("image")(t.`type`) assertResult("svg")(t.subType.value) assertResult(Some("xml"))(t.subType.suffix) assertResult("""image/svg+xml""")(t.show) } "parse parameters" in { val Success(t) = MediaType.parse("""text/html; charset=utf-8; foo=bar""") assertResult("text")(t.`type`) assertResult("html")(t.subType.value) assertResult(Map( "charset" -> "utf-8", "foo" -> "bar" ))(t.parameters) } "lower-case type" in { val Success(t) = MediaType.parse("""IMAGE/SVG+XML""") assertResult("""image/svg+xml""")(t.show) } "lower-case parameter names" in { val Success(t) = MediaType.parse("""text/html; Charset=utf-8""") assertResult("text")(t.`type`) assertResult("html")(t.subType.value) assertResult(Map( "charset" -> "utf-8" ))(t.parameters) } "parse a vendor type" in { val Success(t) = MediaType.parse("application/vnd.dcos.test+json; charset=utf-8; version=v1") assertResult(testMediaType)(t) } "unquote parameter values" in { val Success(t) = MediaType.parse("""text/html; charset="utf-8"""") assertResult("text")(t.`type`) assertResult("html")(t.subType.value) assertResult(Map( "charset" -> "utf-8" ))(t.parameters) } } "A MediaType should show correctly" - { "text/html" in { assertResult("text/html")(MediaType("text", MediaTypeSubType("html")).show) } "application/xhtml+xml" in { assertResult("application/xhtml+xml")(MediaType("application", MediaTypeSubType("xhtml", Some("xml"))).show) } "application/vnd.dcos.custom-request+json" in { assertResult("application/vnd.dcos.custom-request+json")( MediaType("application", MediaTypeSubType("vnd.dcos.custom-request", Some("json"))).show ) } } "MediaType.show followed by MediaType.parse should be the identity function" in { forAll { (mediaType: MediaType) => assertResult(Success(mediaType))(MediaType.parse(mediaType.show)) } } // TODO package-add: Test for case-insensitive comparison }
Example 99
Source File: ResponseSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.converter import com.mesosphere.cosmos.converter.Response._ import com.mesosphere.cosmos.error.ServiceMarathonTemplateNotFound import com.mesosphere.cosmos.rpc import com.mesosphere.cosmos.thirdparty.marathon.model.AppId import com.mesosphere.universe import com.mesosphere.universe.bijection.UniverseConversions._ import com.mesosphere.universe.v3.model.Cli import com.twitter.bijection.Conversion.asMethod import com.twitter.util.Return import com.twitter.util.Throw import com.twitter.util.Try import org.scalacheck.Gen import org.scalatest.FreeSpec import org.scalatest.Matchers import org.scalatest.prop.GeneratorDrivenPropertyChecks.forAll final class ResponseSpec extends FreeSpec with Matchers { "Conversion[rpc.v2.model.InstallResponse,Try[rpc.v1.model.InstallResponse]]" - { val vstring = "9.87.654.3210" val ver = universe.v3.model.Version(vstring) val name = "ResponseSpec" val appid = AppId("foobar") val clis = List(None, Some("post install notes")) val notes = List(None, Some(Cli(None))) val validV2s = for { n <- Gen.oneOf(clis) c <- Gen.oneOf(notes) } yield (rpc.v2.model.InstallResponse(name, ver, Some(appid), n, c)) val invalidV2s = for { n <- Gen.oneOf(clis) c <- Gen.oneOf(notes) } yield (rpc.v2.model.InstallResponse(name, ver, None, n, c)) "success" in { val v1 = rpc.v1.model.InstallResponse(name, ver.as[universe.v2.model.PackageDetailsVersion], appid) forAll(validV2s) { x => x.as[Try[rpc.v1.model.InstallResponse]] shouldBe Return(v1) } } "failure" in { //expecting failure due to missing marathon mustache forAll(invalidV2s) { x => x.as[Try[rpc.v1.model.InstallResponse]] shouldBe Throw( ServiceMarathonTemplateNotFound(name, ver).exception ) } } } }
Example 100
Source File: AppIdSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.thirdparty.marathon.model import io.lemonlabs.uri.dsl._ import org.scalatest.FreeSpec final class AppIdSpec extends FreeSpec { private[this] val relative: String = "cassandra/dcos" private[this] val absolute: String = s"/$relative" "AppId should" - { "consistently render" - { "absolute" in { assertResult(absolute)(AppId(absolute).toString) } "relative" in { assertResult(absolute)(AppId(relative).toString) } } "consistently construct" - { "absolute" in { assertResult(AppId(absolute))(AppId(absolute)) assertResult(AppId(absolute).hashCode)(AppId(absolute).hashCode) } "relative" in { assertResult(AppId(absolute))(AppId(relative)) assertResult(AppId(absolute).hashCode)(AppId(relative).hashCode) } } "generate uri" - { "absolute" in { assertResult("/cassandra" / "dcos")(AppId(absolute).toUri) } "relative" in { assertResult("/cassandra" / "dcos")(AppId(relative).toUri) } } } }
Example 101
Source File: EncodersDecodersSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.thirdparty.marathon.circe import com.mesosphere.cosmos.thirdparty.marathon.model.AppId import io.circe.Json import io.circe.jawn.decode import io.circe.syntax._ import org.scalatest.FreeSpec import scala.util.Right class EncodersDecodersSpec extends FreeSpec { "AppId" - { val relative: String = "cassandra/dcos" val absolute: String = s"/$relative" "encode" in { assertResult(Json.fromString(absolute))(AppId(relative).asJson) } "decode" in { val id = AppId(absolute) val Right(decoded) = decode[AppId](relative.asJson.noSpaces) assertResult(id)(decoded) } } }
Example 102
Source File: EncodersDecodersSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.thirdparty.adminrouter.circe import com.mesosphere.cosmos.thirdparty.adminrouter.model.DcosVersion import com.mesosphere.universe.v3.model.DcosReleaseVersion import com.mesosphere.universe.v3.model.DcosReleaseVersion._ import io.circe.Json import io.circe.syntax._ import org.scalatest.FreeSpec import scala.util.Right class EncodersDecodersSpec extends FreeSpec { "DcosVersion" - { "decode" in { val json = Json.obj( "version" -> "1.7.1".asJson, "dcos-image-commit" -> "0defde84e7a71ebeb5dfeca0936c75671963df48".asJson, "bootstrap-id" -> "f12bff891be7108962c7c98e530e1f2cd8d4e56b".asJson ) val (major, minor, patch) = (1, 7, 1) val expected = DcosVersion( DcosReleaseVersion(Version(major), List(Version(minor), Version(patch))), "0defde84e7a71ebeb5dfeca0936c75671963df48", "f12bff891be7108962c7c98e530e1f2cd8d4e56b" ) val Right(actual) = json.as[DcosVersion] assertResult(expected)(actual) } } }
Example 103
Source File: HttpClientSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos import com.mesosphere.cosmos.error.CosmosException import com.mesosphere.cosmos.error.EndpointUriSyntax import com.mesosphere.universe import io.lemonlabs.uri.Uri import io.lemonlabs.uri.dsl._ import com.twitter.finagle.stats.NullStatsReceiver import com.twitter.finagle.stats.StatsReceiver import com.twitter.util.Await import io.circe.jawn.parse import org.scalatest.FreeSpec import scala.io.Source final class HttpClientSpec extends FreeSpec { import HttpClientSpec._ "HttpClient.fetch() retrieves the given URL" in { val future = HttpClient.fetch(JsonContentUrl) { responseData => assertResult(universe.MediaTypes.UniverseV4Repository)(responseData.contentType) val contentString = Source.fromInputStream(responseData.contentStream).mkString assert(parse(contentString).isRight) } Await.result(future) } "HttpClient.fetch() reports URL syntax problems" - { "relative URI" in { val cosmosException = intercept[CosmosException]( Await.result(HttpClient.fetch("foo/bar")(_ => ())) ) assert(cosmosException.error.isInstanceOf[EndpointUriSyntax]) } "unknown protocol" in { val cosmosException = intercept[CosmosException]( Await.result(HttpClient.fetch("foo://bar.com")(_ => ())) ) assert(cosmosException.error.isInstanceOf[EndpointUriSyntax]) } "URISyntaxException" in { val cosmosException = intercept[CosmosException]( Await.result(HttpClient.fetch("/\\")(_ => ())) ) assert(cosmosException.error.isInstanceOf[EndpointUriSyntax]) } } } object HttpClientSpec { implicit val stats: StatsReceiver = NullStatsReceiver val JsonContentUrl: Uri = "https://downloads.mesosphere.com/universe/" + "ae6a07ac0b53924154add2cd61403c5233272d93/repo/repo-up-to-1.10.json" }
Example 104
Source File: ServicesSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos import com.mesosphere.cosmos.error.CosmosException import com.mesosphere.cosmos.error.ServiceUnavailable import io.lemonlabs.uri.dsl._ import com.twitter.conversions.storage._ import com.twitter.finagle.http.RequestBuilder import com.twitter.util.Await import com.twitter.util.Return import io.netty.handler.codec.http.HttpResponseStatus import org.scalatest.FreeSpec final class ServicesSpec extends FreeSpec { "Services" - { "adminRouterClient should" - { "be able to be created with a well formed URI with a domain that doesn't resolve" in { val url = "http://some.domain.that-im-pretty-sure-doesnt-exist-anywhere" val Return(client) = Services.adminRouterClient(url, 5.megabytes) try { val request = RequestBuilder().url(url).buildGet() Await.result(client(request)) } catch { case e: CosmosException if e.error.isInstanceOf[ServiceUnavailable] => assertResult(HttpResponseStatus.SERVICE_UNAVAILABLE)(e.error.status) } finally { val _ = Await.ready(client.close()) } } } } }
Example 105
Source File: ServiceClientSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos import _root_.io.circe.Json import com.mesosphere.cosmos.http.Authorization import com.mesosphere.cosmos.http.RequestSession import com.mesosphere.cosmos.test.TestUtil import com.mesosphere.http.OriginHostScheme import io.lemonlabs.uri.Uri import com.twitter.finagle.http.Request import org.scalatest.FreeSpec import org.scalatest.Inside final class ServiceClientSpec extends FreeSpec with Inside { import ServiceClientSpec._ "A ServiceClient" - { "supports an optional AuthorizationHeaderName request header" - { "so that Cosmos can interact with security-enabled AdminRouters" - { val testClient = new AuthorizationTestClient() "header not provided" - { implicit val session = TestUtil.Anonymous "with baseRequestBuilder()" in { val requestBuilder = testClient.baseRequestBuilder(Uri.parse("/foo/bar/baz")) assert(!requestBuilder.buildGet.headerMap.contains(AuthorizationHeaderName)) } "with get()" in { assert(!testClient.testGet.headerMap.contains(AuthorizationHeaderName)) } "with post()" in { assert(!testClient.testPost.headerMap.contains(AuthorizationHeaderName)) } "with postForm()" in { assert(!testClient.testPostForm.headerMap.contains(AuthorizationHeaderName)) } "with delete()" in { assert(!testClient.testDelete.headerMap.contains(AuthorizationHeaderName)) } } "header provided" - { implicit val session = RequestSession( Some(Authorization("credentials")), OriginHostScheme("localhost", OriginHostScheme.Scheme.http) ) "with baseRequestBuilder()" in { val requestBuilder = testClient.baseRequestBuilder(Uri.parse("/foo/bar/baz")) val headerOpt = requestBuilder.buildDelete.headerMap.get(AuthorizationHeaderName) inside(headerOpt) { case Some(header) => assertResult("credentials")(header) } } "with get()" in { val headerOpt = testClient.testGet.headerMap.get(AuthorizationHeaderName) inside (headerOpt) { case Some(header) => assertResult("credentials")(header) } } "with post()" in { val headerOpt = testClient.testPost.headerMap.get(AuthorizationHeaderName) inside (headerOpt) { case Some(header) => assertResult("credentials")(header) } } "with postForm()" in { val headerOpt = testClient.testPostForm.headerMap.get(AuthorizationHeaderName) inside (headerOpt) { case Some(header) => assertResult("credentials")(header) } } "with delete()" in { val headerOpt = testClient.testDelete.headerMap.get(AuthorizationHeaderName) inside (headerOpt) { case Some(header) => assertResult("credentials")(header) } } } } } } } object ServiceClientSpec { private[cosmos] val PathUri: Uri = Uri.parse("/foo/bar/baz") private val AuthorizationHeaderName: String = "Authorization" } private final class AuthorizationTestClient extends ServiceClient(Uri.parse("http://example.com")) { def testGet(implicit session: RequestSession): Request = get(ServiceClientSpec.PathUri) def testPost(implicit session: RequestSession): Request = post(ServiceClientSpec.PathUri, Json.Null) def testPostForm(implicit session: RequestSession): Request = postForm(ServiceClientSpec.PathUri, "") def testDelete(implicit session: RequestSession): Request = delete(ServiceClientSpec.PathUri) }
Example 106
Source File: UrisSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos import io.lemonlabs.uri.dsl._ import com.twitter.util.{Return, Throw} import org.scalatest.FreeSpec class UrisSpec extends FreeSpec { "extractHostAndPort should" - { "succeed for" - { val http = 80 val https = 443 val httpAlt = 8080 "http://domain" in { assertResult(Return(ConnectionDetails("domain", http, tls = false)))(Uris.extractHostAndPort("http://domain")) } "https://domain" in { assertResult(Return(ConnectionDetails("domain", https, tls = true)))(Uris.extractHostAndPort("https://domain")) } "http://domain:8080" in { assertResult(Return(ConnectionDetails("domain", httpAlt, tls = false)))(Uris.extractHostAndPort("http://domain:8080")) } "http://sub.domain" in { assertResult(Return(ConnectionDetails("sub.domain", http, tls = false)))(Uris.extractHostAndPort("http://sub.domain")) } "https://sub.domain" in { assertResult(Return(ConnectionDetails("sub.domain", https, tls = true)))(Uris.extractHostAndPort("https://sub.domain")) } "http://10.0.0.1" in { assertResult(Return(ConnectionDetails("10.0.0.1", http, tls = false)))(Uris.extractHostAndPort("http://10.0.0.1")) } "https://10.0.0.1" in { assertResult(Return(ConnectionDetails("10.0.0.1", https, tls = true)))(Uris.extractHostAndPort("https://10.0.0.1")) } } "fail for" - { "domain" in { val Throw(err) = Uris.extractHostAndPort("domain") val expectedMessage = "Unsupported or invalid URI. Expected format 'http[s]://example.com[:port][/base-path]' actual 'domain'" assertResult(expectedMessage)(err.getMessage) } "domain:8080" in { val Throw(err) = Uris.extractHostAndPort("domain:8080") val expectedMessage = "Unsupported or invalid URI. Expected format 'http[s]://example.com[:port][/base-path]' actual 'domain:8080'" assertResult(expectedMessage)(err.getMessage) } "ftp://domain" in { val Throw(err) = Uris.extractHostAndPort("ftp://domain") val expectedMessage = "Unsupported or invalid URI. Expected format 'http[s]://example.com[:port][/base-path]' actual 'ftp://domain'" assertResult(expectedMessage)(err.getMessage) } } } }
Example 107
Source File: TrysSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos import org.scalatest.FreeSpec import com.twitter.util.Return import com.twitter.util.Throw final class TrysSpec extends FreeSpec { "join[A,B]" in { assertResult(Return((1,2)))(Trys.join(Return(1), Return(2))) val e = new IllegalArgumentException val n = new NoSuchElementException assertResult(Throw(e))(Trys.join(Throw(e), Return(2))) assertResult(Throw(e))(Trys.join(Return(1), Throw(e))) assertResult(Throw(n))(Trys.join(Throw(n), Throw(e))) } "join[A,B,C]" in { assertResult(Return((1,2,3)))(Trys.join(Return(1), Return(2), Return(3))) val e = new IllegalArgumentException val n = new NoSuchElementException assertResult(Throw(e))(Trys.join(Throw(e), Return(2), Return(3))) assertResult(Throw(e))(Trys.join(Return(1), Throw(e), Return(3))) assertResult(Throw(n))(Trys.join(Throw(n), Throw(e), Return(3))) } }
Example 108
Source File: DefaultRepositoriesSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.repository import com.mesosphere.cosmos.repository.DefaultRepositories._ import com.mesosphere.cosmos.rpc.v1.model.PackageRepository import io.lemonlabs.uri.dsl._ import com.twitter.util.Return import io.circe.ParsingFailure import org.scalatest.FreeSpec class DefaultRepositoriesSpec extends FreeSpec { val repo1 = PackageRepository("repo1", "http://someplace/repo1") val repo2 = PackageRepository("repo2", "http://someplace/repo2") val repo3 = PackageRepository("repo3", "http://someplace/repo3") "DefaultRepositories for" - { "empty-list.json should" - { val emptyList = new DefaultRepositories("/com/mesosphere/cosmos/repository/empty-list.json") "property load" in { val expected = List() assertResult(expected)(emptyList.getOrThrow) } } "repos.json should" - { val repos = new DefaultRepositories("/com/mesosphere/cosmos/repository/repos.json") "property load" in { val expected = List(repo1, repo2, repo3) assertResult(expected)(repos.getOrThrow) } } "malformed.json should" - { val malformed = new DefaultRepositories("/com/mesosphere/cosmos/repository/malformed.json") "collect an error" in { val Return(xor) = malformed.get() assert(xor.isLeft) } "throw an error for a getOrThrow()" in { try { val _ = malformed.getOrThrow } catch { case ParsingFailure(_, _) => // expected } } "return a default for a getOrElse()" in { val actual = malformed.getOrElse(List(repo1)) val expected = List(repo1) assertResult(expected)(actual) } } "non-existent resource should" - { "throw IllegalStateException" in { try { val _ = new DefaultRepositories("/does/not/exist").getOrThrow } catch { case _: IllegalStateException => // expected } } } "default-repositories.json should load" in { val defaults = DefaultRepositories().getOrThrow assertResult(List(PackageRepository("default", "http://default")))(defaults) } } }
Example 109
Source File: ZkRepositoryListSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.repository import cats.data.Ior import com.mesosphere.cosmos.rpc.v1.model.PackageRepository import io.lemonlabs.uri.dsl._ import org.scalatest.FreeSpec class ZkRepositoryListSpec extends FreeSpec { val real = PackageRepository("real", "http://real.real") val fake = PackageRepository("fake", "http://fake.fake") val realFake = PackageRepository("real", "http://fake.fake") "ZkRepositoryList" - { "getPredicate should" - { "work when" - { "only name is specified" in { val predicate = ZkRepositoryList.getPredicate(Ior.Left("real")) assert(predicate(real)) assert(!predicate(fake)) assert(predicate(realFake)) } "only uri is specified" in { val predicate = ZkRepositoryList.getPredicate(Ior.Right("http://real.real")) assert(predicate(real)) assert(!predicate(fake)) assert(!predicate(realFake)) } "both name and uri are specified" in { val predicate = ZkRepositoryList.getPredicate(Ior.Both("real", "http://real.real")) assert(predicate(real)) assert(!predicate(fake)) assert(!predicate(realFake)) } } } } }
Example 110
Source File: ZooKeeperUriSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.model import org.scalatest.FreeSpec import org.scalatest.prop.TableDrivenPropertyChecks final class ZooKeeperUriSpec extends FreeSpec with TableDrivenPropertyChecks { import ZooKeeperUriSpec._ "parse()" - { "invalid uris" in { forAll(InvalidUris) { stringToParse => assert(ZooKeeperUri.parse(stringToParse).isLeft) } } "valid uris" in { forAll(ValidUris) { (stringToParse, connectString, path) => val Right(zkUri) = ZooKeeperUri.parse(stringToParse) assertResult(connectString)(zkUri.connectString) assertResult(path)(zkUri.path) } } } "toString" - { "valid uris" in { forAll(ValidUris) { (stringToParse, _, _) => val Right(uri) = ZooKeeperUri.parse(stringToParse) assertResult(stringToParse)(uri.toString) } } } } object ZooKeeperUriSpec extends TableDrivenPropertyChecks { val InvalidUris = Table( "string uri", "", "host1", "host1:2181", "/path/to/znode", "zk://", "zk://host1", "zk://host1/", "zk://host1:2181,host2:2181,host3:2181/invalid//path", "zk://host1:port1/valid/path" ) val ValidUris = Table( ("string uri", "connection string", "path"), ("zk://host1/z", "host1", "/z"), ("zk://host1:2181/path", "host1:2181", "/path"), ("zk://foo:12,bar:34,baz:56/path/to/znode", "foo:12,bar:34,baz:56", "/path/to/znode") ) }
Example 111
Source File: StorageEnvelopeSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.model import com.mesosphere.cosmos.error.CosmosException import com.mesosphere.cosmos.finch.MediaTypedDecoder import com.mesosphere.cosmos.finch.MediaTypedEncoder import com.mesosphere.error.ResultOps import com.mesosphere.http.MediaType import com.mesosphere.http.MediaTypeSubType import io.circe.Decoder import io.circe.Encoder import io.circe.generic.semiauto._ import org.scalatest.FreeSpec class StorageEnvelopeSpec extends FreeSpec { import StorageEnvelopeSpec._ "Decoding the result of an encoding should yield back the original data" in { val exp = TestClass("fooooo") val act = StorageEnvelope.decodeData[TestClass]( StorageEnvelope.encodeData(exp) ).getOrThrow assertResult(exp)(act) } "Decoding the result of an encoding as a different type should throw an exception" in { val input = TestClass2("fooooo") intercept[CosmosException] { StorageEnvelope.decodeData[TestClass](StorageEnvelope.encodeData(input)).getOrThrow } () } } object StorageEnvelopeSpec { private case class TestClass(name: String) private val TestClassMt = MediaType( "application", MediaTypeSubType("vnd.dcos.package.repository.test-class", Some("json")), Map( "charset" -> "utf-8", "version" -> "v1" ) ) private implicit val decodeTestClass: Decoder[TestClass] = deriveDecoder private implicit val encodeTestClass: Encoder[TestClass] = deriveEncoder private implicit val mtdTestClass: MediaTypedDecoder[TestClass] = MediaTypedDecoder(TestClassMt) private implicit val mteTestClass: MediaTypedEncoder[TestClass] = MediaTypedEncoder(TestClassMt) private case class TestClass2(name: String) private val TestClass2Mt = MediaType( "application", MediaTypeSubType("vnd.dcos.package.repository.test-class2", Some("json")), Map( "charset" -> "utf-8", "version" -> "v1" ) ) private implicit val encodeTestClass2: Encoder[TestClass2] = deriveEncoder private implicit val mteTestClass2: MediaTypedEncoder[TestClass2] = MediaTypedEncoder(TestClass2Mt) }
Example 112
Source File: ServiceUpdateHandlerSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.handler import com.mesosphere.cosmos.error.CosmosException import com.mesosphere.cosmos.handler.ServiceUpdateHandler._ import com.mesosphere.cosmos.rpc import io.circe.syntax._ import org.scalatest.FreeSpec import org.scalatest.Matchers class ServiceUpdateHandlerSpec extends FreeSpec with Matchers { import ServiceUpdateHandlerSpec._ "The mergeStoredAndProvided() method" - { "should always return provided when replace is true" in { val replace = true val provided = configA mergeStoredAndProvided(None, provided, replace) shouldBe provided mergeStoredAndProvided(configB, provided, replace) shouldBe provided mergeStoredAndProvided(configC, provided, replace) shouldBe provided } "should return the merge of stored and provided when" + " replace is false, and stored is present" in { val replace = false val stored = configA mergeStoredAndProvided(stored, None, replace) shouldBe stored mergeStoredAndProvided(stored, configB, replace) shouldBe configAB mergeStoredAndProvided(stored, configC, replace) shouldBe configAC } "should throw an error when replace is false and there are no stored options" in { val replace = false val stored = None val errorType = "OptionsNotStored" val error1 = rpc.v1.model.ErrorResponse( intercept[CosmosException]( mergeStoredAndProvided(stored, None, replace) ).error ) error1.`type` shouldBe errorType val error2 = rpc.v1.model.ErrorResponse( intercept[CosmosException]( mergeStoredAndProvided(stored, configA, replace) ).error ) error2.`type` shouldBe errorType } } } object ServiceUpdateHandlerSpec { // scalastyle:off multiple.string.literals val configA = Map( "a" -> "aa-val", "b" -> "ab-val", "c" -> "ac-val" ).asJson.asObject val configB = Map( "d" -> "bd-val", "e" -> "be-val" ).asJson.asObject val configC = Map( "c" -> "cc-val", "d" -> "cd-val", "e" -> "ce-val" ).asJson.asObject val configAB = Map( "a" -> "aa-val", "b" -> "ab-val", "c" -> "ac-val", "d" -> "bd-val", "e" -> "be-val" ).asJson.asObject val configAC = Map( "a" -> "aa-val", "b" -> "ab-val", "c" -> "cc-val", "d" -> "cd-val", "e" -> "ce-val" ).asJson.asObject // scalastyle:on multiple.string.literals }
Example 113
Source File: ResourceProxyHandlerSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.handler import com.mesosphere.Generators.Implicits._ import com.mesosphere.cosmos.error.CosmosException import com.mesosphere.cosmos.error.GenericHttpError import io.lemonlabs.uri.Uri import org.scalacheck.Arbitrary.arbitrary import org.scalacheck.Gen import org.scalatest.FreeSpec import org.scalatest.Matchers import org.scalatest.prop.PropertyChecks final class ResourceProxyHandlerSpec extends FreeSpec with PropertyChecks with Matchers { type TestData = (Long, Uri) "When Content-Length is provided by the upstream server" - { "When Content-Length matches the actual content stream length" - { // scalastyle:off magic.number val genTestData: Gen[TestData] = for { actualLength <- Gen.chooseNum(2, 10) uri <- arbitrary[Uri] } yield { (actualLength.toLong, uri) } "Succeeds if Content-Length is below the limit" in { forAll(genTestData) { case (actualLength, uri) => ResourceProxyHandler.validateContentLength(uri, Some(actualLength)) } } "Fails if Content-Length is zero" in { forAll (genTestData) { case (_, uri) => val exception = intercept[CosmosException](ResourceProxyHandler.validateContentLength(uri, Some(0))) assert(exception.error.isInstanceOf[GenericHttpError]) } } } } "Fails when Content-Length is not provided by the upstream server" in { val exception = intercept[CosmosException](ResourceProxyHandler.validateContentLength(Uri.parse("/random"), None)) assert(exception.error.isInstanceOf[GenericHttpError]) } "Parses the filename correctly" in { ResourceProxyHandler.getFileNameFromUrl( Uri.parse("http://doesntreallymatter.com/c.d") ) shouldEqual Some("c.d") ResourceProxyHandler.getFileNameFromUrl( Uri.parse("http://doesntreallymatter.com/a/b/c.d") ) shouldEqual Some("c.d") ResourceProxyHandler.getFileNameFromUrl( Uri.parse("http://doesntreallymatter.com/a/b/c") ) shouldEqual Some("c") ResourceProxyHandler.getFileNameFromUrl( Uri.parse("http://doesntreallymatter.com/a/b/c/") ) shouldEqual Some("c") // These should never happen, but just in case. ResourceProxyHandler.getFileNameFromUrl( Uri.parse("http://doesntreallymatter.com/") ) shouldEqual None ResourceProxyHandler.getFileNameFromUrl( Uri.parse("https://doesntreallymatter.com") ) shouldEqual None } }
Example 114
Source File: DispatchingMediaTypedEncoderSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.finch import cats.data.NonEmptyList import com.mesosphere.http.CompoundMediaType import com.mesosphere.http.MediaType import io.circe.Encoder import io.circe.Json import io.circe.syntax._ import org.scalatest.FreeSpec final class DispatchingMediaTypedEncoderSpec extends FreeSpec { import DispatchingMediaTypedEncoderSpec._ "encoderFor(MediaType) should" - { "return the first encoder with a media type compatible with the argument" in { val Some(mediaTypedEncoder) = ThreeElementEncoder(CompoundMediaType(MediaType("foo", "bar"))) assertResult(Json.fromInt(1))(mediaTypedEncoder.encoder(())) assertResult(NonEmptyList.of(MediaType("foo", "bar")))(mediaTypedEncoder.mediaTypes) } "indicate failure if no compatible encoder is found" - { "because there are no encoders" in { val dispatchingEncoder = DispatchingMediaTypedEncoder(Set.empty[MediaTypedEncoder[String]]) assertResult(None)(dispatchingEncoder(CompoundMediaType(TestingMediaTypes.applicationJson))) } "because there are only incompatible encoders" in { assertResult(None)(ThreeElementEncoder(CompoundMediaType(TestingMediaTypes.applicationJson))) } } } "mediaTypes should return the media types of each of the encoders" - { "zero elements" in { assertResult(Set.empty)(DispatchingMediaTypedEncoder(Set.empty[MediaTypedEncoder[String]]).mediaTypes) } "three elements" in { val expected = Set(MediaType("foo", "foo"), MediaType("foo", "bar"), MediaType("foo", "baz")) assertResult(expected)(ThreeElementEncoder.mediaTypes) } } } object DispatchingMediaTypedEncoderSpec { val ThreeElementEncoder: DispatchingMediaTypedEncoder[Unit] = DispatchingMediaTypedEncoder(Set( MediaTypedEncoder(Encoder.instance[Unit](_ => 0.asJson), MediaType("foo", "foo")), MediaTypedEncoder(Encoder.instance[Unit](_ => 1.asJson), MediaType("foo", "bar")), MediaTypedEncoder(Encoder.instance[Unit](_ => 2.asJson), MediaType("foo", "baz")) )) }
Example 115
Source File: JsonSchemaSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.jsonschema import com.github.fge.jsonschema.main.JsonSchemaFactory import io.circe.Json import io.circe.JsonObject import io.circe.jawn.parse import io.circe.syntax._ import org.scalatest.FreeSpec import org.scalatest.Tag import scala.io.Source import scala.util.Right class JsonSchemaSpec extends FreeSpec { private[this] implicit val jsf = JsonSchemaFactory.byDefault() "JsonSchema should" - { "be able to validate a document against a schema" - { // the draft v4 json schema itself should be able to validate itself val jsonSchemaDraftV4String = classpathJsonString("/draftv4/schema") "as io.circe.JsonObject" in { val Right(parsedJson: Json) = parse(jsonSchemaDraftV4String) val xor = JsonSchema.jsonMatchesSchema(parsedJson, parsedJson) assert(xor.isRight) } "as io.circe.Json" in { val Right(parsedJson: Json) = parse(jsonSchemaDraftV4String) val jObject: JsonObject = parsedJson.asObject.get val xor = JsonSchema.jsonObjectMatchesSchema(jObject, jObject) assert(xor.isRight) } } "be able to extract default property values from a schema" - { val expected = JsonObject.fromMap(Map( "prop1" -> 57.asJson, "prop2" -> Json.obj( "sub1" -> "ta-da".asJson ) )) "when schema does not use definition refs" in { val s = classpathJsonString("/com/mesosphere/cosmos/jsonschema/no-definition-ref-used.json") val Right(schema) = parse(s) val defaults = JsonSchema.extractDefaultsFromSchema(schema.asObject.get) assertResult(expected)(defaults) } "when schema does use definition refs" taggedAs Tag("https://mesosphere.atlassian.net/browse/DCOS-10455") ignore { val s = classpathJsonString("/com/mesosphere/cosmos/jsonschema/definition-ref-used.json") val Right(schema) = parse(s) val defaults = JsonSchema.extractDefaultsFromSchema(schema.asObject.get) assertResult(expected)(defaults) } } } private[this] def classpathJsonString(resourceName: String): String = { Option(this.getClass.getResourceAsStream(resourceName)) match { case Some(is) => Source.fromInputStream(is).mkString case _ => throw new IllegalStateException(s"Unable to load classpath resource: $resourceName") } } }
Example 116
Source File: ImagesEncoderDecoderSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.v2.circe import com.mesosphere.universe.v2.model.Images import io.circe.Decoder import io.circe.Json import io.circe.syntax._ import org.scalatest.FreeSpec class ImagesEncoderDecoderSpec extends FreeSpec { "Images" - { val json = Json.obj( "icon-small" -> "http://some.place/icon-small.png".asJson, "icon-medium" -> "http://some.place/icon-medium.png".asJson, "icon-large" -> Json.Null, "screenshots" -> List( "http://some.place/screenshot-1.png", "http://some.place/screenshot-2.png" ).asJson ) val images = Images( iconSmall = Some("http://some.place/icon-small.png"), iconMedium = Some("http://some.place/icon-medium.png"), iconLarge = None, screenshots = Some(List( "http://some.place/screenshot-1.png", "http://some.place/screenshot-2.png" )) ) "encoder" in { assertResult(json)(images.asJson) } "decode" in { assertResult(images)(decodeJson[Images](json)) } "round-trip" in { assertResult(images)(decodeJson[Images](images.asJson)) } } private[this] def decodeJson[A: Decoder](json: Json)(implicit decoder: Decoder[A]): A = { decoder.decodeJson(json).getOrElse(throw new AssertionError("Unable to decode")) } }
Example 117
Source File: VersionSpecificationSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.v3.model import com.mesosphere.Generators.Implicits.arbVersion import org.scalatest.FreeSpec import org.scalatest.prop.PropertyChecks final class VersionSpecificationSpec extends FreeSpec with PropertyChecks { "The matches() method" - { "always returns true on AnyVersion" in { forAll { (version: Version) => assert(AnyVersion.matches(version)) } } "returns true on ExactVersion when the versions are equal" in { forAll { (version: Version) => assert(ExactVersion(version).matches(version)) } } "returns false on ExactVersion when the versions are unequal" in { forAll { (v1: Version, v2: Version) => whenever (v1 != v2) { assert(!ExactVersion(v1).matches(v2)) } } } } }
Example 118
Source File: PackageDefinitionSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.v3.model import com.mesosphere.Generators.nonNegNum import org.scalacheck.Gen import org.scalatest.FreeSpec import org.scalatest.prop.PropertyChecks final class PackageDefinitionSpec extends FreeSpec with PropertyChecks { "PackageDefinition$.ReleaseVersion" - { "ReleaseVersion$.validate should" - { "succeed on non-negative numbers" in { forAll (nonNegNum[Long]) { n => whenever (n >= 0) { assert(ReleaseVersion.validate(n).isReturn) } } } "fail on negative numbers" in { forAll (Gen.negNum[Long]) { n => whenever (n < 0) { assert(ReleaseVersion.validate(n).isThrow) } } } } "ReleaseVersion.value" in { forAll (nonNegNum[Long]) { n => assertResult(n)(ReleaseVersion(n).value) } } "ReleaseVersion$.ordering orders by value" in { forAll (nonNegNum[Long], nonNegNum[Long]) { (a, b) => whenever (a >= 0 && b >= 0) { val aVersion = ReleaseVersion(a) val bVersion = ReleaseVersion(b) assertResult(Ordering[Long].compare(a, b)) { Ordering[ReleaseVersion].compare(aVersion, bVersion) } } } } } }
Example 119
Source File: V3PackageSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.v3.model import com.mesosphere.universe import java.nio.ByteBuffer import org.scalatest.FreeSpec import org.scalatest.Matchers import scala.util.Random class V3PackageSpec extends FreeSpec with Matchers { val input = List( // scalastyle:off magic.number ("pkg1", Version("1.0-1"), ReleaseVersion(1)), ("pkg1", Version("1.0-2"), ReleaseVersion(2)), ("pkg1", Version("1.0-3"), ReleaseVersion(3)), ("pkg2", Version("1.0"), ReleaseVersion(1)), ("pkg2", Version("2.0"), ReleaseVersion(2)), ("pkg3", Version("1.0"), ReleaseVersion(3)), ("pkg4", Version("1.0"), ReleaseVersion(4)), ("pkg5", Version("1.0-1"), ReleaseVersion(1)), ("pkg5", Version("2.0-1"), ReleaseVersion(2)), ("pkg5", Version("1.1-1"), ReleaseVersion(3)), ("pkg6", Version("0.0.0.1"), ReleaseVersion(1)), ("pkg6", Version("0.0.0.5"), ReleaseVersion(2)), ("pkg6", Version("0.0.0.2"), ReleaseVersion(3)), ("pkg7", Version("0.0.1"), ReleaseVersion(1)), ("pkg7", Version("0.0.4.2"), ReleaseVersion(10)) // scalastyle:on magic.number ) "V3Package" - { "Ordering should work" in { val expected = input.map(v3Package(_)) val actual = Random.shuffle(expected).sorted actual shouldBe expected } } "V2Package" - { "Ordering should work" in { val expected = input.map(v2Package(_)) val actual = Random.shuffle(expected).sorted actual shouldBe expected } } "PackageDefinition" - { "Ordering should work" in { val expected = input.map(packageDefinition(_)) val actual = Random.shuffle(expected).sorted actual shouldBe expected } } def v3Package(tuple: (String, Version, ReleaseVersion)): V3Package = { val (name, version, relVer) = tuple V3Package( V3PackagingVersion, name, version, relVer, "[email protected]", "doesn't matter" ) } def v2Package(tuple: (String, Version, ReleaseVersion)): V2Package = { val (name, version, relVer) = tuple V2Package( V2PackagingVersion, name, version, relVer, "[email protected]", "doesn't matter", Marathon(ByteBuffer.allocate(0)) ) } def packageDefinition(tuple: (String, Version, ReleaseVersion)): universe.v4.model.PackageDefinition = { if (Random.nextBoolean) { v2Package(tuple) } else { v3Package(tuple) } } }
Example 120
Source File: PackagingVersionSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.v3.model import com.mesosphere.universe import com.mesosphere.universe.test.TestingPackages import org.scalatest.FreeSpec import org.scalatest.Matchers import org.scalatest.prop.TableDrivenPropertyChecks final class PackagingVersionSpec extends FreeSpec with Matchers with TableDrivenPropertyChecks { "PackagingVersion.show" - { forAll(TestingPackages.validPackagingVersions) { (version, string) => s"PackagingVersion $string" in { version.show should be(string) } } } "PackagingVersions.allVersions" in { val allVersions = TestingPackages.validPackagingVersions.map(_._1) allVersions should be(universe.v4.model.PackagingVersion.allVersions) } }
Example 121
Source File: SemVerSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.v3.model import com.mesosphere.Generators.Implicits._ import org.scalatest.FreeSpec import org.scalatest.Matchers import org.scalatest.prop.PropertyChecks import scala.util.Random final class SemVerSpec extends FreeSpec with PropertyChecks with Matchers { "For all SemVer => String => SemVer" in { forAll { (expected: SemVer) => val string = expected.toString val actual = SemVer(string).get actual shouldBe expected } } "Test semver ordering" in { val expected = List( "1.0.0-alpha", "1.0.0-alpha.1", "1.0.0-alpha.beta", "1.0.0-beta", "1.0.0-beta.2", "1.0.0-beta.11", "1.0.0-rc.1", "1.0.0", "1.0.2", "1.2.0", "1.11.0", "1.11.11", "2", "11.11.11" ).map(SemVer(_).get) val actual = Random.shuffle(expected).sorted actual shouldBe expected } }
Example 122
Source File: DcosReleaseVersionEncoderDecoderSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.v3.circe import com.mesosphere.universe.v3.model.DcosReleaseVersion import com.mesosphere.universe.v3.model.DcosReleaseVersion._ import io.circe.Json import io.circe.jawn.decode import io.circe.syntax._ import org.scalatest.FreeSpec import scala.util.Right class DcosReleaseVersionEncoderDecoderSpec extends FreeSpec { "DcosReleaseVersion" - { val str = "2.10.153-beta" val (major, minor, patch) = (2, 10, 153) val subVersions = List(Version(minor), Version(patch)) val obj = DcosReleaseVersion(Version(major), subVersions, Some(Suffix("beta"))) "decode" in { val stringToDecode = s""""$str"""" val Right(decoded) = decode[DcosReleaseVersion](stringToDecode) assertResult(obj)(decoded) } "encode" in { assertResult(Json.fromString(str))(obj.asJson) } } }
Example 123
Source File: ReleaseVersionConverterSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.bijection import com.mesosphere.universe import com.mesosphere.universe.bijection.UniverseConversions._ import com.twitter.bijection.Conversion.asMethod import com.twitter.bijection.{Bijection, Injection} import org.scalatest.FreeSpec import scala.util.{Failure, Success, Try} final class ReleaseVersionConverterSpec extends FreeSpec { "v2ReleaseVersionToString should" - { "always succeed in the forward direction" in { assertResult("0")(universe.v2.model.ReleaseVersion("0").as[String]) } "always succeed in the reverse direction" in { assertResult(universe.v2.model.ReleaseVersion("1"))("1".as[universe.v2.model.ReleaseVersion]) } } "v3ReleaseVersionToLong should" - { "always succeed in the forward direction" in { val version = universe.v3.model.ReleaseVersion(2) assertResult(2)(version.as[Long]) } "succeed in the reverse direction on nonnegative numbers" in { val version = universe.v3.model.ReleaseVersion(0) assertResult(Success(version))( 0L.as[Try[universe.v3.model.ReleaseVersion]] ) } "fail in the reverse direction on negative numbers" in { val Failure(iae) = (-1L).as[Try[universe.v3.model.ReleaseVersion]] val expectedMessage = "Expected integer value >= 0 for release version, but found [-1]" assertResult(expectedMessage)(iae.getMessage) } } "v3ReleaseVersionToString should" - { behave like v3ReleaseVersionStringConversions[String] } "v3ReleaseVersionToV2ReleaseVersion should" - { behave like v3ReleaseVersionStringConversions[universe.v2.model.ReleaseVersion] } private[this] def v3ReleaseVersionStringConversions[A](implicit versionToA: Injection[universe.v3.model.ReleaseVersion, A], aToString: Bijection[A, String] ): Unit = { "always succeed in the forward direction" in { val version = 42L assertResult("42") { universe.v3.model.ReleaseVersion(version).as[A].as[String] } } "succeed in the reverse direction on nonnegative version numbers" in { val version = 24L assertResult(Success(universe.v3.model.ReleaseVersion(version))) { "24".as[A].as[Try[universe.v3.model.ReleaseVersion]] } } "fail in the reverse direction on negative version numbers" in { val Failure(iae) = "-2".as[A].as[Try[universe.v3.model.ReleaseVersion]] val message = "Expected integer value >= 0 for release version, but found [-2]" assertResult(message)(iae.getMessage) } "fail in the reverse direction on non-number values" in { val Failure(iae) = "foo".as[A].as[Try[universe.v3.model.ReleaseVersion]] val message = "Failed to invert: foo" assertResult(message)(iae.getMessage) } } }
Example 124
Source File: JsonUtilSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.common import io.circe.Encoder import io.circe.Json import io.circe.JsonObject import io.circe.generic.semiauto._ import io.circe.syntax._ import org.scalatest.FreeSpec import org.scalatest.prop.TableDrivenPropertyChecks class JsonUtilSpec extends FreeSpec with TableDrivenPropertyChecks { case class Foo(bar: Option[Int],far: Option[Int]) implicit val encodeFoo: Encoder[Foo] = { deriveEncoder[Foo] } "dropNullKeys" in { val ls = Foo(None,None) val string = JsonUtil.dropNullKeysPrinter.pretty(ls.asJson) assertResult("{}")(string) } "Merging JSON objects" - { "should pass on all examples" in { forAll (Examples) { (defaultsJson, optionsJson, mergedJson) => assertResult(mergedJson)(JsonUtil.merge(defaultsJson, optionsJson)) } } } private[this] val Examples = Table( ("defaults JSON", "options JSON", "merged JSON"), (JsonObject.empty, JsonObject.empty, JsonObject.empty), ( JsonObject.empty, JsonObject.singleton("a", Json.False), JsonObject.singleton("a", Json.False) ), ( JsonObject.singleton("a", Json.False), JsonObject.empty, JsonObject.singleton("a", Json.False) ), ( JsonObject.singleton("a", Json.False), JsonObject.singleton("a", Json.True), JsonObject.singleton("a", Json.True) ), ( JsonObject.singleton("a", Json.obj("a" -> Json.False)), JsonObject.singleton("a", Json.obj()), JsonObject.singleton("a", Json.obj("a" -> Json.False)) ), ( JsonObject.singleton("a", Json.obj("a" -> Json.False)), JsonObject.singleton("a", Json.obj("a" -> Json.True)), JsonObject.singleton("a", Json.obj("a" -> Json.True)) ), ( JsonObject.singleton("a", Json.obj("a" -> Json.False)), JsonObject.singleton("a", Json.obj("b" -> Json.False)), JsonObject.singleton("a", Json.obj("a" -> Json.False, "b" -> Json.False)) ), ( JsonObject.singleton("a", Json.obj("a" -> Json.False)), JsonObject.singleton("a", Json.True), JsonObject.singleton("a", Json.True) ) ) }
Example 125
Source File: ByteBuffersSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.universe.common import org.scalatest.FreeSpec import java.nio.ByteBuffer import java.nio.charset.StandardCharsets import java.util class ByteBuffersSpec extends FreeSpec { "ByteBuffers.toBytes(ByteBuffer) should" - { "work for an array backed ByteBuffer" in { val bytes = "hello".getBytes(StandardCharsets.UTF_8) val bb = ByteBuffer.wrap(bytes) val actual = ByteBuffers.getBytes(bb) assert(util.Arrays.equals(bytes, actual)) } "work for a non-array backed ByteBuffer" in { val bytes = "hello".getBytes(StandardCharsets.UTF_8) val bb = ByteBuffer.allocateDirect(bytes.size) bytes.foreach(bb.put) bb.rewind() // rewind the position back to the beginning after having written val actual = ByteBuffers.getBytes(bb) assert(util.Arrays.equals(bytes, actual)) } "check read index bounds" in { val bytes = "hello".getBytes(StandardCharsets.UTF_8) val bb = ByteBuffer.allocateDirect(bytes.size) bytes.foreach(bb.put) try { val _ = ByteBuffers.getBytes(bb) } catch { case ioobe: IndexOutOfBoundsException => assertResult("5 > 0")(ioobe.getMessage) } } } }
Example 126
Source File: ErrorResponseSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.rpc.v1.model import com.mesosphere.cosmos.http.HttpRequest import com.mesosphere.cosmos.http.PackageRpcPath import com.mesosphere.cosmos.rpc.MediaTypes import com.mesosphere.cosmos.test.CosmosIntegrationTestClient._ import com.twitter.finagle.http.Fields import com.twitter.finagle.http.Status import io.circe.jawn._ import org.scalatest.FreeSpec final class ErrorResponseSpec extends FreeSpec { "An ErrorResponse should be returned as the body when a request can't be parsed" in { val request = HttpRequest.post( PackageRpcPath("install"), Map("invalid" -> true), MediaTypes.InstallRequest, MediaTypes.V1InstallResponse ) val response = CosmosClient.submit(request) assertResult(Status.BadRequest)(response.status) assertResult(MediaTypes.ErrorResponse.show)(response.headerMap(Fields.ContentType)) val Right(err) = decode[ErrorResponse](response.contentString) val msg = err.message assert(msg.contains("body")) assert(msg.contains("decode value")) assert(msg.contains("packageName")) } }
Example 127
Source File: ListVersionsSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.handler import _root_.io.circe.jawn.parse import _root_.io.circe.syntax._ import com.mesosphere.cosmos.IntegrationBeforeAndAfterAll import com.mesosphere.cosmos.http.CosmosRequests import com.mesosphere.cosmos.rpc import com.mesosphere.cosmos.test.CosmosIntegrationTestClient.CosmosClient import com.twitter.finagle.http.Status import org.scalatest.FreeSpec import org.scalatest.Matchers final class ListVersionsSpec extends FreeSpec with Matchers with IntegrationBeforeAndAfterAll{ "ListVersionHandler should" - { "list only helloworld versions found in the first repo with helloworld packages" in { val request = CosmosRequests.packageListVersions( rpc.v1.model.ListVersionsRequest("helloworld", includePackageVersions = true)) val response = CosmosClient.submit(request) val expectedContent = Map( "results" -> Map( "0.4.2" -> "5", "0.4.1" -> "4", "0.4.0" -> "3", "0.1.0" -> "0" ) ).asJson response.status shouldBe Status.Ok parse(response.contentString) shouldBe Right(expectedContent) } } }
Example 128
Source File: CapabilitiesHandlerSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos.handler import com.mesosphere.cosmos.http.CosmosRequests import com.mesosphere.cosmos.rpc.MediaTypes import com.mesosphere.cosmos.rpc.v1.model.CapabilitiesResponse import com.mesosphere.cosmos.rpc.v1.model.Capability import com.mesosphere.cosmos.test.CosmosIntegrationTestClient._ import com.twitter.finagle.http.Fields import com.twitter.finagle.http.Status import io.circe.jawn._ import org.scalatest.FreeSpec import scala.util.Right final class CapabilitiesHandlerSpec extends FreeSpec { "The capabilities handler should return a document" in { val response = CosmosClient.submit(CosmosRequests.capabilities) assertResult(Status.Ok)(response.status) assertResult(MediaTypes.CapabilitiesResponse.show)(response.headerMap(Fields.ContentType)) val Right(body) = decode[CapabilitiesResponse](response.contentString) val expected = CapabilitiesResponse(List( Capability("PACKAGE_MANAGEMENT"), Capability("SUPPORT_CLUSTER_REPORT"), Capability("METRONOME"), Capability("LOGGING"), Capability("LOGGING_V2") )) assertResult(expected)(body) } }
Example 129
Source File: ServicesIntegrationSpec.scala From cosmos with Apache License 2.0 | 5 votes |
package com.mesosphere.cosmos import io.lemonlabs.uri.dsl._ import com.twitter.conversions.storage._ import com.twitter.finagle.http.RequestBuilder import com.twitter.finagle.http.Status import com.twitter.util.Await import com.twitter.util.Return import org.scalatest.FreeSpec final class ServicesIntegrationSpec extends FreeSpec { "Services" - { "adminRouterClient should" - { "be able to connect to an https site" in { val url = "https://www.google.com" val Return(client) = Services.adminRouterClient(url, 5.megabytes) val request = RequestBuilder().url(url).buildGet() val response = Await.result(client(request)) assertResult(response.status)(Status.Ok) } } } }
Example 130
Source File: WaveSpec.scala From chisel-gui with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See README.md for license details. package visualizer.models import org.scalatest.{FreeSpec, Matchers} class WaveSpec extends FreeSpec with Matchers { def makeTransitions(seq: (Long, BigInt)*): Seq[Transition] = { seq.map { case (t, v) => Transition(t, v) } } "Creating a wave gets one automatic entry" in { val wave = new Wave wave.start(0) should be(0L) wave.end(0) should be(Long.MaxValue) wave.value(0) should be(BigInt(0)) wave.findStartIndex(0) should be(0L) wave.findStartIndex(1000000L) should be(0L) wave.end(0) should be > (0L) } "Adding segments to wave should work properly" in { val wave = new Wave var transitions = makeTransitions((0, 50), (10, 40)) wave.addChanges(transitions) wave.length should be(2) wave.start(0) should be(0) wave.end(0) should be(10) wave.value(0) should be(BigInt(50)) wave.start(1) should be(10) wave.end(1) should be(Long.MaxValue) wave.value(1) should be(BigInt(40)) println(s"starts: ${wave.starts}") println(s"values: ${wave.values}") transitions = makeTransitions((0, 12), (10, 40), (15, 20)) wave.addChanges(transitions) wave.length should be(3) wave.start(0) should be(0) wave.end(0) should be(10) wave.value(0) should be(BigInt(12)) wave.start(1) should be(10) wave.end(1) should be(15) wave.value(1) should be(BigInt(40)) wave.start(2) should be(15) wave.end(2) should be(Long.MaxValue) wave.value(2) should be(BigInt(20)) println(s"starts: ${wave.starts}") println(s"values: ${wave.values}") // // Wave can be shortened // transitions = makeTransitions((0, 7), (2, 4)) wave.addChanges(transitions) wave.length should be(2) wave.start(0) should be(0) wave.end(0) should be(2) wave.value(0) should be(BigInt(7)) wave.start(1) should be(2) wave.end(1) should be(Long.MaxValue) wave.value(1) should be(BigInt(4)) println(s"starts: ${wave.starts}") println(s"values: ${wave.values}") } }
Example 131
Source File: DecoupledFireRestrictor.scala From chisel-gui with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See README.md for license details. package visualizer.models import org.scalatest.{FreeSpec, Matchers} import scala.collection.mutable object DecoupledFireRestrictor { case class Interval(start: Long, end: Long, value: BigInt) def buildTimeVector(buffer: mutable.ArrayBuffer[Transition]): List[Interval] = { val b = buffer.toList.sliding(2) val newList = b.flatMap { case transition1 :: transition2 :: Nil => List( Interval(transition1.timestamp, transition2.timestamp, transition1.value), Interval(transition1.timestamp, transition2.timestamp, transition1.value) ) case transition :: Nil => List.empty } newList }.toList } class DecoupledFireRestrictorTest extends FreeSpec with Matchers { "select from an array based on values" in {} }
Example 132
Source File: FibonacciSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import org.scalatest.{FreeSpec, Matchers} class FibonacciSpec extends FreeSpec with Matchers { "the examples in the `fibonacci` chapter should work as expected" - { "unfold" in { //#unfold import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() // the "infinite" stream of all Fibonacci numbers def fibonacciNumbers: Spout[Int] = Spout.unfold(0 -> 1) { case (a, b) => Spout.Unfolding.Emit(elem = a, next = b -> (a + b)) } val result: Future[List[Int]] = fibonacciNumbers .take(8) .drainToList(limit = 100) result.value.get.get shouldEqual List(0, 1, 1, 2, 3, 5, 8, 13) //#unfold } "cycle" in { //#cycle import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() // the "infinite" stream of all Fibonacci numbers def fibonacciNumbers: Spout[Int] = { val c = Coupling[Int] Spout(0, 1) .concat(c.out) .fanOutBroadcast(eagerCancel = true) .sub.buffer(2, Buffer.RequestStrategy.Always).sliding(2).map(_.sum).to(c.in) .subContinue } val result: Future[List[Int]] = fibonacciNumbers .take(8) .drainToList(limit = 100) result.value.get.get shouldEqual List(0, 1, 1, 2, 3, 5, 8, 13) //#cycle } } }
Example 133
Source File: FahrenheitSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import java.nio.charset.StandardCharsets._ import java.nio.file.Files import org.scalatest.{BeforeAndAfterAll, FreeSpec, Matchers} class FahrenheitSpec extends FreeSpec with Matchers with BeforeAndAfterAll { val testFileContent = """// Temperature Readings in Fahrenheit |50.8 |80.2 |63.0 |-14 | |// Especially important temperatures |32 |-459.67 |451 """.stripMargin val inputFile = { val path = Files.createTempFile("fahrenheit-spec-input", ".tmp") Files.newBufferedWriter(path, UTF_8).append(testFileContent).close() path.toFile } val outputFile = Files.createTempFile("fahrenheit-spec-output", ".tmp").toFile "the examples in the `Fahrenheit` chapter should work as expected" - { "example" in { def println(s: String) = () // disable the printing below //#example import java.io.File import scala.util.{Failure, Success} import scala.concurrent.Future import swave.core.io.files._ // enables `Spout.fromFile` import swave.compat.scodec._ // enables `ByteVector` support import swave.core.text._ // enables text transformations import swave.core._ implicit val env = StreamEnv() import env.defaultDispatcher // for the future transformations below def fahrenheitToCelsius(f: Double): Double = (f - 32.0) * (5.0/9.0) def converter(fahrenheitReadingsInput: File, celciusReadingsOutput: File): RunnableStreamGraph[Future[Long]] = Spout.fromFile(fahrenheitReadingsInput) // Spout[ByteVector] .utf8Decode // Spout[String] .lines // Spout[String] .filterNot(_.trim.isEmpty) // Spout[String] .filterNot(_ startsWith "//") // Spout[String] .map(_.toDouble) // Spout[Double] .map(fahrenheitToCelsius) // Spout[Double] .map("%.2f" format _) // Spout[String] .intersperse("\n") // Spout[String] .utf8Encode // Spout[ByteVector] .to(Drain.toFile(celciusReadingsOutput)) // StreamGraph[Future[Long]] .seal() // RunnableStreamGraph[Future[Long]] // when we are ready to roll, start the stream val run: StreamRun[Future[Long]] = converter(inputFile, outputFile).run() // since the stream runs asynchronously we can't directly access the result run.result.onComplete { case Success(x) => println(s"OK, $x bytes written") case Failure(e) => println(s"Error: $e") } // shut down when everything has terminated env.shutdownOn(run.termination) //#example import swave.core.util._ run.termination.await() FileIO.readFile(outputFile).decodeUtf8 shouldEqual Right { """10.44 |26.78 |17.22 |-25.56 |0.00 |-273.15 |232.78""".stripMargin } } } override protected def afterAll(): Unit = Files.delete(inputFile.toPath) }
Example 134
Source File: SimpleTransformSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import org.scalatest.{FreeSpec, Matchers} class SimpleTransformSpec extends FreeSpec with Matchers { "the examples in the `simple transformations` chapter should work as expected" - { "example" in { //#example import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() val result: Future[String] = Spout(1, 2, 3, 4, 5) // Spout[Int] .map(_ * 2) // Spout[Int] .filter(_ > 5) // Spout[Int] .reduce(_ + _) // Spout[Int] .map(_.toString) // Spout[String] .drainToHead() // Future[String] result.value.get.get shouldEqual "24" //#example } } }
Example 135
Source File: PipeSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import org.scalatest.{FreeSpec, Matchers} import scala.concurrent.Future import swave.core._ class PipeSpec extends FreeSpec with Matchers { implicit val env = StreamEnv() "the examples in the `Pipes` chapter should work as expected" in { //#simplePipe def simplePipe: Pipe[Int, String] = Pipe[Int].filter(_ % 2 == 0).map(_.toString) //#simplePipe //#slice def slice[A](startIndex: Long, length: Long): Pipe[A, A] = Pipe[A] drop startIndex take length named "slice" //#slice //#spout-via def someInts: Spout[Int] = Spout.ints(from = 0).via(slice(10, 42)) someInts.drainToList(limit = 100) .value.get.get shouldEqual (10 to 51) //#spout-via //#pipe-to-drain def intDrain: Drain[Int, Future[String]] = Pipe[Int].map(_ * 2).to(Drain.mkString(limit = 100, sep = ", ")) Spout.ints(from = 0) .take(5) .drainTo(intDrain) .value.get.get shouldEqual "0, 2, 4, 6, 8" //#pipe-to-drain //#pipe-to-pipe def shareOfZeroFigures: Pipe[Int, Double] = Pipe[Int] .map(_.toString) .map { s => s.foldLeft(0) { case (zeroes, '0') => zeroes + 1 case (zeroes, _) => zeroes } / s.length.toDouble } def stringify: Pipe[Double, String] = Pipe[Double].map("%.2f" format _) def compound: Pipe[Int, String] = shareOfZeroFigures via stringify Spout.ints(from = 98) .take(5) .via(compound) .drainToMkString(limit = 100, sep = ", ") .value.get.get shouldEqual "0.00, 0.00, 0.67, 0.33, 0.33" //#pipe-to-pipe } }
Example 136
Source File: StreamOfStreamsSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import org.scalatest.{FreeSpec, Matchers} class StreamOfStreamsSpec extends FreeSpec with Matchers { "the examples in the `streams-of-streams` chapter should work as expected" - { "takeEvery" in { //#takeEvery import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() // simple extension for `Spout[T]`, could also be a value class implicit class RichSpout[T](underlying: Spout[T]) { def takeEvery(n: Long): Spout[T] = underlying // Spout[T] .injectSequential() // Spout[Spout[T]] .map(_.drop(n-1).take(1)) // Spout[Spout[T]] .flattenConcat() // Spout[T] } val result: Future[List[Int]] = Spout.ints(from = 1) .takeEvery(10) .take(5) .drainToList(limit = 100) result.value.get.get shouldEqual Seq(10, 20, 30, 40, 50) //#takeEvery } } }
Example 137
Source File: DrainSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import org.scalatest.{FreeSpec, Matchers} class DrainSpec extends FreeSpec with Matchers { "the examples in the `drains` chapter should work as expected" - { "examples" in { //#examples import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() // a drain, which produces the sum of all `Int` elements it receives def sumDrain: Drain[Int, Future[Int]] = Drain.fold(0)(_ + _) Spout(1 to 100) // Spout[Int] .to(sumDrain) // StreamGraph[Int] .run() // StreamRun[Future[Int] .result // Future[Int] .value // Option[Try[Int]] .get // Try[Int] .get shouldEqual 5050 // same but shorter Spout(1 to 100) .drainTo(sumDrain) // shortcut for `.to(sumDrain).run().result` .value.get.get shouldEqual 5050 //#examples } } }
Example 138
Source File: MD5Spec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import java.nio.file.Files import java.nio.charset.StandardCharsets._ import org.scalatest.{BeforeAndAfterAll, FreeSpec, Matchers} import scala.concurrent.duration._ import swave.core.util._ class MD5Spec extends FreeSpec with Matchers with BeforeAndAfterAll { val testFileContent = "swave rocks!" val testPath = { val path = Files.createTempFile("md5-spec", ".tmp") Files.newBufferedWriter(path, UTF_8).append(testFileContent).close() path } "the examples in the `MD5` chapter should work as expected" - { "example-0" in { //#example-0 import java.security.MessageDigest import java.io.File import scala.concurrent.Future import swave.core.io.files._ // enables `Spout.fromFile` import swave.compat.scodec._ // enables `ByteVector` support import swave.core._ implicit val env = StreamEnv() def md5sum(file: File): Future[String] = { val md5 = MessageDigest.getInstance("MD5") Spout.fromFile(file) // Spout[ByteVector] .fold(md5) { (m, bytes) => m.update(bytes.toArray); m } // Spout[MessageDigest] .flatMap(_.digest().iterator) // Spout[Byte] .map(_ & 0xFF) // Spout[Int] .map("%02x" format _) // Spout[String] .drainToMkString(limit = 32) // Future[String] } // don't forget to shutdown the StreamEnv at application exit with // env.shutdown() //#example-0 try md5sum(testPath.toFile).await(2.seconds) shouldEqual "e1b2b603f9cca4a909c07d42a5788fe3" finally { Thread.sleep(100) env.shutdown() } } "example-1" in { //#example-1 import java.io.File import scala.concurrent.Future import swave.core.io.files._ // enables `Spout.fromFile` import swave.core.hash._ // enables the `md5` transformation import swave.compat.scodec._ // enables `ByteVector` support import swave.core._ implicit val env = StreamEnv() def md5sum(file: File): Future[String] = Spout.fromFile(file) // Spout[ByteVector] .md5 // Spout[ByteVector] .flattenConcat() // Spout[Byte] .map(_ & 0xFF) // Spout[Int] .map("%02x" format _) // Spout[String] .drainToMkString(limit = 32) // Future[String] // don't forget to shutdown the StreamEnv at application exit with // env.shutdown() //#example-1 try md5sum(testPath.toFile).await(2.seconds) shouldEqual "e1b2b603f9cca4a909c07d42a5788fe3" finally { Thread.sleep(100) env.shutdown() } } } override protected def afterAll(): Unit = Files.delete(testPath) }
Example 139
Source File: CouplingSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import org.scalatest.{FreeSpec, Matchers} class CouplingSpec extends FreeSpec with Matchers { "the examples in the `couplings` chapter should work as expected" - { "fibonacci" in { //#fibonacci import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() def fibonacciNumbers = { val c = Coupling[Int] Spout(0, 1) .concat(c.out) .fanOutBroadcast(eagerCancel = true) .sub.buffer(2, Buffer.RequestStrategy.Always).sliding(2).map(_.sum).to(c.in) .subContinue } val result: Future[List[Int]] = fibonacciNumbers .take(8) .drainToList(limit = 100) result.value.get.get shouldEqual List(0, 1, 1, 2, 3, 5, 8, 13) //#fibonacci } } }
Example 140
Source File: FanInSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import org.scalatest.{FreeSpec, Matchers} class FanInSpec extends FreeSpec with Matchers { "the examples in the `fan-in` chapter should work as expected" - { "example1" in { //#example1 import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() def foo = Spout(1, 2, 3) def bar = Spout(10, 20, 30) def baz = Spout(100, 200, 300) val result: Future[List[Int]] = foo .attach(bar) .attach(baz) //.attach(...) .fanInConcat() .drainToList(limit = 10) result.value.get.get shouldEqual Seq(1, 2, 3, 10, 20, 30, 100, 200, 300) //#example1 } "example2" in { //#example2 import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() def reds = Spout.ints(from = 0, step = 10) def greens = Spout.ints(from = 100, step = 20) def blues = Spout.ints(from = 200, step = 30) val result: Future[List[(Int, Int, Int)]] = greens // open = greens .attach(blues) // open = greens :: blues .attachLeft(reds) // open = reds :: greens :: blues .fanInToTuple // Spout[(Int, Int, Int)] .take(3) .drainToList(limit = 10) result.value.get.get shouldEqual List( (0, 100, 200), (10, 120, 230), (20, 140, 260) ) //#example2 } "example3" in { //#example3 import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() case class Person(id: Long, name: String, age: Int) def ids = Spout.longs(1L) def names = Spout("Alice", "Bob", "Charlie", "David") def ages = Spout(27, 21, 48, 36) val result: Future[List[Person]] = ids .attach(names) .attach(ages) .fanInToProduct[Person] .drainToList(limit = 10) result.value.get.get shouldEqual List( Person(1L, "Alice", 27), Person(2L, "Bob", 21), Person(3L, "Charlie", 48), Person(4L, "David", 36) ) //#example3 } } }
Example 141
Source File: SpoutSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.docs import org.scalatest.{FreeSpec, Matchers} class SpoutSpec extends FreeSpec with Matchers { "the examples in the `spouts` chapter should work as expected" - { "option-flatmap" in { //#option-flatmap import scala.concurrent.Future import swave.core._ implicit val env = StreamEnv() def evenIntsViaFlatmap = Spout.ints(from = 0) .flatMap(i => if (i % 2 == 0) Some(i) else None) def evenIntsViaFilter = Spout.ints(from = 0) .filter(_ % 2 == 0) val a: Future[List[Int]] = evenIntsViaFlatmap.take(5).drainToList(limit = 5) val b: Future[List[Int]] = evenIntsViaFilter.take(5).drainToList(limit = 5) // since both streams in this example run synchronously // both futures are already fulfilled and we can access // their `value` members to compare results a.value.get shouldEqual b.value.get //#option-flatmap } "streamable-either" in { //#streamable-either import scala.util.Success import shapeless.Lub import swave.core._ implicit val env = StreamEnv() implicit def forEither[A, B, C](implicit ev: Lub[A, B, C]) = new Streamable[Either[A, B]] { type Out = C def apply(value: Either[A, B]): Spout[C] = value.fold(a => Spout.one(ev left a), b => Spout.one(ev right b)) } def eitherStream: Spout[Either[(Char, Int), String]] = Spout(Left('0' -> 0), Right("1"), Right("2")) def anyRefStream: Spout[AnyRef] = eitherStream.flattenConcat() anyRefStream .drainToList(5) .value.get shouldEqual Success(List('0' -> 0, "1", "2")) //#streamable-either } } }
Example 142
Source File: RegionSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core import org.scalatest.{FreeSpec, Matchers} import swave.core.graph.GlyphSet import swave.core.macros._ import scala.concurrent.Promise import scala.io.Source import scala.util.control.NonFatal // format: OFF class RegionSpec extends FreeSpec with Matchers { implicit val env = StreamEnv() "Example 1" tests { Spout.ints(0) .map(_.toString) .to(Drain.head) } "Example 2" tests { Spout.ints(0) .take(10) .asyncBoundary() .map(_.toString) .to(Drain.head) } "Example 3" tests { Spout(1, 2, 3) .fanOutBroadcast() .sub.map(_.toString).end .sub.asyncBoundary().end .fanInConcat() .to(Drain.head) } "Example 4" tests { def upperChars(s: String): Spout[Char] = Spout(s.iterator).map(_.toUpper) def drain(promise: Promise[String]): Drain[Char, Unit] = Drain.mkString(limit = 100) .captureResult(promise) .async("bar") val result2 = Promise[String]() upperChars("Hello") .asyncBoundary("foo") .fanOutBroadcast() .sub.drop(2).concat(upperChars("-Friend-").asyncBoundary()).end .sub.take(2).asyncBoundary().multiply(2).end .fanInConcat() .tee(Pipe[Char].asyncBoundary().deduplicate.to(drain(result2))) .map(_.toLower) .to(Drain.mkString(100)) } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// val examples: Map[String, String] = Source.fromInputStream(getClass.getResourceAsStream("/RegionSpec.examples.txt")) .getLines() .scanLeft(Left(Nil): Either[List[String], List[String]]) { case (Left(lines), "") ⇒ Right(lines.reverse) case (Left(lines), line) ⇒ Left(line :: lines) case (Right(_), line) ⇒ Left(line :: Nil) } .collect { case Right(renderString) ⇒ renderString } .toList .groupBy(_.head) .map { case (name, listOfRenderLines) ⇒ requireArg(listOfRenderLines.size == 1, ", which means you have an example name duplication in examples.txt") name → listOfRenderLines.head.tail.mkString("\n") } implicit class Example(name: String) { def tests(pipeNet: ⇒ StreamGraph[_]): Unit = name in { val expectedRendering = examples.getOrElse(name + ':', sys.error(s"Section for '$name' not found in examples.txt")) val superRegions = Graph.superRegions(pipeNet.seal().regions) val rendering = superRegions.map { sr => Graph.from(sr.head.entryPoint, Graph.ExpandModules.All) .withGlyphSet(GlyphSet.`2x2 ASCII`) .withStageFormat((s, _) => s"${s.kind.name}: ${sr.indexOf(s.region)}") .render() }.mkString("\n***\n") try rendering shouldEqual expectedRendering catch { case NonFatal(e) ⇒ println(rendering) throw e } } } }
Example 143
Source File: IllegalConstructsSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core import org.scalatest.FreeSpec import shapeless.test.illTyped class IllegalConstructsSpec extends FreeSpec { "Illegal DSL constructs shouldn't compile" - { "illegal switch fanout continue with too few substreams" in { illTyped( """Spout(1, 2, 3) .fanOutSwitch(2)(i => if (i > 0) 1 else 0) .sub.end .continue""", "Cannot `continue` here! You still have at least one unconsumed fan-out sub-stream.") } "illegal switch fanout subContinue with too few substreams" in { illTyped( """Spout(1, 2, 3) .fanOutSwitch(2)(i => if (i > 0) 1 else 0) .subContinue""", "`subContinue` is only possible with exactly one remaining fan-out sub-stream unconsumed!") } "illegal switch fanout sub with too many substreams" in { illTyped( """Spout(1, 2, 3) .fanOutSwitch(2)(i => if (i > 0) 1 else 0) .sub.end .sub.end .sub.end""", "Illegal substream definition! All available fan-out sub-streams have already been consumed.") } "illegal fanout continue with zero unterminated fanin sub-streams" in { illTyped( """Spout(1, 2, 3) .fanOutBroadcast() .continue""", "Continuation is only possible with exactly one open fan-in sub-stream!") } "illegal fanout continue with two unterminated fanin sub-streams" in { illTyped( """Spout(1, 2, 3) .fanOutBroadcast() .sub.end .sub.end .continue""", "Continuation is only possible with exactly one open fan-in sub-stream!") } "illegal zero sub fanin" in { illTyped( """Spout(1, 2, 3) .fanOutBroadcast() .fanInConcat()""", "Cannot fan-in here. You need to have at least two open fan-in sub-streams.") } } }
Example 144
Source File: DropWithinSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core.impl.stages import scala.util.Success import org.scalatest.FreeSpec import scala.concurrent.duration._ import swave.testkit.Probes import swave.core.{NotOnTravis, StreamEnv, StreamEnvShutdown} import swave.core.util._ import Probes._ final class DropWithinSpec extends FreeSpec with StreamEnvShutdown { implicit val env = StreamEnv() "DropWithin must" - { "deliver elements after the duration, but not before" taggedAs NotOnTravis in { val input = Iterator.from(1) val spout = SpoutProbe[Int] val drain = DrainProbe[Int] spout.dropWithin(100.millis).drainTo(drain) shouldBe a[Success[_]] drain.sendRequest(100) val demand1 = spout.expectRequestAggregated(20.millis).toInt demand1.times { spout.rawSendNext(input.next()) } val demand2 = spout.expectRequestAggregated(20.millis).toInt demand2.times { spout.rawSendNext(input.next()) } val demand3 = spout.expectRequestAggregated(100.millis).toInt spout.expectNoSignal() demand3.times { spout.rawSendNext(input.next()) } ((demand1 + demand2 + 1) to (demand1 + demand2 + demand3)).foreach(drain.expectNext(_)) spout.sendComplete() drain.expectComplete() } "deliver completion even before the duration" taggedAs NotOnTravis in { val spout = SpoutProbe[Int] val drain = DrainProbe[Int] spout.dropWithin(1.second).drainTo(drain) shouldBe a[Success[_]] spout.sendComplete() drain.expectComplete() drain.verifyCleanStop() } } }
Example 145
Source File: TakeWithinSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core.impl.stages import scala.concurrent.duration._ import scala.util.Success import org.scalatest.FreeSpec import swave.core.util._ import swave.core.{NotOnTravis, StreamEnv, StreamEnvShutdown} import swave.testkit.Probes._ final class TakeWithinSpec extends FreeSpec with StreamEnvShutdown { implicit val env = StreamEnv() "TakeWithin must" - { "deliver elements before the timeout, but not after" taggedAs NotOnTravis in { val input = Iterator.from(1) val spout = SpoutProbe[Int] val drain = DrainProbe[Int] spout.takeWithin(100.millis).drainTo(drain) shouldBe a[Success[_]] drain.sendRequest(100) val demand1 = spout.expectRequestAggregated(20.millis).toInt demand1.times { spout.rawSendNext(input.next()) } val demand2 = spout.expectRequestAggregated(20.millis).toInt demand2.times { spout.rawSendNext(input.next()) } val demand3 = spout.expectRequestAggregated(100.millis).toInt spout.expectNoSignal() demand3.toInt.times { spout.rawSendNext(input.next()) } (1 to (demand1 + demand2)).foreach(drain.expectNext(_)) spout.sendComplete() drain.expectComplete() } "deliver completion even before the duration" taggedAs NotOnTravis in { val spout = SpoutProbe[Int] val drain = DrainProbe[Int] spout.takeWithin(1.second).drainTo(drain) shouldBe a[Success[_]] spout.sendComplete() drain.expectComplete() drain.verifyCleanStop() } } }
Example 146
Source File: AkkaCompatSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.compat.akka import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.stream.scaladsl._ import scala.concurrent.duration._ import org.scalatest.{BeforeAndAfterAll, FreeSpec, Inside, Matchers} import swave.core._ import swave.core.util._ class AkkaCompatSpec extends FreeSpec with Matchers with Inside with BeforeAndAfterAll { implicit val env = StreamEnv() implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() "Akka compatibility should work as expected" - { "Source.toSpout" in { Source(1 to 10).toSpout.drainToList(100).await() shouldEqual (1 to 10) } "Spout.toAkkaSource" in { Spout(1 to 10).toAkkaSource.runWith(Sink.seq).await() shouldEqual (1 to 10) } "Flow.toPipe" in { val flow = Flow[Int].map(_ * -1) Spout(1 to 10).via(flow.toPipe).drainToList(100).await() shouldEqual (-1 to -10 by -1) } "Pipe.toAkkaFlow" in { val pipe = Pipe[Int].map(_ * -1) Source(1 to 10).via(pipe.toAkkaFlow).runWith(Sink.seq).await() shouldEqual (-1 to -10 by -1) } "Sink.toDrain" in { val sink = Sink.seq[Int] Spout(1 to 10).drainTo(sink.toDrain).await() shouldEqual (1 to 10) } "Drain.toAkkaSink" in { val drain = Drain.seq[Int](100) Source(1 to 10).runWith(drain.toAkkaSink).await() shouldEqual (1 to 10) } } override val invokeBeforeAllAndAfterAllEvenIfNoTestsAreExpected = true override protected def afterAll(): Unit = { val envTermination = env.shutdown() system.terminate().await(2.seconds) envTermination.awaitTermination(2.seconds) } }
Example 147
Source File: RingBufferSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core.util import org.scalacheck.Gen import org.scalatest.prop.GeneratorDrivenPropertyChecks import org.scalatest.{FreeSpec, Matchers} import swave.core.impl.util.RingBuffer class RingBufferSpec extends FreeSpec with Matchers with GeneratorDrivenPropertyChecks { "A RingBuffer should" - { val bufferGen = for { bit ← Gen.choose(0, 8) } yield new RingBuffer[String](cap = 1 << bit) "take in exactly `capacity` elements" in { forAll(bufferGen) { buf ⇒ val a = Stream.continually("x").takeWhile(buf.write).toArray a.length shouldEqual buf.capacity } } "read back exactly the number of elems previously written" in { val gen = for { buf ← bufferGen count ← Gen.choose(0, buf.capacity) } yield (buf, count) forAll(gen) { case (buf, count) ⇒ val values = List.tabulate(count)(_.toString) values.foreach(s ⇒ buf.write(s) shouldBe true) List.fill(count)(buf.read()) shouldEqual values buf.isEmpty shouldBe true a[NoSuchElementException] should be thrownBy buf.read() } } "pass a simple stress-test" in { val gen = for { buf ← bufferGen opCount ← Gen.choose(5, 20) ops ← Gen.listOfN(opCount, Gen.choose(-10, 20)) } yield (buf, ops) forAll(gen) { case (buf, ops) ⇒ val queue = collection.mutable.Queue[String]() val ints = Iterator.from(0) ops foreach { case readCount if readCount < 0 ⇒ -readCount times { buf.isEmpty shouldEqual queue.isEmpty if (queue.nonEmpty) queue.dequeue() shouldEqual buf.read() else a[NoSuchElementException] should be thrownBy buf.read() } case writeCount if writeCount > 0 ⇒ writeCount times { val next = ints.next().toString if (buf.write(next)) queue.enqueue(next) } case 0 ⇒ // ignore } } } } }
Example 148
Source File: RichLongSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core.util import org.scalatest.prop.GeneratorDrivenPropertyChecks import org.scalatest.{FreeSpec, Matchers} class RichLongSpec extends FreeSpec with Matchers with GeneratorDrivenPropertyChecks { "RichLong" - { val longMin = BigDecimal(Long.MinValue) val longMax = BigDecimal(Long.MaxValue) def bounded(d: BigDecimal) = if (d < longMin) Long.MinValue else if (d > longMax) Long.MaxValue else d.longValue() "⊹" in { forAll { (x: Long, y: Long) ⇒ x ⊹ y shouldEqual bounded(BigDecimal(x) + BigDecimal(y)) } } "×" in { forAll { (x: Long, y: Long) ⇒ (x × y) shouldEqual bounded(BigDecimal(x) * BigDecimal(y)) } } } }
Example 149
Source File: ResizableRingBufferSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core.util import org.scalacheck.Gen import org.scalatest.prop.GeneratorDrivenPropertyChecks import org.scalatest.{FreeSpec, Matchers} import swave.core.impl.util.ResizableRingBuffer class ResizableRingBufferSpec extends FreeSpec with Matchers with GeneratorDrivenPropertyChecks { "A ResizableRingBuffer should" - { val bufferGen = for { bit ← Gen.choose(0, 8) } yield new ResizableRingBuffer[String](initialCap = 1, maxCap = 1 << bit) "take in exactly `maxAvailable` elements" in { forAll(bufferGen) { buf ⇒ Stream.continually("x").takeWhile(buf.write).toArray.length shouldEqual buf.maxCapacity } } "read back exactly the number of elems previously written" in { val gen = for { buf ← bufferGen count ← Gen.choose(0, buf.maxCapacity) } yield (buf, count) forAll(gen) { case (buf, count) ⇒ val values = List.tabulate(count)(_.toString) values.foreach(s ⇒ buf.write(s) shouldBe true) List.fill(count)(buf.read()) shouldEqual values buf.isEmpty shouldBe true a[NoSuchElementException] should be thrownBy buf.read() } } "pass a simple stress-test" in { val gen = for { buf ← bufferGen opCount ← Gen.choose(5, 50) ops ← Gen.listOfN(opCount, Gen.choose(-20, 50)) } yield (buf, ops) forAll(gen) { case (buf, ops) ⇒ val queue = collection.mutable.Queue[String]() val ints = Iterator.from(0) ops foreach { case readCount if readCount < 0 ⇒ -readCount times { buf.isEmpty shouldEqual queue.isEmpty if (queue.nonEmpty) queue.dequeue() shouldEqual buf.read() else a[NoSuchElementException] should be thrownBy buf.read() } case writeCount if writeCount > 0 ⇒ writeCount times { val next = ints.next().toString if (buf.write(next)) queue.enqueue(next) } case 0 ⇒ // ignore } } } } }
Example 150
Source File: RichListSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core.util import org.scalacheck.Gen import org.scalatest.{FreeSpec, Matchers} import org.scalatest.prop.GeneratorDrivenPropertyChecks class RichListSpec extends FreeSpec with Matchers with GeneratorDrivenPropertyChecks { "RichList" - { "fastReverse" in { forAll { (list: List[Int]) ⇒ list.fastReverse shouldEqual list.reverse } } "remove" in { forAll(Gen.choose(0, 5), Gen.choose(0, 4)) { (n: Int, x: Int) ⇒ val list = List.tabulate(n)(identity) list.remove(x) shouldEqual list.filterNot(_ == x) } } } }
Example 151
Source File: RichRefArraySpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core.util import org.scalacheck.Gen import org.scalatest.prop.GeneratorDrivenPropertyChecks import org.scalatest.{FreeSpec, Matchers} class RichRefArraySpec extends FreeSpec with Matchers with GeneratorDrivenPropertyChecks { "RichRefArray" - { val stringArrays = Gen.containerOf[Array, String](Gen.alphaStr) "fastIndexOf" in { val arrayWithIndex = for { arr ← stringArrays ix ← Gen.chooseNum(0, arr.length + 1) } yield arr.map(Symbol(_)) → ix forAll(arrayWithIndex) { case (array, ix) ⇒ val specimen = if (ix < array.length) array(ix) else 'foo array.fastIndexOf(specimen) shouldEqual array.indexOf(specimen) } } "reverse_!" in { forAll(stringArrays) { array ⇒ val array2 = array.drop(0) array2.reverse_!() array2 shouldEqual array.reverse } } } }
Example 152
Source File: XorShiftRandomSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core.util import org.scalacheck.Gen import org.scalatest.prop.GeneratorDrivenPropertyChecks import org.scalatest.{FreeSpec, Matchers} class XorShiftRandomSpec extends FreeSpec with Matchers with GeneratorDrivenPropertyChecks { "XorShiftRandom" - { "nextLong" in { val random = XorShiftRandom() forAll(Gen.posNum[Long]) { bound ⇒ random.nextLong(bound) should (be >= 0L and be < bound) } } "nextInt" in { val random = XorShiftRandom() forAll(Gen.posNum[Int]) { bound ⇒ random.nextInt(bound) should (be >= 0 and be < bound) } } "nextDouble" in { val random = XorShiftRandom() forAll { (_: Unit) ⇒ random.nextDouble() should (be >= 0.0 and be < 1.0) } } "shuffle" in { val random = XorShiftRandom() val array = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9") val array2 = java.util.Arrays.copyOf(array, array.length) random.shuffle_!(array2) array2 should not equal array // will fail once every approx. 10! = 3.628.800 test runs array2.sorted shouldEqual array } } }
Example 153
Source File: NumericTaggingSuite.scala From protoless with Apache License 2.0 | 5 votes |
package io.protoless.tag import org.scalatest.FreeSpec import shapeless.test.illTyped class NumericTaggingSuite extends FreeSpec { "Numeric tagging must be allowed on" - { "Int" in { signed(1) unsigned(1) fixed(1) signedFixed(1) } "Long" in { signed(1L) unsigned(1L) fixed(1L) signedFixed(1L) } "no other numeric type" in { illTyped("""signed(1f)""") illTyped("""signed(1d)""") illTyped("""signed(1.toShort)""") illTyped("""signed(BigDecimal(1))""") } "no other exotic type" in { illTyped("""signed("1")""") illTyped("""signed(Seq(1, 2, 3))""") illTyped("""signed(true)""") } } }
Example 154
Source File: IPythonFormatSpec.scala From polynote with Apache License 2.0 | 5 votes |
package polynote.server.repository.format.ipynb import io.circe.parser.parse import org.scalatest.{FreeSpec, Matchers} import polynote.messages.{NotebookCell, CellID} import polynote.server.repository.NotebookContent import polynote.testing.ZIOSpec class IPythonFormatSpec extends FreeSpec with Matchers with ZIOSpec { private val subject = new IPythonFormat "IPythonFormat" - { "applies notebook language when no cell language is present" in { val nb = subject.decodeNotebook("dummy", """{ | "metadata": { | "language_info": { | "name": "python" | } | }, | "nbformat": 4, | "nbformat_minor": 0, | "cells": [ | { | "cell_type": "code", | "execution_count": 0, | "source": [ | "some python code" | ], | "outputs": [] | }, | { | "cell_type": "code", | "execution_count": 1, | "metadata": { | "language": "scala" | }, | "source": [ | "some scala code" | ], | "outputs": [] | } | ] |}""".stripMargin).runIO() nb.cells.head.language shouldEqual "python" nb.cells(1).language shouldEqual "scala" } "Saves language_info with most-used language from notebook" in { val encoded = subject.encodeNotebook(NotebookContent( List( NotebookCell(CellID(0), "aaa", "aaa"), NotebookCell(CellID(1), "ccc", "ccc"), NotebookCell(CellID(2), "bbb", "bbb"), NotebookCell(CellID(3), "aaa", "aaa")), None )).runIO() val langInfoName = parse(encoded).right.get.hcursor .downField("metadata") .downField("language_info") .downField("name") .as[String] langInfoName shouldEqual Right("aaa") } } }
Example 155
Source File: ServerHostTest.scala From polynote with Apache License 2.0 | 5 votes |
package polynote.server import java.net.{HttpURLConnection, InetAddress, InetSocketAddress, URL} import org.scalamock.scalatest.MockFactory import org.scalatest.{FreeSpec, Matchers} import polynote.app.{App, Args, Environment, MainArgs} import polynote.config._ import polynote.kernel.{BaseEnv, Kernel} import polynote.kernel.environment.Config import polynote.kernel.environment.Env.LayerOps import polynote.kernel.interpreter.Interpreter import polynote.kernel.logging.Logging import polynote.server.auth.IdentityProvider import polynote.server.repository.NotebookRepository import polynote.server.repository.fs.FileSystems import polynote.testing.{ConfiguredZIOSpec, ZIOSpec} import zio.{RIO, Task, ZIO, ZLayer} import zio.blocking.effectBlocking class ServerHostTest extends FreeSpec with Matchers with ConfiguredZIOSpec with MockFactory { override val config: PolynoteConfig = PolynoteConfig( listen = Listen(host = "0.0.0.0", port = 0) ) val configLayer: ZLayer[BaseEnv, Nothing, Config] = ZLayer.succeed(config) private def request(uri: String) = effectBlocking { val conn = new URL(uri).openConnection().asInstanceOf[HttpURLConnection] conn.setConnectTimeout(500) conn.connect() val responseCode = conn.getResponseCode responseCode shouldEqual 200 } "Server" - { "listens on all interfaces when given listen=0.0.0.0" ignore { val kernel = mock[Kernel] val kernelFactory = Kernel.Factory.const(kernel) val server = new Server val serverEnv: ZLayer[BaseEnv, Throwable, server.MainEnv with MainArgs] = (configLayer andThen IdentityProvider.layer) ++ Interpreter.Factories.load ++ ZLayer.succeed(kernelFactory) ++ ZLayer.succeed(Args(watchUI = true)) ++ (configLayer ++ FileSystems.live >>> NotebookRepository.live) val run = server.server("TESTKEY").provideSomeLayer[BaseEnv](serverEnv).use { server => for { localAddress <- effectBlocking(InetAddress.getLocalHost.getCanonicalHostName) _ <- server.awaitUp port <- server.localAddress.map(_.asInstanceOf[InetSocketAddress].getPort) _ <- request(s"http://$localAddress:$port/") _ <- request(s"http://127.0.0.1:$port/") _ <- server.shutdown() } yield () } run.runIO() } } }
Example 156
Source File: LocationTest.scala From polynote with Apache License 2.0 | 5 votes |
package polynote.env.ops import org.scalatest.{FreeSpec, Matchers} object TestInATopLevelModule { val loc: Location = Location.materialize } class LocationTest extends FreeSpec with Matchers { private val fileName = "LocationTest.scala" private val className = "polynote.env.ops.LocationTest" private def at(line: Int, method: String): Location = Location(fileName, line, method, className) def testInAMethod(): Location = { val loc = Location.materialize loc } def testAsAnImplicit(implicit loc: Location): Location = loc class TestInAnInnerClass { val topLoc: Location = Location.materialize def method(): Location = { val loc = Location.materialize loc } } object TestInAnInnerModule { val topLoc: Location = Location.materialize def method(): Location = { val loc = Location.materialize loc } } "in a block" in { val loc = Location.materialize loc shouldEqual at(40, "<unknown>") } "in a method" - { "inside the method" in { val loc = testInAMethod() loc shouldEqual at(15, "testInAMethod") } "as an implicit argument" in { val loc = testAsAnImplicit loc shouldEqual at(51, "<unknown>") } } "in an inner class" - { val inst = new TestInAnInnerClass "top level" in { inst.topLoc shouldEqual Location(fileName, 22, "<unknown>", s"$className.TestInAnInnerClass") } "method" in { inst.method() shouldEqual Location(fileName, 25, "method", s"$className.TestInAnInnerClass") } } "in a top-level module" in { TestInATopLevelModule.loc shouldEqual Location(fileName, 6, "<unknown>", "polynote.env.ops.TestInATopLevelModule") } "in an inner module" - { "top level" in { TestInAnInnerModule.topLoc shouldEqual Location(fileName, 31, "<unknown>", s"$className.TestInAnInnerModule") } "method" in { TestInAnInnerModule.method() shouldEqual Location(fileName, 34, "method", s"$className.TestInAnInnerModule") } } }
Example 157
Source File: CollectionReprsSpec.scala From polynote with Apache License 2.0 | 5 votes |
package polynote.runtime.test import java.nio.ByteBuffer import java.nio.charset.StandardCharsets import org.scalatest.{FreeSpec, Matchers} import polynote.runtime.{DataEncoder, GroupAgg, ReprsOf, StreamingDataRepr} class CollectionReprsSpec extends FreeSpec with Matchers { "Streaming repr of structs" - { case class Example(label: String, i: Int, d: Double) "Aggregates correctly" - { "mean" in { val l = List(Example("a", 10, 10.0), Example("b", 11, 11.0), Example("c", 12, 12.0), Example("a", 12, 12.0)) val de = implicitly[DataEncoder.StructDataEncoder[Example]] val h = ReprsOf.StructSeqStreamHandle[Example, Example](0, l, l => l, de) val Right(h1) = h.modify(List(GroupAgg(List("label"), List("i" -> "mean", "d" -> "mean")))).right.map(_.apply(1)) def decode(buf: ByteBuffer) = { buf.rewind() val labelLength = buf.getInt() val labelArr = new Array[Byte](labelLength) buf.get(labelArr) val label = new String(labelArr, StandardCharsets.UTF_8) val avgI = buf.getDouble() val avgD = buf.getDouble() (label, avgI, avgD) } h1.iterator.map(decode).toList should contain theSameElementsAs List( ("a", 11.0, 11.0), ("b", 11.0, 11.0), ("c", 12.0, 12.0) ) } } } }
Example 158
Source File: DataReprsTest.scala From polynote with Apache License 2.0 | 5 votes |
package polynote.runtime package test import org.scalacheck.{Arbitrary, Gen} import Arbitrary.arbitrary import org.scalatest.{FreeSpec, Matchers} import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks import scala.reflect.runtime.universe.TypeTag class DataReprsTest extends FreeSpec with Matchers with ScalaCheckDrivenPropertyChecks { implicit val arbString: Arbitrary[String] = Arbitrary(Gen.listOf(arbitrary[Char]).map(_.mkString)) def test[T : Arbitrary](implicit typeTag: TypeTag[T], reprsOf: ReprsOf.DataReprsOf[T]): Unit = { s"${typeTag}" in { forAll { (value: T) => reprsOf.encode(value) } } } "DataReprsOf checks" - { "Maps" - { test[Map[String, String]] test[Map[String, Int]] test[Map[String, Map[String, Int]]] test[List[Map[Int, String]]] } "Sequences" - { test[List[String]] test[List[Int]] } "Structs" - { test[(String, Int, Map[Int, String])] test[List[(Int, Boolean, String, String, Option[Int], Option[String], List[String], Map[String, Int])]] } } }
Example 159
Source File: SchemaLoaderSpec.scala From troy with Apache License 2.0 | 5 votes |
package troy package schema import org.scalatest.FreeSpec import troy.schema.Messages._ class SchemaLoaderSpec extends FreeSpec with VMatchers { import VTestUtils._ "String Schema Loader should" - { "load schema from a valid string" in { new StringSchemaLoader(""" CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy' , 'replication_factor': '1'}; CREATE TABLE test.posts (author_id uuid, post_id uuid, PRIMARY KEY ((author_id), post_id) ); """).load should beSuccess } "fail on non-valid schema string" in { new StringSchemaLoader(""" CREATE TABLE test.posts (author_id uuid, PRIMARY KEY ((author_id), post_id) ); """).load should failWith[KeyspaceNotFound] } } "Resource File Schema Loader should" - { "load valid path" in { new ResourceFileSchemaLoader("/test.cql").load should beSuccess } "fail with SchemaNotFound on wrong path" in { new ResourceFileSchemaLoader("/non_existent.cql").load should failWith[SchemaNotFound] } } "Resource Folder Schema Loader should" - { "load valid path" in { new ResourceFolderSchemaLoader("/test/").load should beSuccess } "fail with SchemaNotFound on wrong path" in { new ResourceFolderSchemaLoader("/non_existent").load should failWith[SchemaNotFound] } } "Resource File or Folder Schema Loader should" - { "load file if existing, ignoring the folder path" in { new ResourceFileOrFolderSchemaLoader("/test.cql", "/non_existent").load should beSuccess } "load folder, if file didn't exist" in { new ResourceFileOrFolderSchemaLoader("/non_existent.cql", "/test/").load should beSuccess } "fail if both didn't exist" in { new ResourceFileOrFolderSchemaLoader("/non_existent.cql", "/non_existent").load should failWith[SchemaNotFound] } } }
Example 160
Source File: ContextVersionTest.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.lang import com.wavesplatform.lang.directives.values._ import com.wavesplatform.lang.v1.evaluator.ctx.impl.waves.Types import org.scalatest.{FreeSpec, Matchers} class ContextVersionTest extends FreeSpec with Matchers { "InvokeScriptTransaction" - { "exist in lib version 3" in { val types = Types.buildWavesTypes(true, V3) types.count(c => c.name == "InvokeScriptTransaction") shouldEqual 1 } "doesn't exist in lib version 2" in { val types = Types.buildWavesTypes(true, V2) types.count(c => c.name == "InvokeScriptTransaction") shouldEqual 0 } } }
Example 161
Source File: UTXAllowance.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync.smartcontract import com.typesafe.config.{Config, ConfigFactory} import com.wavesplatform.account.KeyPair import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.it.api.SyncHttpApi._ import com.wavesplatform.it.sync._ import com.wavesplatform.it.transactions.NodesFromDocker import com.wavesplatform.it.util._ import com.wavesplatform.it.{ReportingTestName, WaitForHeight2} import com.wavesplatform.lang.v1.estimator.v2.ScriptEstimatorV2 import com.wavesplatform.transaction.smart.script.ScriptCompiler import org.scalatest.{CancelAfterFailure, FreeSpec, Matchers} class UTXAllowance extends FreeSpec with Matchers with WaitForHeight2 with CancelAfterFailure with ReportingTestName with NodesFromDocker { import UTXAllowance._ override protected def nodeConfigs: Seq[Config] = Configs private def nodeA = nodes.head private def nodeB = nodes.last "create two nodes with scripted accounts and check UTX" in { val accounts = List(nodeA, nodeB).map(i => { val nodeAddress = i.createAddress() val acc = KeyPair.fromSeed(i.seed(nodeAddress)).explicitGet() i.transfer(i.address, nodeAddress, 10.waves, 0.005.waves, None, waitForTx = true) val scriptText = s"""true""".stripMargin val script = ScriptCompiler(scriptText, isAssetScript = false, ScriptEstimatorV2).explicitGet()._1.bytes().base64 i.setScript(acc.toAddress.toString, Some(script), setScriptFee, waitForTx = true) acc }) assertBadRequestAndMessage( nodeA .transfer( accounts.head.toAddress.toString, recipient = accounts.head.toAddress.toString, assetId = None, amount = 1.waves, fee = minFee + 0.004.waves, version = 2 ), "transactions from scripted accounts are denied from UTX pool" ) val txBId = nodeB .transfer( accounts(1).toAddress.toString, recipient = accounts(1).toAddress.toString, assetId = None, amount = 1.01.waves, fee = minFee + 0.004.waves, version = 2 ) .id nodes.waitForHeightArise() nodeA.findTransactionInfo(txBId) shouldBe None } } object UTXAllowance { import com.wavesplatform.it.NodeConfigs._ private val FirstNode = ConfigFactory.parseString(s""" |waves { | utx.allow-transactions-from-smart-accounts = false | miner { | quorum = 0 | enable = yes | } |}""".stripMargin) private val SecondNode = ConfigFactory.parseString(s""" |waves { | utx.allow-transactions-from-smart-accounts = true | miner { | enable = no | } |}""".stripMargin) val Configs: Seq[Config] = Seq( FirstNode.withFallback(Default.head), SecondNode.withFallback(Default(1)) ) }
Example 162
Source File: NetworkUniqueConnectionsTestSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync.network import com.typesafe.config.{Config, ConfigFactory} import com.wavesplatform.it.api.SyncHttpApi._ import com.wavesplatform.it.{DockerBased, Node, NodeConfigs, Nodes} import com.wavesplatform.utils.ScorexLogging import org.scalatest.{FreeSpec, Matchers} import scala.concurrent._ import scala.concurrent.duration._ import scala.util.{Failure, Success, Try} class NetworkUniqueConnectionsTestSuite extends FreeSpec with Matchers with DockerBased with ScorexLogging with Nodes { import NetworkUniqueConnectionsTestSuite._ "nodes should up and connect with each other" in { val firstNode = docker.startNode(FirstNodeConfig, autoConnect = false) nodes = Seq(firstNode) val status = firstNode.status() log.trace(s"#### $status") assert(status.blockchainHeight >= status.stateHeight) val secondNode = { // Helps to do an incoming connection: second -> first (1) val peersConfig = ConfigFactory.parseString( s"""waves.network.known-peers = [ | "${firstNode.containerNetworkAddress.getHostName}:${firstNode.containerNetworkAddress.getPort}" |]""".stripMargin ) docker.startNode(peersConfig.withFallback(SecondNodeConfig), autoConnect = false) } nodes = Seq(firstNode, secondNode) // Thread dump workaround firstNode.waitForPeers(1) // Outgoing connection: first -> second (2) firstNode.connect(secondNode.containerNetworkAddress) withClue("Should fail with TimeoutException, because the connectionAttempt should fail") { Try(firstNode.waitForPeers(2, 30.seconds)) match { case Failure(ApiCallException(_: TimeoutException)) => // Pass case Failure(exception) => fail(exception) case Success(v) => fail(s"Expected TimeoutException, got $v") } } } protected var nodes: Seq[Node] = Nil protected def nodeConfigs: Seq[Config] = NetworkUniqueConnectionsTestSuite.configs } object NetworkUniqueConnectionsTestSuite { private val configs = NodeConfigs.newBuilder.withDefault(0).withSpecial(2, _.nonMiner).build() val FirstNodeConfig: Config = configs.head val SecondNodeConfig: Config = configs.last }
Example 163
Source File: DetectBrokenConnectionsTestSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync.network import com.typesafe.config.{Config, ConfigFactory} import com.wavesplatform.it.NodeConfigs.Default import com.wavesplatform.it.ReportingTestName import com.wavesplatform.it.api.SyncHttpApi._ import com.wavesplatform.it.transactions.NodesFromDocker import org.scalatest.{FreeSpec, Matchers} import scala.concurrent.duration._ class DetectBrokenConnectionsTestSuite extends FreeSpec with Matchers with ReportingTestName with NodesFromDocker { override protected def nodeConfigs: Seq[Config] = { val highPriorityConfig = ConfigFactory.parseString("waves.network.break-idle-connections-timeout = 20s") Default.take(2).map(highPriorityConfig.withFallback) } "disconnect nodes from the network and wait a timeout for detecting of broken connections" in { dockerNodes().foreach(docker.disconnectFromNetwork) Thread.sleep(30.seconds.toMillis) dockerNodes().foreach { node => docker.connectToNetwork(Seq(node)) node.connectedPeers shouldBe empty docker.disconnectFromNetwork(node) } // To prevent errors in the log docker.connectToNetwork(dockerNodes()) } }
Example 164
Source File: NotActivateFeatureTestSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync.activation import com.typesafe.config.Config import com.wavesplatform.features.BlockchainFeatureStatus import com.wavesplatform.features.api.{FeatureActivationStatus, NodeFeatureStatus} import com.wavesplatform.it.api.SyncHttpApi._ import com.wavesplatform.it.api.BlockHeader import com.wavesplatform.it.transactions.NodesFromDocker import com.wavesplatform.it.{NodeConfigs, ReportingTestName} import org.scalatest.{CancelAfterFailure, FreeSpec, Matchers} class NotActivateFeatureTestSuite extends FreeSpec with Matchers with CancelAfterFailure with ActivationStatusRequest with ReportingTestName with NodesFromDocker { private val votingInterval = 14 private val blocksForActivation = 14 private val votingFeatureNum: Short = 1 private val nonVotingFeatureNum: Short = 2 override protected def nodeConfigs: Seq[Config] = NodeConfigs.newBuilder .overrideBase( _.raw( s"""waves { | blockchain { | custom { | functionality { | pre-activated-features = {} | feature-check-blocks-period = $votingInterval | blocks-for-feature-activation = $blocksForActivation | } | } | } | features.supported=[$nonVotingFeatureNum] | miner.quorum = 1 |}""".stripMargin )) .withDefault(2) .buildNonConflicting() private var activationStatusInfoBefore = Seq.empty[FeatureActivationStatus] private var activationStatusInfoAfter = Seq.empty[FeatureActivationStatus] "get activation status info" in { nodes.waitForHeight(votingInterval - 1) activationStatusInfoBefore = nodes.map(_.featureActivationStatus(votingFeatureNum)) nodes.waitForHeight(votingInterval + 1) activationStatusInfoAfter = nodes.map(_.featureActivationStatus(votingFeatureNum)) } "supported blocks is not increased when nobody votes for feature" in { val generatedBlocks: Seq[BlockHeader] = nodes.head.blockHeadersSeq(1, votingInterval - 1) val featuresMapInGeneratedBlocks = generatedBlocks.flatMap(b => b.features.getOrElse(Seq.empty)).groupBy(x => x) val votesForFeature1 = featuresMapInGeneratedBlocks.getOrElse(votingFeatureNum, Seq.empty).length votesForFeature1 shouldBe 0 activationStatusInfoBefore.foreach(assertVotingStatus(_, votesForFeature1, BlockchainFeatureStatus.Undefined, NodeFeatureStatus.Implemented)) } "feature is still in VOTING status on the next voting interval" in { activationStatusInfoAfter.foreach(assertVotingStatus(_, 0, BlockchainFeatureStatus.Undefined, NodeFeatureStatus.Implemented)) } }
Example 165
Source File: FeatureActivationTestSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync.activation import com.typesafe.config.Config import com.wavesplatform.features.api.NodeFeatureStatus import com.wavesplatform.features.{BlockchainFeatureStatus, BlockchainFeatures} import com.wavesplatform.it.transactions.NodesFromDocker import com.wavesplatform.it.{NodeConfigs, ReportingTestName} import org.scalatest.{CancelAfterFailure, FreeSpec, Matchers} import com.wavesplatform.it.api.SyncHttpApi._ class FeatureActivationTestSuite extends FreeSpec with Matchers with CancelAfterFailure with NodesFromDocker with ActivationStatusRequest with ReportingTestName { private val votingInterval = 12 private val blocksForActivation = 12 // should be even private val featureNum: Short = BlockchainFeatures.SmallerMinimalGeneratingBalance.id private val featureDescr = BlockchainFeatures.SmallerMinimalGeneratingBalance.description override protected def nodeConfigs: Seq[Config] = { NodeConfigs.newBuilder .overrideBase(_.raw(s"""waves { | blockchain.custom.functionality { | pre-activated-features = {} | feature-check-blocks-period = $votingInterval | blocks-for-feature-activation = $blocksForActivation | } | features.supported = [$featureNum] | miner.quorum = 1 |}""".stripMargin)) .withDefault(2) .buildNonConflicting() } "supported blocks increased when voting starts" in { nodes.waitForHeight(votingInterval * 2 / 3) val status = nodes.map(_.featureActivationStatus(featureNum)) status.foreach { s => s.description shouldBe featureDescr assertVotingStatus(s, s.supportingBlocks.get, BlockchainFeatureStatus.Undefined, NodeFeatureStatus.Voted) } } "supported blocks counter resets on the next voting interval" in { nodes.waitForHeight(votingInterval * 2 - blocksForActivation / 2) val info = nodes.map(_.featureActivationStatus(featureNum)) info.foreach(i => i.blockchainStatus shouldBe BlockchainFeatureStatus.Undefined) } "blockchain status is APPROVED in second voting interval" in { val checkHeight = votingInterval * 2 nodes.waitForHeight(checkHeight) val statusInfo = nodes.map(_.featureActivationStatus(featureNum)) statusInfo.foreach { si => si.description shouldBe featureDescr // Activation will be on a next voting interval assertApprovedStatus(si, checkHeight + votingInterval, NodeFeatureStatus.Voted) } } "blockchain status is ACTIVATED in third voting interval" in { val checkHeight = votingInterval * 3 nodes.waitForHeight(checkHeight) val statusInfo = nodes.map(_.featureActivationStatus(featureNum)) statusInfo.foreach { si => si.description shouldBe featureDescr assertActivatedStatus(si, checkHeight, NodeFeatureStatus.Implemented) } } }
Example 166
Source File: AssetsApiGrpcSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync.grpc import com.typesafe.config.Config import com.wavesplatform.it.sync._ import com.wavesplatform.it.sync.activation.ActivationStatusRequest import com.wavesplatform.it.transactions.NodesFromDocker import com.wavesplatform.it.{GrpcIntegrationSuiteWithThreeAddress, NodeConfigs, ReportingTestName} import org.scalatest.{FreeSpec, Matchers} class AssetsApiGrpcSuite extends FreeSpec with Matchers with NodesFromDocker with ActivationStatusRequest with ReportingTestName with GrpcIntegrationSuiteWithThreeAddress { "nftList returns all NFT" in { import com.wavesplatform.it.api.SyncGrpcApi._ val txs = Map( "non_nft_asset" -> sender.broadcastIssue(firstAcc, "non_nft_asset", 100, 8, reissuable = true, issueFee + smartFee), "nft_asset_1" -> sender.broadcastIssue(firstAcc, "nft_asset_1", 1, 0, reissuable = false, issueFee + smartFee), "nft_asset_2" -> sender.broadcastIssue(firstAcc, "nft_asset_2", 1, 0, reissuable = false, issueFee + smartFee) ) txs.values.foreach(tx => sender.waitForTransaction(tx.id)) val allNft = Map( "nft_asset_1" -> txs("nft_asset_1").id, "nft_asset_2" -> txs("nft_asset_2").id ) val nftList = sender.nftList(firstAddress, 10) nftList should have size 2 nftList.map(_.assetInfo.get.name) should contain theSameElementsAs allNft.keySet } override def nodeConfigs: Seq[Config] = NodeConfigs.newBuilder .overrideBase(_.quorum(0)) .withDefault(1) .buildNonConflicting() }
Example 167
Source File: BlockV5GrpcSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync.grpc import com.google.protobuf.ByteString import com.typesafe.config.Config import com.wavesplatform.api.grpc.BlockRangeRequest import com.wavesplatform.block.Block import com.wavesplatform.common.state.ByteStr import com.wavesplatform.crypto import com.wavesplatform.it.api.SyncGrpcApi._ import com.wavesplatform.it.sync.activation.ActivationStatusRequest import com.wavesplatform.it.transactions.NodesFromDocker import com.wavesplatform.it.{GrpcIntegrationSuiteWithThreeAddress, NodeConfigs, ReportingTestName} import org.scalatest.{CancelAfterFailure, FreeSpec, Matchers, OptionValues} import scala.concurrent.duration._ class BlockV5GrpcSuite extends FreeSpec with Matchers with CancelAfterFailure with NodesFromDocker with ActivationStatusRequest with ReportingTestName with OptionValues with GrpcIntegrationSuiteWithThreeAddress { override def nodeConfigs: Seq[Config] = NodeConfigs.newBuilder .overrideBase(_.quorum(0)) .withDefault(1) .withSpecial(1, _.nonMiner) .buildNonConflicting() "block v5 appears and blockchain grows" - { "when feature activation happened" in { sender.waitForHeight(sender.height + 1, 2.minutes) val currentHeight = sender.height val blockV5 = sender.blockAt(currentHeight) val blockV5ById = sender.blockById(ByteString.copyFrom(blockV5.id().arr)) blockV5.header.version shouldBe Block.ProtoBlockVersion blockV5.id().arr.length shouldBe crypto.DigestLength blockV5.signature.arr.length shouldBe crypto.SignatureLength blockV5.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5.transactionsRootValid(), "transactionsRoot is not valid") blockV5ById.header.version shouldBe Block.ProtoBlockVersion blockV5ById.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5ById.transactionsRootValid(), "transactionsRoot is not valid") sender.waitForHeight(currentHeight + 1, 2.minutes) val blockAfterVRFUsing = sender.blockAt(currentHeight + 1) val blockAfterVRFUsingById = sender.blockById(ByteString.copyFrom(blockAfterVRFUsing.id().arr)) blockAfterVRFUsing.header.version shouldBe Block.ProtoBlockVersion blockAfterVRFUsing.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength ByteStr(sender.blockHeaderAt(currentHeight + 1).reference.toByteArray) shouldBe blockV5.id() blockAfterVRFUsingById.header.version shouldBe Block.ProtoBlockVersion blockAfterVRFUsingById.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockAfterVRFUsingById.transactionsRootValid(), "transactionsRoot is not valid") val blockSeqOfBlocksV5 = sender.blockSeq(currentHeight, currentHeight + 2) for (blockV5 <- blockSeqOfBlocksV5) { blockV5.header.version shouldBe Block.ProtoBlockVersion blockV5.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5.transactionsRootValid(), "transactionsRoot is not valid") } val blockSeqOfBlocksV5ByAddress = sender.blockSeqByAddress(miner.address, currentHeight, currentHeight + 2) for (blockV5 <- blockSeqOfBlocksV5ByAddress) { blockV5.header.generator shouldBe miner.keyPair.publicKey blockV5.header.version shouldBe Block.ProtoBlockVersion blockV5.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5.transactionsRootValid(), "transactionsRoot is not valid") } val blockSeqOfBlocksV5ByPKGrpc = NodeExtGrpc(sender).blockSeq( currentHeight, currentHeight + 2, BlockRangeRequest.Filter.GeneratorPublicKey(ByteString.copyFrom(miner.keyPair.publicKey.arr)) ) for (blockV5 <- blockSeqOfBlocksV5ByPKGrpc) { blockV5.header.generator shouldBe miner.keyPair.publicKey blockV5.header.version shouldBe Block.ProtoBlockVersion blockV5.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5.transactionsRootValid(), "transactionsRoot is not valid") } } } }
Example 168
Source File: MicroblocksSponsoredFeeTestSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync import com.typesafe.config.Config import com.wavesplatform.it.NodeConfigs import com.wavesplatform.it.api.SyncHttpApi._ import com.wavesplatform.it.transactions.NodesFromDocker import com.wavesplatform.state.Sponsorship import com.wavesplatform.state.diffs.FeeValidation import com.wavesplatform.utils.ScorexLogging import org.scalatest.{CancelAfterFailure, FreeSpec, Matchers} class MicroblocksSponsoredFeeTestSuite extends FreeSpec with Matchers with CancelAfterFailure with NodesFromDocker with ScorexLogging { private def notMiner = nodes.head val sponsor = nodes(1) val Token = 100L val sponsorAssetTotal = 100000 * Token val minSponsorFee = Token val SmallFee = Token + Token / 2 private def secondAddress = nodes(2).address private def txRequestsGen(n: Int, sponsorAssetId: String): Unit = { 1 to n map (_ => { sponsor.transfer(sponsor.address, secondAddress, Token, fee = SmallFee, None, Some(sponsorAssetId)) }) } "fee distribution with sponsorship" - { val sponsorAssetId = sponsor .issue(sponsor.address, "SponsoredAsset", "Created by Sponsorship Suite", sponsorAssetTotal, decimals = 2, reissuable = false, fee = issueFee) .id nodes.waitForHeightAriseAndTxPresent(sponsorAssetId) val transferTxToSecondAddress = sponsor.transfer(sponsor.address, secondAddress, sponsorAssetTotal / 2, minFee, Some(sponsorAssetId), None).id nodes.waitForHeightAriseAndTxPresent(transferTxToSecondAddress) val sponsorId = sponsor.sponsorAsset(sponsor.address, sponsorAssetId, baseFee = Token, fee = sponsorReducedFee).id nodes.waitForHeightAriseAndTxPresent(sponsorId) "check fee distribution" in { val height = nodes.waitForHeightArise() txRequestsGen(50, sponsorAssetId) nodes.waitForHeight(height + 2) val blockHeadersSeq = notMiner.blockHeadersSeq(height - 1, height + 2) val filteredBlocks = blockHeadersSeq .zip(blockHeadersSeq.drop(1)) .withFilter(t => t._1.transactionCount != t._2.transactionCount) .map(_._1) :+ blockHeadersSeq.last val filteredBlocksFee = filteredBlocks.map(b => b.transactionCount * FeeValidation.FeeUnit * SmallFee / minSponsorFee) val minerBalances: Seq[Long] = filteredBlocks.map(b => notMiner.debugStateAt(b.height)(b.generator)) minerBalances.zip(filteredBlocksFee).sliding(2).foreach { case Seq((minerBalance1, blockFee1), (minerBalance2, blockFee2)) => minerBalance2 should be(minerBalance1 + blockFee1 * 6 / 10 + blockFee2 * 4 / 10) } val block = notMiner.blockAt(height) val realFee = block.transactions.map(tx => Sponsorship.toWaves(tx.fee, Token)).sum blockHeadersSeq(1).totalFee shouldBe realFee } } override def nodeConfigs: Seq[Config] = NodeConfigs.newBuilder .overrideBase(_.quorum(0)) .overrideBase(_.raw("waves.blockchain.custom.functionality.blocks-for-feature-activation=1")) .overrideBase(_.raw("waves.blockchain.custom.functionality.feature-check-blocks-period=1")) .overrideBase(_.preactivatedFeatures((14, 1000000))) .withDefault(1) .withSpecial(2, _.nonMiner) .buildNonConflicting() }
Example 169
Source File: OrderProofTest.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.transaction.smart import com.wavesplatform.common.state.ByteStr import com.wavesplatform.lang.directives.values.{V1, V2, V3} import com.wavesplatform.lang.v1.compiler.Terms.{ARR, CaseObj} import com.wavesplatform.lang.v1.evaluator.ctx.impl.waves.Bindings import com.wavesplatform.lang.v1.traits.domain.{APair, Ord, OrdType, Recipient} import org.scalatest.{FreeSpec, Matchers} class OrderProofTest extends FreeSpec with Matchers { private def getBindingObjProofStr(obj: CaseObj): Option[String] = obj.fields.get("proofs").map(arr => arr.asInstanceOf[ARR].xs.toList.head.toString) "OrderBindings" - { "order obj for account scripts and V1/V2 should contains proofs" in { val ord = Ord( id = ByteStr.empty, sender = Recipient.Address(ByteStr.empty), senderPublicKey = ByteStr.empty, matcherPublicKey = ByteStr.empty, assetPair = APair(None, None), orderType = OrdType.Buy, price = 0L, amount = 0L, timestamp = 0L, expiration = 0L, matcherFee = 0L, bodyBytes = ByteStr.empty, proofs = List(ByteStr.fromLong(42L)).toIndexedSeq ) getBindingObjProofStr(Bindings.orderObject(ord, true, V1)) shouldBe Some("1111111j") getBindingObjProofStr(Bindings.orderObject(ord, true, V2)) shouldBe Some("1111111j") getBindingObjProofStr(Bindings.orderObject(ord, true, V3)) shouldBe Some("1111111j") getBindingObjProofStr(Bindings.orderObject(ord, false, V1)) shouldBe Some("1111111j") getBindingObjProofStr(Bindings.orderObject(ord, false, V2)) shouldBe Some("1111111j") } "order obj for asset scripts and V3 should not contains proofs" in { val ord = Ord( id = ByteStr.empty, sender = Recipient.Address(ByteStr.empty), senderPublicKey = ByteStr.empty, matcherPublicKey = ByteStr.empty, assetPair = APair(None, None), orderType = OrdType.Buy, price = 0L, amount = 0L, timestamp = 0L, expiration = 0L, matcherFee = 0L, bodyBytes = ByteStr.empty, proofs = List(ByteStr.fromLong(42L)).toIndexedSeq ) getBindingObjProofStr(Bindings.orderObject(ord, false, V3)) shouldBe None } } }
Example 170
Source File: ResponseFormatsTest.scala From daml with Apache License 2.0 | 4 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.http.json import com.daml.http.util.Collections._ import akka.actor.ActorSystem import akka.stream.Materializer import akka.stream.scaladsl.Source import akka.util.ByteString import org.scalacheck.Gen import org.scalatest.compatible.Assertion import org.scalatest.prop.GeneratorDrivenPropertyChecks import org.scalatest.{FreeSpec, Inside, Matchers} import scalaz.syntax.show._ import scalaz.{Show, \/} import spray.json._ import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContext, Future} class ResponseFormatsTest extends FreeSpec with Matchers with Inside with GeneratorDrivenPropertyChecks { implicit val asys: ActorSystem = ActorSystem(this.getClass.getSimpleName) implicit val mat: Materializer = Materializer(asys) implicit val ec: ExecutionContext = asys.dispatcher implicit override val generatorDrivenConfig: PropertyCheckConfiguration = PropertyCheckConfiguration(minSuccessful = 100) "resultJsObject should serialize Source of Errors and JsValues" in forAll( Gen.listOf(errorOrJsNumber), Gen.option(Gen.nonEmptyListOf(Gen.identifier))) { (input, warnings) => import spray.json.DefaultJsonProtocol._ val jsValWarnings: Option[JsValue] = warnings.map(_.toJson) val (failures, successes): (Vector[JsString], Vector[JsValue]) = input.toVector.partitionMap(_.leftMap(e => JsString(e.shows))) val jsValSource = Source[DummyError \/ JsValue](input) val responseF: Future[ByteString] = ResponseFormats .resultJsObject(jsValSource, jsValWarnings) .runFold(ByteString.empty)((b, a) => b ++ a) val resultF: Future[Assertion] = responseF.map { str => JsonParser(str.utf8String) shouldBe expectedResult(failures, successes, jsValWarnings) } Await.result(resultF, 10.seconds) } private def expectedResult( failures: Vector[JsValue], successes: Vector[JsValue], warnings: Option[JsValue]): JsObject = { val map1: Map[String, JsValue] = warnings match { case Some(x) => Map("warnings" -> x) case None => Map.empty } val map2 = if (failures.isEmpty) Map[String, JsValue]("result" -> JsArray(successes), "status" -> JsNumber("200")) else Map[String, JsValue]( "result" -> JsArray(successes), "errors" -> JsArray(failures), "status" -> JsNumber("501")) JsObject(map1 ++ map2) } private lazy val errorOrJsNumber: Gen[DummyError \/ JsNumber] = Gen.frequency( 1 -> dummyErrorGen.map(\/.left), 5 -> jsNumberGen.map(\/.right) ) private lazy val dummyErrorGen: Gen[DummyError] = Gen.identifier.map(DummyError.apply) private lazy val jsNumberGen: Gen[JsNumber] = Gen.posNum[Long].map(JsNumber.apply) } final case class DummyError(message: String) object DummyError { implicit val ShowInstance: Show[DummyError] = Show shows { e => s"DummyError(${e.message})" } }
Example 171
Source File: ConsumerEventSpec.scala From reactive-kinesis with Apache License 2.0 | 4 votes |
package com.weightwatchers.reactive.kinesis.models import java.nio.ByteBuffer import java.nio.charset.StandardCharsets.UTF_8 import org.joda.time.DateTime import org.scalacheck.Gen import org.scalatest.prop.GeneratorDrivenPropertyChecks import org.scalatest.{FreeSpec, Matchers} class ConsumerEventSpec extends FreeSpec with Matchers with GeneratorDrivenPropertyChecks { "A ConsumerEvent" - { "can be read as string" in { forAll(Gen.alphaNumStr) { string => val event = ConsumerEvent(CompoundSequenceNumber("123", 0), ByteBuffer.wrap(string.getBytes(UTF_8)), DateTime.now()) event.payloadAsString(UTF_8) shouldBe string } } } }