Skip to content

Commit

Permalink
refactoring: переход на testify в тестах для удобства
Browse files Browse the repository at this point in the history
  • Loading branch information
kuznetsovin committed Jul 13, 2021
1 parent 4ec14da commit 12736fd
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 520 deletions.
50 changes: 23 additions & 27 deletions cli/receiver/config_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package main

import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestConfigLoad(t *testing.T) {
Expand All @@ -26,37 +25,34 @@ exchange = "receiver"
level = "DEBUG"`

file, err := ioutil.TempFile("/tmp", "config.toml")
if err != nil {
t.Fatal(err)
if !assert.NoError(t, err) {
return
}
defer os.Remove(file.Name())

if _, err = file.WriteString(cfg); err != nil {
t.Fatal(err)
if _, err = file.WriteString(cfg); assert.NoError(t, err) {
return
}

conf := settings{}
if err = conf.Load(file.Name()); err != nil {
t.Fatal(err)
}

testCfg := settings{
Srv: service{
Host: "127.0.0.1",
Port: "5020",
ConLiveSec: 10,
if assert.NoError(t, conf.Load(file.Name())) {
assert.Equal(t, settings{
Srv: service{
Host: "127.0.0.1",
Port: "5020",
ConLiveSec: 10,
},
Store: map[string]string{
"exchange": "receiver",
"host": "localhost",
"password": "guest",
"plugin": "rabbitmq.so",
"port": "5672",
"user": "guest",
},
Log: logSection{Level: "DEBUG"},
},
Store: map[string]string{
"exchange": "receiver",
"host": "localhost",
"password": "guest",
"plugin": "rabbitmq.so",
"port": "5672",
"user": "guest",
},
Log: logSection{Level: "DEBUG"},
}
if diff := cmp.Diff(testCfg, conf); diff != "" {
t.Errorf("Записи не совпадают: (-нужно +сейчас)\n%s", diff)
conf,
)
}
}
20 changes: 8 additions & 12 deletions cli/receiver/main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"bytes"
"github.com/stretchr/testify/assert"
"net"
"testing"
"time"
Expand Down Expand Up @@ -32,18 +32,14 @@ func TestServer(t *testing.T) {

time.Sleep(500 * time.Microsecond)
conn, err := net.Dial("tcp", srv)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
if assert.NoError(t, err) {
_ = conn.SetWriteDeadline(time.Now().Add(2 * time.Second))
_, _ = conn.Write(message)

_ = conn.SetWriteDeadline(time.Now().Add(2 * time.Second))
_, _ = conn.Write(message)
buf := make([]byte, 29)
_, _ = conn.Read(buf)

buf := make([]byte, 29)
_, _ = conn.Read(buf)

if !bytes.Equal(buf, response) {
t.Errorf("Ответне совпадает: %X != %X ", buf, response)
assert.Equal(t, buf, response)
}
defer conn.Close()
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.12
require (
github.com/BurntSushi/toml v0.3.1
github.com/golang/protobuf v1.3.5
github.com/google/go-cmp v0.3.0
github.com/kr/pretty v0.1.0 // indirect
github.com/labstack/gommon v0.2.8
github.com/lib/pq v1.1.1
Expand All @@ -15,7 +14,7 @@ require (
github.com/nats-io/nats.go v1.9.2
github.com/satori/go.uuid v1.2.0
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94
github.com/stretchr/testify v1.3.0 // indirect
github.com/stretchr/testify v1.3.0
github.com/tarantool/go-tarantool v0.0.0-20190330192518-aaa93c4bdc35
github.com/valyala/fasttemplate v1.0.1 // indirect
google.golang.org/appengine v1.6.5 // indirect
Expand Down
9 changes: 3 additions & 6 deletions libs/egts/crc_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package egts

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_crc8(t *testing.T) {
crc := crc8([]byte("123456789"))
checkVal := byte(0xF7)

if crc != checkVal {
t.Errorf("Incorrect value: %x != %x\n", crc, checkVal)
}
assert.Equal(t, crc, checkVal)
}

func Test_crc16(t *testing.T) {
crc := crc16([]byte("123456789"))
checkVal := uint16(0x29b1)

if crc != checkVal {
t.Errorf("Incorrect value: %x != %x\n", crc, checkVal)
}
assert.Equal(t, crc, checkVal)
}
127 changes: 21 additions & 106 deletions libs/egts/egts_pkg_test.go
Original file line number Diff line number Diff line change
@@ -1,76 +1,15 @@
package egts

import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

var (
egtsPkgPosDataBytes = []byte{0x01, 0x00, 0x03, 0x0B, 0x00, 0x23, 0x00, 0x8A, 0x00, 0x01, 0x49, 0x18, 0x00, 0x61,
0x00, 0x99, 0xB0, 0x09, 0x02, 0x00, 0x02, 0x02, 0x10, 0x15, 0x00, 0xD5, 0x3F, 0x01, 0x10, 0x6F, 0x1C, 0x05, 0x9E,
0x7A, 0xB5, 0x3C, 0x35, 0x01, 0xD0, 0x87, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x00, 0xCC, 0x27}

egtsPkgPosData = Package{
ProtocolVersion: 1,
SecurityKeyID: 0,
Prefix: "00",
Route: "0",
EncryptionAlg: "00",
Compression: "0",
Priority: "11",
// HeaderLength: 11,
HeaderEncoding: 0,
FrameDataLength: 35,
PacketIdentifier: 138,
PacketType: 1,
HeaderCheckSum: 73,
ServicesFrameData: &ServiceDataSet{
ServiceDataRecord{
// RecordLength: 24,
RecordNumber: 97,
SourceServiceOnDevice: "1",
RecipientServiceOnDevice: "0",
Group: "0",
RecordProcessingPriority: "11",
TimeFieldExists: "0",
EventIDFieldExists: "0",
ObjectIDFieldExists: "1",
ObjectIdentifier: 133552,
SourceServiceType: 2,
RecipientServiceType: 2,
RecordDataSet: RecordDataSet{
RecordData{
SubrecordType: 16,
// SubrecordLength: 21,
SubrecordData: &SrPosData{
NavigationTime: time.Date(2018, time.July, 5, 20, 8, 53, 0, time.UTC),
Latitude: 55.55389399769574,
Longitude: 37.43236696287812,
ALTE: "0",
LOHS: "0",
LAHS: "0",
MV: "0",
BB: "0",
CS: "0",
FIX: "0",
VLD: "1",
DirectionHighestBit: 1,
AltitudeSign: 0,
Speed: 200,
Direction: 172,
Odometer: []byte{0x01, 0x00, 0x00},
DigitalInputs: 0,
Source: 0,
},
},
},
},
},
ServicesFrameDataCheckSum: 10188, //52263
}
)

func TestEgtsPackagePosData_Encode(t *testing.T) {
Expand Down Expand Up @@ -129,12 +68,8 @@ func TestEgtsPackagePosData_Encode(t *testing.T) {
}

posDataBytes, err := egtsPkgPosData.Encode()
if err != nil {
t.Errorf("Ошибка кодирования: %v\n", err)
}

if !bytes.Equal(posDataBytes, egtsPkgPosDataBytes) {
t.Errorf("Байтовые строки не совпадают: %v != %v ", posDataBytes, testEgtsPkgBytes)
if assert.NoError(t, err) {
assert.Equal(t, posDataBytes, egtsPkgPosDataBytes)
}
}

Expand Down Expand Up @@ -199,12 +134,8 @@ func TestEgtsPackagePosData_Decode(t *testing.T) {
ServicesFrameDataCheckSum: 10188,
}

if _, err := egtsPkg.Decode(egtsPkgPosDataBytes); err != nil {
t.Errorf("Ошибка декадирования: %v\n", err)
}

if diff := cmp.Diff(egtsPkg, egtsPkgPosData); diff != "" {
t.Errorf("Записи не совпадают: (-нужно +сейчас)\n%s", diff)
if _, err := egtsPkg.Decode(egtsPkgPosDataBytes); assert.NoError(t, err) {
assert.Equal(t, egtsPkg, egtsPkgPosData)
}
}

Expand Down Expand Up @@ -269,21 +200,14 @@ func TestFullCycleCoding(t *testing.T) {
ServicesFrameDataCheckSum: 10188,
}

if _, err := egtsPkg.Decode(egtsPkgPosDataBytes); err != nil {
t.Errorf("Ошибка декадирования: %v\n", err)
}
_, err := egtsPkg.Decode(egtsPkgPosDataBytes)
if assert.NoError(t, err) {
assert.Equal(t, egtsPkg, egtsPkgPosData)

if diff := cmp.Diff(egtsPkg, egtsPkgPosData); diff != "" {
t.Errorf("Записи не совпадают: (-нужно +сейчас)\n%s", diff)
}

posDataBytes, err := egtsPkg.Encode()
if err != nil {
t.Errorf("Ошибка кодирования: %v\n", err)
}

if !bytes.Equal(posDataBytes, egtsPkgPosDataBytes) {
t.Errorf("Байтовые строки не совпадают: %v != %v ", posDataBytes, testEgtsPkgBytes)
posDataBytes, err := egtsPkg.Encode()
if assert.NoError(t, err) {
assert.Equal(t, posDataBytes, egtsPkgPosDataBytes)
}
}
}

Expand Down Expand Up @@ -364,18 +288,12 @@ func TestRebuildCycleCoding(t *testing.T) {
0x00, 0x00, 0x1b, 0x07, 0x00, 0x41, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xe8}

encodePkg, err := egtsPkg.Encode()
if err != nil {
t.Errorf("Ошибка кодирования: %v\n", err)
}
if assert.NoError(t, err) {
assert.Equal(t, encodePkg, p)

if !bytes.Equal(encodePkg, p) {
t.Errorf("Байтовые строки не совпадают: %v != %v ", encodePkg, p)
}

dp := Package{}
_, err = dp.Decode(encodePkg)
if err != nil {
t.Errorf("Ошибка разбора: %v\n", err)
dp := Package{}
_, err = dp.Decode(encodePkg)
assert.NoError(t, err)
}
}

Expand Down Expand Up @@ -453,13 +371,10 @@ func TestRebuildOID(t *testing.T) {
}

encodePkg, err := egtsPkg.Encode()
if err != nil {
t.Errorf("Ошибка кодирования: %v\n", err)
}
if assert.NoError(t, err) {
p := Package{}

p := Package{}
_, err = p.Decode(encodePkg)
if err != nil {
t.Errorf("Ошибка разбора: %v. Пакет: %v\n", err, encodePkg)
_, err = p.Decode(encodePkg)
assert.NoError(t, err)
}
}
20 changes: 6 additions & 14 deletions libs/egts/egts_pt_response_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package egts

import (
"bytes"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"testing"
)

Expand Down Expand Up @@ -33,23 +32,16 @@ var (
func TestEgtsPkgResp_Encode(t *testing.T) {

posDataBytes, err := egtsPkgResp.Encode()
if err != nil {
t.Errorf("Ошибка кодирования: %v\n", err)
}

if !bytes.Equal(posDataBytes, testEgtsPkgBytes) {
t.Errorf("Байтовые строки не совпадают: %v != %v ", posDataBytes, testEgtsPkgBytes)
if assert.NoError(t, err) {
assert.Equal(t, posDataBytes, testEgtsPkgBytes)
}
}

func TestEgtsPkgResp_Decode(t *testing.T) {
egtsPkg := Package{}

if _, err := egtsPkg.Decode(testEgtsPkgBytes); err != nil {
t.Errorf("Ошибка декадирования: %v\n", err)
}

if diff := cmp.Diff(egtsPkg, egtsPkgResp); diff != "" {
t.Errorf("Записи не совпадают: (-нужно +сейчас)\n%s", diff)
_, err := egtsPkg.Decode(testEgtsPkgBytes)
if assert.NoError(t, err) {
assert.Equal(t, egtsPkg, egtsPkgResp)
}
}
Loading

0 comments on commit 12736fd

Please sign in to comment.