github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/tslib/tslib_integration_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     5  // Use of this source code is governed by a license that can
     6  // be found in the LICENSE file.
     7  
     8  package tslib
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
    16  )
    17  
    18  type Expected struct {
    19  	name string
    20  	bn   base.Blknum
    21  	ts   base.Timestamp
    22  	date string
    23  }
    24  
    25  func TestLoadTimestampsPass(t *testing.T) {
    26  	expected := []Expected{
    27  		{name: "Block Zero", bn: 1, ts: 1438269988, date: "2015-07-30 15:26:28"},
    28  		{name: "Block 1 Mil", bn: 1000000, ts: 1455404053, date: "2016-02-13 22:54:13"},
    29  	}
    30  
    31  	config.GetRootConfig()
    32  	for _, e := range expected {
    33  		bn, err := FromTsToBn(utils.GetTestChain(), e.ts)
    34  		if err != nil {
    35  			t.Error(err)
    36  		} else if bn != e.bn {
    37  			t.Error("Expected block number", e.bn, "got", bn)
    38  		}
    39  
    40  		bn, err = FromDateToBn(utils.GetTestChain(), e.date)
    41  		if err != nil {
    42  			t.Error(err)
    43  		} else if bn != e.bn {
    44  			t.Error("Expected block number", e.bn, "got", bn)
    45  		}
    46  
    47  		ts, err := FromBnToTs(utils.GetTestChain(), e.bn)
    48  		if err != nil {
    49  			t.Error(err)
    50  		} else if bn != e.bn {
    51  			t.Error("Expected timestamp", e.ts, "got", ts)
    52  		}
    53  
    54  		ts, err = FromDateToTs(e.date)
    55  		if err != nil {
    56  			t.Error(err)
    57  		} else if bn != e.bn {
    58  			t.Error("Expected timestamp", e.ts, "got", ts)
    59  		}
    60  
    61  		d, err := FromBnToDate(utils.GetTestChain(), e.bn)
    62  		s := d.Format("YYYY-MM-DD HH:mm:ss")
    63  		if err != nil {
    64  			t.Error(err)
    65  		} else if s != e.date {
    66  			t.Error("Expected date", e.date, "got", d)
    67  		}
    68  
    69  		d, err = FromTsToDate(e.ts)
    70  		s = d.Format("YYYY-MM-DD HH:mm:ss")
    71  		if err != nil {
    72  			t.Error(err)
    73  		} else if s != e.date {
    74  			t.Error("Expected date", e.date, "got", d)
    75  		}
    76  	}
    77  }
    78  
    79  func TestLoadTimestampsFail(t *testing.T) {
    80  	expected := []Expected{
    81  		{name: "Bad Block", bn: 100000000, ts: 12, date: "2012-02-13 22:54:13"},
    82  	}
    83  
    84  	for _, e := range expected {
    85  		_, err := FromTsToBn(utils.GetTestChain(), e.ts)
    86  		if err == nil {
    87  			t.Error("Expected failure for FromTsToBn")
    88  		}
    89  
    90  		_, err = FromDateToBn(utils.GetTestChain(), e.date)
    91  		if err == nil {
    92  			t.Error("Expected failure for FromDateToBn")
    93  		}
    94  
    95  		_, err = FromBnToTs(utils.GetTestChain(), e.bn)
    96  		if err == nil {
    97  			t.Error("Expected failure for FromBnToTs")
    98  		}
    99  
   100  		// Never fails
   101  		// _, err = FromDateToTs(e.date)
   102  		// if err == nil {
   103  		// 	t.Error("Expected failure for FromDateToTs")
   104  		// }
   105  
   106  		_, err = FromBnToDate(utils.GetTestChain(), e.bn)
   107  		if err == nil {
   108  			t.Error("Expected failure for FromBnToDate")
   109  		}
   110  
   111  		// Never fails
   112  		// _, err = FromTsToDate(e.ts)
   113  		// if err == nil {
   114  		// 	t.Error("Expected failure for FromTsToDate")
   115  		// }
   116  	}
   117  }
   118  
   119  // TODO: Test FromDateToBn with periods of hourly, daily, weekly, monthly, and annually