github.com/hdt3213/godis@v1.2.9/database/geo_test.go (about)

     1  package database
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hdt3213/godis/lib/utils"
     6  	"github.com/hdt3213/godis/redis/protocol"
     7  	"github.com/hdt3213/godis/redis/protocol/asserts"
     8  	"strconv"
     9  	"testing"
    10  )
    11  
    12  func TestGeoHash(t *testing.T) {
    13  	execFlushDB(testDB, utils.ToCmdLine())
    14  	key := utils.RandString(10)
    15  	pos := utils.RandString(10)
    16  	result := execGeoAdd(testDB, utils.ToCmdLine(key, "13.361389", "38.115556", pos))
    17  	asserts.AssertIntReply(t, result, 1)
    18  	result = execGeoHash(testDB, utils.ToCmdLine(key, pos))
    19  	asserts.AssertMultiBulkReply(t, result, []string{"sqc8b49rnys00"})
    20  }
    21  
    22  func TestGeoRadius(t *testing.T) {
    23  	execFlushDB(testDB, utils.ToCmdLine())
    24  	key := utils.RandString(10)
    25  	pos1 := utils.RandString(10)
    26  	pos2 := utils.RandString(10)
    27  	execGeoAdd(testDB, utils.ToCmdLine(key,
    28  		"13.361389", "38.115556", pos1,
    29  		"15.087269", "37.502669", pos2,
    30  	))
    31  	result := execGeoRadius(testDB, utils.ToCmdLine(key, "15", "37", "200", "km"))
    32  	asserts.AssertMultiBulkReplySize(t, result, 2)
    33  }
    34  
    35  func TestGeoRadiusByMember(t *testing.T) {
    36  	execFlushDB(testDB, utils.ToCmdLine())
    37  	key := utils.RandString(10)
    38  	pos1 := utils.RandString(10)
    39  	pos2 := utils.RandString(10)
    40  	pivot := utils.RandString(10)
    41  	execGeoAdd(testDB, utils.ToCmdLine(key,
    42  		"13.361389", "38.115556", pos1,
    43  		"17.087269", "38.502669", pos2,
    44  		"13.583333", "37.316667", pivot,
    45  	))
    46  	result := execGeoRadiusByMember(testDB, utils.ToCmdLine(key, pivot, "100", "km"))
    47  	asserts.AssertMultiBulkReplySize(t, result, 2)
    48  }
    49  
    50  func TestGeoPos(t *testing.T) {
    51  	execFlushDB(testDB, utils.ToCmdLine())
    52  	key := utils.RandString(10)
    53  	pos1 := utils.RandString(10)
    54  	pos2 := utils.RandString(10)
    55  	execGeoAdd(testDB, utils.ToCmdLine(key,
    56  		"13.361389", "38.115556", pos1,
    57  	))
    58  	result := execGeoPos(testDB, utils.ToCmdLine(key, pos1, pos2))
    59  	expected := "*2\r\n*2\r\n$18\r\n13.361386698670685\r\n$17\r\n38.11555536696687\r\n*0\r\n"
    60  	if string(result.ToBytes()) != expected {
    61  		t.Error("test failed")
    62  	}
    63  }
    64  
    65  func TestGeoDist(t *testing.T) {
    66  	execFlushDB(testDB, utils.ToCmdLine())
    67  	key := utils.RandString(10)
    68  	pos1 := utils.RandString(10)
    69  	pos2 := utils.RandString(10)
    70  	execGeoAdd(testDB, utils.ToCmdLine(key,
    71  		"13.361389", "38.115556", pos1,
    72  		"15.087269", "37.502669", pos2,
    73  	))
    74  	result := execGeoDist(testDB, utils.ToCmdLine(key, pos1, pos2, "km"))
    75  	bulkReply, ok := result.(*protocol.BulkReply)
    76  	if !ok {
    77  		t.Error(fmt.Sprintf("expected bulk protocol, actually %s", result.ToBytes()))
    78  		return
    79  	}
    80  	dist, err := strconv.ParseFloat(string(bulkReply.Arg), 10)
    81  	if err != nil {
    82  		t.Error(err)
    83  		return
    84  	}
    85  	if dist < 166.274 || dist > 166.275 {
    86  		t.Errorf("expected 166.274, actual: %f", dist)
    87  	}
    88  
    89  	result = execGeoDist(testDB, utils.ToCmdLine(key, pos1, pos2, "m"))
    90  	bulkReply, ok = result.(*protocol.BulkReply)
    91  	if !ok {
    92  		t.Error(fmt.Sprintf("expected bulk protocol, actually %s", result.ToBytes()))
    93  		return
    94  	}
    95  	dist, err = strconv.ParseFloat(string(bulkReply.Arg), 10)
    96  	if err != nil {
    97  		t.Error(err)
    98  		return
    99  	}
   100  	if dist < 166274 || dist > 166275 {
   101  		t.Errorf("expected 166274, actual: %f", dist)
   102  	}
   103  }