github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/client/scripting_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestEval(t *testing.T) {
     8  	rp, err := r.Eval("return {KEYS[1], KEYS[2], ARGV[1], ARGV[2]}", []string{"key1", "key2"}, []string{"arg1", "arg2"})
     9  	if err != nil {
    10  		t.Error(err)
    11  	} else if l, err := rp.ListValue(); err != nil {
    12  		t.Error(err)
    13  	} else if l[0] != "key1" || l[3] != "arg2" {
    14  		t.Fail()
    15  	}
    16  	rp, err = r.Eval("return redis.call('set','foo','bar')", nil, nil)
    17  	if err != nil {
    18  		t.Error(err)
    19  	} else if err := rp.OKValue(); err != nil {
    20  		t.Error(err)
    21  	}
    22  	rp, err = r.Eval("return 10", nil, nil)
    23  	if err != nil {
    24  		t.Error(err)
    25  	} else if n, err := rp.IntegerValue(); err != nil {
    26  		t.Error(err)
    27  	} else if n != 10 {
    28  		t.Fail()
    29  	}
    30  	rp, err = r.Eval("return {1,2,{3,'Hello World!'}}", nil, nil)
    31  	if err != nil {
    32  		t.Error(err)
    33  	} else if len(rp.Multi) != 3 {
    34  		t.Fail()
    35  	} else if rp.Multi[2].Multi[0].Integer != 3 {
    36  		t.Fail()
    37  	} else if s, err := rp.Multi[2].Multi[1].StringValue(); err != nil || s != "Hello World!" {
    38  		t.Fail()
    39  	}
    40  }
    41  
    42  func TestEvalSha(t *testing.T) {
    43  	r.ScriptFlush()
    44  	sha1, _ := r.ScriptLoad("return 10")
    45  	if rp, err := r.EvalSha(sha1, nil, nil); err != nil {
    46  		t.Error(err)
    47  	} else if rp.Type != IntegerReply {
    48  		t.Fail()
    49  	} else if rp.Integer != 10 {
    50  		t.Fail()
    51  	}
    52  }
    53  
    54  func TestScriptExists(t *testing.T) {
    55  	r.ScriptFlush()
    56  	sha1, _ := r.ScriptLoad("return 10")
    57  	if bs, err := r.ScriptExists(sha1, "sha1"); err != nil {
    58  		t.Error(err)
    59  	} else if len(bs) != 2 {
    60  		t.Fail()
    61  	} else if !bs[0] {
    62  		t.Fail()
    63  	} else if bs[1] {
    64  		t.Fail()
    65  	}
    66  }
    67  
    68  func TestScriptFlush(t *testing.T) {
    69  	sha1, _ := r.ScriptLoad("return 10")
    70  	r.ScriptFlush()
    71  	if bs, err := r.ScriptExists(sha1); err != nil {
    72  		t.Error(err)
    73  	} else if bs[0] {
    74  		t.Fail()
    75  	}
    76  }
    77  
    78  func TestScriptKill(t *testing.T) {
    79  	if err := r.ScriptKill(); err == nil {
    80  		t.Error(err)
    81  	}
    82  }