def test_table_and_id_args(self): # type: () -> None # For realms, streams, and users, tests that the new rows are going to # the appropriate *Count table, and that using a different zerver_object # results in a new row being created self.current_property = 'test' second_realm = Realm.objects.create(string_id='moo', name='moo', domain='moo') stat = LoggingCountStat('test', RealmCount, CountStat.DAY) do_increment_logging_stat(self.default_realm, stat, None, self.TIME_ZERO) do_increment_logging_stat(second_realm, stat, None, self.TIME_ZERO) self.assertTableState(RealmCount, ['realm'], [[self.default_realm], [second_realm]]) user1 = self.create_user() user2 = self.create_user() stat = LoggingCountStat('test', UserCount, CountStat.DAY) do_increment_logging_stat(user1, stat, None, self.TIME_ZERO) do_increment_logging_stat(user2, stat, None, self.TIME_ZERO) self.assertTableState(UserCount, ['user'], [[user1], [user2]]) stream1 = self.create_stream_with_recipient()[0] stream2 = self.create_stream_with_recipient()[0] stat = LoggingCountStat('test', StreamCount, CountStat.DAY) do_increment_logging_stat(stream1, stat, None, self.TIME_ZERO) do_increment_logging_stat(stream2, stat, None, self.TIME_ZERO) self.assertTableState(StreamCount, ['stream'], [[stream1], [stream2]])
def test_aggregation(self): # type: () -> None stat = LoggingCountStat('realm test', RealmCount, CountStat.DAY) do_increment_logging_stat(self.default_realm, stat, None, self.TIME_ZERO) process_count_stat(stat, self.TIME_ZERO) user = self.create_user() stat = LoggingCountStat('user test', UserCount, CountStat.DAY) do_increment_logging_stat(user, stat, None, self.TIME_ZERO) process_count_stat(stat, self.TIME_ZERO) stream = self.create_stream_with_recipient()[0] stat = LoggingCountStat('stream test', StreamCount, CountStat.DAY) do_increment_logging_stat(stream, stat, None, self.TIME_ZERO) process_count_stat(stat, self.TIME_ZERO) self.assertTableState( InstallationCount, ['property', 'value'], [['realm test', 1], ['user test', 1], ['stream test', 1]]) self.assertTableState( RealmCount, ['property', 'value'], [['realm test', 1], ['user test', 1], ['stream test', 1]]) self.assertTableState(UserCount, ['property', 'value'], [['user test', 1]]) self.assertTableState(StreamCount, ['property', 'value'], [['stream test', 1]])
def test_frequency(self): # type: () -> None times = [self.TIME_ZERO - self.MINUTE*i for i in [0, 1, 61, 24*60+1]] stat = LoggingCountStat('day test', RealmCount, CountStat.DAY) for time_ in times: do_increment_logging_stat(self.default_realm, stat, None, time_) stat = LoggingCountStat('hour test', RealmCount, CountStat.HOUR) for time_ in times: do_increment_logging_stat(self.default_realm, stat, None, time_) self.assertTableState(RealmCount, ['value', 'property', 'end_time'], [[3, 'day test', self.TIME_ZERO], [1, 'day test', self.TIME_ZERO - self.DAY], [2, 'hour test', self.TIME_ZERO], [1, 'hour test', self.TIME_LAST_HOUR], [1, 'hour test', self.TIME_ZERO - self.DAY]])
def test_increment(self): # type: () -> None stat = LoggingCountStat('test', RealmCount, CountStat.DAY) self.current_property = 'test' do_increment_logging_stat(self.default_realm, stat, None, self.TIME_ZERO, increment=-1) self.assertTableState(RealmCount, ['value'], [[-1]]) do_increment_logging_stat(self.default_realm, stat, None, self.TIME_ZERO, increment=3) self.assertTableState(RealmCount, ['value'], [[2]]) do_increment_logging_stat(self.default_realm, stat, None, self.TIME_ZERO) self.assertTableState(RealmCount, ['value'], [[3]])
def test_process_logging_stat(self): # type: () -> None end_time = self.TIME_ZERO user_stat = LoggingCountStat('user stat', UserCount, CountStat.DAY) stream_stat = LoggingCountStat('stream stat', StreamCount, CountStat.DAY) realm_stat = LoggingCountStat('realm stat', RealmCount, CountStat.DAY) user = self.create_user() stream = self.create_stream_with_recipient()[0] realm = self.default_realm UserCount.objects.create( user=user, realm=realm, property=user_stat.property, end_time=end_time, value=5) StreamCount.objects.create( stream=stream, realm=realm, property=stream_stat.property, end_time=end_time, value=5) RealmCount.objects.create( realm=realm, property=realm_stat.property, end_time=end_time, value=5) # Normal run of process_count_stat for stat in [user_stat, stream_stat, realm_stat]: process_count_stat(stat, end_time) self.assertTableState(UserCount, ['property', 'value'], [[user_stat.property, 5]]) self.assertTableState(StreamCount, ['property', 'value'], [[stream_stat.property, 5]]) self.assertTableState(RealmCount, ['property', 'value'], [[user_stat.property, 5], [stream_stat.property, 5], [realm_stat.property, 5]]) self.assertTableState(InstallationCount, ['property', 'value'], [[user_stat.property, 5], [stream_stat.property, 5], [realm_stat.property, 5]]) # Change the logged data and mark FillState as dirty UserCount.objects.update(value=6) StreamCount.objects.update(value=6) RealmCount.objects.filter(property=realm_stat.property).update(value=6) FillState.objects.update(state=FillState.STARTED) # Check that the change propagated (and the collected data wasn't deleted) for stat in [user_stat, stream_stat, realm_stat]: process_count_stat(stat, end_time) self.assertTableState(UserCount, ['property', 'value'], [[user_stat.property, 6]]) self.assertTableState(StreamCount, ['property', 'value'], [[stream_stat.property, 6]]) self.assertTableState(RealmCount, ['property', 'value'], [[user_stat.property, 6], [stream_stat.property, 6], [realm_stat.property, 6]]) self.assertTableState(InstallationCount, ['property', 'value'], [[user_stat.property, 6], [stream_stat.property, 6], [realm_stat.property, 6]])
def test_get_or_create(self): # type: () -> None stat = LoggingCountStat('test', RealmCount, CountStat.HOUR) # All these should trigger the create part of get_or_create. # property is tested in test_frequency, and id_args are tested in test_id_args, # so this only tests a new subgroup and end_time do_increment_logging_stat(self.default_realm, stat, 'subgroup1', self.TIME_ZERO) do_increment_logging_stat(self.default_realm, stat, 'subgroup2', self.TIME_ZERO) do_increment_logging_stat(self.default_realm, stat, 'subgroup1', self.TIME_LAST_HOUR) self.current_property = 'test' self.assertTableState(RealmCount, ['value', 'subgroup', 'end_time'], [[1, 'subgroup1', self.TIME_ZERO], [1, 'subgroup2', self.TIME_ZERO], [1, 'subgroup1', self.TIME_LAST_HOUR]]) # This should trigger the get part of get_or_create do_increment_logging_stat(self.default_realm, stat, 'subgroup1', self.TIME_ZERO) self.assertTableState(RealmCount, ['value', 'subgroup', 'end_time'], [[2, 'subgroup1', self.TIME_ZERO], [1, 'subgroup2', self.TIME_ZERO], [1, 'subgroup1', self.TIME_LAST_HOUR]])
def test_no_aggregated_zeros(self): # type: () -> None stat = LoggingCountStat('test stat', UserCount, CountStat.HOUR) do_aggregate_to_summary_table(stat, self.TIME_ZERO) self.assertFalse(RealmCount.objects.exists()) self.assertFalse(InstallationCount.objects.exists())