Module:Wikibase utils/testcases

From Wikisource
Jump to navigation Jump to search
local p = require('Module:UnitTests')

local WBU = require( 'Module:Wikibase utils')

local function wdlink( id )
	if string.sub( id, 1, 1 ) == 'P' then
		return '[[wikidata:Property:' .. id .. '|' .. id .. ']]'	
	end
	return '[[wikidata:' .. id .. '|' .. id .. ']]'	
end

--[=[
Test that we can get entities from title or QIDs.
]=]
function p:test_get_entity()

  local cases = {
    {
      name = 'From QID string',
      titleOrQid = 'Q108662232',
      targetQid = 'Q108662232',
    },
    {
      name = 'From QID number',
      titleOrQid = 108662232,
      targetQid = 'Q108662232',
    },
    {
      name = 'From title string',
      titleOrQid = 'Ruth of the U. S. A. (McClurg)', -- local enWS page
      targetQid = 'Q108662232',
    },
    {
      name = 'From title object',
      titleOrQid = mw.title.new( 'Ruth of the U. S. A. (McClurg)' ), -- local enWS page
      targetQid = 'Q108662232',
    },
    {
      name = 'No entity',
      titleOrQid = 'Sense and Sensibility but with zombies and sharks and does not even exist',
      targetQid = nil,
    }
  }

  for _, case in pairs( cases ) do
    local entity = WBU.getEntity( case.titleOrQid )

    if case.targetQid == nil then
    	-- not expecting anything
    	self:equals( case.name, entity, nil )
    else
    	self:equals( case.name, entity.id, case.targetQid )
    end
  end
end

--[=[
Test that we can get entities from title or QIDs.
]=]
function p:test_get_matching_quals()

  local cases = {
    {
      qid = 'Q108662232',
      pid = 'P6216',
      qpid = 'P1001',
      expected = true
    },
    {
      qid = 'Q108662232',
      pid = 'P6216',
      qpid = 'P7002',
      expected = false
    },
  }

  for _, case in pairs( cases ) do
    local entity = WBU.getEntity( case.qid )
	local statements = entity:getAllStatements( case.pid )
	
	local found = false
	for _, statement in pairs( statements ) do
		local matchingQualifiers = WBU.getQualifiersOfStatementWithPid( statement, case.qpid )
		for mq in pairs( matchingQualifiers ) do
			found = true
		end
	end
	
	self:equals( wdlink( case.qpid ) .. ' in statement ' .. wdlink( case.pid ) .. ' of ' .. wdlink( case.qid ),
		found, case.expected )
  end
end

--[=[
Test that we can check snak values
]=]
function p:test_match_snak_value_ids()
  local cases = {
    {
      qid = 'Q108662232',
      pid = 'P50',
      expectedValue = 'Q4385379'
    },
  }
  
	for _, case in pairs( cases ) do
    	local entity = WBU.getEntity( case.qid )
		local statements = entity:getAllStatements( case.pid )
		
		for _, statement in pairs( statements ) do
			local actualValue = WBU.getSnakValueQid( statement.mainsnak )
			self:equals( 'Value of ' .. wdlink( case.pid ) .. ' of ' .. wdlink( case.qid ),
				actualValue, case.expectedValue)
			break
		end
	end
end

--[=[
Test that we can check snak years
]=]
function p:test_match_snak_value_years()
  local cases = {
    {
      qid = 'Q19095438',
      pid = 'P577',
      expectedValue = 2006
    },
  }
  
	for _, case in pairs( cases ) do
    	local entity = WBU.getEntity( case.qid )
		local statements = entity:getAllStatements( case.pid )
		
		for _, statement in pairs( statements ) do
			local actualValue = WBU.getSnakValueYear( statement.mainsnak )
			self:equals( 'Value of ' .. wdlink( case.pid ) .. ' of ' .. wdlink( case.qid ),
				actualValue, case.expectedValue)
			break
		end
	end
end

return p