Add test for factors old values replacement
authorVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Thu, 14 Dec 2017 17:55:43 +0000 (20:55 +0300)
committerVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Thu, 14 Dec 2017 17:55:43 +0000 (20:55 +0300)
plugins/stats-collector/test/com/intellij/stats/personalization/AggregatedFactorTest.kt

index 283082c0fd6b404464973ec1d8bfbcfd6b7c04bb..ac4af663230ac6da59c2e47d61013ff81bb3778e 100644 (file)
@@ -4,7 +4,6 @@ import com.intellij.stats.personalization.impl.*
 import com.intellij.testFramework.UsefulTestCase
 import junit.framework.TestCase
 import org.junit.Assert
-import org.junit.Test
 import java.util.*
 
 /**
@@ -12,19 +11,27 @@ import java.util.*
  */
 class AggregatedFactorTest : UsefulTestCase() {
     private companion object {
-        val DATE_1 = Calendar.Builder().setDate(2010, 1, 1).build().time
-        val DATE_2 = Calendar.Builder().setDate(2010, 1, 2).build().time
-        val DATE_3 = Calendar.Builder().setDate(2010, 1, 3).build().time
-        val DATE_4 = Calendar.Builder().setDate(2010, 1, 4).build().time
-        val DATE_5 = Calendar.Builder().setDate(2010, 1, 6).build().time
+        val DATE_1 = DateUtil.byDate(Calendar.Builder().setDate(2010, 1, 1).build().time)
+        val DATE_2 = DATE_1.update(1)
+        val DATE_3 = DATE_1.update(2)
+        val DATE_4 = DATE_1.update(3)
+        val DATE_5 = DATE_1.update(4)
+
+        fun Day.update(count: Int): Day {
+            return Calendar.getInstance().let {
+                it.set(year, month, dayOfMonth)
+                it.add(Calendar.DATE, count)
+                DateUtil.byDate(it.time)
+            }
+        }
     }
 
     fun `test min is correct`() {
         val aggregateFactor: MutableDoubleFactor = UserFactorStorageBase.DailyAggregateFactor()
 
-        aggregateFactor.setOnDate(DateUtil.byDate(DATE_1), "count", 10.0)
-        aggregateFactor.setOnDate(DateUtil.byDate(DATE_3), "count", 20.0)
-        aggregateFactor.setOnDate(DateUtil.byDate(DATE_5), "delay", 1000.0)
+        aggregateFactor.setOnDate(DATE_1, "count", 10.0)
+        aggregateFactor.setOnDate(DATE_3, "count", 20.0)
+        aggregateFactor.setOnDate(DATE_5, "delay", 1000.0)
 
         val mins = createFactorForTests().aggregateMin()
         TestCase.assertEquals(2, mins.size)
@@ -53,23 +60,46 @@ class AggregatedFactorTest : UsefulTestCase() {
         UsefulTestCase.assertEquals(1000.0, maximums["delay"]!!, 1e-10)
     }
 
-    @Test
     fun `test average does not lose precision`() {
         val factor: MutableDoubleFactor = UserFactorStorageBase.DailyAggregateFactor()
-        factor.setOnDate(DateUtil.byDate(DATE_1), "key1", Double.MAX_VALUE)
-        factor.setOnDate(DateUtil.byDate(DATE_2), "key1", Double.MAX_VALUE)
+        factor.setOnDate(DATE_1, "key1", Double.MAX_VALUE)
+        factor.setOnDate(DATE_2, "key1", Double.MAX_VALUE)
 
         val avg = factor.aggregateAverage()
         TestCase.assertEquals(1, avg.size)
         Assert.assertNotEquals(Double.POSITIVE_INFINITY, avg["key1"]!!)
     }
 
+    fun `test factor stores information only for the last 10 days`() {
+        val fieldName = "count"
+        val factor: MutableDoubleFactor = UserFactorStorageBase.DailyAggregateFactor()
+        for (i in 0 until 10) {
+            TestCase.assertTrue(factor.updateOnDate(DATE_1.update(i)) {
+                put(fieldName, i.toDouble())
+            })
+        }
+
+        TestCase.assertEquals(45.0, factor.aggregateSum()[fieldName]!!, 1e-10)
+        TestCase.assertNotNull(factor.onDate(DATE_1))
+        TestCase.assertFalse(factor.updateOnDate(DATE_1.update(-100)) { put(fieldName, 100.0) })
+        TestCase.assertEquals(9.0, factor.aggregateMax()[fieldName]!!, 1e-10)
+
+        TestCase.assertTrue(factor.updateOnDate(DATE_1.update(100)) { put(fieldName, 1.0) })
+        TestCase.assertEquals(46.0, factor.aggregateSum()[fieldName]!!, 1e-10)
+    }
+
+    fun `test order on days is correct`() {
+        TestCase.assertTrue(DATE_1 < DATE_2)
+        TestCase.assertTrue(DATE_3 > DATE_2)
+
+    }
+
     private fun createFactorForTests(): DailyAggregatedDoubleFactor {
         val aggregateFactor: MutableDoubleFactor = UserFactorStorageBase.DailyAggregateFactor()
 
-        aggregateFactor.setOnDate(DateUtil.byDate(DATE_1), "count", 10.0)
-        aggregateFactor.setOnDate(DateUtil.byDate(DATE_3), "count", 20.0)
-        aggregateFactor.setOnDate(DateUtil.byDate(DATE_5), "delay", 1000.0)
+        aggregateFactor.setOnDate(DATE_1, "count", 10.0)
+        aggregateFactor.setOnDate(DATE_3, "count", 20.0)
+        aggregateFactor.setOnDate(DATE_5, "delay", 1000.0)
 
         return aggregateFactor
     }