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 js = require('Module:JSONstat')

local bundle = '{"bar":{"class":"dataset"},"foo":{"class":"dataset"}}'
local dataset = [[{
	"dataset": {
		"status": {
			"4245": ".",
			"4300": "."
		},
		"dimension": {
			"Horizontal": {
				"label": "horizontal",
				"category": {
					"index": { "A": 0, "B": 1 },
					"label": { "A": "test a", "B": "test b" }
				}
			},
			"Vertical": {
				"label": "vertical",
				"category": {
					"index": { "X": 0, "Y": 1, "Z": 2 },
					"label": { "X": "test x", "Y": "test y", "Z": "test z" },
					"unit": {
						"X": { "base": "apples" },
						"Y": { "base": "bananas" },
						"Z": { "base": "oranges"
						}
					}
				}
			},
			"id": [ "Horizontal", "Vertical" ],
			"size": [ 3, 2 ],
			"role": { "geo": [ "Horizontal" ], "metric": [ "Vertical" ] }
		},
		"label": "Humtidumpti",
		"source": "Test Statistics",
		"updated": "2015-08-13T22:52:21Z",
		"value": [ 1, 2, 3, 4, 5, 6 ]
	}
}]]

function p:test_load()
    self:preprocess_equals('{{#invoke:JSONstat | load | Bruker:Jeblad/SSB01222.json | signature }}', 'JsDataset')
    self:preprocess_equals('{{#invoke:JSONstat | load | Bruker:Jeblad/SSB01222.json | label }}', 'Population and changes during the quarter, by region, contents and time')
    self:preprocess_equals('{{#invoke:JSONstat | load | Bruker:Jeblad/SSB01222.json | updated }}', '2015-08-13T22:52:21Z')
    self:preprocess_equals('{{#invoke:JSONstat | load | Bruker:Jeblad/SSB01222.json | dimension-signature }}', 'JsDimension')
    self:preprocess_equals('{{#invoke:JSONstat | load | Bruker:Jeblad/SSB01222.json | ${source} }}', 'Statistics Norway')
    self:preprocess_equals('{{#invoke:JSONstat | load | Bruker:Jeblad/SSB01222.json | ${source:%.10s} }}', 'Statistics')
    self:preprocess_equals('{{#invoke:JSONstat | load | { "dataset": {} } | signature }}', 'JsDataset')
    self:preprocess_equals('{{#invoke:JSONstat | load | { "foo": { "class": "dataset" } } | signature }}', 'JsBundle')
    self:preprocess_equals('{{#invoke:JSONstat | load | { "foo": { "class": "dataset" } } | ${foo.class} }}', 'dataset')
    self:preprocess_equals('{{#invoke:JSONstat | load | { "dimension": {} } | signature }}', 'JsDimension')
    self:preprocess_equals('{{#invoke:JSONstat | load | { "foo": { "class": "dimension" } } | signature }}', 'JsBundle')
    self:preprocess_equals('{{#invoke:JSONstat | load | { "foo": { "class": "dimension" } } | ${foo.class} }}', 'dimension')
    self:preprocess_equals('{{#invoke:JSONstat | load | { "collection": {} } | signature }}', 'JsCollection')
    self:preprocess_equals('{{#invoke:JSONstat | load | { "foo": { "class": "collection" } } | signature }}', 'JsBundle')
    self:preprocess_equals('{{#invoke:JSONstat | load | { "foo": { "class": "collection" } } | ${foo.class} }}', 'collection')
end

function p:test_category()
	local category = js.load(dataset).dimension().get('Horizontal').category()
	local categoryAsTable = mw.text.jsonDecode( dataset ).dataset.dimension['Horizontal'].category
	self:equals('Recall signature from predefined small dataset', category.signature(), 'JsCategory')
end

function p:test_axis()
	local axis = js.load(dataset).dimension().get('Horizontal')
	local axisAsTable = mw.text.jsonDecode( dataset ).dataset.dimension['Horizontal']
	self:equals('Recall signature from predefined small dataset', axis.signature(), 'JsAxis')
	self:equals('Recall label from predefined small dataset', axis.label(), 'horizontal')
	self:equals('Recall label from predefined small dataset', axis.category().signature(), 'JsCategory')
	self:equals_deep('Recall dataset predefined as a small bundle and compare it to a table', mw.text.jsonDecode( axis.toJSON() ), axisAsTable )
	self:equals_deep('Recall dataset predefined as a small bundle and compare it to a json struct', axis.toTable(), axisAsTable)
end

function p:test_dimension()
	local dimension = js.load(dataset).dimension()
	local dimensionAsTable = mw.text.jsonDecode( dataset ).dataset.dimension
	self:equals('Recall signature from predefined small dataset', dimension.signature(), 'JsDimension')
	self:equals('Recall an axis from predefined small dataset', dimension.get('Horizontal').signature(), 'JsAxis')
	self:equals('Recall an axis from predefined small dataset', dimension.get('Test'), nil)
	self:equals_deep('Recall id from predefined small dataset', dimension.id(), dimensionAsTable.id)
	self:equals_deep('Recall size from predefined small dataset', dimension.size(), dimensionAsTable.size)
	self:equals_deep('Recall role from predefined small dataset', dimension.role(), dimensionAsTable.role)
	self:equals_deep('Recall dataset predefined as a small bundle and compare it to a table', mw.text.jsonDecode( dimension.toJSON() ), dimensionAsTable )
	self:equals_deep('Recall dataset predefined as a small bundle and compare it to a json struct', dimension.toTable(), dimensionAsTable)
end

function p:test_large_dataset()
	local dset = js.load('Bruker:Jeblad/SSB01222.json')
	self:equals('Recall signature by method from Bruker:Jeblad/SSB01222.json', dset.signature(), 'JsDataset')
	self:equals_deep('Recall value [0,0] by from Bruker:Jeblad/SSB01222.json', dset.value(1,1,1), 30328)
	self:equals_deep('Recall value [0,1] by from Bruker:Jeblad/SSB01222.json', dset.value(1,2,1), 70)
	self:equals_deep('Recall value [3,2] by from Bruker:Jeblad/SSB01222.json', dset.value(430,11,1), 10205)
	self:equals_deep('Recall value [0,0] by from Bruker:Jeblad/SSB01222.json', dset.value{Region=1,ContentsCode='Folketallet1',Tid=1}, 30328)
	self:equals_deep('Recall value [0,1] by from Bruker:Jeblad/SSB01222.json', dset.value{Region=1,ContentsCode='Fodte2',Tid=1}, 70)
	self:equals_deep('Recall value [3,2] by from Bruker:Jeblad/SSB01222.json', dset.value{Region=2030,ContentsCode='Folketallet11',Tid=1}, 10205)
end

function p:test_dataset()
	local dset = js.load(dataset)
	local datasetAsTable = mw.text.jsonDecode( dataset )['dataset']
	self:equals('Recall signature by from predefined small dataset', dset.signature(), 'JsDataset')
	self:equals('Recall label by from predefined small dataset', dset.label(), 'Humtidumpti')
	self:equals('Recall source by from predefined small dataset', dset.source(), 'Test Statistics')
	self:equals('Recall updated by from predefined small dataset', dset.updated(), '2015-08-13T22:52:21Z')
	self:equals('Recall dimension by from predefined small dataset', dset.dimension().signature(), 'JsDimension')
	self:equals_deep('Recall status by from predefined small dataset', dset.status(), datasetAsTable.status)
	self:equals_deep('Recall values by from predefined small dataset', dset.value(), datasetAsTable.value)
	self:equals_deep('Recall value [0,0] by from predefined small dataset', dset.value(1,1), 1)
	self:equals_deep('Recall value [0,1] by from predefined small dataset', dset.value(1,2), 2)
	self:equals_deep('Recall value [3,2] by from predefined small dataset', dset.value(3,2), 6)
	self:equals_deep('Recall dataset predefined as a small bundle and compare it to a table', mw.text.jsonDecode( dset.toJSON() ), datasetAsTable )
	self:equals_deep('Recall dataset predefined as a small bundle and compare it to a json struct', dataset.toTable(), dsetAsTable)
end

function p:test_bundle()
	local bdl = js.load(bundle)
	self:equals('Recall signature by from predefined small bundle', bdl.signature(), 'JsBundle')
	self:equals('Recall dataset by get("foo") from predefined small bundle', bdl.get('foo').signature(), 'JsDataset')
	self:equals('Recall dataset by get("test") from predefined small bundle', bdl.get('test'), nil)
	self:equals_deep('Recall dataset predefined as a small bundle and compare it to a table', mw.text.jsonDecode( bdl.toJSON() ), mw.text.jsonDecode( bundle ) )
	self:equals_deep('Recall dataset predefined as a small bundle and compare it to a json struct', bdl.toTable(), mw.text.jsonDecode( bundle ))
end

return p