Browse Source

re structure test, in object oriented way

nas 2 years ago
parent
commit
97f791aa4b
4 changed files with 176 additions and 96 deletions
  1. 37
    0
      test/remote/NRT/Batch.pm
  2. 13
    0
      test/remote/NRT/Helper.pm
  3. 110
    0
      test/remote/NRT/Test.pm
  4. 16
    96
      test/remote/test.pl

+ 37
- 0
test/remote/NRT/Batch.pm View File

@@ -0,0 +1,37 @@
1
+use NRT::Test;# qw( rq assertEqual);
2
+use NRT::Helper;
3
+#require NRT::Test ;
4
+#require NRT::Helper;
5
+
6
+package Batch;
7
+
8
+sub new {
9
+    $tester = Test::new( %config );
10
+    my ($classe, %config) = @_;
11
+    my $this = {};
12
+    $classe = ref($classe) || $classe;
13
+    bless ( $this, $classe );
14
+    $this->{tester} = Test->new( %config );
15
+    return $this;
16
+}
17
+
18
+sub test_rq_empty {
19
+  my $this = shift;
20
+  my $rq = $this->{tester}->get_code();  
21
+  return $this->{tester}->assertEqual( $rq, "404 Not Found" );
22
+}
23
+
24
+sub run {
25
+  my $this = shift;
26
+  $this->test_rq_empty();
27
+  $this->brief();
28
+}
29
+
30
+sub brief {
31
+  my $this = shift;;
32
+  print "\nResults :\n";
33
+  Helper::printColor( "bright_green", "Tests succeed : ".$this->{tester}->{countSuccess}."\n" );
34
+  Helper::printColor( "bright_red", "Tests failed  : ".$this->{tester}->{countFailure}."\n" );
35
+}
36
+
37
+return 1;

+ 13
- 0
test/remote/NRT/Helper.pm View File

@@ -0,0 +1,13 @@
1
+use Term::ANSIColor;
2
+
3
+package Helper;
4
+
5
+sub printColor {
6
+  my $color = (exists $_[0])? $_[0] : "";
7
+  my $text = (exists $_[0])? $_[1] : "";
8
+  print Term::ANSIColor::color( "$color" );
9
+  print "$text";
10
+  print Term::ANSIColor::color('reset')
11
+}
12
+
13
+return 1;

+ 110
- 0
test/remote/NRT/Test.pm View File

@@ -0,0 +1,110 @@
1
+use NRT::Helper;
2
+
3
+package Test;
4
+require Exporter;
5
+#@ISA = qw(Exporter);
6
+#@EXPORT = qw(get_countSuccess get_countFailure rq assertEqual assertNotEqual get_code);
7
+
8
+sub new {
9
+  my ($classe, %config) = @_;
10
+  my $this = {};
11
+  $classe = ref($classe) || $classe;
12
+  bless ( $this, $classe );
13
+  $this->{url} = "$config{Authentification}{Protocol}$config{Authentification}{Host}$config{Authentification}{Path}";
14
+  $this->{ua} = LWP::UserAgent->new(timeout => 10);
15
+  $this->{ua}->env_proxy;
16
+  $this->{countSuccess} = 0;
17
+  $this->{countFailure} = 0;
18
+  return $this;
19
+}
20
+
21
+sub rq {
22
+  my $path = (exists $_[0])? $_[0] : "";
23
+  my $method = (exists $_[1])? $_[1] : "";
24
+  my $content = (exists $_[2])? $_[2] : "";
25
+
26
+  my $req = HTTP::Request->new("$method" => "$url$path");
27
+  $req->content_type('application/json');
28
+  $req->content("$content");
29
+  my $res = $ua->request($req);
30
+  return $res->decoded_content;
31
+}
32
+
33
+sub assertEqual {
34
+  my ($this, $value1, $value2, $equal ) = @_;
35
+  $equal ||= 1;
36
+  my $success = 0;
37
+
38
+  my $test = ( $value1 eq $value2 );
39
+  $test = ( $equal )? $test : !$test;
40
+
41
+  if( $test )
42
+  {
43
+    $this->record_result( 1 );
44
+    print " $value1 ";
45
+    Helper::printColor( "bright_blue", "equal");
46
+    print " $value2\n";
47
+    return 1;
48
+  }else{
49
+    $this->record_result( 0 );
50
+    print " $value1 ";
51
+    Helper::printColor( "bright_blue", "NOT equal");
52
+    print " $value2\n";
53
+    return 0;
54
+  };
55
+}
56
+
57
+sub assertNotEqual {
58
+  my $this = shift;
59
+  $this->assertEqual( $_[0], $_[1], 0 );
60
+}
61
+
62
+sub get_code {
63
+  my ($this, $path) = @_;
64
+  if(!defined $path){
65
+    $path = "";
66
+    #exit 1;
67
+  }
68
+  my $response = $this->{ua}->get( "$this->{url}$path" );
69
+
70
+  if ($response->is_success) {
71
+    return $response->decoded_content;
72
+  }else{
73
+    return $response->status_line;
74
+  }
75
+}
76
+
77
+sub record_result {
78
+  my $this = shift ;
79
+  my $status = (exists $_[0])? $_[0] : false;
80
+
81
+  if( $status ){
82
+    Helper::printColor( "green", "Success :");
83
+    $this->{countSuccess}++;
84
+  }else{
85
+    Helper::printColor( "red", "Failure :");
86
+    $this->{countFailure}++;
87
+  }
88
+}
89
+
90
+sub json_parse {
91
+  my $json_text = (exists $_[0])? $_[0] : "";
92
+  my $json = JSON->new;
93
+  return $json->decode($json_text);
94
+}
95
+
96
+sub get_countSuccess {
97
+  my $this = shift;
98
+  return $this->{countSuccess} ;
99
+}
100
+
101
+sub get_countFailure{
102
+  return $countFailure ;
103
+}
104
+
105
+sub get_url{
106
+  my $this = shift;
107
+  return $this->{url} ;
108
+}
109
+
110
+return 1;

+ 16
- 96
test/remote/test.pl View File

@@ -1,112 +1,32 @@
1
-# @file journal.pl
2 1
 #!/usr/bin/perl
2
+# @file journal.pl
3 3
 # @author nas
4 4
 # @date 31/01/2022
5 5
 # @brief Remote call to the API in order to test
6
-
6
+BEGIN { push @INC,'.'; };
7 7
 use Config::Std;
8 8
 use LWP::UserAgent ();
9 9
 use lib qw(..);
10 10
 use JSON qw( );
11 11
 use Data::Dumper;
12
-use Term::ANSIColor;
13 12
 
14
-read_config 'test.cfg' => my %config; 
13
+use NRT::User;
14
+use NRT::Batch;
15
+#require NRT::User;
16
+#require NRT::Batch;
15 17
 
16
-local $url = "$config{Authentification}{Protocol}$config{Authentification}{Host}$config{Authentification}{Path}";
18
+print User::v ;
19
+read_config 'test.cfg' => our %config; 
20
+
21
+our $url = "$config{Authentification}{Protocol}$config{Authentification}{Host}$config{Authentification}{Path}";;
17 22
 local $ua = LWP::UserAgent->new(timeout => 10);
18 23
 $ua->env_proxy;
19 24
 
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
-
49
-sub get_code {
50
-  my $path = (exists $_[0])? $_[0] : "";
51
-  my $response = $ua->get( "$url$path" );
52
-
53
-  if ($response->is_success) {
54
-    return $response->decoded_content;
55
-  }
56
-  else {
57
-    return $response->status_line;
58
-  }
59
-}
60
-
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
-}
87
-
88
-sub rq {
89
-  my $path = (exists $_[0])? $_[0] : "";
90
-  my $method = (exists $_[1])? $_[1] : "";
91
-  my $content = (exists $_[2])? $_[2] : "";
92
-
93
-  my $req = HTTP::Request->new("$method" => "$url$path");
94
-  $req->content_type('application/json');
95
-  $req->content("$content");
96
-  my $res = $ua->request($req);
97
-  return $res->decoded_content;
98
-  return $res->as_string;
99
-}
100
-
101
-sub json_parse {
102
-  my $json_text = (exists $_[0])? $_[0] : "";
103
-  my $json = JSON->new;
104
-  return $json->decode($json_text);
105
-}
106
-
25
+our $batch = Batch->new( %config );
107 26
 print "\nMaking all tests\n";
108
-assertEqual( get_code(), "404 Not Found" );
27
+#NRT::Basic::rq_empty;
109 28
 
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();
29
+#Test::init;
30
+#Batch::brief;
31
+#Batch::run();
32
+$batch->run();

Loading…
Cancel
Save