Pārlūkot izejas kodu

basic testing funciton and skelton

nas 2 gadus atpakaļ
vecāks
revīzija
89101280ee
1 mainītis faili ar 78 papildinājumiem un 15 dzēšanām
  1. 78
    15
      test/remote/test.pl

+ 78
- 15
test/remote/test.pl Parādīt failu

@@ -1,49 +1,112 @@
1
-#!/usr/bin/perl
2 1
 # @file journal.pl
2
+#!/usr/bin/perl
3 3
 # @author nas
4 4
 # @date 31/01/2022
5 5
 # @brief Remote call to the API in order to test
6 6
 
7 7
 use Config::Std;
8 8
 use LWP::UserAgent ();
9
+use lib qw(..);
10
+use JSON qw( );
11
+use Data::Dumper;
12
+use Term::ANSIColor;
9 13
 
10 14
 read_config 'test.cfg' => my %config; 
11
-print "Username : $config{Authentification}{Username}\n";
12
-print "Host : $config{Authentification}{Host}\n";
13
-print "Host : $config{Authentification}{Path}\n";
14
-print "Protocol : $config{Authentification}{Protocol}\n";
15 15
 
16 16
 local $url = "$config{Authentification}{Protocol}$config{Authentification}{Host}$config{Authentification}{Path}";
17 17
 local $ua = LWP::UserAgent->new(timeout => 10);
18 18
 $ua->env_proxy;
19 19
 
20
+my $count_success = 0;
21
+my $count_failure = 0;
22
+
23
+sub print_color {
24
+  my $color = (exists $_[0])? $_[0] : "";
25
+  my $text = (exists $_[0])? $_[1] : "";
26
+  print color( "$color" );
27
+  print "$text";
28
+  print color('reset')
29
+}
30
+
31
+sub record_result {
32
+  my $status = (exists $_[0])? $_[0] : false;
33
+
34
+  if( $status ){
35
+    print_color( "green", "Success :");
36
+    $count_success++;
37
+  }else{
38
+    print_color( "red", "Failure :");
39
+    $count_failure++;
40
+  }
41
+}
42
+
43
+sub batch_brief {
44
+    print "\nResults :\n";
45
+    print_color( "bright_green", "Tests succeed : $count_success\n" );
46
+    print_color( "bright_red", "Tests failed  : $count_failure\n" );
47
+}
48
+
20 49
 sub get_code {
21 50
   my $path = (exists $_[0])? $_[0] : "";
22 51
   my $response = $ua->get( "$url$path" );
23
-  print "rq $url$path\n";
52
+
24 53
   if ($response->is_success) {
25
-    print $response->decoded_content;
26
-    print "\n";
54
+    return $response->decoded_content;
27 55
   }
28 56
   else {
29
-    print $response->status_line;
30
-    print "\n";
57
+    return $response->status_line;
31 58
   }
32 59
 }
33 60
 
34
-get_code();
35
-get_code( "tokens" );
61
+sub assertEqual {
62
+  my $value1 = (exists $_[0])? $_[0] : "";
63
+  my $value2 = (exists $_[1])? $_[1] : "";
64
+  my $equal = (exists $_[1])? $_[1] : 1;
65
+  my $success = 0;
66
+
67
+  my $test = ( $value1 eq $value2 );
68
+  $test = ( $equal )? $test : !$test;
69
+
70
+  if( $test )
71
+  {
72
+    record_result( 1 );
73
+    print " $value1 ";
74
+    print_color( "bright_blue", "equal");
75
+    print " $value2\n";
76
+  }else{
77
+    record_result( 0 );
78
+    print " $value1 ";
79
+    print_color( "bright_blue", "NOT equal");
80
+    print " $value2\n";
81
+  };
82
+}
83
+
84
+sub assertNotEqual {
85
+    assertEqual( $_[0], $_[1], 0 );
86
+}
36 87
 
37 88
 sub rq {
38 89
   my $path = (exists $_[0])? $_[0] : "";
39 90
   my $method = (exists $_[1])? $_[1] : "";
40 91
   my $content = (exists $_[2])? $_[2] : "";
41 92
 
42
-  my $req = HTTP::Request->new(POST => "$url$path");
93
+  my $req = HTTP::Request->new("$method" => "$url$path");
43 94
   $req->content_type('application/json');
44 95
   $req->content("$content");
45 96
   my $res = $ua->request($req);
46
-  print $res->as_string;
97
+  return $res->decoded_content;
98
+  return $res->as_string;
47 99
 }
48 100
 
49
-rq( "tokens", "GET", "{\"user\" : {\"email\" : \"$config{Authentification}{Email}\", \"password\" : \"$config{Authentification}{Password}\"}}" );
101
+sub json_parse {
102
+  my $json_text = (exists $_[0])? $_[0] : "";
103
+  my $json = JSON->new;
104
+  return $json->decode($json_text);
105
+}
106
+
107
+print "\nMaking all tests\n";
108
+assertEqual( get_code(), "404 Not Found" );
109
+
110
+my $result = json_parse( rq( "tokens", "POST", "{\"user\" : {\"email\" : \"$config{Authentification}{Email}\", \"password\" : \"$config{Authentification}{Password}\"}}" ));
111
+assertEqual( "$config{Authentification}{Email}", $result->{"email"});
112
+batch_brief();

Notiek ielāde…
Atcelt
Saglabāt