Everything is in the title : How do I do for import a trait in a worksheet for testing it? I have a function in that trait that I want to test..
Just import it like you always would:
import com.mytrait.MyTrait
Related
Let me refresh already asked question, as the answer there is not clear for newbie.
I'm trying to start with Play, Slick 3.2.3 and unsupported database (RDB to be precise). I began from play-scala-isolated-slick-example taken from Play site. RDB database is not supported by Slick, so I tried to use generic Jdbc profile (fit-all as I think):
package test.mydb.slick
import javax.inject.{Inject, Singleton}
import slick.driver.JdbcProfile
import slick.jdbc.JdbcBackend.Database
import test.mydb.{MyTblDAO, Tbl} // case class defined there
import scala.concurrent.{ExecutionContext, Future}
import scala.language.implicitConversions
import scala.reflect.ClassTag
#Singleton
class SlickMyTblDAO #Inject()(db: Database)(implicit ec: ExecutionContext)
extends MyTblDAO with test.mydb.slick.Tables {
// override val profile: JdbcProfile = _root_.slick.jdbc.JdbcProfile
override val profile: JdbcProfile = slick.driver.JdbcProfile
import profile.api._
def lookup(id: String): Future[Option[MyTbl]] = {.... and so on
This code is not compiled because of:
type mismatch;
[error] found : slick.driver.JdbcProfile.type
[error] required: slick.driver.JdbcProfile
[error] (which expands to) slick.jdbc.JdbcProfile
[error] override val profile: JdbcProfile = slick.driver.JdbcProfile
[error] ^
Not sure I fully understand the root of the problem, but I guess one can't use Jdbc profile directly. The answer says that "other databases can be supported with a custom implementation of the trait slick.jdbc.JdbcProfile". Does it mean that I need to implement profile myself? Is it achievable for starter? I need just a simple DML, no DDL, no joins for start.
The error message is telling you that profile needs to extend the trait JdbcProfile, but you're passing it the companion object JdbcProfile, which does not extend the trait of the same name.
To answer your other question - yes, I'm afraid you would have to implement JdbcProfile yourself, and I believe that could be quite a mouthful for a newbie, because Slick's API is quite advanced.
I'm pretty new to Scala and now I'm trying to learn how to write tests in Scalatest. Here is an example they provided with the using custom matchers section:
import org.scalatest._
import matchers._
trait CustomMatchers {
class FileEndsWithExtensionMatcher(expectedExtension: String) extends Matcher[java.io.File] {
def apply(left: java.io.File) = {
val name = left.getName
MatchResult(
name.endsWith(expectedExtension),
s"""File $name did not end with extension "$expectedExtension"""",
s"""File $name ended with extension "$expectedExtension"""""
)
}
}
def endWithExtension(expecedExtension: String) = new FileEndsWithExtensionMatcher(expectedExtension)
}
// Make them easy to import with:
// import CustomMatchers._
object CustomMatchers extends CustomMatchers
I do not quite understand the reason they pu the FileEndsWithExtensionMatcher class into a trait? Why? Is it idiomatic scala way or some? Can't you explain it?
It is just for your convienience.
That is one of many ways to modularize your code and than reuse it in desired suites. You can organize it in packages or objects and it will work either. With this approach it is just easy to mix traits into your tests. What's more it allows you to nicely organize connected matchers.
For example you can create trait with group of matchers for currencies and another trait matching physical units. Next you can mixin one or both traits into your Test.
The purpose here is to "put all the matchers into one place from which you can easily access them in other code". You could put all these traits into a package and import that package. But the link you reference states that "One good way to organize custom matchers is to place them inside one or more traits that you can then mix into the suites that need them". It would work either way. It's just a matter of preference.
I'm a new in Scala. I created a package object in my code:
package mypackage.spark
import scala.language.implicitConversions
import org.apache.spark.SparkContext
import mypackage.spark.SparkContextFunctions
package object spark {
implicit def toSparkContextFunctions(sc: SparkContext): SparkContextFunctions =
new SparkContextFunctions(sc)
}
I expect that when I use import mypackage.spark._, I will able to use methods from SparkContextFunctions class. This approach works for me, when I use only only one imported package object. But when I add additional import in my code. For example:
import mypackage.spark._
import com.datastax.spark.connector._
com.datastax.spark.connector._ doing the same for org.apache.spark.SparkContext class. My code stop compile and I have an error that used method is not a member of SparkContext class. When I change the order of imports the compiler starts see methods from mypackage.spark._ and stops see methods from com.datastax.spark.connector._
Maybe I missed something? Or Scala doesn't support this?
Thanks.
If you need to use two classes named SparkContext at the same time, you can alias them:
import my.package.name.{SparkContext => MySparkContext}
import some.other.package.name.{SparkContext => OtherSparkContext}
Classes from the same package you can be aliased in the same import:
import my.package.name.{SparkContext => MySparkContext, SomethingElse => MySomethingElse}
You may want to choose better names than MyXXX and OtherXXX.
The imports may conflict in two ways: either both use toSparkContextFunctions for the implicit conversion name or both provide extension methods with the same name (even if different signature).
If neither is the case, there should be no problem. If one is, change your method names, since you can't change the ones in com.datastax.spark.connector.
I want to use an immutable Queue in Scala, like this:
var a:Queue[Int] = Queue.empty[Int]
However, I get the following error:
error: not found: type Queue
I tried importing the library containing it but there was no effect:
import scala.collection.immutable
Pretty sure you need to add ._ to your import, like so:
import scala.collection.immutable._
Or import Queue specifically as:
import scala.collection.immutable.Queue
Say I have:
import org.scalatest.ShouldMatchers._;
This brings a few implicit conversions into scope.
How can I alias them in a package object so that I can bring the implicits into scope with:
import code.ThePackageObject._;
Apparently the ShouldMatchers object extends the ShouldMatchers trait (where the actual definition of the implicits are done). This is a common idiom that allows to simply mix the trait where you need it. So you can simply mix ShouldMatchers (the trait) in your package object:
package object ThePackageObject extends ShouldMatchers