Skip to content

Commit

Permalink
#138 more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 26, 2024
1 parent 7049cb4 commit 27e908f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 44 deletions.
126 changes: 82 additions & 44 deletions test/pages/test_awards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,81 @@
# License:: MIT
class TestAwards < Minitest::Test
def test_payables
xml = xslt(
"<xsl:copy-of select=\"z:payables('dude')\"/>",
"
<fb>
<f>
<what>reconciliation</what>
<when>#{(Time.now - (60 * 60)).utc.iso8601}</when>
<since>#{(Time.now - (50 * 60 * 60)).utc.iso8601}</since>
<who_name>dude</who_name>
<awarded>100</awarded>
<payout>70</payout>
<balance>30</balance>
</f>
<f>
<is_human>1</is_human>
<when>#{(Time.now - (10 * 60 * 60)).utc.iso8601}</when>
<who_name>dude</who_name>
<award>40</award>
</f>
<f>
<is_human>1</is_human>
<when>#{(Time.now - (10 * 60 * 60)).utc.iso8601}</when>
<who_name>dude</who_name>
<award>60</award>
</f>
<f>
<is_human>1</is_human>
<when>#{Time.now.utc.iso8601}</when>
<who_name>dude</who_name>
<award>25</award>
</f>
</fb>
",
'today' => (Time.now - (10 * 60 * 60)).utc.iso8601
)
assert_equal('55', xml.xpath('/td/text()').to_s, xml)
end

def test_monday
xml = xslt(
'<r><xsl:value-of select="z:monday(1)"/></r>',
'
<fb>
<f>
<what>pmp</what>
<area>hr</area>
<days_of_running_balance>7</days_of_running_balance>
</f>
</fb>
',
'today' => '2024-09-26T04:04:04Z'
)
assert_equal('2024-09-23Z', xml.xpath('/r/text()').to_s, xml)
end

def test_in_week
xml = xslt(
'<r><xsl:value-of select="z:in-week(\'2024-09-20T04:04:04Z\', 1)"/></r>',
'
<fb>
<f>
<what>pmp</what>
<area>hr</area>
<days_of_running_balance>14</days_of_running_balance>
</f>
</fb>
',
'today' => '2024-09-26T04:04:04Z'
)
assert_equal('true', xml.xpath('/r/text()').to_s, xml)
end

private

def xslt(template, xml, vars = {})
Dir.mktmpdir do |dir|
xsl = File.join(dir, 'foo.xsl')
File.write(
Expand All @@ -42,60 +117,23 @@ def test_payables
xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:z='https://www.zerocracy.com'
version='2.0' exclude-result-prefixes='xs z'>
<xsl:import href='#{File.join(__dir__, '../../xsl/vitals.xsl')}'/>
<xsl:template match='/'>
<xsl:copy-of select=\"z:payables('dude')\"/>
</xsl:template>
<xsl:template match='/'>#{template}</xsl:template>
</xsl:stylesheet>
"
)
xml = File.join(dir, 'input.xml')
File.write(
xml,
"
<fb>
<f>
<what>reconciliation</what>
<when>#{(Time.now - (60 * 60)).utc.iso8601}</when>
<since>#{(Time.now - (50 * 60 * 60)).utc.iso8601}</since>
<who_name>dude</who_name>
<awarded>100</awarded>
<payout>70</payout>
<balance>30</balance>
</f>
<f>
<is_human>1</is_human>
<when>#{(Time.now - (10 * 60 * 60)).utc.iso8601}</when>
<who_name>dude</who_name>
<award>40</award>
</f>
<f>
<is_human>1</is_human>
<when>#{(Time.now - (10 * 60 * 60)).utc.iso8601}</when>
<who_name>dude</who_name>
<award>60</award>
</f>
<f>
<is_human>1</is_human>
<when>#{Time.now.utc.iso8601}</when>
<who_name>dude</who_name>
<award>25</award>
</f>
</fb>
"
)
input = File.join(dir, 'input.xml')
File.write(input, xml)
output = File.join(dir, 'output.xml')
qbash(
[
"java -jar #{Shellwords.escape(File.join(__dir__, '../../target/saxon.jar'))}",
"-s:#{Shellwords.escape(xml)}",
"-s:#{Shellwords.escape(input)}",
"-xsl:#{Shellwords.escape(xsl)}",
"-o:#{Shellwords.escape(output)}",
"today=#{Shellwords.escape((Time.now + 10).utc.iso8601)}"
],
"-o:#{Shellwords.escape(output)}"
] + vars.map { |k, v| Shellwords.escape("#{k}=#{v}") },
log: Loog::NULL
)
out = Nokogiri::XML.parse(File.read(output))
assert_equal('55', out.xpath('/td/text()').to_s, out)
Nokogiri::XML.parse(File.read(output))
end
end
end
4 changes: 4 additions & 0 deletions xsl/awards.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ SOFTWARE.
<xsl:variable name="since" select="xs:dateTime($today) - xs:dayTimeDuration(concat('P', $days, 'D'))"/>
<xsl:variable name="facts" select="$fb/f[award and xs:dateTime(when) &gt; $since and is_human = 1]"/>
<xsl:function name="z:monday" as="xs:date">
<!--
Takes week number (e.g. 4) and returns ISO-8601 date of the
Monday of this week. Weeks counting starts from $today.
-->
<xsl:param name="week" as="xs:integer"/>
<xsl:variable name="d" select="xs:dateTime($today) - xs:dayTimeDuration(concat('P', ($weeks - $week) * 7, 'D'))"/>
<xsl:variable name="dow" select="xs:integer(format-date(xs:date($d), '[F1]'))"/>
Expand Down

0 comments on commit 27e908f

Please sign in to comment.