33namespace ProcessWire \GraphQL \Test ;
44
55use ProcessWire \GraphQL \Test \GraphQLTestCase ;
6+ use ProcessWire \GraphQL \Utils ;
67
78class DbQueryCountTest extends GraphQLTestCase {
89
@@ -16,6 +17,7 @@ public static function getSettings()
1617 'architects ' , 'born ' , 'resume ' ,
1718 'height ' , 'floors ' , 'year '
1819 ],
20+ 'maxLimit ' => 1000 ,
1921 ];
2022 }
2123
@@ -24,48 +26,53 @@ public static function getSettings()
2426 *
2527 * @var integer
2628 */
27- public static $ bestQueryCount = 127 ;
29+ public static $ bestQueryCount = 180 ;
2830
2931 /**
3032 * The query we use to test the performance of the module by counting
3133 * the queries to the database.
3234 *
3335 * @var string
3436 */
35- public static $ performanceQuery = '{
36- skyscraper(s: "architects.count>1") {
37- getTotal
38- list{
39- id
40- title
41- height
42- floors
43- year
44- images {
45- url
46- width
37+ public static $ performanceQuery = [
38+ 'query ' => 'query getSkyscrapers($selector: Selector!) {
39+ skyscraper(s: $selector) {
40+ getTotal
41+ list{
42+ id
43+ title
4744 height
48- description
49- }
50- architects{
51- list{
52- id
53- title
54- created
55- born
56- resume{
57- url
58- description
45+ floors
46+ year
47+ images {
48+ url
49+ width
50+ height
51+ description
52+ }
53+ architects{
54+ list{
55+ id
56+ title
57+ created
58+ born
59+ resume{
60+ url
61+ description
62+ }
5963 }
6064 }
6165 }
6266 }
63- }
64- } ' ;
67+ } ' ,
68+ 'variables ' => [
69+ 'selector ' => 'architects.count>1 '
70+ ]
71+ ];
6572
6673 public function testDbQueryCount () {
6774 $ queryCountStart = \ProcessWire \Database::getQueryLog ();
68- self ::execute (self ::$ performanceQuery );
75+ $ res = self ::execute (self ::$ performanceQuery[ ' query ' ], self :: $ performanceQuery [ ' variables ' ] );
6976 $ queryCountEnd = \ProcessWire \Database::getQueryLog ();
7077 $ queryCount = count ($ queryCountEnd ) - count ($ queryCountStart );
7178
@@ -75,8 +82,56 @@ public function testDbQueryCount() {
7582 $ performanceChangeReport = "\nPerformance Improvement \n$ improvementPercent% " ;
7683 \ProcessWire \GraphQL \log ("$ queryCountReport$ performanceChangeReport " );
7784
78- // assert
85+ // assert performance
7986 assertLessThanOrEqual (self ::$ bestQueryCount , $ queryCount );
8087 assertGreaterThanOrEqual (self ::$ bestQueryCount / 5 , $ queryCount , "It can't be true! Performance increased five times! " );
88+
89+ // assert result count
90+ $ skyscrapers = Utils::pages ()->find (self ::$ performanceQuery ['variables ' ]['selector ' ]);
91+ assertEquals ($ skyscrapers ->count (), count ($ res ->data ->skyscraper ->list ), 'Incorrect number of skyscrapers fetched. ' );
92+
93+ // assert valid skyscraper
94+ $ expected = $ skyscrapers ->get ("images.count>1, sort=random " );
95+ assertNotNull ($ expected , 'No expected skyscraper to check. ' );
96+ $ actual = self ::getListItemId ($ res ->data ->skyscraper ->list , $ expected ->id );
97+ assertNotNull ($ actual , 'No actual skyscraper to check. ' );
98+ assertEquals ($ actual ->id , $ expected ->id , 'Incorrect id. ' );
99+ assertEquals ($ actual ->title , $ expected ->title , 'Incorrect title. ' );
100+ assertEquals ($ actual ->height , $ expected ->height , 'Incorrect height. ' );
101+ assertEquals ($ actual ->floors , $ expected ->floors , 'Incorrect floors. ' );
102+ assertEquals ($ actual ->year , $ expected ->year , 'Incorrect year. ' );
103+
104+ // assert valid skyscraper images data
105+ assertEquals (count ($ expected ->images ), count ($ actual ->images ), 'Incorrect images amount. ' );
106+ assertEquals ($ expected ->images ->first ()->url , $ actual ->images [0 ]->url , 'Incorrect image url. ' );
107+ assertEquals ($ expected ->images ->first ()->width , $ actual ->images [0 ]->width , 'Incorrect image width. ' );
108+ assertEquals ($ expected ->images ->first ()->height , $ actual ->images [0 ]->height , 'Incorrect image height. ' );
109+ assertEquals ($ expected ->images ->first ()->description , $ actual ->images [0 ]->description , 'Incorrect image description. ' );
110+
111+ // assert valid skyscraper architects data
112+ $ expected = $ skyscrapers ->find ("architects.count>0 " );
113+ $ arhitectsIds = $ expected ->implode ('| ' , 'architects ' );
114+ $ architectsWithResumes = Utils::pages ()->find ("id= $ arhitectsIds, resume.count>0 " );
115+ $ expected = $ skyscrapers ->get ("architects= $ architectsWithResumes, sort=random " );
116+ assertNotNull ($ expected , 'No expected skyscraper to check. ' );
117+ $ actual = self ::getListItemId ($ res ->data ->skyscraper ->list , $ expected ->id );
118+ assertEquals (count ($ expected ->architects ), count ($ actual ->architects ), 'Incorrect architects amount. ' );
119+ assertEquals ($ expected ->architects [0 ]->id , $ actual ->architects ->list [0 ]->id , 'Incorrect architect id. ' );
120+ assertEquals ($ expected ->architects [0 ]->title , $ actual ->architects ->list [0 ]->title , 'Incorrect architect title. ' );
121+ assertEquals ($ expected ->architects [0 ]->created , $ actual ->architects ->list [0 ]->created , 'Incorrect architect created. ' );
122+ assertEquals ($ expected ->architects [0 ]->born , $ actual ->architects ->list [0 ]->born , 'Incorrect architect born. ' );
123+ assertEquals (count ($ expected ->architects [0 ]->resume ), count ($ actual ->architects ->list [0 ]->resume ), 'Incorrect architects resume amount. ' );
124+ assertEquals ($ expected ->architects [0 ]->resume ->first ()->url , $ actual ->architects ->list [0 ]->resume [0 ]->url , 'Incorrect architect resume url. ' );
125+ assertEquals ($ expected ->architects [0 ]->resume ->first ()->description , $ actual ->architects ->list [0 ]->resume [0 ]->description , 'Incorrect architect resume description. ' );
126+ }
127+
128+ public static function getListItemId ($ list , $ id )
129+ {
130+ foreach ($ list as $ item ) {
131+ if ((int ) $ item ->id === (int ) $ id ) {
132+ return $ item ;
133+ }
134+ }
135+ return null ;
81136 }
82137}
0 commit comments