__author__ = 'jcorbett'

from mongoengine import *
from .serialize import serializable

class ResultsByStatus(EmbeddedDocument):
    PASS = IntField(default=0)
    FAIL = IntField(default=0)
    BROKEN_TEST = IntField(default=0)
    NOT_TESTED = IntField(default=0)
    SKIPPED = IntField(default=0)
    NO_RESULT = IntField(default=0)
    PASSED_ON_RETRY = IntField(default=0)


class TestrunSummary(EmbeddedDocument):
    totalTime = IntField(default=0)
    resultsByStatus = EmbeddedDocumentField(ResultsByStatus, default=ResultsByStatus())

    dynamic_types = {
        'statusListOrdered': ListField(StringField()),
        'total': IntField()
    }

    def dynamic_fields(self):
        return {
            'statusListOrdered': self.statusListOrdered(),
            'total': self.total()
        }

    @serializable
    def statusListOrdered(self):
        retval = []
        for status in ["PASS", "FAIL", "BROKEN_TEST", "NOT_TESTED", "SKIPPED", "NO_RESULT", "PASSED_ON_RETRY"]:
            if hasattr(self.resultsByStatus, status) and getattr(self.resultsByStatus, status) > 0:
                retval.append(status)
        return retval

    @serializable
    def total(self):
        retval = 0
        for status in ["PASS", "FAIL", "BROKEN_TEST", "NOT_TESTED", "SKIPPED", "NO_RESULT", "PASSED_ON_RETRY"]:
            if hasattr(self.resultsByStatus, status) and getattr(self.resultsByStatus, status) > 0:
                retval += getattr(self.resultsByStatus, status)
        return retval

@@ -149,24 +149,66 @@
 (default=0)%0A
+    PASSED_ON_RETRY = IntField(default=0)%0A
     FAIL = I
@@ -222,24 +222,24 @@
 (default=0)%0A
-
     BROKEN_T
@@ -375,50 +375,8 @@
 =0)%0A
-    PASSED_ON_RETRY = IntField(default=0)%0A
 %0A%0Acl
@@ -894,32 +894,51 @@
 atus in %5B%22PASS%22,
+ %22PASSED_ON_RETRY%22,
  %22FAIL%22, %22BROKEN
@@ -981,35 +981,16 @@
 _RESULT%22
-, %22PASSED_ON_RETRY%22
 %5D:%0A     
@@ -1233,16 +1233,35 @@
 %5B%22PASS%22,
+ %22PASSED_ON_RETRY%22,
  %22FAIL%22,
@@ -1312,35 +1312,16 @@
 _RESULT%22
-, %22PASSED_ON_RETRY%22
 %5D:%0A     

Fix order of statuses