How to unit test class with no logic? - unit-testing

I find it easy to write unit tests for algorithms. For example, sort(List), it is easy to write tests like:
list = [2, 1, 4];
assert(sort(list) == [1, 2, 4]);
But I find it really hard to test methods that have no logic, no if statements, just a set of calls.
There are mainly 2 examples that I'd like an answer for how to unit test them:
Example 1:
Let's say I have a class that is responsible for writing some data to a file, but that data is written in a specific way by external functions (writeHeaderToFile, writeSerializedData and writeEndOfFile).
The data is not written straight as it is to the file, so if data is something like:
{
list: [
"item 1",
"item 2"
],
name: "aaa"
}
That doesn't mean that the file will be neither the plain version of that data (without white spaces) nor it will be a simple serialized version or encrypted version into file. The actual file binary will be something unknown to me. All I know is that I can use those 3 methods to write in the right way.
This file also contains some other information that doesn't come directly from those 3 methods, like a specific type of header (that again, I have no idea how it will be represented in the file binary).
That is the class:
class FileCreator {
populateFileWithData(File file, Data data) {
doBlockWithLock(file, {
Header header;
header.format = SomeFormat;
header.version = SomeVersion;
writeHeaderToFile(file, header);
writeSerializedData(file, data);
writeEndOfFile(file);
});
}
// Private
void doBlockWithLock(File file, Lambda block) {
file.holdWritingLock();
block();
file.releaseWritingLock();
}
}
Example 2:
class Controller {
var screenOne = new ScreenOne();
var screenTwo = new ScreenTwo();
var screenThree = new ScreenThree();
void reloadButtonWasClicked() {
screenOne.reload();
screenTwo.reload();
screenThree.reload();
}
}
For this one I could do something like this:
var mockScreenOne = Mock<ScreenOne>().reload.expectOneCall();
var mockScreenTwo = Mock<ScreenTwo>().reload.expectOneCall();
var mockScreenThree = Mock<ScreenThree>().reload.expectOneCall();
Controller controller = new Controller();
controller.screenOne = mockScreenOne;
controller.screenTwo = mockScreenTwo;
controller.screenThree = mockScreenThree;
controller.reloadButtonWasClicked();
mockScreenOne.verify();
mockScreenTwo.verify();
mockScreenThree.verify();
But I don't find much value in it since I'm just asserting that I'm doing the same thing I'm doing in the implementation. Seems like code repetition to me.
What would be the proper way of testing my 2 examples?

In the first example, if you wrote the messages in question and satisfied with your test coverage, there's no reason to reproduce that testing logic on FileCreator. You just need to test the FileCreator populateFileWithData method to make sure the file is written and maybe that the locking mechanism works.
You are right, your last test is rather trivial. I'd be tempted to omit writing it. But it depends. Is it likely that someone might come along and comment out one of those panel constructors? Do you have other tests that would identify such a problem?

Related

In Kotlin, how can one unit test that a data class, sealed class, or class field are not changed?

Basically we might have some data like so
open class RealmCart : RealmObject() {
#PrimaryKey
var _id: String = UUID.randomUUID().toString()
var items: RealmList<RealmCartItem> = RealmList()
var discountCode: String? = null
var userId: String = ""
}
And we do not want people editing these by mistake. We have some failsafe like code owners, labels in the repo, but we also want to have a unit test that can also prevent a merge if the data is changed in any way (add, change, or remove data). Basically, we do not want any accidents, and people are not perfect.
What is the best way to go about such a thing?
This is what I ended up doing:
I created an extension function for my data models
fun RealmObject.testDeclaredFields(): List<String> {
val fields = this::class.java.fields.map { it.name }
return this::class.java.declaredFields
.map { it.name }
.filterNot { fields.contains(it) }
.sorted()
}
Basically this just gets the data model fields, excluding things like companion objects.
Then I was able to create a test simply like
class RealmMessageTest {
#Test
fun `RealmMessage fields match spec`() {
val item = RealmMessage().testDeclaredFields()
assertContentEquals(item, fieldSpec)
}
private val fieldSpec = listOf(
"_id",
"acknowledgeStatusValue",
"body",
"completed",
"createdAt",
"deliveryStatusValue",
"from",
"meta",
"organizationId",
"platforms",
"threadId",
"title"
).sorted()
}
Why do this? Sometimes when someone is making changes carelessly, they will not realize that they have added a field, changed a field, or removed an important field in a data model that is sync'd to the backend. This does not prevent the developer from changing it, but given that they need to now change it in two places, they will be more cognizant whether they need to make this change or not.
I noticed a lot of people questioned why you would need to do this. My answer is that, I work in a very large repo where newer developers edit this without a second thought. This is just to make them more cognizant of changes to these important models, before they break develop. There are code owners for the repo, but they may not always see these changes. It is just an extra precaution.
How about using a mechanism like githooks to prevent the editing of certain files from being committed?
I'm not familiar with githooks, so I can't show you exactly how to do it, but I think it would be good to prevent commits and inform the developer of the situation with an error message.

Spark: Unit Test - I have one function that unions 3 input datasets. Should I do Unit test on them?

I have written a code part of which is as below
Object Cal{
def mergedatasets(df: Dataset[Row], df1: Dataset[Row],df2: Dataset[Row]):Dataset[Row]={
df.union(df1).union(df2)
//other logic
}
}
object readDataframes{
def readFirstDF(spark:SparkSession):Dataset[Row]={
spark.read.json(somefile)
}
def readSecondDF(spark:SparkSession):Dataset[Row]={
spark.read.json(somefile)
}
def readThirdDF(spark:SparkSession):Dataset[Row]={
spark.read.json(somefile)
}
}
In the above code I am reading 3 files and then merging them into one which I use further for processing.
Based on the above scenario my questions are as follows:
Does it make sense to Unit test the function mergdatasets? If yes, What are the basic/minimal things to test for?How to check for corner cases, if any?
Does it make sense to Unit test readDataframes?If yes what to test for ?Would it be to check if inferred schema is as expected? and anything else?
I would like to extend the above questions for the following functions too
def timeIntervalAgg(df: Dataset[Row]): Dataset[Row] = {
val timeInterval = df
.groupBy("id","page_number")
.agg(sum("timeInterval").alias("timeInterval"))
timeIntervalAgg
}
def timeInterval(df: Dataset[Row]): Dataset[Row] ={
val windowSpec = Window.partitionBy("id").orderBy("date_time")
val timeFmt = "yyyy-MM-dd'T'HH:mm:ss"
val endTime = lead(col("date_time"),1).over(windowSpec)
val startTime = col("date_time")
val timeDiff = (unix_timestamp(endTime, timeFmt)
- unix_timestamp(startTime, timeFmt))
val timeInterval = df
.withColumn("timeInterval", lit(when(col("event") === "this_event",lit(null)
.cast("long"))
.otherwise(timeDiff)))
.where("""event != "this_event" """)
timeInterval
}
def addOddpages(df: Dataset[Row]) :Dataset[Row] = {
val odd = df
.where("""view_mode = "twin" """)
.withColumn("page_odd", col("page") + 1)
.drop("page")
.select(col("id"), col("date_time")
.cast("timestamp"),col("page_odd")
.alias("page"), col("page_view_mode"),
col("event"),col("timeInterval"))
val timeIntervalWithoddPage = df.union(odd)
timeIntervalWithoddPage
}
Please suggest if it is needed to refactor the code in a better way
to enable better testing.
My goal is to understand what to test for? what to look out while
writing test for code like above? All this questions are for Spark
code Unit testing not other language code testing.
How to unit test without redundantly testing spark which is already
tested?
Is it needed to test every function like this(since the logic/code is not very complicated) or is best to test the
function that combines the above functions in proper order.By doing
so can it be called unit testing?
Please feel free to share some sample Unit tests that you may write
for above code.
Read JSON files: If you just read JSON files, you don't need to test this.
In addition, it might be better to read the files with explicit schema in schema() to avoid some issues with inferred schema. Also, you do not need 3 identical methods for reading the files.
Union Datasets: Since Spark 2.3.0 there is unionByName() function.
That function resolves columns by name (not by position). You can consider the functions to avoid issues with union when your DataFrames have different order of columns. Of course, this function doesn't need to be tested. It's hard to say about the //other logic code inside the mergedatasets() method.
For unit testing, you can use ScalaTest or other tools.
Create SparkSession with master("local");
Create a DataFrame with the expected data;
Create an input DataFrame for each method you want to test.;
Compare expected and actual DataFrames;
The following project might be useful. You can find there how to compare two DataFrames. Also, there are several examples in the README: https://github.com/MrPowers/spark-fast-tests

How do I name test method correctly

I have one doubt about How to name test method, becuase I Lastly have doing unit test to a big project, and based in my experience I think that I am set name test method bad or worse, Here an example about my code.
public function notificationApproved(Request $request, User $user) {
$user = $user->getId();
$request = $request->getRequest();
$this->notification = $this->em->getRepository('AppBundle:Notification')->find(Notification::APPROVED);
$this->notificationCategory = NotificationCategory::APPROVED;
$this->notificationStatus = $this->em->getRepository('AppBundle:NotificationStatus')->find(1);
$this->reason = $reason;
//notification approvers project
foreach ($projectHasUserUnits as $keyData => $valueData) {
$projectUserUnitResponsability = $valueData->getProjectUserUnitResponsability()->last();
$responsability = $projectUserUnitResponsability->getResponsabilityProject();
if (is_null($projectUserUnitResponsability->getEndAt()) && ($responsability->getId() == Responsability::TYPE_RESPONSIBLE || $responsability->getId() == Responsability::TYPE_ACCOUNT_MANAGER || $responsability->getId() == Responsability::TYPE_PROJECT_MANAGER)) {
$user = $valueData->getUserHasUnit()->getUser();
if( !in_array($user,$this->users)){
$this->users[] = $user;
$description_label = 'notification_description_project_50';
$short_description = 'notification_content_project_50';
$notification = $this->generateNotification($Project, $user, $description_label,$short_description);
if ($notification) {
$this->notificationTrigger
->sendProjectNotification($user, $notification, $Project
, $notification->getDescription(), date('l m-d-y H:i a'));
}
}
}
}
}
it is a big method, I know, but don't worry for its logic, Just see the conditionals, and think how to name a method test like this, In this case, when itself come in all conditionals... maybe:
test_notificationApprovedWhenAllConditionsAreTrue
test_notificationApprovedWhenProjectHasUserUnitsIsBiggerThanZeroAndProjectUserUnitResponsabilityGetEndAtIsNotNullAndResponsabilityGetIdIsEqualToResponsabilityTYPERESPONSABILITYorResponsabilityGetIdIsEqualToResponsabilityTYPE_PROJECT_MANAGERanduserInArrayIsTrueAndNotificationIsTrue...
You only imagine read that !
However when it come only one conditional is easier that the above, !Of Course!, like this:
test_notificationApprovedWhenProjectHasUserUnitsIsBiggerThanZeroAndProjectUserUnitResponsabilityGetEndAtIsNotNullAndResponsabilityGetIdIsEqualToResponsabilityTYPERESPONSABILITYorResponsabilityGetIdIsEqualToResponsabilityTYPE_PROJECT_MANAGER
I've tried with comments inside test, but If a test fail, the idea would be guiding for its name (to fix the error rapidly)
So, do you have any idea ?
I have good experience with given<one-or-more-conditions>_action_result naming of test methods. Just a simple example for your case:
test_givenUserUnitGreaterThanZeroAndProjectIdEqualToOne_whenApproveNotification_thenNotificationIsSent()
I've simplified things for the sake of readability but you get the point. This nomenclature is taken from behavior-driven test frameworks. In languages that do not need the test_ you can save it.
Tests are really good indicators for code quality. Generally speaking if writing unit tests is easy then it's because the code to test is of "good" quality. You can follow some simple guidelines:
List every precondition exactly as it is necessary for the use case
Exactly one action. If not make multiple tests.
Exactly one result. If not extract methods and test them separately.
If method names get too long because of many preconditions then it might be time to extract a new class because this one has too many responsibilities
A bit of advice: if you'd add a test annotation, you would not need to prefix method names with test.
/**
* #test
*/
function givenThis_producesThat () {

Is it good practice to reference services in html templates in Angular 2?

As the question states, is there any downside in referencing the service directly in the template as such :
[disabled]="stateService.selectedClient == null || stateService.currentStep == 1"
In my opinion this doesn't seem like good practice and I'd much rather keep a "selectedClient" object in whatever component needs to use it. How can I get the state and store it into local variables, while observing the changes:
example: I want to move from step1 to step2 by changing "currentStep" in the "stateService", however I want the component that keeps "currentStep" ALSO as a local variable to reflect the change in the state?
Is it good practice to reference services in html templates in Angular
2?
I'd generally avoid it. It seems to bring more chaos than good.
Cons:
Coming from OOP background, this approach looks like it breaks the Law of Demeter, but more importantly,
It's no longer MVC, where your controller (Angular2's Component) acts like a mediator between the view and the services.
Like Ced said, what if a call to a service's member is costly and we need to refer to it multiple times in the view?
At the moment my editor of choice (VS Code) does not fully support Angular2 templates; referencing too many things outside of its own Component's scope in a template makes refactoring not fun anymore.
Pros:
Sometimes it looks more elegant (because it saves you 2 lines of code), but trust me, it's not.
How can I get the state and store it into local variables, while
observing the changes
Madhu Ranjan has a good answer to this. I'll just try to make it more complete here for your particular example:
In your StateService, define:
currentStep : Subject<number> = new Subject<number>();
selectedClient: Subject<Client> = new Subject<Client>();
changeStep(nextStep: number){
this.currentStep.next(nextStep);
}
selectClient(client: Client) {
this.selectedClient.next(client);
}
In your Component:
currentStep: number;
constructor(stateService : StateService){
stateService.currentStep.combineLatest(
stateService.selectedClient,
(currStep, client) => {
if (client == null) {
// I'm assuming you are not showing any step here, replace it with your logic
return -1;
}
return currStep;
})
.subscribe(val => {
this.currentStep = val;
});
}
You may try below,
stateService
currentStep : Subject<number> = new Subject<number>();
somestepChangeMethod(){
this.currentStep.next(<set step here to depending on your logic>);
}
component
// use this in template
currentStep: number;
constructor(stateService : stateServiceClass){
stateService.currentStep.subscribe(val => {
this.currentStep = val;
});
}
Hope this helps!!
It is probably not a good idea to expose your subject inside of your state service. Something like this would be better.
StateService
private currentStep: Subject<number> = new Subject<number>();
changeStep(value: number) {
this.currentStep.next(value);
}
get theCurrentStep(): Observable<number> {
this.currentStep.asObservable();
}
Component
currentStep: number;
constructor(private stateService: StateService) {
this.currentStep = this.stateService.theCurrentStep;
}
Template
[disabled]="(currentStep | async) == 1" // Not sure if this part would work

PlayFramework 2.0 - Not able to call functions from other templates

I want to place some helper functions in another file, since they will be overly reused. I took the Computer-Databse sample's listing file:
https://github.com/playframework/Play20/blob/master/samples/scala/computer-database/app/views/list.scala.html
I created a new file, called "listing.scala.html" under the app/views package, and moved the #link function from the original file to it. This new file looks like this:
#(currentSortBy: String, currentOrder: String, currentFilter: String)
#****************************************
* Helper generating navigation links *
****************************************#
#link(newPage:Int, newSortBy:String) = #{
var sortBy = currentSortBy
var order = currentOrder
if(newSortBy != null) {
sortBy = newSortBy
if(currentSortBy == newSortBy) {
if(currentOrder == "asc") {
order = "desc"
} else {
order = "asc"
}
} else {
order = "asc"
}
}
// Generate the link
routes.Application.listPerfil(newPage, sortBy, order, currentFilter)
}
So, on my original file, I replaced the #link call, with this one:
#title
And the problem is, when I try to compile I get this error:
value link is not a member of play.api.templates.Html
But according to the documentation (http://www.playframework.org/documentation/2.0.4/ScalaTemplateUseCases) it seems to be ok.
Any guess?
Play's templates aren't the best place for placing advanced conditions, most probably you'll get better flexibility by processing it in some controller (or other method) which will return you only required link
ie.:
#title
In your case proposed link(...) function of Application controller can also return a reverse-route.
Keep in mind that including other templates is best option for repeating blocks of HTML but sometimes it's hard to get specified string (mainly because of not trimmed spaces). As you can see there is also problem with calling nested functions. Most probably you can generate whole A tag in the listing.scala.html however using it isn't comfortable enough (IMHO).