tests: Timeclock -> easytest
This commit is contained in:
		
							parent
							
								
									8d1506a4a4
								
							
						
					
					
						commit
						a8175d8b14
					
				| @ -52,8 +52,7 @@ import Hledger.Utils.Test | |||||||
| 
 | 
 | ||||||
| tests_Hledger_Data = TestList | tests_Hledger_Data = TestList | ||||||
|     [ |     [ | ||||||
|      tests_Hledger_Data_Timeclock |      tests_Hledger_Data_Transaction | ||||||
|     ,tests_Hledger_Data_Transaction |  | ||||||
|     ] |     ] | ||||||
| 
 | 
 | ||||||
| easytests_Data = tests "Data" [ | easytests_Data = tests "Data" [ | ||||||
| @ -62,4 +61,5 @@ easytests_Data = tests "Data" [ | |||||||
|   ,easytests_Journal |   ,easytests_Journal | ||||||
|   ,easytests_Ledger |   ,easytests_Ledger | ||||||
|   ,easytests_Posting |   ,easytests_Posting | ||||||
|  |   ,easytests_Timeclock | ||||||
|   ] |   ] | ||||||
|  | |||||||
| @ -8,8 +8,12 @@ converted to 'Transactions' and queried like a ledger. | |||||||
| 
 | 
 | ||||||
| {-# LANGUAGE CPP, OverloadedStrings #-} | {-# LANGUAGE CPP, OverloadedStrings #-} | ||||||
| 
 | 
 | ||||||
| module Hledger.Data.Timeclock | module Hledger.Data.Timeclock ( | ||||||
|  |    timeclockEntriesToTransactions | ||||||
|  |   ,easytests_Timeclock | ||||||
|  | ) | ||||||
| where | where | ||||||
|  | 
 | ||||||
| import Data.Maybe | import Data.Maybe | ||||||
| -- import Data.Text (Text) | -- import Data.Text (Text) | ||||||
| import qualified Data.Text as T | import qualified Data.Text as T | ||||||
| @ -22,7 +26,7 @@ import System.Locale (defaultTimeLocale) | |||||||
| #endif | #endif | ||||||
| import Text.Printf | import Text.Printf | ||||||
| 
 | 
 | ||||||
| import Hledger.Utils | import Hledger.Utils hiding (is) | ||||||
| import Hledger.Data.Types | import Hledger.Data.Types | ||||||
| import Hledger.Data.Dates | import Hledger.Data.Dates | ||||||
| import Hledger.Data.Amount | import Hledger.Data.Amount | ||||||
| @ -107,12 +111,16 @@ entryFromTimeclockInOut i o | |||||||
|       ps       = [posting{paccount=acctname, pamount=amount, ptype=VirtualPosting, ptransaction=Just t}] |       ps       = [posting{paccount=acctname, pamount=amount, ptype=VirtualPosting, ptransaction=Just t}] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| tests_Hledger_Data_Timeclock = TestList [ | -- tests | ||||||
| 
 | 
 | ||||||
|    "timeclockEntriesToTransactions" ~: do | is :: (Eq a, Show a, HasCallStack) => a -> a -> Test () | ||||||
|      today <- getCurrentDay | is = flip expectEq' | ||||||
|      now' <- getCurrentTime | 
 | ||||||
|      tz <- getCurrentTimeZone | easytests_Timeclock = tests "Timeclock" [ | ||||||
|  |   do | ||||||
|  |    today <- io getCurrentDay | ||||||
|  |    now' <- io getCurrentTime | ||||||
|  |    tz <- io getCurrentTimeZone | ||||||
|    let now = utcToLocalTime tz now' |    let now = utcToLocalTime tz now' | ||||||
|        nowstr = showtime now |        nowstr = showtime now | ||||||
|        yesterday = prevday today |        yesterday = prevday today | ||||||
| @ -124,21 +132,17 @@ tests_Hledger_Data_Timeclock = TestList [ | |||||||
|                   parseTime defaultTimeLocale "%H:%M:%S" |                   parseTime defaultTimeLocale "%H:%M:%S" | ||||||
| #endif | #endif | ||||||
|        showtime = formatTime defaultTimeLocale "%H:%M" |        showtime = formatTime defaultTimeLocale "%H:%M" | ||||||
|          assertEntriesGiveStrings name es ss = assertEqual name ss (map (T.unpack . tdescription) $ timeclockEntriesToTransactions now es) |        txndescs = map (T.unpack . tdescription) . timeclockEntriesToTransactions now  | ||||||
| 
 |        future = utcToLocalTime tz $ addUTCTime 100 now' | ||||||
|      assertEntriesGiveStrings "started yesterday, split session at midnight" |  | ||||||
|                                   [clockin (mktime yesterday "23:00:00") "" ""] |  | ||||||
|                                   ["23:00-23:59","00:00-"++nowstr] |  | ||||||
|      assertEntriesGiveStrings "split multi-day sessions at each midnight" |  | ||||||
|                                   [clockin (mktime (addDays (-2) today) "23:00:00") "" ""] |  | ||||||
|                                   ["23:00-23:59","00:00-23:59","00:00-"++nowstr] |  | ||||||
|      assertEntriesGiveStrings "auto-clock-out if needed" |  | ||||||
|                                   [clockin (mktime today "00:00:00") "" ""] |  | ||||||
|                                   ["00:00-"++nowstr] |  | ||||||
|      let future = utcToLocalTime tz $ addUTCTime 100 now' |  | ||||||
|        futurestr = showtime future |        futurestr = showtime future | ||||||
|      assertEntriesGiveStrings "use the clockin time for auto-clockout if it's in the future" |    tests "timeclockEntriesToTransactions" [  | ||||||
|                                   [clockin future "" ""] |      test "started yesterday, split session at midnight" $ | ||||||
|                                   [printf "%s-%s" futurestr futurestr] |       txndescs [clockin (mktime yesterday "23:00:00") "" ""] `is` ["23:00-23:59","00:00-"++nowstr] | ||||||
| 
 |      ,test "split multi-day sessions at each midnight" $ | ||||||
|  |       txndescs [clockin (mktime (addDays (-2) today) "23:00:00") "" ""] `is `["23:00-23:59","00:00-23:59","00:00-"++nowstr] | ||||||
|  |      ,test "auto-clock-out if needed" $ | ||||||
|  |       txndescs [clockin (mktime today "00:00:00") "" ""] `is` ["00:00-"++nowstr] | ||||||
|  |      ,test "use the clockin time for auto-clockout if it's in the future" $ | ||||||
|  |       txndescs [clockin future "" ""] `is` [printf "%s-%s" futurestr futurestr] | ||||||
|  |      ] | ||||||
|  ] |  ] | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user