@@ -41,7 +41,7 @@ public function testSuccessfullyAddRecords() {
4141 ]);
4242 }
4343
44- public function testDoesNotAddDuplicateRecordsWithOnlyDateChange () {
44+ public function testNoDuplicateRecordsWithOnlyDateChange () {
4545 $ wiki = Wiki::factory ()->create ([
4646 'domain ' => 'thisfake.wikibase.cloud ' ,
4747 ]);
@@ -57,38 +57,81 @@ public function testDoesNotAddDuplicateRecordsWithOnlyDateChange() {
5757 'pages ' => 0 ,
5858 'is_deleted ' => 0 ,
5959 ]);
60+
61+ // Run saveMetrics()
6062 (new WikiMetrics )->saveMetrics ($ wiki );
6163
62- // Assert No new record was created for today
64+ // Assert no new record was created for today
6365 $ this ->assertDatabaseMissing ('wiki_daily_metrics ' , [
6466 'wiki_id ' => $ wiki ->id ,
6567 'date ' => Carbon::today ()->toDateString (),
6668 ]);
6769 }
6870
69- public function testAddRecordsWikiIsDeleted () {
71+ public function testRecordCreatedWhenWikiFirstDeleted () {
7072 $ wiki = Wiki::factory ()->create ([
7173 'domain ' => 'thisfake.wikibase.cloud ' ,
7274 ]);
7375
7476 $ wikiDb = WikiDb::first ();
7577 $ wikiDb ->update (['wiki_id ' => $ wiki ->id ]);
7678
79+ $ wikiSiteStats = $ wiki ->wikiSiteStats ()->create ();
80+ $ wikiSiteStats ->update ([
81+ 'pages ' => 10 ,
82+ 'users ' => 5 ,
83+ ]);
84+
7785 // Insert an old metric value for a wiki
7886 WikiDailyMetrics::create ([
7987 'id ' => $ wiki ->id . '_ ' . Carbon::yesterday ()->toDateString (),
8088 'wiki_id ' => $ wiki ->id ,
8189 'date ' => Carbon::yesterday ()->toDateString (),
8290 'pages ' => 0 ,
91+ 'is_deleted ' => 0 ,
92+ 'total_user_count ' => null ,
93+ ]);
94+
95+ // Delete the wiki
96+ $ wiki ->delete ();
97+ $ wiki ->save ();
98+
99+ // Run saveMetrics()
100+ (new WikiMetrics )->saveMetrics ($ wiki );
101+
102+ // Assert new record was created for newly deleted wiki
103+ $ this ->assertDatabaseHas ('wiki_daily_metrics ' , [
104+ 'wiki_id ' => $ wiki ->id ,
83105 'is_deleted ' => 1 ,
106+ 'total_user_count ' => 5 ,
107+ 'date ' => now ()->toDateString (),
108+ ]);
109+ }
110+
111+ public function testNoDuplicateRecordsForDeletedWiki () {
112+ $ wiki = Wiki::factory ()->create ([
113+ 'domain ' => 'thisfake.wikibase.cloud ' ,
84114 ]);
85- // delete the wiki
115+ $ wikiDb = WikiDb::first ();
116+ $ wikiDb ->update (['wiki_id ' => $ wiki ->id ]);
117+
118+ // Delete the wiki
86119 $ wiki ->delete ();
87120 $ wiki ->save ();
88121
122+ // Create an old metric record for the wiki
123+ WikiDailyMetrics::create ([
124+ 'id ' => $ wiki ->id . '_ ' . Carbon::yesterday ()->toDateString (),
125+ 'wiki_id ' => $ wiki ->id ,
126+ 'date ' => Carbon::yesterday ()->toDateString (),
127+ 'pages ' => 0 ,
128+ 'is_deleted ' => 1 ,
129+ ]);
130+
131+ // Run saveMetrics()
89132 (new WikiMetrics )->saveMetrics ($ wiki );
90133
91- // Assert No new record was created for today
134+ // Assert no new record was created for previously deleted wiki
92135 $ this ->assertDatabaseMissing ('wiki_daily_metrics ' , [
93136 'wiki_id ' => $ wiki ->id ,
94137 'is_deleted ' => 1 ,
0 commit comments