From 42af80b7fe9475a09dbaf8021e81ff6c7eece2ba Mon Sep 17 00:00:00 2001 From: Yakov Permyakov Date: Mon, 13 Jun 2022 12:25:33 +0300 Subject: [PATCH] some test added --- parser/proxy/proxy_test.go | 38 ++++++++++++++++++++ scaper_test.go | 74 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 parser/proxy/proxy_test.go create mode 100644 scaper_test.go diff --git a/parser/proxy/proxy_test.go b/parser/proxy/proxy_test.go new file mode 100644 index 0000000..e8e6ea8 --- /dev/null +++ b/parser/proxy/proxy_test.go @@ -0,0 +1,38 @@ +package proxy + +import "testing" + +func TestProxy_GetAddress(t *testing.T) { + type fields struct { + Ipv4 string + Port int + ProxyType string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Make address as string ip:port", + fields: fields{ + Ipv4: "0.0.0.0", + Port: 1234, + ProxyType: "https", + }, + want: "0.0.0.0:1234", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := Proxy{ + Ipv4: tt.fields.Ipv4, + Port: tt.fields.Port, + ProxyType: tt.fields.ProxyType, + } + if got := p.GetAddress(); got != tt.want { + t.Errorf("GetAddress() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/scaper_test.go b/scaper_test.go new file mode 100644 index 0000000..04e9a29 --- /dev/null +++ b/scaper_test.go @@ -0,0 +1,74 @@ +package proxyscraper + +import ( + "github.com/texnicii/proxy-scraper/parser/proxy" + "reflect" + "testing" +) + +func TestCollectUniq(t *testing.T) { + a := proxy.Proxy{ + Ipv4: "0.0.0.0", + Port: 0, + ProxyType: "0", + } + b := proxy.Proxy{ + Ipv4: "1.0.0.0", + Port: 0, + ProxyType: "0", + } + //is not uniq by ip address with "a" + c := proxy.Proxy{ + Ipv4: "0.0.0.0", + Port: 0, + ProxyType: "1", + } + + type args struct { + inputData []proxy.Proxy + errorCh <-chan error + debug bool + } + tests := []struct { + name string + args args + want map[string]proxy.Proxy + }{ + { + name: "Two uniq elements write to channel", + args: args{ + inputData: []proxy.Proxy{a, b}, + errorCh: nil, + debug: false, + }, + want: map[string]proxy.Proxy{ + "0.0.0.0": a, + "1.0.0.0": b, + }, + }, + { + name: "Two uniq elements write to channel", + args: args{ + inputData: []proxy.Proxy{a, c}, + errorCh: nil, + debug: false, + }, + want: map[string]proxy.Proxy{ + "0.0.0.0": a, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + inputCh := make(chan []proxy.Proxy) + go func() { + inputCh <- tt.args.inputData + close(inputCh) + }() + if got := CollectUniq(inputCh, nil, tt.args.debug); !reflect.DeepEqual(got, tt.want) { + t.Errorf("CollectUniq() = %v, want %v", got, tt.want) + } + }) + } +}