Hopp til innhold

Bruker:Jeblad/Module:JSONstat/testtilfeller

Fra Wikipedia, den frie encyklopedi


-- Unit tests for [[Module:Wb]]. Click talk page to run tests.
-- Note that the test set at [[Bruker:Jeblad/SSB01222.json]] is not live and only for test purposes.
local p = require('Module:UnitTests')
local jsonstat = require('Module:JSONstat')

local categoryAsJson =
[[{
	"index": { "A": 0, "B": 1 },
	"label": { "A": "test a", "B": "test b" },
	"unit": {
		"X": { "base": "apples" },
		"Y": { "base": "bananas" },
	}
}]]
local categoryAsTable = mw.text.jsonDecode( categoryAsJson )

local axisAsJson =
[[{
	"label": "horizontal",
	"category": {}
}]]
local axisAsTable = mw.text.jsonDecode( axisAsJson )

local dimensionAsJson =
[[{
	"Horizontal": {},
	"Vertical": {},
	"id": [ "Horizontal", "Vertical" ],
	"size": [ 3, 2 ],
	"role": { "geo": [ "Horizontal" ], "metric": [ "Vertical" ] },
	"class": "dimension",
	"updated": "2015-08-13T22:52:21Z"
}]]
local dimensionAsTable = mw.text.jsonDecode( dimensionAsJson )

local datasetAsJson =
[[{
		"status": {
			"4245": ".",
			"4300": "."
		},
		"dimension": {},
		"label": "Humtidumpti",
		"source": "Test Statistics",
		"updated": "2015-08-13T22:52:21Z",
		"value": [ 1, 2, 3, 4, 5, 6 ]
	}
]]
local datasetAsTable = mw.text.jsonDecode( datasetAsJson )

local bundleAsJson = 
[[{
	"bar":{"class":"dataset"},
	"foo":{"class":"dataset"}
}]]
local bundleAsTable = mw.text.jsonDecode( bundleAsJson )

local typecasts = {
	{ '{ "dataset": {} }', 'JSONstatDataset' },
	{ '{ "dimension": {} }', 'JSONstatDimension' },
	{ '{ "collection": {} }', 'JSONstatCollection' },
	{ '{ "class": "dataset" }', 'JSONstatDataset' },
	{ '{ "class": "dimension" }', 'JSONstatDimension' },
	{ '{ "class": "collection" }', 'JSONstatCollection' },
	{ '{}', 'JSONstatBundle' },
}

local data = require('Modul:JSONstat/testdata')
local ssb01222AsTable = data['SSB01222']
local ssb01222AsJson = mw.text.jsonEncode( ssb01222AsTable )

function p:test_category()
	local instance = jsonstat.Category( categoryAsTable )
	self:equals( 'Recall signature', instance.signature(), 'JSONstatCategory' )
	self:equals_deep( 'Call toTable to get data as table', instance.toTable(), categoryAsTable )
	self:equals_deep( 'Call toJSON to get data as JSON', mw.text.jsonDecode( instance.toJSON() ), categoryAsTable )
	self:equals_deep( 'Call index to get the indicies on table form', instance.index(), categoryAsTable.index )
	self:equals( 'Call index to get a single index', instance.index( 'A' ), categoryAsTable.index['A'] )
	self:equals( 'Call index to get a single non-existing index', instance.index( 'C' ), categoryAsTable.index['C'] )
	self:equals_deep( 'Call label to get the labels on table form', instance.label(), categoryAsTable.label )
	self:equals( 'Call label to get a single index', instance.label( 'A' ), categoryAsTable.label['A'] )
	self:equals( 'Call label to get a single non-existing index', instance.label( 'C' ), categoryAsTable.label['C'] )
	self:equals_deep( 'Call unit to get the units on table form', instance.unit(), categoryAsTable.unit )
	self:equals_deep( 'Call unit to get a single unit', instance.unit( 'X' ), categoryAsTable.unit['X'] )
	self:equals_deep( 'Call unit to get a single non-existing unit', instance.unit( 'Z' ), categoryAsTable.label['Z'] )
end

function p:test_axis()
	local instance = jsonstat.Axis( axisAsTable )
	self:equals( 'Recall signature', instance.signature(), 'JSONstatAxis' )
	self:equals_deep( 'Call toTable to get data as table', instance.toTable(), axisAsTable )
	self:equals_deep( 'Call toJSON to get data as JSON', mw.text.jsonDecode( instance.toJSON() ), axisAsTable )
	self:equals( 'Call label to get the single label', instance.label(), axisAsTable.label )
	self:equals( 'Call category to get the single category and check its signature', instance.category().signature(), 'JSONstatCategory' )
end

function p:test_dimension()
	local instance = jsonstat.Dimension( dimensionAsTable )
	self:equals( 'Recall signature', instance.signature(), 'JSONstatDimension' )
	self:equals_deep( 'Call toTable to get data as table', instance.toTable(), dimensionAsTable )
	self:equals_deep( 'Call toJSON to get data as JSON', mw.text.jsonDecode( instance.toJSON() ), dimensionAsTable )
	self:equals( 'Call class to get the instance registered class', instance.class(), dimensionAsTable.class )
	self:equals( 'Call get to get a single id and check its signature', instance.get( 'Horizontal' ).signature(), 'JSONstatAxis' )
	self:equals( 'Call updated to get a single timestamp', instance.updated(), '2015-08-13T22:52:21Z')
	self:equals_deep( 'Call id to get the ids on table form', instance.id(), dimensionAsTable.id )
	self:equals_deep( 'Call size to get the sizes on table form', instance.size(), dimensionAsTable.size )
	self:equals_deep( 'Call role to get the roles on table form', instance.role(), dimensionAsTable.role )
	-- the test data is insufficient to test the metod "each"
end

function p:test_dataset()
	local instance = jsonstat.Dataset( datasetAsTable )
	self:equals( 'Recall signature', instance.signature(), 'JSONstatDataset' )
	self:equals_deep( 'Call toTable to get data as table', instance.toTable(), datasetAsTable )
	self:equals_deep( 'Call toJSON to get data as JSON', mw.text.jsonDecode( instance.toJSON() ), datasetAsTable )
	self:equals( 'Call class to get the instance registered class', instance.class(), datasetAsTable.class )
	self:equals_deep( 'Call status to get the statuses on table form', instance.status(), datasetAsTable.status )
	self:equals( 'Call label to get the single label', instance.label(), 'Humtidumpti')
	self:equals( 'Call source to get the single source', instance.source(), 'Test Statistics')
	self:equals( 'Call updated to get the single timestamp', instance.updated(), '2015-08-13T22:52:21Z')
	self:equals_deep( 'Call value to get the values on table form', instance.value(), datasetAsTable.value )
	-- the test data is insufficient tu test the method "value" for single value recall
	self:equals( 'Call dimensjon to get it and check its signature', instance.dimension().signature(), 'JSONstatDimension' )
	-- the test data is insufficient to test the method "each"
	-- the test data is insufficient to test the method "expand"
	-- the test data is insufficient tu test the method "slice"
end

function p:test_bundle()
	local instance = jsonstat.Bundle( bundleAsTable )
	self:equals( 'Recall signature', instance.signature(), 'JSONstatBundle' )
	self:equals_deep( 'Call toTable to get data as table', instance.toTable(), bundleAsTable )
	self:equals_deep( 'Call toJSON to get data as JSON', mw.text.jsonDecode( instance.toJSON() ), bundleAsTable )
	self:equals( 'Call get to get a single dataset and check its signature', instance.get( 'foo' ).signature(), 'JSONstatDataset' )
	self:equals( 'Call get to get a single non-existing dataset', instance.get( 'baz' ), bundleAsTable['baz'] )
end

function p:test_typecasts()
	for i,v in ipairs( typecasts ) do
		self:equals( 'Test cast to ' .. v[2],  jsonstat.typecast( mw.text.jsonDecode( v[1] ) ).signature(), v[2] )
	end
end

function p:test_dimension_SSB01222()
	local instance = jsonstat.typecast( ssb01222AsTable )
	self:equals( 'Check that we have a dataset', instance.signature(), 'JSONstatDataset' )
	self:equals_deep('Recall value [0,0,0] from SSB01222', instance.value(1,1,1), 30328)
	self:equals_deep('Recall value [0,1,0] from SSB01222', instance.value(1,2,1), 70)
	self:equals_deep('Recall value [430,11,1] from SSB01222', instance.value(430,11,1), 10205)
	self:equals_deep("Recall value [Region='0101',ContentsCode='Folketallet1',Tid='2015K1'] from SSB01222", instance.value{Region='0101',ContentsCode='Folketallet1',Tid='2015K1'}, 30328)
	self:equals_deep("Recall value [Region='0101',ContentsCode='Fodte2',Tid='2015K1'] from SSB01222", instance.value{Region='0101',ContentsCode='Fodte2',Tid='2015K1'}, 70)
	self:equals_deep("Recall value [Region='0104',ContentsCode='Folketallet1',Tid='2015K1'] from SSB01222", instance.value{Region='0104',ContentsCode='Folketallet1',Tid='2015K1'}, 31802)
	self:equals_deep("Recall value [Region='0104',ContentsCode='Fodte2',Tid='2015K1'] from SSB01222", instance.value{Region='0104',ContentsCode='Fodte2',Tid='2015K1'}, 86)
	self:equals_deep("Recall value [Region='2030',ContentsCode='Folketallet1',Tid='2015K1'] from SSB01222", instance.value{Region='2030',ContentsCode='Folketallet1',Tid='2015K1'}, 10221)
	self:equals_deep("Recall value [Region='2030',ContentsCode='Fodte2',Tid='2015K1'] from SSB01222", instance.value{Region='2030',ContentsCode='Fodte2',Tid='2015K1'}, 23)
	--self:equals_deep('Recall value [Region=1,ContentsCode=Fodte2,Tid=1] from SSB01222', instance.value{Region=1,ContentsCode='Fodte2',Tid=1}, 70)
	--self:equals_deep("Recall value [Region='2030',ContentsCode='Folketallet11',Tid=1] from SSB01222", instance.value{Region=430,ContentsCode=11,Tid=1}, 10205)
end

function p:test_dataset_SSB01222()
	local instance = jsonstat.typecast( ssb01222AsTable )
	self:equals( 'Check that we have a dataset', instance.signature(), 'JSONstatDataset' )
end

--function p:test_load_SSB01222()
--end

return p