tests/testthat/test-transactions.R

context("Transactions")
library(testthat)
library(googlesheets)
library(magrittr)
suppressMessages(library(tidyverse))

sampleTransactionSchema <- "https://docs.google.com/spreadsheets/d/1NKCZu41xrWmI0iSDkiRbOp0_BVl4K8xoNq3E0ud7Ea8/edit?usp=sharing"

test_that("Check Transaction Sheet Schema", {
  skip("Googlesheets is not reachable")
  s <- getGoogleSheetFromUrl (sampleTransactionSchema)
  #There should be no error. The second parameters specifies that there should be no error.
  expect_error(checkSchema(s,initTranSchema),NA)
})

test_that("Check Transform schema", {

  tr1 = createTran(Date="11/26/2017",
                   Store = "Wegman's",
                   ReceiptNo = "3276",
                   Total = "$10.00")
  tr2 = createTran(Date="11/28/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "3333",
                   Total = "$11.10")

  initDs <- dplyr::union_all(tr1,tr2)

  trs <- transformTran(initDs)

  expect_error(checkSchema(trs,clnTranSchema),NA)
})

test_that("Validate Total Amount", {

  #11/15/2017, Trader joes sweet potato should be the only errored
  tr1 = createTran(Date="11/17/2017",
                   Store = "Trader Joe's",
                   Item = "sweet potato",
                   ReceiptNo = "1815",
                   Total = "")
  tr2 = createTran(Date="11/28/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "3333",
                   Total = "$11.10")

  initDs <- dplyr::union_all(tr1,tr2)


  #There should be no error. The second parameters specifies that there should be no error.
  trs <- transformTran(initDs)

  vld <- validateTran(trs) %>% filter(failed)

  tst <- tibble(Store=c("Trader Joe's"),
                    trYearMonth=c("17-Nov"),
                    Item=c("sweet potato"),
                    ReceiptNo=c("1815"))

  expect_equal(nrow(vld), 1)
  expect_identical(vld$Store, tst$Store)
  expect_equal(vld$trYearMonth, tst$trYearMonth)
  expect_equal(vld$Item, tst$Item)
  expect_equal(vld$ReceiptNo, tst$ReceiptNo)


})

test_that("Check Record order",{
  tr1 = createTran(Date="11/26/2017",
                   Store = "Wegman's",
                   ReceiptNo = "3276",
                   Total = "$10.00")
  tr2 = createTran(Date="11/28/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "3333",
                   Total = "$11.10")

  m1 = createMint(BilledDate = "11/26/2017",
                    Establishment = "Wegman's",
                  Amount = "-$10")

  initDs <- dplyr::union_all(tr1,tr2)

  trs <- transformTran(initDs)
  mn <- transformMint(m1)
  sumTran <- getsummaryTran(trs)

  rcn <- getReconTran(sumTran,mn)

  #check if the order is maintained
  expect_equal(rcn$TransactionDate,c("11/28/2017" ,"11/26/2017"))

  #check if the billed date is populated for matched record
  matched <- filter(rcn,TransactionDate=="11/26/2017" &
                    Store == "Wegman's" &
                    ReceiptNo == "3276" &
                      smAmount == -10.00)
  expect_equal(matched$trBilledDateStr,c("11/26/2017"))

  #check that non matched record has NA
  nonMatched <- filter(rcn,TransactionDate=="11/28/2017" &
                      Store == "Trader Joe's" &
                      ReceiptNo == "3333" &
                        smAmount == -11.1)

  expect_equal(is.na(nonMatched$trBilledDateStr[1]),TRUE)
})



test_that("Summary Transaction",{
  tr1 = createTran(Date="11/26/2017",
                   Store = "Wegman's",
                   ReceiptNo = "3276",
                   Item="Carrot",
                   Total = "$10.00")
  tr2 = createTran(Date="11/26/2017",
                   Store = "Wegman's",
                   ReceiptNo = "3276",
                   Item="Mushrooms",
                   Total = "$11.10")

  m1 = createMint(BilledDate = "11/26/2017",
                  Establishment = "Wegman's",
                  Amount = "-$10")

  initDs <- dplyr::union_all(tr1,tr2)

  trs <- transformTran(initDs)
  mn <- transformMint(m1)
  sum <- getsummaryTran(trs)

  #check if the order is maintained
  expect_equal(sum$TransactionDate,c("11/26/2017"))

  #check if the billed date is populated for matched record
  matched <- filter(sum,TransactionDate=="11/26/2017" &
                      Store == "Wegman's" &
                      ReceiptNo == "3276" )

  expect_equal(matched$smAmount,-21.1)

})



test_that("Sum multiple transactions and recon",{
  tr1 = createTran(Date="11/26/2017",
                   Store = "Wegman's",
                   ReceiptNo = "3276",
                   Total = "$10.00")
  tr2 = createTran(Date="11/26/2017",
                   Store = "Wegman's",
                   ReceiptNo = "3276",
                   Total = "$32.00")
  tr3 = createTran(Date="11/28/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "3333",
                   Total = "$11.10")

  m1 = createMint(BilledDate = "11/26/2017",
                  Establishment = "Wegman's",
                  Amount = "-$42")

  #Join all 3 records
  initDs <- dplyr::union_all(dplyr::union_all(tr1,tr2),tr3)


  trs <- transformTran(initDs)
  mn <- transformMint(m1)
  sumTran <- getsummaryTran(trs)

  rcn <- getReconTran(sumTran,mn)
  #check if the order is maintained
  expect_equal(rcn$TransactionDate,c("11/28/2017" ,"11/26/2017"))

  #check if the billed date is populated for matched record
  matched <- filter(rcn,TransactionDate=="11/26/2017" &
                      Store == "Wegman's" &
                      ReceiptNo == "3276" &
                      smAmount == -42.00)
  expect_equal(matched$trBilledDateStr,c("11/26/2017"))

  #check that non matched record has NA
  nonMatched <- filter(rcn,TransactionDate=="11/28/2017" &
                         Store == "Trader Joe's" &
                         ReceiptNo == "3333" &
                         smAmount == -11.1)

  expect_equal(is.na(nonMatched$trBilledDateStr[1]),TRUE)
})

test_that("Check 'all' button in Transaction Recon page",{

  curDate <- Sys.Date()

  #a record less than a week
  ltWeek1 <- format(curDate - 5, "%m/%d/%Y")

  #a record gt than a week but less than 2 weeks
  ltWeek2 <- format(curDate - 9, "%m/%d/%Y")

  # a record older than 2 weeks
  ltWeekOld <- format(curDate - 15, "%m/%d/%Y")

  tr1 = createTran(Date=ltWeek1,
                   Store = "Wegman's",
                   ReceiptNo = "1 week",
                   Total = "$10.00")
  tr2 = createTran(Date=ltWeek1,
                   Store = "Wegman's",
                   ReceiptNo = "1 week",
                   Total = "$32.00")
  tr3 = createTran(Date=ltWeek2,
                   Store = "Trader Joe's",
                   ReceiptNo = "2 week",
                   Total = "$11.10")
  tr4 = createTran(Date=ltWeekOld,
                   Store = "Trader Joe's",
                   ReceiptNo = "old",
                   Total = "$11.10")

  m1 = createMint(BilledDate = "11/26/2017",
                  Establishment = "Wegman's",
                  Amount = "-$42")

  #Join all 3 records
  initDs <- rbind(tr1,tr2,tr3,tr4)


  trs <- transformTran(initDs,'ALL')

  #check if the order is maintained
  expect_equal(trs$trDateStr,c(ltWeek1,ltWeek1, ltWeek2,ltWeekOld))

})

test_that("Check '1 week' button in Transaction Recon page",{

  curDate <- Sys.Date()

  #a record less than a week
  ltWeek1 <- format(curDate - 5, "%m/%d/%Y")

  #a record gt than a week but less than 2 weeks
  ltWeek2 <- format(curDate - 9, "%m/%d/%Y")

  # a record older than 2 weeks
  ltWeekOld <- format(curDate - 15, "%m/%d/%Y")

  tr1 = createTran(Date=ltWeek1,
                   Store = "Wegman's",
                   ReceiptNo = "1 week",
                   Total = "$10.00")
  tr2 = createTran(Date=ltWeek1,
                   Store = "Wegman's",
                   ReceiptNo = "1 week",
                   Total = "$32.00")
  tr3 = createTran(Date=ltWeek2,
                   Store = "Trader Joe's",
                   ReceiptNo = "2 week",
                   Total = "$11.10")
  tr4 = createTran(Date=ltWeekOld,
                   Store = "Trader Joe's",
                   ReceiptNo = "old",
                   Total = "$11.10")

  m1 = createMint(BilledDate = "11/26/2017",
                  Establishment = "Wegman's",
                  Amount = "-$42")

  #Join all 3 records
  initDs <- rbind(tr1,tr2,tr3,tr4)


  trs <- transformTran(initDs,'1')

  #check if the order is maintained
  expect_equal(trs$trDateStr,c(ltWeek1,ltWeek1))

})

test_that("Check '2 week' button in Transaction Recon page",{

  curDate <- Sys.Date()

  #a record less than a week
  ltWeek1 <- format(curDate - 5, "%m/%d/%Y")

  #a record gt than a week but less than 2 weeks
  ltWeek2 <- format(curDate - 9, "%m/%d/%Y")

  # a record older than 2 weeks
  ltWeekOld <- format(curDate - 15, "%m/%d/%Y")

  tr1 = createTran(Date=ltWeek1,
                   Store = "Wegman's",
                   ReceiptNo = "1 week",
                   Total = "$10.00")
  tr2 = createTran(Date=ltWeek1,
                   Store = "Wegman's",
                   ReceiptNo = "1 week",
                   Total = "$32.00")
  tr3 = createTran(Date=ltWeek2,
                   Store = "Trader Joe's",
                   ReceiptNo = "2 week",
                   Total = "$11.10")
  tr4 = createTran(Date=ltWeekOld,
                   Store = "Trader Joe's",
                   ReceiptNo = "old",
                   Total = "$11.10")

  m1 = createMint(BilledDate = "11/26/2017",
                  Establishment = "Wegman's",
                  Amount = "-$42")

  #Join all 3 records
  initDs <- rbind(tr1,tr2,tr3,tr4)


  trs <- transformTran(initDs,'2')


  #check if the order is maintained
  expect_equal(trs$trDateStr,c(ltWeek1,ltWeek1,ltWeek2))

})
ravi9884/PersonalFinance documentation built on May 4, 2019, 6:38 p.m.