Skip to content

Commit

Permalink
Add behaviour tests for showing and saving datasets
Browse files Browse the repository at this point in the history
Three scenarios were added, testing showing a dataset, saving a dataset,
and bulk saving datasets (`save_datasets`).

Signed-off-by: Martin Raspaud <martin.raspaud@smhi.se>
  • Loading branch information
mraspaud committed Dec 14, 2015
1 parent 57bb739 commit 0b392ff
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
24 changes: 24 additions & 0 deletions mpop/tests/features/feature-save.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Created by a001673 at 2015-12-07
Feature: Simple and intuitive saving

Visualization of the data is important and should be an easy one-line, like eg
show(my_dataset). In a similar way, saving the data to disk should be simple,
for example save(dataset, filename), with sensible defaults provided depending
on the filename extension (eg. geotiff for .tif, netcdf for .nc). Saving
several datasets at once would be nice to have.

Scenario: 1-step showing dataset
Given a dataset is available
When the show command is called
Then an image should pop up

Scenario: 1-step saving dataset
Given a dataset is available
When the save_dataset command is called
Then a file should be saved on disk

Scenario: 1-step saving all datasets
Given a bunch of datasets are available
When the save_datasets command is called
Then a bunch of files should be saved on disk

93 changes: 93 additions & 0 deletions mpop/tests/features/steps/steps-save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from behave import *
from mock import patch

use_step_matcher("re")


@given("a dataset is available")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
from mpop.scene import Scene
from datetime import datetime
from mpop.projectable import Projectable
scn = Scene(platform_name="Suomi-NPP", sensor="viirs",
start_time=datetime(2015, 3, 11, 11, 20),
end_time=datetime(2015, 3, 11, 11, 26))
scn["MyDataset"] = Projectable([[1, 2], [3, 4]])
context.scene = scn


@when("the show command is called")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
with patch('trollimage.image.Image.show') as mock_show:
context.scene.show("MyDataset")
mock_show.assert_called_once_with()


@then("an image should pop up")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
pass


@when("the save_dataset command is called")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
context.filename = "/tmp/test_dataset.png"
context.scene.save_dataset("MyDataset", context.filename)


@then("a file should be saved on disk")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
import os
assert(os.path.exists(context.filename))
os.remove(context.filename)


@given("a bunch of datasets are available")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
from mpop.scene import Scene
from datetime import datetime
from mpop.projectable import Projectable
scn = Scene(platform_name="Suomi-NPP", sensor="viirs",
start_time=datetime(2015, 3, 11, 11, 20),
end_time=datetime(2015, 3, 11, 11, 26))
scn["MyDataset"] = Projectable([[1, 2], [3, 4]])
scn["MyDataset2"] = Projectable([[5, 6], [7, 8]])
context.scene = scn



@when("the save_datasets command is called")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
context.scene.save_datasets(writer="simple_image", file_pattern="{name}.png")



@then("a bunch of files should be saved on disk")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
import os
for filename in ["MyDataset.png", "MyDataset2.png"]:
assert(os.path.exists(filename))
os.remove(filename)

0 comments on commit 0b392ff

Please sign in to comment.