Modul:Wikidata/lib/testcases

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

function suite:testAddWdClass()
	self:assertStringContains( '>string<', myModule.addWdClass( 'string' ), true )
	self:assertStringContains( 'class="wd"', myModule.addWdClass( 'string' ), true )
end

function suite:testGetInterwikiPrefix()
	local sites = {
		en = 'en:',
		enwiki = 'w:en:',
		commons = 'commons:',
		commonswiki = 'w:commons:',
		cswiki = 'w:cs:',
		cswikiversity = 'v:cs:',
		dewikiquote = 'q:de:',
		q = 'q:',
		wikidata = 'd:',
		wikidatawiki = 'd:',
		wikiquote = 'q:',
	}
	for site, prefix in pairs(sites) do
		self:assertEquals(prefix, myModule.getInterwikiPrefix(site))
	end
end

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

function suite:testFormatDateRange()
	local Time = require 'Modul:Time'
	local Formatters = require 'Modul:Wikidata/Formatters'
	local formatRawValue = function (value, options)
		local _value = mw.clone(value)
		_value.precision = math.min(options.precision, _value.precision)
		return tostring(_value)
	end
	Formatters.Formatters.time = {
		formatValue = function (value, options)
			return formatRawValue(Time.newFromWikidataValue(value), options)
		end,
		getRawValue = Time.newFromWikidataValue,
		formatRawValue = formatRawValue,
	}
	local provider = {
		{ { begin = '1890-01-09', ending = '1938-12-25' }, 11, '1890-01-09 – 1938-12-25' },
		{ { begin = '1890-01-09', ending = '1938-12-25' }, 10, '1890-01 – 1938-12' },
		{ { begin = '1890-01-09', ending = '1938-12-25' },  9, '1890–1938' },
		{ { begin = '1890-01',    ending = '1938-12'    }, 11, '1890-01 – 1938-12' },
		{ { begin = '1890-01-09', ending = nil          }, 11, '1890-01-09' },
		{ { begin = '1890-01-09', ending = nil          }, 10, '1890-01' },
		{ { begin = nil,          ending = '1938-12-25' }, 11, '1938-12-25' },
		{ { begin = nil,          ending = '1938-12-25' }, 10, '1938-12' },
		{ { begin = '1890',       ending = '1890'       },  9, '1890' },
		{ { begin = '1890',       ending = '1890'       }, 10, '1890' },
		{ { begin = '1890-01',    ending = '1890-12'    },  9, '1890-01 – 1890-12' },
	}
	for _, data in ipairs(provider) do
		for key, value in pairs(data[1]) do
			data[1][key] = builder.buildSnak{ datatype = 'time', value = value }
		end
		local range = myModule.formatDateRange(data[1], { precision = data[2] })
		if not data[1].ending then
			local begin = Formatters.getFormattedValue(data[1].begin, { precision = data[2] })
			self:assertStringContains(begin, range, true)
			self:assertNotStringContains('–', range, true)
		elseif not data[1].begin then
			local ending = Formatters.getFormattedValue(data[1].ending, { precision = data[2] })
			self:assertStringContains(ending, range, true)
			self:assertNotStringContains('–', range, true)
		else
			self:assertEquals(data[3], range)
		end
	end
end

function suite:testFormatFromPattern()
	self:assertEquals(
		'http://www.wikidata.org/wiki/abcdefg',
		myModule.formatFromPattern( 'abcdefg', 'http://www.wikidata.org/wiki/$1' )
	)
	self:assertEquals(
		'http://www.wikidata.org/wiki/abcdefg/abcdefg',
		myModule.formatFromPattern( 'abcdefg', 'http://www.wikidata.org/wiki/$1/$1' )
	)
	self:assertEquals(
		'http://www.wikidata.org/wiki/',
		myModule.formatFromPattern( 'abcdefg', 'http://www.wikidata.org/wiki/' )
	)
end

function suite:testGetEntityIdFromValue()
	self:assertEquals(
		'Q1',
		myModule.getEntityIdFromValue( builder.buildValueFromDatatype{ datatype = "wikibase-item", value = 1 } )
	)
	self:assertEquals(
		'P5',
		myModule.getEntityIdFromValue( builder.buildValueFromDatatype{ datatype = "wikibase-property", value = 5 } )
	)
end

function suite:testGetItemIdFromURI()
	self:assertEquals( 'Q1985727', myModule.getItemIdFromURI('http://www.wikidata.org/entity/Q1985727') )
	self:assertFalse( myModule.getItemIdFromURI('http://www.wikidata.org/entity/Foo') )
	self:assertFalse( myModule.getItemIdFromURI('http://www.wikidata.org/entity/Q') )
	self:assertFalse( myModule.getItemIdFromURI('http://www.wikidata.org/entity/') )
end

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

function suite:testIsOptionTrue()
	local options = {
		addcat = true,
		addclass = 'true',
		addlink = 'yes',
		property = nil,
		showmore = '1',
		showsource = 1,
	}
	self:assertTrue( myModule.IsOptionTrue( options, 'addcat' ) )
	self:assertTrue( myModule.IsOptionTrue( options, 'addclass' ) )
	self:assertTrue( myModule.IsOptionTrue( options, 'addlink' ) )
	self:assertTrue( myModule.IsOptionTrue( options, 'showmore' ) )
	self:assertTrue( myModule.IsOptionTrue( options, 'showsource' ) )
	self:assertFalse( myModule.IsOptionTrue( options, 'property' ) )
end

function suite:testIsPropertyId()
	for _, val in ipairs{ 'P1', 'P999', 'P99999' } do
		self:assertTrue( myModule.isPropertyId( val ) )
	end
	for _, val in ipairs{ '', 'xxx', 'P', 'P0', 'P00' } do
		self:assertFalse( myModule.isPropertyId( val ) )
	end
end

function suite:testIsSnakValue()
	self:assertTrue( myModule.IsSnakValue( builder.buildSnak{
		snaktype = 'value', datatype = 'wikibase-item', value = 1 } ) )
	self:assertFalse( myModule.IsSnakValue( builder.buildSnak{ snaktype = 'somevalue' } ) )
	self:assertFalse( myModule.IsSnakValue( builder.buildSnak{ snaktype = 'novalue' } ) )
end

function suite:testSimpleCompare()
	local provider = {
		{
			first = 1,
			second = 2,
			expected = -1
		},
		{
			first = 2,
			second = 1,
			expected = 1
		},
		{
			first = 2,
			second = 2,
			expected = 0
		},
		{
			first = 'x',
			second = 'y',
			expected = -1
		},
	}
	for _, data in ipairs( provider ) do
		self:assertEquals( data.expected, myModule.simpleCompare( data.first, data.second ) )
	end
end

function suite:testTextToTable()
	self:assertDeepEquals(
		{ 'a', 'b', 'c' },
		myModule.textToTable( 'a,b,c' )
	)
	self:assertDeepEquals(
		{ 'a' },
		myModule.textToTable( 'a' )
	)
	self:assertDeepEquals(
		{ 'a', 'b', 'c' },
		myModule.textToTable( 'a-b-c', { split_pattern = '-' } )
	)
	self:assertDeepEquals(
		{ 'a', 'b', 'c' },
		myModule.textToTable( { 'a', 'b', 'c' } )
	)
end

return suite