Test for Pruner

This commit is contained in:
Senad Uka
2018-08-13 18:54:12 +02:00
parent 5684e58826
commit 973f1e1093
15 changed files with 2888 additions and 53 deletions

2682
test/fixtures/correct_tree.json vendored Normal file

File diff suppressed because it is too large Load Diff

47
test/fixtures/missing_indicators.json vendored Normal file
View File

@@ -0,0 +1,47 @@
[
{
"id": 1,
"name": "Urban Extent",
"sub_themes": [
{
"categories": [
{
"id": 1
},
{
"id": 2,
"name": "Population",
"unit": "(thousands)"
},
{
"id": 3,
"name": "Population density",
"unit": "(people per sq. km.)"
}
],
"id": 1,
"name": "Administrative"
},
{
"categories": [
{
"id": 4,
"name": "Area",
"unit": "(sq. km.)"
},
{
"id": 5,
"name": "Population",
"unit": "(thousands)"
},
{
"id": 6,
"name": "Population density",
"unit": "(people per sq. km.)"
}
],
"id": 2,
"name": "Built up"
}]
}
]

61
test/pruner_test.rb Normal file
View File

@@ -0,0 +1,61 @@
require 'minitest/autorun'
require_relative 'test_helper'
require_relative '../lib/pruner'
class TestPruner < MiniTest::Test
def test_prune_happy_path
correct_tree = json_fixture('fixtures/correct_tree.json')
pruner = Pruning::Processing::Pruner.new(correct_tree)
tree = pruner.prune_tree([299])
expected = [{"id"=>1, "name"=>"Urban Extent",
"sub_themes"=>[{"categories"=>[{"id"=>1,
"indicators"=>[{"id"=>299, "name"=>"Total"}],
"name"=>"Area",
"unit"=>"(sq. km.)"}],
"id"=>1,
"name"=>"Administrative"}]}]
assert_equal(expected, tree)
end
def test_different_top_level_nodes
correct_tree = json_fixture('fixtures/correct_tree.json')
pruner = Pruning::Processing::Pruner.new(correct_tree)
tree = pruner.prune_tree([299, 131])
expected = [{"id"=>1, "name"=>"Urban Extent",
"sub_themes"=>[{"categories"=>[{"id"=>1, "indicators"=>[{"id"=>299, "name"=>"Total"}],
"name"=>"Area", "unit"=>"(sq. km.)"}], "id"=>1, "name"=>"Administrative"}]},
{"id"=>8, "name"=>"Business",
"sub_themes"=>[{"categories"=>[{"id"=>55, "indicators"=>[{"id"=>131, "name"=>"6-9"}],
"name"=>"Non-agriculture enterprises by size", "unit"=>"(percent of establishments)"}],
"id"=>24, "name"=>"Economic census"}]}]
assert_equal(expected, tree)
end
def test_no_indicators
correct_tree = json_fixture('fixtures/correct_tree.json')
pruner = Pruning::Processing::Pruner.new(correct_tree)
tree = pruner.prune_tree([])
assert_equal([], tree)
end
def test_with_nonexistant_ids
correct_tree = json_fixture('fixtures/correct_tree.json')
pruner = Pruning::Processing::Pruner.new(correct_tree)
tree = pruner.prune_tree([6000, 7000])
assert_equal([], tree)
end
def test_broken_tree
missing_indicators_tree = json_fixture('fixtures/missing_indicators.json')
pruner = Pruning::Processing::Pruner.new(missing_indicators_tree)
tree = pruner.prune_tree([299])
assert_equal([], tree)
end
end

19
test/test_helper.rb Normal file
View File

@@ -0,0 +1,19 @@
ENV['RACK_ENV'] = 'test'
require 'bundler/setup'
require 'dotenv/load'
require 'json'
require 'minitest/autorun'
require 'bogus/minitest/spec'
require_relative '../lib/api/app'
Bogus.configure { |config| config.search_modules << Pruning }
Thread.abort_on_exception = true
def json_fixture(file_name)
file = File.read(File.expand_path(File.dirname(__FILE__) + '/' + file_name))
JSON.parse(file)
end