示例#1
0
    def verify_aggregates_in_mongo(self, resolution):
        """
        verify aggregates from mongo:
            - origin, max, min, sum sum2
        :param resolution: resolutions type (  month | day | hour | minute | second )
        """
        time_zone = 2
        find_dict = {"_id.attrName" :  {'$regex':'%s.*' % (self.attributes_name)}, #the regular expression is because in  multi attribute the name is with postfix + <_value>. ex: temperature_0
                     "_id.resolution" : resolution}

        origin_year   = general_utils.get_date_only_one_value(self.date_time, "year")
        origin_month  = general_utils.get_date_only_one_value(self.date_time, "month")
        origin_day    = general_utils.get_date_only_one_value(self.date_time, "day")
        origin_hour   = int(general_utils.get_date_only_one_value(self.date_time, "hour"))-time_zone
        if origin_hour < 10: origin_hour = u'0' + str(origin_hour)
        origin_minute = general_utils.get_date_only_one_value(self.date_time, "minute")
        origin_second = general_utils.get_date_only_one_value(self.date_time, "second")

        world.mongo.connect("%s_%s" % (STH_DATABASE_PREFIX, self.service))
        world.mongo.choice_collection("%s_%s_%s_%s.%s" % (STH_COLLECTION_PREFIX, self.service_path, self.entity_id, self.entity_type, AGGR))
        cursor = world.mongo.find_with_retry(find_dict)
        assert cursor.count() != 0, " ERROR - the aggregated has not been stored in mongo successfully "
        doc_list = world.mongo.get_cursor_value(cursor)   # get all dictionaries into a cursor, return a list

        for doc in doc_list:

            offset = int(general_utils.get_date_only_one_value(self.date_time, resolution))
            if resolution == "month":
                offset=offset-1
                origin_by_resolution = "%s-01-01 00:00:00" % (origin_year)
            elif resolution == "day":
                offset=offset-1
                origin_by_resolution = "%s-%s-01 00:00:00" % (origin_year, origin_month)
            elif resolution == "hour":
                offset=offset-time_zone
                origin_by_resolution = "%s-%s-%s 00:00:00" % (origin_year, origin_month, origin_day)
            elif resolution == "minute":
                origin_by_resolution = "%s-%s-%s %s:00:00" % (origin_year, origin_month, origin_day, origin_hour)
            elif resolution == "second":
                c = 0
                MAX_SECS = 20
                while (c < MAX_SECS):
                    if float(doc["points"][offset]["min"]) == float(self.attributes_value):
                        break
                    offset = offset - 1
                    if offset < 0: offset = 59
                    c = c + 1
                if (origin_second < c): origin_minute = origin_minute - 1
                origin_by_resolution = "%s-%s-%s %s:%s:00" % (origin_year, origin_month, origin_day, origin_hour, origin_minute)
            else:
                assert False, " ERROR - resolution type \"%s\" is not allowed, review your tests in features..." % (resolution)

            assert str(doc["_id"]["origin"]) == origin_by_resolution, " ERROR -- in origin field by the %s resolution in %s attribute" % (resolution, str(doc["_id"]["attrName"]))
            assert float(doc["points"][offset]["min"]) == float(self.attributes_value), " ERROR -- in minimun value into offset %s in %s attribute" % (str(offset), str(doc["_id"]["attrName"]))
            assert float(doc["points"][offset]["max"]) == float(self.attributes_value), " ERROR -- in maximun value into offset %s in %s attribute" % (str(offset), str(doc["_id"]["attrName"]))
            assert float(doc["points"][offset]["sum"]) == float(self.attributes_value), " ERROR -- in sum value into offset %s in %s attribute" % (str(offset), str(doc["_id"]["attrName"]))
            assert float(doc["points"][offset]["sum2"]) == (float(self.attributes_value)*float(self.attributes_value)), " ERROR -- in sum2 value into offset %s in %s attribute" % (str(offset), str(doc["_id"]["attrName"]))
        world.mongo.disconnect()
示例#2
0
    def validate_that_the_aggregated_is_returned_successfully(self):
        """
        validate that the aggregated is returned successfully via REST
        """
        time_zone = 2
        dupla = {"month": "year", "day": "month", "hour": "day", "minute": "hour", "second": "minute"}
        offset = int(general_utils.get_date_only_one_value(self.date_time, self.resolution))
        origin_year = general_utils.get_date_only_one_value(self.date_time, "year")
        origin_month = general_utils.get_date_only_one_value(self.date_time, "month")
        origin_day = general_utils.get_date_only_one_value(self.date_time, "day")
        origin_hour = int(general_utils.get_date_only_one_value(self.date_time, "hour")) - time_zone
        if origin_hour < 10:
            origin_hour = u"0" + str(origin_hour)
        origin_minute = general_utils.get_date_only_one_value(self.date_time, "minute")
        origin_second = general_utils.get_date_only_one_value(self.date_time, "second")

        context_json = general_utils.convert_str_to_dict(self.resp.text, general_utils.JSON)
        if self.resolution == "month":
            origin_by_resolution = "%s-01-01T00:00:00.000Z" % (origin_year)  # 2015-01-01T00:00:00.000Z
        elif self.resolution == "day":
            origin_by_resolution = "%s-%s-01T00:00:00.000Z" % (origin_year, origin_month)
        elif self.resolution == "hour":
            offset = offset - time_zone
            origin_by_resolution = "%s-%s-%sT00:00:00.000Z" % (origin_year, origin_month, origin_day)
        elif self.resolution == "minute":
            origin_by_resolution = "%s-%s-%sT%s:00:00.000Z" % (origin_year, origin_month, origin_day, origin_hour)
        elif self.resolution == "second":
            offset = int(
                context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0][
                    "offset"
                ]
            )
            c = int(origin_second) - int(offset)
            if (int(origin_second) - c) < 0:
                origin_minute = str(int(origin_minute) - 1)
            origin_by_resolution = "%s-%s-%sT%s:%s:00.000Z" % (
                origin_year,
                origin_month,
                origin_day,
                origin_hour,
                origin_minute,
            )
        else:
            assert False, ' ERROR - resolution type "%s" is not allowed, review your tests in features...' % (
                self.resolution
            )

        # attributes
        assert str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["name"]) == "%s_%s" % (
            self.attributes_name,
            u"0",
        ), "  ERROR - in aggregated with name %s_%s" % (self.attributes_name, u"0")
        # values
        assert (
            str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["entityId"])
            == self.entity_id
        ), "  ERROR - in aggregated with entity id %s" % (self.entity_id)
        assert (
            str(
                context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["entityType"]
            )
            == self.entity_type
        ), "  ERROR - in aggregated with entity type %s" % (self.entity_type)
        assert str(
            context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["attrName"]
        ) == "%s_%s" % (self.attributes_name, u"0"), (
            "  ERROR - in aggregated with name %s_%s" % (self.attributes_name, u"0")
        )
        assert (
            str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["origin"])
            == origin_by_resolution
        ), "  ERROR - in aggregated with origin %s" % (origin_by_resolution)
        assert (
            str(
                context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["resolution"]
            )
            == self.resolution
        ), "  ERROR - in aggregated with resolution %s" % (self.resolution)
        assert (
            str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["range"])
            == dupla[self.resolution]
        ), "  ERROR - in aggregated with range %s" % (dupla[self.resolution])
        # points
        assert str(
            context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["offset"]
        ) == str(offset), "  ERROR - in aggregated with offset %s" % (offset)
        if self.method == "sum":
            assert float(
                context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["sum"]
            ) == float(self.attributes_value), "  ERROR - in aggregated with sum %s" % (str(self.attributes_value))
        elif self.method == "sum2":
            assert float(
                context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["sum2"]
            ) == (float(self.attributes_value) * float(self.attributes_value)), (
                "  ERROR - in aggregated with sum2 %s"
                % (str((float(self.attributes_value) * float(self.attributes_value))))
            )
        elif self.method == "min":
            assert float(
                context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["min"]
            ) == float(self.attributes_value), "  ERROR - in aggregated with min %s" % (str(self.attributes_value))
        elif self.method == "max":
            assert float(
                context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["max"]
            ) == float(self.attributes_value), "  ERROR - in aggregated with max %s" % (str(self.attributes_value))

        # contextElement
        assert (
            str(context_json["contextResponses"][0]["contextElement"]["id"]) == self.entity_id
        ), "  ERROR - in aggregated with entity id %s" % (self.entity_id)
        assert (
            str(context_json["contextResponses"][0]["contextElement"]["type"]) == self.entity_type
        ), "  ERROR - in aggregated with entity type %s" % (self.entity_type)
        assert (
            str(context_json["contextResponses"][0]["contextElement"]["isPattern"]) == "False"
        ), "  ERROR - in aggregated with isPattern equal to false"

        # context Responses
        assert (
            str(context_json["contextResponses"][0]["statusCode"]["code"]) == "200"
        ), "  ERROR - in aggregated with status Code equal to 200"
        assert (
            str(context_json["contextResponses"][0]["statusCode"]["reasonPhrase"]) == "OK"
        ), "  ERROR - in aggregated with reason Phrase equal to OK"
示例#3
0
    def validate_that_the_aggregated_is_returned_successfully(self):
        """
        validate that the aggregated is returned successfully via REST
        """
        time_zone = 2
        dupla = {
            "month": "year",
            "day": "month",
            "hour": "day",
            "minute": "hour",
            "second": "minute"
        }
        offset = int(
            general_utils.get_date_only_one_value(self.date_time,
                                                  self.resolution))
        origin_year = general_utils.get_date_only_one_value(
            self.date_time, "year")
        origin_month = general_utils.get_date_only_one_value(
            self.date_time, "month")
        origin_day = general_utils.get_date_only_one_value(
            self.date_time, "day")
        origin_hour = int(
            general_utils.get_date_only_one_value(self.date_time,
                                                  "hour")) - time_zone
        if origin_hour < 10: origin_hour = u'0' + str(origin_hour)
        origin_minute = general_utils.get_date_only_one_value(
            self.date_time, "minute")
        origin_second = general_utils.get_date_only_one_value(
            self.date_time, "second")

        context_json = general_utils.convert_str_to_dict(
            self.resp.text, general_utils.JSON)
        if self.resolution == "month":
            origin_by_resolution = "%s-01-01T00:00:00.000Z" % (
                origin_year)  # 2015-01-01T00:00:00.000Z
        elif self.resolution == "day":
            origin_by_resolution = "%s-%s-01T00:00:00.000Z" % (origin_year,
                                                               origin_month)
        elif self.resolution == "hour":
            offset = offset - time_zone
            origin_by_resolution = "%s-%s-%sT00:00:00.000Z" % (
                origin_year, origin_month, origin_day)
        elif self.resolution == "minute":
            origin_by_resolution = "%s-%s-%sT%s:00:00.000Z" % (
                origin_year, origin_month, origin_day, origin_hour)
        elif self.resolution == "second":
            offset = int(context_json["contextResponses"][0]["contextElement"]
                         ["attributes"][0]["values"][0]["points"][0]["offset"])
            c = int(origin_second) - int(offset)
            if (int(origin_second) - c) < 0:
                origin_minute = str(int(origin_minute) - 1)
            origin_by_resolution = "%s-%s-%sT%s:%s:00.000Z" % (
                origin_year, origin_month, origin_day, origin_hour,
                origin_minute)
        else:
            assert False, " ERROR - resolution type \"%s\" is not allowed, review your tests in features..." % (
                self.resolution)

        # attributes
        assert str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["name"]) == "%s_%s" % (self.attributes_name, u'0'), \
            "  ERROR - in aggregated with name %s_%s" % (self.attributes_name, u'0')
        # values
        assert str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["attrName"]) == "%s_%s" % (self.attributes_name, u'0'), \
             "  ERROR - in aggregated with name %s_%s" % (self.attributes_name, u'0')
        assert str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["origin"]) == origin_by_resolution, \
             "  ERROR - in aggregated with origin %s" % (origin_by_resolution)
        assert str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["resolution"]) == self.resolution, \
             "  ERROR - in aggregated with resolution %s" % (self.resolution)
        assert str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["_id"]["range"]) == dupla[self.resolution], \
             "  ERROR - in aggregated with range %s" % (dupla[self.resolution])
        # points
        assert str(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["offset"]) == str(offset), \
             "  ERROR - in aggregated with offset %s" % (offset)
        if self.method == "sum":
            assert float(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["sum"]) == float(self.attributes_value), \
             "  ERROR - in aggregated with sum %s" % (str(self.attributes_value))
        elif self.method == "sum2":
            assert float(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["sum2"]) == (float(self.attributes_value)*float(self.attributes_value)), \
             "  ERROR - in aggregated with sum2 %s" % (str((float(self.attributes_value)*float(self.attributes_value))))
        elif self.method == "min":
            assert float(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["min"]) == float(self.attributes_value), \
             "  ERROR - in aggregated with min %s" % (str(self.attributes_value))
        elif self.method == "max":
            assert float(context_json["contextResponses"][0]["contextElement"]["attributes"][0]["values"][0]["points"][0]["max"]) == float(self.attributes_value), \
             "  ERROR - in aggregated with max %s" % (str(self.attributes_value))

        # contextElement
        assert str(context_json["contextResponses"][0]["contextElement"]["id"]) == self.entity_id, \
             "  ERROR - in aggregated with entity id %s" % (self.entity_id)
        assert str(context_json["contextResponses"][0]["contextElement"]["type"]) == self.entity_type, \
             "  ERROR - in aggregated with entity type %s" % (self.entity_type)
        assert str(context_json["contextResponses"][0]["contextElement"]["isPattern"]) == "False", \
             "  ERROR - in aggregated with isPattern equal to false"

        # context Responses
        assert str(context_json["contextResponses"][0]["statusCode"]["code"]) == "200", \
             "  ERROR - in aggregated with status Code equal to 200"
        assert str(context_json["contextResponses"][0]["statusCode"]["reasonPhrase"]) == "OK", \
             "  ERROR - in aggregated with reason Phrase equal to OK"
示例#4
0
    def verify_aggregates_in_mongo(self, resolution):
        """
        verify aggregates from mongo:
            - origin, max, min, sum sum2
        :param resolution: resolutions type (  month | day | hour | minute | second )
        """
        time_zone = 2
        find_dict = {
            "_id.attrName": {
                '$regex': '%s.*' % (self.attributes_name)
            },  #the regular expression is because in  multi attribute the name is with postfix + <_value>. ex: temperature_0
            "_id.resolution": resolution
        }

        origin_year = general_utils.get_date_only_one_value(
            self.date_time, "year")
        origin_month = general_utils.get_date_only_one_value(
            self.date_time, "month")
        origin_day = general_utils.get_date_only_one_value(
            self.date_time, "day")
        origin_hour = int(
            general_utils.get_date_only_one_value(self.date_time,
                                                  "hour")) - time_zone
        if origin_hour < 10: origin_hour = u'0' + str(origin_hour)
        origin_minute = general_utils.get_date_only_one_value(
            self.date_time, "minute")
        origin_second = general_utils.get_date_only_one_value(
            self.date_time, "second")

        world.mongo.connect("%s_%s" % (STH_DATABASE_PREFIX, self.service))
        world.mongo.choice_collection(
            "%s_%s_%s_%s.%s" % (STH_COLLECTION_PREFIX, self.service_path,
                                self.entity_id, self.entity_type, AGGR))
        cursor = world.mongo.find_with_retry(find_dict)
        assert cursor.count(
        ) != 0, " ERROR - the aggregated has not been stored in mongo successfully "
        doc_list = world.mongo.get_cursor_value(
            cursor)  # get all dictionaries into a cursor, return a list

        for doc in doc_list:

            offset = int(
                general_utils.get_date_only_one_value(self.date_time,
                                                      resolution))
            if resolution == "month":
                offset = offset - 1
                origin_by_resolution = "%s-01-01 00:00:00" % (origin_year)
            elif resolution == "day":
                offset = offset - 1
                origin_by_resolution = "%s-%s-01 00:00:00" % (origin_year,
                                                              origin_month)
            elif resolution == "hour":
                offset = offset - time_zone
                origin_by_resolution = "%s-%s-%s 00:00:00" % (
                    origin_year, origin_month, origin_day)
            elif resolution == "minute":
                origin_by_resolution = "%s-%s-%s %s:00:00" % (
                    origin_year, origin_month, origin_day, origin_hour)
            elif resolution == "second":
                c = 0
                MAX_SECS = 20
                while (c < MAX_SECS):
                    if float(doc["points"][offset]["min"]) == float(
                            self.attributes_value):
                        break
                    offset = offset - 1
                    if offset < 0: offset = 59
                    c = c + 1
                if (origin_second < c): origin_minute = origin_minute - 1
                origin_by_resolution = "%s-%s-%s %s:%s:00" % (
                    origin_year, origin_month, origin_day, origin_hour,
                    origin_minute)
            else:
                assert False, " ERROR - resolution type \"%s\" is not allowed, review your tests in features..." % (
                    resolution)

            assert str(
                doc["_id"]["origin"]
            ) == origin_by_resolution, " ERROR -- in origin field by the %s resolution in %s attribute" % (
                resolution, str(doc["_id"]["attrName"]))
            assert float(doc["points"][offset]["min"]) == float(
                self.attributes_value
            ), " ERROR -- in minimun value into offset %s in %s attribute" % (
                str(offset), str(doc["_id"]["attrName"]))
            assert float(doc["points"][offset]["max"]) == float(
                self.attributes_value
            ), " ERROR -- in maximun value into offset %s in %s attribute" % (
                str(offset), str(doc["_id"]["attrName"]))
            assert float(doc["points"][offset]["sum"]) == float(
                self.attributes_value
            ), " ERROR -- in sum value into offset %s in %s attribute" % (
                str(offset), str(doc["_id"]["attrName"]))
            assert float(doc["points"][offset]["sum2"]) == (
                float(self.attributes_value) * float(self.attributes_value)
            ), " ERROR -- in sum2 value into offset %s in %s attribute" % (
                str(offset), str(doc["_id"]["attrName"]))
        world.mongo.disconnect()