github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/chain/link_test.go (about)

     1  //@author wuyong
     2  //@date   2020/7/7
     3  //@desc
     4  
     5  package chain
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  type PreloadBaseData struct {
    13  	StrategyType int
    14  	StrategyId   int64
    15  	CourseId     int64
    16  	LessonId     int64
    17  	LecturerUid  int64
    18  }
    19  
    20  type PreloadRoomData struct {
    21  	*PreloadBaseData
    22  	LiveRoomId int64
    23  }
    24  
    25  type PreloadOrgData struct {
    26  	*PreloadBaseData
    27  	NodeId int64
    28  }
    29  
    30  type PreloadUserData struct {
    31  	*PreloadRoomData
    32  	*PreloadOrgData
    33  	Uid int64
    34  }
    35  
    36  func (data *PreloadBaseData) ValidateData() (ok bool) {
    37  
    38  	return true
    39  }
    40  
    41  func (data *PreloadBaseData) Process(input IParam) (err error, output IParam) {
    42  	var ok bool
    43  	if data, ok = input.(*PreloadBaseData); !ok {
    44  		return fmt.Errorf("preload init data: %v fail~! input data: %v", data, input), input
    45  	}
    46  
    47  	if !data.ValidateData() {
    48  		return fmt.Errorf("preload base data: %v validate fail~! input data: %v", data, input), input
    49  	}
    50  
    51  	output = &PreloadRoomData{
    52  		PreloadBaseData: data,
    53  		LiveRoomId:      100000,
    54  	}
    55  
    56  	fmt.Printf("preload base data: %v validate ok~! input data: %v output data: %v \n", data, input, output)
    57  
    58  	return
    59  }
    60  
    61  func (data *PreloadRoomData) ValidateData() (ok bool) {
    62  
    63  	return true
    64  }
    65  
    66  func (data *PreloadRoomData) Process(input IParam) (err error, output IParam) {
    67  	var ok bool
    68  	if data, ok = input.(*PreloadRoomData); !ok {
    69  		return fmt.Errorf("preload init data: %v fail~! input data: %v", data, input), input
    70  	}
    71  
    72  	if !data.ValidateData() {
    73  		return fmt.Errorf("room data: %v validate fail~! input data: %v", data, input), input
    74  	}
    75  
    76  	output = &PreloadOrgData{
    77  		PreloadBaseData: data.PreloadBaseData,
    78  		NodeId:          111111,
    79  	}
    80  
    81  	fmt.Printf("preload room data: %v validate ok~! input data: %v output data: %v \n", data, input, output)
    82  
    83  	return
    84  }
    85  
    86  func (data *PreloadOrgData) ValidateData() (ok bool) {
    87  
    88  	return true
    89  }
    90  
    91  func (data *PreloadOrgData) Process(input IParam) (err error, output IParam) {
    92  	var ok bool
    93  	if data, ok = input.(*PreloadOrgData); !ok {
    94  		return fmt.Errorf("preload init data: %v fail~! input data: %v", data, input), input
    95  	}
    96  
    97  	if !data.ValidateData() {
    98  		return fmt.Errorf("org data: %v validate fail~! input data: %v", data, input), input
    99  	}
   100  
   101  	output = &PreloadUserData{
   102  		PreloadRoomData: &PreloadRoomData{
   103  			PreloadBaseData: data.PreloadBaseData,
   104  			LiveRoomId:      100000,
   105  		},
   106  		PreloadOrgData: &PreloadOrgData{
   107  			PreloadBaseData: data.PreloadBaseData,
   108  			NodeId:          111111,
   109  		},
   110  		Uid: 1213123123,
   111  	}
   112  
   113  	fmt.Printf("preload org data: %v validate ok~! input data: %v output data: %v \n", data, input, output)
   114  
   115  	return
   116  }
   117  
   118  func (data *PreloadUserData) ValidateData() (ok bool) {
   119  
   120  	return true
   121  }
   122  
   123  func (data *PreloadUserData) Process(input IParam) (err error, output IParam) {
   124  	var ok bool
   125  	if data, ok = input.(*PreloadUserData); !ok {
   126  		return fmt.Errorf("preload init data: %v fail~! input data: %v", data, input), input
   127  	}
   128  
   129  	if !data.ValidateData() {
   130  		return fmt.Errorf("user data: %v validate fail~! input data: %v", data, input), input
   131  	}
   132  	output = input
   133  
   134  	fmt.Printf("preload user data: %v validate ok~! input data: %v output data: %v \n", data, input, output)
   135  
   136  	return
   137  }
   138  
   139  func TestMain(m *testing.M) {
   140  	m.Run()
   141  }
   142  
   143  func TestLinkHandle(t *testing.T) {
   144  	baseData := &PreloadBaseData{StrategyType: 1, StrategyId: 2, CourseId: 1, LessonId: 2, LecturerUid: 3}
   145  	linkHeadItem := NewLinkItem(false, "base", baseData)
   146  	link := InitLink(linkHeadItem)
   147  	t.Logf("link:%v linkItem:%v", link, linkHeadItem)
   148  
   149  	roomData := &PreloadRoomData{baseData, 10}
   150  	linkItem := NewLinkItem(true, "room", roomData)
   151  	link.SetNextItem(linkItem)
   152  	t.Logf("link:%v linkItem:%v", link, linkItem)
   153  
   154  	orgData := &PreloadOrgData{baseData, 110}
   155  	linkItem = NewLinkItem(false, "org", orgData)
   156  	link.SetNextItem(linkItem)
   157  	t.Logf("link:%v linkItem:%v", link, linkItem)
   158  
   159  	userData := &PreloadUserData{roomData, orgData, 123}
   160  	linkItem = NewLinkItem(true, "user", userData)
   161  	link.SetNextItem(linkItem)
   162  	t.Logf("link:%v linkItem:%v", link, linkItem)
   163  
   164  	err := link.CheckSameName()
   165  	if err != nil {
   166  		t.Errorf("error: %v", err)
   167  	}
   168  
   169  	err = link.Handle(baseData)
   170  	t.Logf("linkProcess cost %s", link.FormatCost())
   171  	if err != nil {
   172  		t.Errorf("error: %v", err)
   173  	}
   174  
   175  	return
   176  }