Skip to content
Snippets Groups Projects
Commit 131b37fa authored by Joakim Aleksandersen's avatar Joakim Aleksandersen
Browse files

add unit tests in sentinel huvb

parent 64913bad
No related branches found
No related tags found
No related merge requests found
import unittest
from unittest.mock import patch, mock_open
import json
from sentinelhub import BBox, CRS
from server.Sentinelhub import box_funcitons
# Sample data for mocking the box.json content
mock_box_json = {
"type": "MultiPolygon",
"coordinates": [
[
[
[10.0, 60.0],
[10.0, 61.0],
[11.0, 61.0],
[11.0, 60.0]
]
],
[
[
[12.0, 62.0],
[12.0, 63.0],
[13.0, 63.0],
[13.0, 62.0]
]
]
]
}
class TestBoxFunctions(unittest.TestCase):
@patch('server.Sentinelhub.box_funcitons.open', new_callable=mock_open, read_data=json.dumps(mock_box_json))
@patch('server.Sentinelhub.box_funcitons.os.path.realpath', return_value='/path/to/fake/dir')
def test_get_all_box(self, mock_realpath, mock_open):
boxes = box_funcitons.get_all_box()
expected_boxes = [
[[10.0, 60.0], [10.0, 61.0], [11.0, 61.0], [11.0, 60.0]],
[[12.0, 62.0], [12.0, 63.0], [13.0, 63.0], [13.0, 62.0]]
]
self.assertEqual(boxes, expected_boxes)
def test_create_BBox(self):
box = [[10.0, 60.0], [10.0, 61.0], [11.0, 61.0], [11.0, 60.0]]
bbox = box_funcitons.create_BBox(box)
expected_bbox = BBox((10.0, 60.0, 11.0, 61.0), CRS.WGS84)
self.assertEqual(bbox, expected_bbox)
@patch('server.Sentinelhub.box_funcitons.get_all_box')
def test_get_all_bbox(self, mock_get_all_box):
mock_get_all_box.return_value = [
[[10.0, 60.0], [10.0, 61.0], [11.0, 61.0], [11.0, 60.0]],
[[12.0, 62.0], [12.0, 63.0], [13.0, 63.0], [13.0, 62.0]]
]
bboxes = box_funcitons.get_all_bbox()
expected_bboxes = [
BBox((10.0, 60.0, 11.0, 61.0), CRS.WGS84),
BBox((12.0, 62.0, 13.0, 63.0), CRS.WGS84)
]
self.assertEqual(bboxes, expected_bboxes)
def test_get_distance(self):
box = [[10.0, 60.0], [10.0, 61.0], [11.0, 61.0], [11.0, 60.0]]
point = (10.5, 60.5)
distance = box_funcitons.get_distance(box, point)
self.assertEqual(distance, 0.0)
point_outside = (15.0, 65.0)
distance_outside = box_funcitons.get_distance(box, point_outside)
self.assertAlmostEqual(distance_outside, 5.656, places=2)
@patch('server.Sentinelhub.box_funcitons.get_all_box')
def test_get_closest_bbox_and_id(self, mock_get_all_box):
mock_get_all_box.return_value = [
[[10.0, 60.0], [10.0, 61.0], [11.0, 61.0], [11.0, 60.0]],
[[12.0, 62.0], [12.0, 63.0], [13.0, 63.0], [13.0, 62.0]]
]
bbox, idx = box_funcitons.get_closest_bbox_and_id(10.5, 60.5)
expected_bbox = BBox((10.0, 60.0, 11.0, 61.0), CRS.WGS84)
self.assertEqual(bbox, expected_bbox)
self.assertEqual(idx, 0)
bbox, idx = box_funcitons.get_closest_bbox_and_id(12.5, 62.5)
expected_bbox = BBox((12.0, 62.0, 13.0, 63.0), CRS.WGS84)
self.assertEqual(bbox, expected_bbox)
self.assertEqual(idx, 1)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
import unittest
import pandas as pd
import datetime as dt
from server.Sentinelhub import getAreaInfo
class MyTestCase(unittest.TestCase):
def test_stats_to_df(self):
stats_data = {
"data": [
{
"interval": {"from": "2023-01-01T00:00:00Z", "to": "2023-01-02T00:00:00Z"},
"outputs": {
"default": {
"bands": {
"B0": {
"stats": {
"sampleCount": 100,
"noDataCount": 0,
"min": 0.0,
"max": 1.0,
"mean": 0.5,
"stDev": 0.1,
"percentiles": {"10.0": 0.1, "90.0": 0.9}
}
}
}
}
}
}
]
}
expected_df = pd.DataFrame({
"interval_from": [dt.date(2023, 1, 1)],
"interval_to": [dt.date(2023, 1, 2)],
"default_B0_sampleCount": [100],
"default_B0_noDataCount": [0],
"default_B0_min": [0.0],
"default_B0_max": [1.0],
"default_B0_mean": [0.5],
"default_B0_stDev": [0.1],
"default_B0_percentiles_10.0": [0.1],
"default_B0_percentiles_90.0": [0.9]
})
result_df = getAreaInfo.stats_to_df(stats_data)
pd.testing.assert_frame_equal(result_df, expected_df)
def test_classify_ice(self):
data = pd.DataFrame({
'default_B0_stDev': [36, 10, 5, 8],
'default_B0_noDataCount': [500, 500, 500, 500]
})
expected_conditions = ['High probability of Cloud', 'No ice', 'Ice', 'No ice']
result = getAreaInfo.classify_ice(data)
self.assertEqual(result['ice_condition'].tolist(), expected_conditions)
def test_get_last_date(self):
data = pd.DataFrame({
'interval_to': ['2023-01-01', '2023-02-01', '2023-03-01']
})
expected_last_date = '2023-03-01'
self.assertEqual(getAreaInfo.get_last_date(data), expected_last_date)
expected_last_date_none = (
dt.datetime.now().year - 1 if dt.datetime.now().month < 8 else dt.datetime.now().year, 8, 1)
self.assertEqual(getAreaInfo.get_last_date(None), dt.datetime(*expected_last_date_none).strftime("%Y-%m-%d"))
def test_get_ice_run_dates(self):
data = pd.DataFrame({
'interval_to': ['2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01'],
'interval_from': ['2023-01-02', '2023-02-02', '2023-03-02', '2023-04-02'],
'ice_condition': ['Ice', 'No ice', 'Ice', 'High probability of Cloud']
})
expected_dates = ['2023-01-17', '2023-02-15', '2023-03-17']
self.assertEqual(getAreaInfo.get_ice_run_dates(data), expected_dates)
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment