@@ -209,3 +209,89 @@ func (sr *slowReader) Read(out []byte) (n int, err error) {
209209
210210 return numBytes , nil
211211}
212+
213+ func TestAggregateCmd_DifferentDates_WithoutForceDate (t * testing.T ) {
214+ file1 , file2 , cleanup := createDNSMagFilesWithDifferentDates (t , "2024-04-04" , "2025-05-05" )
215+ defer cleanup ()
216+
217+ // Try to aggregate the two DNSMAG files without --force-date
218+ aggregateCmd := newAggregateCmd ()
219+ aggregateCmd .SetArgs ([]string {
220+ file1 ,
221+ file2 ,
222+ })
223+
224+ var aggregateBuf bytes.Buffer
225+ aggregateCmd .SetOut (& aggregateBuf )
226+ aggregateCmd .SetErr (& aggregateBuf )
227+
228+ err := aggregateCmd .Execute ()
229+ if err == nil {
230+ t .Fatalf ("Expected error when aggregating datasets with different dates, but got none. Output: %s" , aggregateBuf .String ())
231+ }
232+
233+ // Verify the error message mentions date mismatch
234+ output := aggregateBuf .String ()
235+ if ! regexp .MustCompile (`date mismatch` ).MatchString (err .Error ()) {
236+ t .Errorf ("Expected 'date mismatch' error, got: %v\n Output: %s" , err , output )
237+ }
238+
239+ t .Logf ("Expected error occurred: %v" , err )
240+ }
241+
242+ func TestAggregateCmd_DifferentDates_WithForceDate (t * testing.T ) {
243+ file1 , file2 , cleanup := createDNSMagFilesWithDifferentDates (t , "2024-04-04" , "2025-05-05" )
244+ defer cleanup ()
245+
246+ // Aggregate the two DNSMAG files WITH --force-date
247+ aggregateCmd := newAggregateCmd ()
248+ aggregateCmd .SetArgs ([]string {
249+ "--force-date" , "2026-01-14" ,
250+ file1 ,
251+ file2 ,
252+ })
253+
254+ var aggregateBuf bytes.Buffer
255+ aggregateCmd .SetOut (& aggregateBuf )
256+ aggregateCmd .SetErr (& aggregateBuf )
257+
258+ err := aggregateCmd .Execute ()
259+ if err != nil {
260+ t .Fatalf ("Aggregate command with --force-date failed: %v\n Output: %s" , err , aggregateBuf .String ())
261+ }
262+
263+ output := aggregateBuf .String ()
264+
265+ // Verify the output contains expected patterns
266+ expectedPatterns := []* regexp.Regexp {
267+ regexp .MustCompile (`Aggregated statistics for 2 datasets:` ),
268+ regexp .MustCompile (`Date\s+:\s+2026-01-14` ), // The forced date
269+ regexp .MustCompile (`Total queries\s+:\s+40` ), // 25 + 15
270+ regexp .MustCompile (`Total domains\s+:\s+2` ), // com, org
271+ regexp .MustCompile (`Warning: Overriding date 2025-05-05 with forced date 2026-01-14` ), // Warning message
272+ }
273+
274+ for _ , pattern := range expectedPatterns {
275+ if ! pattern .MatchString (output ) {
276+ t .Errorf ("Expected pattern %q not found in output:\n %s" , pattern .String (), output )
277+ }
278+ }
279+
280+ t .Logf ("Aggregate command with --force-date output:\n %s" , output )
281+ }
282+
283+ // createDNSMagFilesWithDifferentDates creates two temporary DNSMAG files with different dates
284+ // Returns the two file paths and a cleanup function
285+ func createDNSMagFilesWithDifferentDates (t * testing.T , date1 , date2 string ) (string , string , func ()) {
286+ t .Helper ()
287+
288+ file1 := createDNSMAGFromCSV (t , "192.168.1.1,example.com,25" , date1 )
289+ file2 := createDNSMAGFromCSV (t , "10.0.1.1,example.org,15" , date2 )
290+
291+ cleanup := func () {
292+ os .Remove (file1 )
293+ os .Remove (file2 )
294+ }
295+
296+ return file1 , file2 , cleanup
297+ }
0 commit comments