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

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package identifiers
     6  
     7  import (
     8  	"encoding/json"
     9  	"testing"
    10  )
    11  
    12  func TestPointToPointTypeBlock(t *testing.T) {
    13  	point := &Point{Number: 100}
    14  	result := getPointType(point)
    15  
    16  	if result != BlockNumber {
    17  		t.Error("Bad point type returned")
    18  	}
    19  }
    20  
    21  func TestPointToPointTypeSpecial(t *testing.T) {
    22  	point := &Point{Special: "london"}
    23  	result := getPointType(point)
    24  
    25  	if result != BlockSpecial {
    26  		t.Error("Bad point type returned")
    27  	}
    28  }
    29  
    30  func TestPointToPointTypeDate(t *testing.T) {
    31  	point := &Point{Date: "2021-10-03"}
    32  	result := getPointType(point)
    33  
    34  	if result != BlockDate {
    35  		t.Error("Bad point type returned")
    36  	}
    37  }
    38  
    39  func TestModifierToModifierTypeStep(t *testing.T) {
    40  	modifier := &Modifier{Step: 15}
    41  	result := getModifierType(modifier)
    42  
    43  	if result != Step {
    44  		t.Error("Bad modifier type returned")
    45  	}
    46  }
    47  
    48  func TestModifierToModifierTypePeriod(t *testing.T) {
    49  	modifier := &Modifier{Period: "daily"}
    50  	result := getModifierType(modifier)
    51  
    52  	if result != Period {
    53  		t.Error("Bad modifier type returned")
    54  	}
    55  }
    56  
    57  func TestNewBlocks(t *testing.T) {
    58  	br, err := NewBlockRange("10-1000:10")
    59  	if err != nil {
    60  		t.Error(err)
    61  	}
    62  
    63  	if br.StartType != BlockNumber {
    64  		t.Error("StartType is not block number")
    65  	}
    66  
    67  	if br.Start.Number != 10 {
    68  		t.Errorf("Wrong start")
    69  	}
    70  
    71  	if br.EndType != BlockNumber {
    72  		t.Error("EndType is not block number")
    73  	}
    74  
    75  	if br.End.Number != 1000 {
    76  		t.Error("Wrong end")
    77  	}
    78  
    79  	if br.ModifierType != Step {
    80  		t.Error("ModifierType is not step")
    81  	}
    82  
    83  	if br.Modifier.Step != 10 {
    84  		t.Error("Wrong modifier")
    85  	}
    86  }
    87  
    88  func TestNewSpecial(t *testing.T) {
    89  	br, err := NewBlockRange("london:weekly")
    90  
    91  	if err != nil {
    92  		t.Error(err)
    93  	}
    94  
    95  	if br.StartType != BlockSpecial {
    96  		t.Error("StartType is not special")
    97  	}
    98  
    99  	if br.Start.Special != "london" {
   100  		t.Errorf("Wrong start")
   101  	}
   102  
   103  	if br.EndType != NotDefined {
   104  		t.Error("EndType is not notdefined")
   105  	}
   106  
   107  	if br.ModifierType != Period {
   108  		t.Error("ModifierType is not period")
   109  	}
   110  
   111  	if br.Modifier.Period != "weekly" {
   112  		t.Error("Wrong modifier")
   113  	}
   114  }
   115  
   116  func TestHandleParserErrors(t *testing.T) {
   117  	_, modifierErr := NewBlockRange("10-100:biweekly")
   118  
   119  	if me, ok := modifierErr.(*WrongModifierError); ok {
   120  		if me.Token != "biweekly" {
   121  			t.Errorf("Wrong token: %s", me.Token)
   122  		}
   123  	} else {
   124  		t.Error("Returned error is not WrongModifier")
   125  		t.Error(modifierErr)
   126  	}
   127  }
   128  
   129  func TestBlockRange_UnmarshalJSON(t *testing.T) {
   130  	type SomeRecord struct {
   131  		Blocks Identifier `json:"blocks"`
   132  	}
   133  
   134  	var record SomeRecord
   135  	source := []byte(`{"blocks":"000000000-10567003"}`)
   136  
   137  	err := json.Unmarshal(source, &record)
   138  	if err != nil {
   139  		t.Error(err)
   140  	}
   141  
   142  	if record.Blocks.StartType != BlockNumber {
   143  		t.Errorf("Wrong StartType %d", record.Blocks.StartType)
   144  	}
   145  
   146  	if record.Blocks.EndType != BlockNumber {
   147  		t.Errorf("Wrong EndType %d", record.Blocks.EndType)
   148  	}
   149  
   150  	if record.Blocks.Start.Number != uint(0) {
   151  		t.Error("Wrong start value")
   152  	}
   153  
   154  	if record.Blocks.End.Number != uint(10567003) {
   155  		t.Errorf("Wrong end value %d", record.Blocks.End.Number)
   156  	}
   157  }
   158  
   159  func TestToString(t *testing.T) {
   160  	br, err := NewBlockRange("1234")
   161  	if err != nil {
   162  		t.Errorf("Could not parse block")
   163  	}
   164  	expected := `{
   165    "startType": 1,
   166    "start": {
   167      "number": 1234
   168    },
   169    "end": {},
   170    "modifier": {}
   171  }`
   172  	got := br.String()
   173  	if got != expected {
   174  		t.Errorf("String printer for blockRange not equal to expected:\n%s\n%s", got, expected)
   175  	}
   176  }