0

I want to make sure the date time stored in database table is not more than 2 minutes than previous captured date time.

the result returned from Database table is in this format.

[[col1:2020-05-28 04:02:21.34]]

my codes

import java.text.SimpleDateFormat
//capture current date time
def date = new Date()
println date.format('yyyy-MM-dd hh:mm:ss.SS',TimeZone.getTimeZone('UTC'))

//wait 2 minutes then capture DB table date time
WebUI.delay(120)
PostgresdbQuery = /SELECT col1 FROM table1.test/
List resultsafter = CustomKeywords.'test.database.getPostgresSQLResults'(GlobalVariable.testPostgresdbConnString , GlobalVariable.testPostgresdbUsername , GlobalVariable.testPostgresdbPassword ,GlobalVariable.testPostgresdbDriver ,PostgresdbQuery )
println(resultsafter)

//assert 
assert resultsafter < date, 'Execute time is within 2 minutes'

error

Reason:
groovy.lang.GroovyRuntimeException: Cannot compare java.util.ArrayList with value '[{col1=2020-05-28 04:02:21.34}]' and java.util.Date with value '5/28/20 1:49 PM'
user2201789
  • 1,083
  • 2
  • 20
  • 45
  • Use TimeCategory and TimeDuration for accurate time calculations: https://stackoverflow.com/a/2757551/3355860 – ou_ryperd May 28 '20 at 06:44
  • @ou_ryperd Saw the time duration, but I couldn't make it right still like below minus calculation. TimeDuration td = TimeCategory.minus( resultsafter, date ) println td // after this td, how do make sure its within 2 minutes? Using assertion or any time syntax? – user2201789 May 28 '20 at 11:59

1 Answers1

0

The result is a list of maps. To make that check work, you would have to write it as:

assert resultsAfter.first().col1 < date

This will only work for the very first result and only if there is one. Assuming, you want to assert that for all elements, you can use every or loop the results and do the asserts for each row.

Yet, at this point i'd just let the DB do the work: select all items, that dont't match the criteria and make sure, no results are found.

cfrick
  • 35,203
  • 6
  • 56
  • 68