tests/testthat/test-mint.R

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

mintSampleUrl <- "https://docs.google.com/spreadsheets/d/1IIOVUQP6y3hgt-htvlwSA4mkt-kCJ8z9n-k2iqPEHpM/edit?usp=sharing"


test_that("Check Transaction Sheet Schema", {

  skip("google sheets not reachable")
  #There should be no error. The second parameters specifies that there should be no error.
  mintSampleDS <- getGoogleSheetFromUrl (mintSampleUrl)
  expect_error(checkSchema(mintSampleDS,initMintSchema),NA)
})

test_that("Check Transaction Sheet wrong schema", {

  skip("google sheets not reachable")
  mintSampleDS <- getGoogleSheetFromUrl (mintSampleUrl)

  #There should be no error. The second parameters specifies that there should be no error.
  expect_error(checkSchema(mintSampleDS,initTranSchema))
})


test_that("Check Transaction Sheet transaformations", {

  m1 = createMint()

  #There should be no error. The second parameters specifies that there should be no error
  trs <- transformMint(m1)
  expect_error(checkSchema(trs,clnMintSchema),NA)
})

test_that("Amount should always have a valid value", {

  m1 = createMint(Amount = "-$100.00")

  #There should be no error. The second parameters specifies that there should be no error
  trs <- transformMint(m1)
  expect_equal(trs$clnAmount,c(-100))

  #validatedDS <- validateMint(trs)
  #expect_error(validatedDS$,NA)
})


test_that("Test inital sample mint sheet download", {
  skip("Get init uses actual sheet, so skipping this")
  #There should be no error. The second parameters specifies that there should be no error.
  expect_error(getMintInit(),NA)
})


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")
  m2 = createMint(BilledDate = "11/28/2017",
                  Establishment = "Trader Joe's",
                  Amount = "-$81")

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

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

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

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

  #check that non matched record has NA
  nonMatched <- filter(rcn,trBilledDateStr=="11/28/2017" &
                         Establishment == "Trader Joe's" &
                         clnAmount == -81.00)

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


test_that("Floating point transactions",{
  tr1 = createTran(Date="12/06/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "5704",
                   Item = "Sweet Potato",
                   Total = "$1.69")
  tr2 = createTran(Date="12/06/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "5704",
                   Item = "Squash Zucchini",
                   Total = "$2.29")
  tr3 = createTran(Date="12/06/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "5704",
                   Item = "Organic chicken tenders",
                   Total = "$9.74")
  tr4 = createTran(Date="12/06/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "5704",
                   Item = "Sockeye Salmon",
                   Total = "$7.25")
  tr5 = createTran(Date="12/06/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "5704",
                   Item = "Sockeye Salmon",
                   Total = "$8.02")
  tr6 = createTran(Date="12/06/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "5704",
                   Item = "Petite Peas",
                   Total = "$1.29")
  tr7 = createTran(Date="12/06/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "5704",
                   Item = "Brussels",
                   Total = "$3.79")
  tr8 = createTran(Date="12/06/2017",
                   Store = "Trader Joe's",
                   ReceiptNo = "5704",
                   Item = "Chicken breast drumstick raisin strips",
                   Total = "$18.63")

  m1 = createMint(BilledDate = "12/05/2017",
                  Establishment = "Trader Joe's",
                  Amount = "–$52.70")
  m2 = createMint(BilledDate = "12/05/2017",
                  Establishment = "Trader Joe's",
                  Amount = "-$81")

  #Join all 3 records
  initDs <- rbind(tr1,tr2,tr3,tr4,tr5,tr6,tr7,tr8)
  initMint <- dplyr::union_all(m1,m2)

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

  rcn <- getReconMint(sumTran,mn)

  #check if the order is maintained
  expect_equal(rcn$trBilledDateStr,c("12/05/2017","12/05/2017"))

  #check if the billed date is populated for matched record
  matched <- filter(rcn,trBilledDateStr=="12/05/2017" &
                      Establishment == "Trader Joe's" &
                      as.character(clnAmount) == "-52.7")
  expect_equal(matched$TransactionDate,c("12/06/2017"))
  expect_equal(matched$ReceiptNo,c("5704"))

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