VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "s_ActiveStateExporter"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ===== Test suite for InfoDatabase =======
Option Explicit

Private db_ As InfoDatabase
Private xl_ As API_XLWrapper
Private out_ As Excel.Workbook
Private exporter_ As ActiveStateExporter

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)
  Set exporter_ = New ActiveStateExporter
  Set xl_ = New API_XLWrapper: Call xl_.SetApplication(ThisWorkbook.Application)
  Set out_ = xl_.NewDocument
End Function

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

Public Function t_Init()
  On Error GoTo PROPAGATE_ERROR
  
  Call exporter_.Init(out_, db_)
  Call Dev_ExpectEQ(2, out_.Sheets.Count, "Sheets count")
  Call Dev_ExpectEQ("Документы", out_.Sheets(1).Name, "Laws sheet")
  Call Dev_ExpectEQ("ID", out_.Sheets(1).Cells(1, S_OD_ID), "Laws sheet")
  Call Dev_ExpectEQ("Связи", out_.Sheets(2).Name, "Links sheet")
  Call Dev_ExpectEQ("Source", out_.Sheets(2).Cells(1, S_OL_SOURCE), "Links sheet")

  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_ExportLaws()
  On Error GoTo PROPAGATE_ERROR
  
  Call exporter_.Init(out_, db_)
  
  Call Dev_NewCase("Empty DB")
  Dim iLaws As Scripting.Dictionary: Set iLaws = exporter_.ExportLaws
  Call Dev_ExpectEQ(0, iLaws.Count)
  Call Dev_ExpectEQ(vbNullString, out_.Sheets(1).Cells(2, S_OD_ID))
  
  Call Dev_NewCase("Valid laws")
  Dim item1 As New ItemLaw: Set item1 = GetDummyData
  Dim item2 As New ItemLaw: Set item2 = GetDummyData
  item2.gid_ = item1.gid_ + 1
  Call db_.SetItem(item1.Clone)
  Call db_.SetItem(item2.Clone)
  Set iLaws = exporter_.ExportLaws
  
  Call Dev_ExpectEQ(1, out_.Sheets(1).Cells(2, S_OD_ID))
  Call Dev_ExpectEQ(DefaultLawLevelFor(item1.type_), out_.Sheets(1).Cells(2, S_OD_LEVEL))
  Call Dev_ExpectEQ(item1.title_, out_.Sheets(1).Cells(2, S_OD_TITLE))
  Call Dev_ExpectEQ(item1.type_, out_.Sheets(1).Cells(2, S_OD_TYPE))
  Call Dev_ExpectEQ(item1.gid_, out_.Sheets(1).Cells(2, S_OD_GID))
  
  Call Dev_ExpectEQ(2, out_.Sheets(1).Cells(3, S_OD_ID))
  Call Dev_ExpectEQ(DefaultLawLevelFor(item1.type_), out_.Sheets(1).Cells(3, S_OD_LEVEL))
  Call Dev_ExpectEQ(item2.title_, out_.Sheets(1).Cells(3, S_OD_TITLE))
  Call Dev_ExpectEQ(item2.type_, out_.Sheets(1).Cells(3, S_OD_TYPE))
  Call Dev_ExpectEQ(item2.gid_, out_.Sheets(1).Cells(3, S_OD_GID))

  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_ExportLinks()
  On Error GoTo PROPAGATE_ERROR
  
  Call exporter_.Init(out_, db_)

  Call Dev_NewCase("Empty DB")
  Dim iLaws As Scripting.Dictionary: Set iLaws = exporter_.ExportLaws
  Call exporter_.ExportLinks(iLaws)
  Call Dev_ExpectEQ(vbNullString, out_.Sheets(2).Cells(2, S_OL_SOURCE))
  
  Call Dev_NewCase("No links")
  Dim item1 As New ItemLaw: Set item1 = GetDummyData
  Dim item2 As New ItemLaw: Set item2 = GetDummyData
  item2.gid_ = item1.gid_ + 1
  Call db_.SetItem(item1.Clone)
  Call db_.SetItem(item2.Clone)
  Set iLaws = exporter_.ExportLaws
  Call exporter_.ExportLinks(iLaws)
  Call Dev_ExpectEQ(vbNullString, out_.Sheets(2).Cells(2, S_OL_SOURCE))
  
  Call Dev_NewCase("Self link")
  Dim iLink As IteratorLinkAll: Set iLink = db_.Links
  Call iLink.GotoInsertion
  iLink.SourceID = item1.gid_
  iLink.DestinationID = item1.gid_
  Call exporter_.ExportLinks(iLaws)
  Call Dev_ExpectEQ(vbNullString, out_.Sheets(2).Cells(2, S_OL_SOURCE))
  
  Call Dev_NewCase("Valid link")
  iLink.DestinationID = item2.gid_
  iLink.LType = T_LINK_ASPECT
  iLink.Status = T_LINKS_ADD
  Call exporter_.ExportLinks(iLaws)
  Call Dev_ExpectEQ(1, out_.Sheets(2).Cells(2, S_OL_SOURCE))
  Call Dev_ExpectEQ(2, out_.Sheets(2).Cells(2, S_OL_TARGET))
  Call Dev_ExpectEQ(T_LINK_ASPECT, out_.Sheets(2).Cells(2, S_OL_TYPE))
  
  Call Dev_NewCase("Invalid laws")
  out_.Sheets(2).Cells(2, S_OL_SOURCE) = vbNullString
  Call exporter_.ExportLinks(New Scripting.Dictionary)
  Call Dev_ExpectEQ(vbNullString, out_.Sheets(2).Cells(2, S_OL_SOURCE))
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function
