@@ -42,7 +42,7 @@ public function testSuccessfullyAddRecords() {
4242 ]);
4343 }
4444
45- public function testDoesNotAddDuplicateRecordsWithOnlyDateChange () {
45+ public function testNoDuplicateRecordsWithOnlyDateChange () {
4646 $ wiki = Wiki::factory ()->create ([
4747 'domain ' => 'thisfake.wikibase.cloud ' ,
4848 ]);
@@ -58,45 +58,110 @@ public function testDoesNotAddDuplicateRecordsWithOnlyDateChange() {
5858 'pages ' => 0 ,
5959 'is_deleted ' => 0 ,
6060 ]);
61+
62+ // Run saveMetrics()
6163 (new WikiMetrics )->saveMetrics ($ wiki );
6264
63- // Assert No new record was created for today
65+ // Assert no new record was created for today
6466 $ this ->assertDatabaseMissing ('wiki_daily_metrics ' , [
6567 'wiki_id ' => $ wiki ->id ,
6668 'date ' => Carbon::today ()->toDateString (),
6769 ]);
6870 }
6971
70- public function testAddRecordsWikiIsDeleted () {
72+ public function testRecordCreatedWhenWikiFirstDeleted () {
7173 $ wiki = Wiki::factory ()->create ([
7274 'domain ' => 'thisfake.wikibase.cloud ' ,
7375 ]);
7476
7577 $ wikiDb = WikiDb::first ();
7678 $ wikiDb ->update (['wiki_id ' => $ wiki ->id ]);
7779
80+ $ wikiSiteStats = $ wiki ->wikiSiteStats ()->create ();
81+ $ wikiSiteStats ->update ([
82+ 'pages ' => 10 ,
83+ 'users ' => 5 ,
84+ ]);
85+
7886 // Insert an old metric value for a wiki
7987 WikiDailyMetrics::create ([
8088 'id ' => $ wiki ->id . '_ ' . Carbon::yesterday ()->toDateString (),
8189 'wiki_id ' => $ wiki ->id ,
8290 'date ' => Carbon::yesterday ()->toDateString (),
8391 'pages ' => 0 ,
92+ 'is_deleted ' => 0 ,
93+ 'total_user_count ' => null ,
94+ ]);
95+
96+ // Delete the wiki
97+ $ wiki ->delete ();
98+ $ wiki ->save ();
99+
100+ // Run saveMetrics()
101+ (new WikiMetrics )->saveMetrics ($ wiki );
102+
103+ // Assert new record was created for newly deleted wiki
104+ $ this ->assertDatabaseHas ('wiki_daily_metrics ' , [
105+ 'wiki_id ' => $ wiki ->id ,
84106 'is_deleted ' => 1 ,
107+ 'total_user_count ' => 5 ,
108+ 'date ' => now ()->toDateString (),
85109 ]);
86- // delete the wiki
110+ }
111+
112+ public function testNoDuplicateRecordsForDeletedWiki () {
113+ $ wiki = Wiki::factory ()->create ([
114+ 'domain ' => 'thisfake.wikibase.cloud ' ,
115+ ]);
116+ $ wikiDb = WikiDb::first ();
117+ $ wikiDb ->update (['wiki_id ' => $ wiki ->id ]);
118+
119+ // Delete the wiki
87120 $ wiki ->delete ();
88121 $ wiki ->save ();
89122
123+ // Create an old metric record for the wiki
124+ WikiDailyMetrics::create ([
125+ 'id ' => $ wiki ->id . '_ ' . Carbon::yesterday ()->toDateString (),
126+ 'wiki_id ' => $ wiki ->id ,
127+ 'date ' => Carbon::yesterday ()->toDateString (),
128+ 'pages ' => 0 ,
129+ 'is_deleted ' => 1 ,
130+ ]);
131+
132+ // Run saveMetrics()
90133 (new WikiMetrics )->saveMetrics ($ wiki );
91134
92- // Assert No new record was created for today
135+ // Assert no new record was created for previously deleted wiki
93136 $ this ->assertDatabaseMissing ('wiki_daily_metrics ' , [
94137 'wiki_id ' => $ wiki ->id ,
95138 'is_deleted ' => 1 ,
96139 'date ' => now ()->toDateString (),
97140 ]);
98141 }
99142
143+ public function testDailyMetricCreatedWhenNoRecordsInWikiSiteStats () {
144+ $ wiki = Wiki::factory ()->create ([
145+ 'domain ' => 'test.wbaas.dev ' ,
146+ ]);
147+ $ wikiDb = WikiDb::first ();
148+ $ wikiDb ->update (['wiki_id ' => $ wiki ->id ]);
149+
150+ // Ensure no wikiSiteStats record exists
151+ $ this ->assertNull ($ wiki ->wikiSiteStats ()->first ());
152+
153+ // Run saveMetrics()
154+ (new WikiMetrics )->saveMetrics ($ wiki );
155+
156+ // Assert new record was created with default values
157+ $ this ->assertDatabaseHas ('wiki_daily_metrics ' , [
158+ 'wiki_id ' => $ wiki ->id ,
159+ 'pages ' => 0 ,
160+ 'total_user_count ' => 0 ,
161+ 'date ' => now ()->toDateString (),
162+ ]);
163+ }
164+
100165 public function testTripleCountSavedSuccessfully () {
101166 $ wiki = Wiki::factory ()->create ([
102167 'domain ' => 'somewikiforunittest.wikibase.cloud ' ,
0 commit comments