VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "s_DatabaseManager"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ====== Test data access ======
Option Explicit

' TODO:
' Public Function PromptNewFileName(target&, theDB As InfoDatabase) As String
' Public Function UpdateStats()
' Public Function Output2Excel(outWB As Excel.Workbook, theDB As InfoDatabase)
' Public Function UpdateHyperlinks(target As Excel.Worksheet)

Private db_ As InfoDatabase

Public Function Setup()
  ' Mandatory setup function
  Dim iPath As New API_Path: Call iPath.FromString(TEST_FOLDER).ToGlobal(ThisWorkbook.Path)
  Set db_ = New InfoDatabase
  Call db_.Init(ThisWorkbook, iPath.Text)
End Function

Public Function Teardown()
  ' Mandatory teardown function
  Call ClearAll
End Function

Public Function t_ClearAll()
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("Empty DB")
  On Error Resume Next
  Call ClearAll
  Call Dev_ExpectNoError
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("Valid clear")
  ThisWorkbook.Names(CELL_STAT_BASE).RefersToRange = 100
  Dim sDocs$: sDocs = GetDocumentsLocation(ThisWorkbook)
  Call SetDocumentsLocation("test")
  ThisWorkbook.Sheets(HT03_SHT_DOCS).Cells(2, 2) = "123"
  ThisWorkbook.Sheets(HT03_SHT_LINKS).Cells(2, 2) = "123"
  
  Call ClearAll
  Call Dev_ExpectEQ(0, ThisWorkbook.Names(CELL_STAT_BASE).RefersToRange.Value, "Stats")
  Call Dev_ExpectEQ(sDocs, GetDocumentsLocation(ThisWorkbook), "4 location")
  Call Dev_ExpectEQ("", ThisWorkbook.Sheets(HT03_SHT_DOCS).Cells(2, 2).Text, "Documents sheet")
  Call Dev_ExpectEQ("", ThisWorkbook.Sheets(HT03_SHT_LINKS).Cells(2, 2).Text, "Links sheet")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_SetDocumentsLocation()
  On Error GoTo PROPAGATE_ERROR
  
  Dim iPath As New API_Path: Call iPath.FromString(TEST_FOLDER).ToGlobal(ThisWorkbook.Path)
  Dim defaultDB As InfoDatabase: Set defaultDB = AccessDatabase
  Call Dev_ExpectEQ(GetDocumentsLocation(ThisWorkbook), defaultDB.docsLocation_, "Default location")
  Call Dev_ExpectEQ(iPath.Text, db_.docsLocation_, "Init location")
  
  Call Dev_NewCase("Set empty location")
  Call SetDocumentsLocation("")
  Call Dev_ExpectFalse(defaultDB.NeedUpdate)
  Call Dev_ExpectEQ(ThisWorkbook.Path, defaultDB.docsLocation_)
  
  Call Dev_NewCase("Set relative location")
  Dim sLocal$: sLocal = "test1"
  Call SetDocumentsLocation(sLocal)
  Call iPath.FromString(sLocal).ToGlobal(ThisWorkbook.Path)
  Call Dev_ExpectEQ(iPath.Text, defaultDB.docsLocation_)
  
  Call Dev_NewCase("Set absolute location")
  Dim sGlobal$: sGlobal = "D:\DEV\testLocation"
  Call SetDocumentsLocation(sGlobal)
  Call Dev_ExpectEQ(sGlobal, defaultDB.docsLocation_)
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_GetIndexDump()
  On Error GoTo PROPAGATE_ERROR

  Dim resArr() As Long: resArr = GetIndexDump()
  Call Dev_ExpectFalse(IsArrayAllocated(resArr), "Empty DB")
  
  Call Dev_NewCase("Valid index dump")
  Dim item1 As ItemLaw: Set item1 = GetDummyData
  Dim item2 As ItemLaw: Set item2 = GetDummyData
  If item1.gid_ = item2.gid_ Then _
    item2.gid_ = item2.gid_ + 1
  item1.status_ = T_LAWS_IGNORE
  item2.status_ = T_LAWS_FULL
  item1.type_ = T_LAW_01_UNKNOWN
  item2.type_ = T_LAW_04_FEDERAL_CONST
  Call db_.SetItem(item1)
  Call db_.SetItem(item2)
  
  resArr = GetIndexDump()
  Call Dev_AssertTrue(IsArrayAllocated(resArr))
  Call Dev_AssertEQ(2, ArraySize(resArr, 1))
  Call Dev_AssertEQ(LawCacheStruct.[_Last], UBound(resArr, 2))
  Call Dev_AssertEQ(LawCacheStruct.[_First], LBound(resArr, 2))
  Call Dev_ExpectEQ(item1.gid_, resArr(1, S_CD_ID))
  Call Dev_ExpectEQ(item1.type_, resArr(1, S_CD_TYPE))
  Call Dev_ExpectEQ(item1.status_, resArr(1, S_CD_STATUS))
  Call Dev_ExpectEQ(item2.gid_, resArr(2, S_CD_ID))
  Call Dev_ExpectEQ(item2.type_, resArr(2, S_CD_TYPE))
  Call Dev_ExpectEQ(item2.status_, resArr(2, S_CD_STATUS))

  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function
  
Public Function t_GetLinksDump()
  On Error GoTo PROPAGATE_ERROR

  Call Dev_ExpectFalse(IsArrayAllocated(GetLinksDump(123)), "Empty DB")
  
  Dim item1 As ItemLaw: Set item1 = GetDummyData
  Dim gid&: gid = item1.gid_
  Call db_.SetItem(item1)
  Call Dev_ExpectFalse(IsArrayAllocated(GetLinksDump(gid + 1)), "Invalid ID")
  Call Dev_ExpectFalse(IsArrayAllocated(GetLinksDump(gid)), "No links")
  
  Dim lnks() As String: lnks = GenerateLinks
  Call db_.SetLinksFor(gid, lnks)
  db_.LawLinks(gid).LType = T_LINK_ASPECT
  db_.LawLinks(gid).Status = T_LINKS_IGNORE
  
  Call Dev_NewCase("Valid links dump")
  Dim resArr() As Long: resArr = GetLinksDump(gid)
  Call Dev_AssertTrue(IsArrayAllocated(resArr))
  Call Dev_AssertEQ(ArraySize(lnks, 1), ArraySize(resArr, 1))
  Call Dev_AssertEQ(LinkCacheStruct.[_Last], UBound(resArr, 2))
  Call Dev_AssertEQ(LinkCacheStruct.[_First], LBound(resArr, 2))
  Call Dev_ExpectEQ(db_.LawLinks(gid).DestinationID, resArr(1, S_CL_DESTINATION_ID))
  Call Dev_ExpectEQ(T_LINK_ASPECT, resArr(1, S_CL_TYPE))
  Call Dev_ExpectEQ(T_LINKS_IGNORE, resArr(1, S_CL_STATUS))
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_SetLinksFor()
  On Error GoTo PROPAGATE_ERROR
  
  Dim docData As New ItemLaw: Set docData = GetDummyData
  Dim gid&: gid = docData.gid_
  Call db_.SetItem(docData)
  
  Dim lnks() As String: lnks = GenerateLinks
  Call SetLinksFor(gid, lnks)
  Call Dev_ExpectEQ(ArraySize(lnks, 1), db_.CountLinks, "Valid add links")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function
