Modul:Wikidata/Filterers/testcases

Z Wikipedie, otevřené encyklopedie
Skočit na navigaci Skočit na vyhledávání
local myModule = require 'Modul:Wikidata/Filterers'
local builder = require 'Modul:Wikidata/build'
local ScribuntoUnit = require 'Modul:ScribuntoUnit'
local suite = ScribuntoUnit:new()

function suite:testFilterByDate()
	self:assertTrue(false)
end

function suite:testFilterByRank()
	local provider = {}
	for _, rank in ipairs({ 'deprecated', 'preferred', 'normal', 'normal', 'deprecated', 'preferred' }) do
		table.insert(provider, builder.buildStatement{ rank = rank, datatype = 'string', value = rank })
	end
	for rank, length in ipairs({ all = 6, [false] = 4, preferred = 2, normal = 2, deprecated = 2, valid = 4, best = 2 }) do
		local statements = mw.clone(provider)
		myModule.filterStatements(statements, { rank = rank or nil })
		self:assertEquals(length, #statements)
	end
end

function suite:testFilterByLanguage()
	local provider = {}
	for _, lang in ipairs({ 'cs', 'en', 'de', 'cs', 'xyz' }) do
		table.insert(provider, builder.buildStatement{ datatype = 'monolingualtext', lang = lang, value = 'foo' })
	end
	table.insert(provider, 3, builder.buildStatement{ datatype = 'monolingualtext', snaktype = 'somevalue' })
	for lang, length in pairs({ cs = 2, en = 1, sk = 0 }) do
		local statements = mw.clone(provider)
		myModule.filterStatements(statements, { rank = 'all', somevalue = true, withlang = lang })
		self:assertEquals(length, #statements)
	end
end

function suite:testFilterHasReferences()
	self:assertTrue(false)
end

function suite:testFilterHasTarget()
	local provider = {}
	for i = 1, 2 do
		for j = i, 2 do
			table.insert(provider, builder.buildStatement{ datatype = 'wikibase-item', value = i })
		end
	end
	for id, length in pairs({ Q1 = 2, Q2 = 1, Q3 = 0 }) do
		local statements = mw.clone(provider)
		myModule.filterStatements(statements, { withtarget = id })
		self:assertEquals(length, #statements)
	end
end

function suite:testFilterHasQualifier()
	local provider = {}
	for i = 1, 3 do
		local statement = builder.buildStatement{ datatype = 'wikibase-item', value = i }
		statement.qualifiers = {}
		for j = 1, i do
			statement.qualifiers['P' .. j] = {
				builder.buildSnak{ datatype = 'wikibase-item', value = j }
			}
		end
		table.insert(provider, statement)
	end
	for prop, length in pairs({ P1 = 3, P2 = 2, P3 = 1, P4 = 0 }) do
		local statements = mw.clone(provider)
		myModule.filterStatements(statements, { withqualifier = prop })
		self:assertEquals(length, #statements)
	end
end

function suite:testFilterHasQualifier_WithValue()
	local provider = {}
	for i = 1, 3 do
		local statement = builder.buildStatement{ datatype = 'wikibase-item', value = i }
		statement.qualifiers = {
			P1 = {}
		}
		for j = 1, i do
			table.insert(statement.qualifiers.P1, builder.buildSnak{ datatype = 'wikibase-item', value = j })
		end
		table.insert(provider, statement)
	end
	for id = 1, 4 do
		local statements = mw.clone(provider)
		local val = mw.ustring.format('Q%s', id)
		myModule.filterStatements(statements, { withqualifier = 'P1', withqualifiervalue = val })
		self:assertEquals(4 - id, #statements)
	end
end

function suite:testFilterItemHasLabel()
	local provider = {}
	for snak, value in pairs({ value = 6, value = 1085, novalue = false }) do
		table.insert(provider, builder.buildStatement{ datatype = 'wikibase-item', snaktype = snak, value = value })
	end
	local statements = mw.clone(provider)
	myModule.filterStatements(statements, { withlabel = true })
	self:assertEquals(1, #statements)
end

function suite:testFilterBySnaktype()
	local provider = {
		{
			expected = 2,
			opts = {},
		},
		{
			expected = 3,
			opts = { novalue = true },
		},
		{
			expected = 3,
			opts = { somevalue = true },
		},
		{
			expected = 4,
			opts = { somevalue = true, novalue = true },
		},
	}
	local statements = {}
	for _, snaktype in ipairs{ 'somevalue', 'novalue' } do
		table.insert(statements, builder.buildStatement{ datatype = 'wikibase-item', snaktype = snaktype })
	end
	for i = 1, 2 do
		table.insert(statements, 2, builder.buildStatement{ datatype = 'wikibase-item', value = i })
	end
	for _, data in ipairs(provider) do
		local _statements = mw.clone(statements)
		myModule.filterStatements(_statements, data.opts)
		self:assertEquals(data.expected, #_statements)
	end
end

return suite