Plots2: even after NID test fixes, a unit test is intermittently failing :-(

Created on 17 Mar 2021  路  5Comments  路  Source: publiclab/plots2

Even after these NID fixes, this still went wrong:

Progress: | FAIL TagSelectionTest#test_graph (0.33s)
        Expected: 18
          Actual: 23
        test/unit/tag_selection_test.rb:14:in `block in <class:TagSelectionTest>'

====================================================================|

I wonder if that's a different issue, non-NID?

_Originally posted by @jywarren in https://github.com/publiclab/plots2/issues/8865#issuecomment-800676438_

Most helpful comment

I tried this out here: https://github.com/publiclab/plots2/pull/9390

but knowing if it's worked may be tough. We can try running the tests over and over for a week? 馃槄

All 5 comments

Hi @jywarren 馃憢馃従

The only difference I see is with the date of the .select and the date within the .select of the graph logic.
In the tag_selection_test the expected count is retrieved from the database using this query:

https://github.com/publiclab/plots2/blob/07a243da39702501d0f5d4de8a3683a24d680a38/test/unit/tag_selection_test.rb#L6-L12

the expected count is then matched with the query in the tag_selection model file...
https://github.com/publiclab/plots2/blob/07a243da39702501d0f5d4de8a3683a24d680a38/app/models/tag_selection.rb#L22-L36

The date passed in the expected query is a range between the start date and the end date (start_time..end_time), but the date passed in the actual query is slightly different since some calculations are made before passing it in the created_at value

    while week >= 1
      month = (fin - (week * 7 - 1).days)
      range = (fin - week.weeks)..(fin - (week - 1).weeks)
      weekly_subs = TagSelection.select(:following, :created_at)
                                .where(following: true, created_at: range)
                                .size
      date_hash[month.to_f * 1000] = weekly_subs
      week -= 1
    end
    date_hash
  end

That's the major difference I see so far...

Ahhhhh. So is this... an aliasing/rounding error? What do you think we should do? Could we potentially better control the situation with Timecop?

https://github.com/publiclab/plots2/search?q=TimeCop for some examples and https://github.com/travisjeffery/timecop

Thanks, Ruth, for this super helpful investigation!!!

Thanks, @jywarren, I'll have a look at the links you've shared and see how they can help us in fixing this issue.

Looking closer, i'm not sure freezing the time will help. I wonder if due to the zillion ways dates are confusing (like, leap years, months of different lengths, exact decimal # of weeks in a year) we are just seeing the counting work differently between the graph code which counts each week individually and the test code which counts the whole year?

i.e. 365/7 = 52.1428571429 馃槶

https://github.com/publiclab/plots2/blob/07a243da39702501d0f5d4de8a3683a24d680a38/app/models/tag_selection.rb#L22-L36

What if we modified the test code to test for 52 weeks, instead of 1 year? Would that be more reliable?

I tried this out here: https://github.com/publiclab/plots2/pull/9390

but knowing if it's worked may be tough. We can try running the tests over and over for a week? 馃槄

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RuthNjeri picture RuthNjeri  路  3Comments

grvsachdeva picture grvsachdeva  路  3Comments

bronwen9 picture bronwen9  路  3Comments

jywarren picture jywarren  路  3Comments

grvsachdeva picture grvsachdeva  路  3Comments