Beispiel #1
0
 def test_encode_simple_job(self):
     r = Result(input=FactA(), output=FactB())
     r_json = r.to_json()
     new_r = Result.from_json(r_json)
     self.assertEqual(r.identifier, new_r.identifier)
     self.assertEqual(r.input.get(), new_r.input.get())
     self.assertEqual(r.output, new_r.output)
     for a, b in zip(r.output, new_r.output):
         self.assertEqual(a, b)
     self.assertEqual(r.clock.start_date, new_r.clock.start_date)
     self.assertEqual(r.clock.end_date, new_r.clock.end_date)
Beispiel #2
0
def execute_collector_by_name(collector_name, fact_or_composite):
    global available_collectors
    logger.info("Execute collector {} with {}".format(collector_name,
                                                      type(fact_or_composite)))
    result = Result(input=fact_or_composite, status=StatusCode.empty)
    if collector_name in available_collectors:
        return available_collectors[collector_name].run(result)
    result.status = (
        StatusCode.error,
        "Could not find collector {}".format(collector_name),
    )
    return result
Beispiel #3
0
    def test_job_result_errored(self):
        a = FactA()
        j = Result(input=a)

        j.status = StatusCode.error
        j_json = json.dumps(j, cls=encode)
        new_j = json.loads(j_json, object_hook=decode)

        self.assertEqual(
            True,
            StatusCode.is_errored(new_j.status["status"]),
            StatusCode.is_errored(j.status["status"]),
        )
        self.assertEqual(j.status, new_j.status)
Beispiel #4
0
 def test_encode_job_using_json(self):
     r = Result()
     r.input = FactA(a="42", b=2)
     r.output = FactB(a="42", b=2)
     r.clock.start()
     r_json = json.dumps(r, cls=encode)
     new_r = json.loads(r_json, object_hook=decode)
     self.assertEqual(r.identifier, new_r.identifier)
     self.assertEqual(r.input.get(), new_r.input.get())
     self.assertEqual(r.output, new_r.output)
     for a, b in zip(r.output, new_r.output):
         self.assertEqual(a, b)
     self.assertEqual(r.clock.start_date, new_r.clock.start_date)
     self.assertEqual(r.clock.end_date, new_r.clock.end_date)
Beispiel #5
0
    def test_job_result_error_msg(self):
        a = FactA()
        j = Result(input=a)

        j.status = StatusCode.finished, "this is an error"
        j_json = json.dumps(j, cls=encode)
        new_j = json.loads(j_json, object_hook=decode)

        self.assertEqual(
            False,
            StatusCode.is_errored(new_j.status["status"]),
            StatusCode.is_errored(j.status["status"]),
        )
        self.assertEqual(j.status, new_j.status)
        self.assertEqual("Finished", j.status["code"], new_j.status["code"])
        self.assertEqual("this is an error", j.status["error"],
                         new_j.status["error"])
Beispiel #6
0
    def test_job_result_composite(self):
        a = FactA()
        b = FactA()
        j = Result(input=Composite(a, b))

        j.status = StatusCode.finished
        j_json = json.dumps(j, cls=encode)
        new_j = json.loads(j_json, object_hook=decode)

        self.assertEqual(
            False,
            StatusCode.is_errored(new_j.status["status"]),
            StatusCode.is_errored(j.status["status"]),
        )
        self.assertEqual(j.status, new_j.status)
        self.assertEqual("Finished", j.status["code"], new_j.status["code"])
        self.assertEqual(j.input, new_j.input)
Beispiel #7
0
    def test_job_result_output2(self):
        r = Result()
        r.input = FactA(a=1, b=2)
        r.output = [FactA(a=10, b=20), FactA(a=12, b=22)]

        r_json = r.to_json()
        r_json2 = json.dumps(r, cls=encode)
        new_r = json.loads(r_json2, object_hook=decode)
        new_r_json = new_r.to_json()

        self.assertEqual(r_json["input"], new_r_json["input"])
        for a, b in zip(r_json["input"], new_r_json["input"]):
            self.assertTrue(a == b)

        self.assertEqual(r_json["output"], new_r_json["output"])
        for a, b in zip(r_json["output"], new_r_json["output"]):
            self.assertTrue(a == b)
Beispiel #8
0
    def test_job_result_output(self):
        a = FactA()
        b = FactA(a=1, b=2, c=3)
        j = Result(input=a, output=b)

        j_json = json.dumps(j, cls=encode)
        new_j = json.loads(j_json, object_hook=decode)
        self.assertEqual(new_j.output, j.output)
        self.assertEqual(False, StatusCode.is_errored(j.status["status"]))
        self.assertEqual(1, len(j.output))
        self.assertEqual(j.output[0], b)
        self.assertEqual(j.output[0].get_info(), b.get_info())
Beispiel #9
0
    def test_encode_complex_job_01(self):
        r = Result()
        r.input = FactA(a="42", b=2)
        r.output = FactB(a="42", b=2)
        r_json = r.to_json()
        new_r = Result.from_json(r_json)

        self.assertEqual(r.identifier, new_r.identifier)

        self.assertIsInstance(r.input, Composable)
        self.assertIsInstance(new_r.input, Composable)
        self.assertEqual(r.input.get(), new_r.input.get())
        self.assertEqual(r.output, new_r.output)
Beispiel #10
0
def _exec_collector(collector):
    print_state(collector)
    allowed_input = collector.get_allowed_input_as_list()
    test_inputs = []
    for input in allowed_input:
        test_inputs.append(_gen_input(input))

    for input in test_inputs:
        print("\n+ Running collector with input: ", input)
        print("+\t -> ", input.get_fields())
        result = Result(input=input)
        result = collector.run(result)

        print("@ Got result: ", result.status)
        if result.output:
            for i in result.output:
                print("@-----------------\n@\t->", i)
                if i:
                    for f in i.get_fields():
                        print("@\t\t->", f, ":", getattr(i, f).value)
Beispiel #11
0
    def test_encode_complex_job_02(self):
        r = Result()
        r.input = Composite(FactA(a="42", b=2), FactB(a=1, b=2))
        r.output = [FactA(a=1, b=4), FactA(a=2, b=3), FactB(a=3, b=2), FactB(a=4, b=1)]
        r_json = r.to_json()
        new_r = Result.from_json(r_json)

        self.assertEqual(r.identifier, new_r.identifier)

        self.assertIsInstance(r.input, Composable)
        self.assertIsInstance(new_r.input, Composable)
        self.assertEqual(r.input.get(), new_r.input.get())

        self.assertEqual(r.output, new_r.output)
        self.assertEqual(len(r.output), 4)
Beispiel #12
0
def create_result(input=None):
    return Result(input=input, status=StatusCode.empty)
Beispiel #13
0
    def test_job_not_altered(self):
        r = Result()
        r.input = Composite(FactA(a=1, b=2), FactA(a=10, b=20))
        r.output = FactA(a=30, b=40)
        r_json = r.to_json()

        input_should_be = [
            {
                "__class__": "FactA",
                "__module__": "test_job",
                "fields": {
                    "a": {
                        "__class__": "IntegerField",
                        "__module__": "opulence.common.fields.fields",
                        "value": 1,
                        "default": None,
                        "mandatory": False,
                    },
                    "b": {
                        "__class__": "StringField",
                        "__module__": "opulence.common.fields.fields",
                        "value": "2",
                        "default": "b",
                        "mandatory": False,
                    },
                    "c": {
                        "__class__": "StringField",
                        "__module__": "opulence.common.fields.fields",
                        "value": "c",
                        "default": "c",
                        "mandatory": False,
                    },
                },
            },
            {
                "__class__": "FactA",
                "__module__": "test_job",
                "fields": {
                    "a": {
                        "__class__": "IntegerField",
                        "__module__": "opulence.common.fields.fields",
                        "value": 10,
                        "default": None,
                        "mandatory": False,
                    },
                    "b": {
                        "__class__": "StringField",
                        "__module__": "opulence.common.fields.fields",
                        "value": "20",
                        "default": "b",
                        "mandatory": False,
                    },
                    "c": {
                        "__class__": "StringField",
                        "__module__": "opulence.common.fields.fields",
                        "value": "c",
                        "default": "c",
                        "mandatory": False,
                    },
                },
            },
        ]

        output_should_be = [{
            "__class__": "FactA",
            "__module__": "test_job",
            "fields": {
                "a": {
                    "__class__": "IntegerField",
                    "__module__": "opulence.common.fields.fields",
                    "value": 30,
                    "default": None,
                    "mandatory": False,
                },
                "b": {
                    "__class__": "StringField",
                    "__module__": "opulence.common.fields.fields",
                    "value": "40",
                    "default": "b",
                    "mandatory": False,
                },
                "c": {
                    "__class__": "StringField",
                    "__module__": "opulence.common.fields.fields",
                    "value": "c",
                    "default": "c",
                    "mandatory": False,
                },
            },
        }]

        self.assertEqual(input_should_be, r_json["input"])
        self.assertEqual(output_should_be, r_json["output"])
Beispiel #14
0
 def test_job_empty(self):
     r = Result()
     r_json = r.to_json()
     new_r = Result.from_json(r_json)
     self.assertEqual(new_r.to_json(), r.to_json())
Beispiel #15
0
    def test_job_result_output3(self):
        r = Result()
        r.input = Composite(FactA(a=1, b=2), FactA(a=10, b=20))
        r.output = FactA(a=10, b=20)

        r_json = r.to_json()
        output_should_be = [{
            "__class__": "FactA",
            "__module__": "test_job",
            "fields": {
                "a": {
                    "__class__": "IntegerField",
                    "__module__": "opulence.common.fields.fields",
                    "value": 10,
                    "default": None,
                    "mandatory": False,
                },
                "b": {
                    "__class__": "StringField",
                    "__module__": "opulence.common.fields.fields",
                    "value": "20",
                    "default": "b",
                    "mandatory": False,
                },
                "c": {
                    "__class__": "StringField",
                    "__module__": "opulence.common.fields.fields",
                    "value": "c",
                    "default": "c",
                    "mandatory": False,
                },
            },
        }]

        input_should_be = [
            {
                "__class__": "FactA",
                "__module__": "test_job",
                "fields": {
                    "a": {
                        "__class__": "IntegerField",
                        "__module__": "opulence.common.fields.fields",
                        "value": 1,
                        "default": None,
                        "mandatory": False,
                    },
                    "b": {
                        "__class__": "StringField",
                        "__module__": "opulence.common.fields.fields",
                        "value": "2",
                        "default": "b",
                        "mandatory": False,
                    },
                    "c": {
                        "__class__": "StringField",
                        "__module__": "opulence.common.fields.fields",
                        "value": "c",
                        "default": "c",
                        "mandatory": False,
                    },
                },
            },
            {
                "__class__": "FactA",
                "__module__": "test_job",
                "fields": {
                    "a": {
                        "__class__": "IntegerField",
                        "__module__": "opulence.common.fields.fields",
                        "value": 10,
                        "default": None,
                        "mandatory": False,
                    },
                    "b": {
                        "__class__": "StringField",
                        "__module__": "opulence.common.fields.fields",
                        "value": "20",
                        "default": "b",
                        "mandatory": False,
                    },
                    "c": {
                        "__class__": "StringField",
                        "__module__": "opulence.common.fields.fields",
                        "value": "c",
                        "default": "c",
                        "mandatory": False,
                    },
                },
            },
        ]

        self.assertEqual(r_json["output"], output_should_be)
        self.assertEqual(r_json["input"], input_should_be)
        new_r = Result.from_json(r_json)
        new_r_json = new_r.to_json()

        for a, b in zip(r.to_json()["input"], new_r_json["input"]):
            self.assertTrue(a == b)
        for a, b in zip(r.to_json()["output"], new_r_json["output"]):
            self.assertTrue(a == b)