Total Lines | 66 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package devto |
||
2 | |||
3 | import ( |
||
4 | "context" |
||
5 | "net/http" |
||
6 | "reflect" |
||
7 | "testing" |
||
8 | ) |
||
9 | |||
10 | func TestNewClient(t *testing.T) { |
||
11 | type args struct { |
||
12 | ctx context.Context |
||
13 | config *Config |
||
14 | bc httpClient |
||
15 | } |
||
16 | |||
17 | c, err := NewConfig(true, "dummy") |
||
18 | if err != nil { |
||
19 | t.Fatal(err) |
||
20 | } |
||
21 | |||
22 | tests := []struct { |
||
23 | name string |
||
24 | want Client |
||
25 | wantErr bool |
||
26 | err error |
||
27 | args args |
||
28 | }{ |
||
29 | { |
||
30 | name: "client is build sucessfully", |
||
31 | want: Client{ |
||
32 | Context: context.Background(), |
||
33 | HTTPClient: http.DefaultClient, |
||
34 | Config: c, |
||
35 | }, |
||
36 | wantErr: false, |
||
37 | err: nil, |
||
38 | args: args{ |
||
39 | ctx: nil, |
||
40 | config: c, |
||
41 | bc: nil, |
||
42 | }, |
||
43 | }, |
||
44 | { |
||
45 | name: "client fails if config is nil", |
||
46 | want: Client{}, |
||
47 | wantErr: true, |
||
48 | err: ErrMissingConfig, |
||
49 | args: args{ |
||
50 | ctx: nil, |
||
51 | config: nil, |
||
52 | bc: nil, |
||
53 | }, |
||
54 | }, |
||
55 | } |
||
56 | |||
57 | for _, tt := range tests { |
||
58 | t.Run(tt.name, func(t *testing.T) { |
||
59 | got, err := NewClient(tt.args.ctx, tt.args.config, tt.args.bc) |
||
60 | if tt.wantErr && err != nil { |
||
61 | if !reflect.DeepEqual(err, tt.err) { |
||
62 | t.Errorf("failed on error expectation, got: %v | want %v", err, tt.err) |
||
63 | } |
||
64 | } else { |
||
65 | if !reflect.DeepEqual(tt.want, *got) { |
||
66 | t.Errorf("missmatched expectation, got: %v, want: %v", got, tt.want) |
||
67 | } |
||
72 |