Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VcfToBqTask unit tests #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ test {
// Print test results
testLogging {
events "passed", "skipped", "failed"
showStandardStreams = true
}
testLogging.showStandardStreams = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ protected void configure() {
bind(VcfParser.class).to(VcfParserImpl.class);
bind(BigQueryRowGenerator.class).to(BigQueryRowGeneratorImpl.class);
bind(VariantToBqUtils.class).to(VariantToBqUtilsImpl.class);
bind(SchemaGenerator.class).to(SchemaGeneratorImpl.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2020 Google LLC

package com.google.gcp_variant_transforms.task;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import com.google.api.services.bigquery.model.TableSchema;
import com.google.common.collect.ImmutableList;
import com.google.gcp_variant_transforms.beam.PipelineRunner;
import com.google.gcp_variant_transforms.library.HeaderReader;
import com.google.gcp_variant_transforms.library.VcfParser;
import com.google.gcp_variant_transforms.library.SchemaGenerator;
import com.google.gcp_variant_transforms.options.VcfToBqContext;
import com.google.gcp_variant_transforms.options.VcfToBqOptions;
import com.google.inject.Injector;
import htsjdk.variant.vcf.VCFHeader;
import java.io.IOException;
import org.apache.beam.sdk.options.PipelineOptions;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.MockitoAnnotations;
import org.mockito.Mock;

/**
* Units tests for VcfToBqTask.java
*/
public class VcfToBqTaskTest {
Injector spyInjector;
VcfToBqTask vcfToBqTask;
VcfToBqTask spyVcfToBqTask;

@Mock private PipelineRunner mockedPipelineRunner;
@Mock private VcfToBqOptions mockedVcfToBqOptions;
@Mock private VcfToBqContext mockedVcfToBqContext;
@Mock private VcfParser mockedVcfParser;
@Mock private HeaderReader mockedHeaderReader;
@Mock private SchemaGenerator mockedSchemaGenerator;
@Mock private VCFHeader mockedVcfHeader;
@Mock private TableSchema mockedTableSchema;

@Captor private ArgumentCaptor<PipelineOptions> argCaptorPipelineOptions;
@Captor private ArgumentCaptor<ImmutableList<String>> argCaptorImmutableList;

@Before
public void initializeSpyAndMocks() throws IOException{
MockitoAnnotations.initMocks(this);
vcfToBqTask = new VcfToBqTask(
mockedPipelineRunner,
mockedHeaderReader,
mockedVcfToBqContext,
mockedVcfParser,
mockedVcfToBqOptions,
mockedSchemaGenerator);
spyVcfToBqTask = spy(vcfToBqTask);
}

public void setUpRunBehaviors() throws IOException {
ImmutableList.Builder<String> headerLinesBuilder = new ImmutableList.Builder<String>();
for (int i = 0; i < 5; i++) {
headerLinesBuilder.add("##sampleHeaderLine=" + i);
}
ImmutableList<String> headerLines = headerLinesBuilder.build();
doReturn(headerLines).when(mockedHeaderReader).getHeaderLines();
doNothing().when(spyVcfToBqTask).setPipelineOptions(argCaptorPipelineOptions.capture());
doNothing().when(mockedVcfToBqContext).setHeaderLines(argCaptorImmutableList.capture());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we, specifically for this case, use headerLines instead of capturing? The problem with PipelineOptions was that the object was changed (type casted into PipelineOptions). Here I don't think we have the same issue.

doNothing().when(mockedPipelineRunner).runPipeline();
}

@Test
public void testVcfTaskConstructor_whenInitialize_thenNotNull() throws IOException {
assertThat(vcfToBqTask).isNotNull();
assertThat(spyVcfToBqTask).isNotNull();
}

@Test
public void testVcfTask_whenRun_thenVerifyCalls() throws IOException {
ImmutableList.Builder<String> headerLines = new ImmutableList.Builder<String>();
for (int i = 0; i < 5; i++) {
headerLines.add("##sampleHeaderLine=" + i);
}
setUpRunBehaviors();
spyVcfToBqTask.run();

verify(spyVcfToBqTask).setPipelineOptions(mockedVcfToBqOptions);
verify(mockedVcfToBqContext).setHeaderLines(headerLines.build());
verify(mockedVcfToBqContext).setVCFHeader(mockedVcfHeader);
verify(mockedVcfToBqContext).setBqSchema(mockedTableSchema);
verify(mockedPipelineRunner).runPipeline();
}
}