Esempio n. 1
0
 def pause(self):
     global current_context
     if self.assert_state_changes:
         assert_eq('resume', self.state)
     self.state = "pause"
     current_context = self.parent
     print(self.name + ': pause')
Esempio n. 2
0
 def resume(self):
     global current_context
     if self.assert_state_changes:
         assert_eq('pause', self.state)
     self.state = "resume"
     current_context = self
     print(self.name + ': resume')
Esempio n. 3
0
def test_intenum():
    assert_is_instance(Python.two, int)
    assert_eq('Python.two', repr(Python.two))
    assert_eq('2', json.dumps(Python.two))
    assert_in(Python.two, Python)
    assert_in(2, Python)
    assert_not_in(4, Python)
Esempio n. 4
0
def test_common():
    o = qcore.MarkerObject("o @ qcore.test_examples")
    assert_is_not(o, qcore.miss)

    o = qcore.dict_to_object({"a": 1, "b": 2})
    assert_eq(o.a, 1)  # Zero overhead!
    assert_eq(o.b, 2)  # Zero overhead!
    def test_raise_value_error_with_invalid_dependencies(self, tmp_path):
        invalid_dependency_STRING = """
            raw_data_dir: "dummy"
            dataset_name: "dummy"
            base_features:
              - name: "TIME"
                dtype: DATETIME
            transforming_features:
              - name: "weekday"
                index: 1
                dtype: STRING
                dependencies:
                  - "date"
        """
        config_path = tmp_path / "tmp.yaml"
        write_str_to_file(invalid_dependency_STRING, config_path)
        with AssertRaises(AssertionError) as assert_raises:
            feature_config_helper.FeatureConfigHelper(config_path)

        error_message = assert_raises.expected_exception_found
        assert_eq(
            True,
            error_message.args[0].startswith(
                "Feature weekday depends on feature date that is undefined."
            ),
        )
Esempio n. 6
0
def test_cached_per_instance_pickling():

    # make sure cached stuff doesn't appear in the pickled representation

    obj = PickleTestClass()
    obj.attr = 'spam'
    assert_eq(set(),
              set(PickleTestClass.f.__cached_per_instance_cache__.keys()))
    obj.f('my hovercraft is full of eels')
    assert_eq({id(obj)},
              set(PickleTestClass.f.__cached_per_instance_cache__.keys()))

    serialized = pickle.dumps(obj)
    assert_not_in(b'my hovercraft is full of eels', serialized)
    assert_in(b'spam', serialized)

    restored = pickle.loads(serialized)
    assert_eq({id(obj)},
              set(PickleTestClass.f.__cached_per_instance_cache__.keys()))
    restored.f('my hovercraft is full of eels')
    assert_eq({id(obj), id(restored)},
              set(PickleTestClass.f.__cached_per_instance_cache__.keys()))
    assert_eq('spam', obj.attr)

    # make sure we can use this with a custom __getstate__

    class X(object):
        @cached_per_instance()
        def f(self, x):
            return x

        def __getstate__(self):
            return {}

    X().f(1)
Esempio n. 7
0
def _assert_write_result(text, indent, expected):
    buf = StringIO()

    with asynq.mock.patch('asynq.debug.stdout', buf):
        asynq.debug.write(text, indent=indent)

    assert_eq(expected, buf.getvalue())
Esempio n. 8
0
 def nested():
     assert_eq(v.get(), inner_val)
     yield DebugBatchItem()
     with v.override('c'):
         yield DebugBatchItem()  # just so other function gets scheduled
         assert_eq(v.get(), 'c')
         yield DebugBatchItem()
    def test_raise_assertion_error_with_duplicate_features(self, tmp_path):
        config_STRING = """
            raw_data_dir: "dummy"
            dataset_name: "dummy"
            base_features:
              - name: "TIME"
                dtype: DATETIME
            transforming_features:
              - name: "weekday"
                index: 1
                dtype: STRING
                dependencies:
                - "TIME"
              - name: "weekday"
                index: 2
                dtype: STRING
        """
        config_path = tmp_path / "tmp.yaml"
        write_str_to_file(config_STRING, config_path)
        with AssertRaises(AssertionError) as assert_raises:
            feature_config_helper.FeatureConfigHelper(config_path)

        error_message = assert_raises.expected_exception_found
        assert_eq(
            True,
            error_message.args[0].startswith(
                "There are duplicate objects in the list: "
            ),
        )
 def test_extract_config_1(self, tmp_path):
     subset_features = ["e"]
     expected_STRING = """
         raw_data_dir: "dummy"
         dataset_name: "dummy"
         base_features:
           - name: "a"
             dtype: STRING
         transforming_features:
           - name: "b"
             index: 1
             dtype: STRING
             dependencies:
               - "a"
           - name: "c"
             index: 2
             dtype: STRING
             dependencies:
               - "a"
               - "b"
           - name: "e"
             index: 4
             dtype: STRING
             dependencies:
               - "c"
     """
     new_config_path = str(tmp_path / "new_tmp.yaml")
     write_str_to_file(expected_STRING, new_config_path)
     new_config = self.fm_helper.extract_config(selected_features=subset_features)
     assert_eq(parse_feature_config(new_config_path), new_config)
Esempio n. 11
0
def _check_deduplicate():
    global i
    i = 0
    AsyncObject.cls_value = 0

    yield increment_value.asynq()
    assert_eq(1, i)

    yield increment_value.asynq(), increment_value.asynq(1)
    assert_eq(2, i)

    obj = AsyncObject()
    yield obj.increment_value_method.asynq(), obj.increment_value_method.asynq(
        1)
    assert_eq(1, obj.value)

    yield AsyncObject.deduplicated_static_method.asynq(), \
        AsyncObject.deduplicated_static_method.asynq(1)
    assert_eq(1, AsyncObject.cls_value)

    i = 0
    yield recursive_call_with_dirty.asynq()

    yield call_with_dirty.asynq()

    if sys.version_info >= (3, 0):
        with AssertRaises(TypeError):
            yield call_with_kwonly_arg.asynq(1)
        assert_eq(1, (yield call_with_kwonly_arg.asynq(arg=1)))
    def test_raise_value_error_with_invalid_indexes(self, tmp_path):
        invalid_index_STRING = """
            # invalid config with indexes are not continuous
            raw_data_dir: "dummy"
            dataset_name: "dummy"
            base_features:
              - name: "TIME"
                dtype: "DATETIME"
            transforming_features:
              - name: "weekday"
                index: 1
                dtype: INT32
                dependencies:
                  - "TIME"
              - name: "hour"
                index: 1
                dtype: INT32
                dependencies:
                  - "TIME"
        """
        config_path = tmp_path / "tmp.yaml"
        write_str_to_file(invalid_index_STRING, config_path)
        with AssertRaises(ValueError) as assert_raises:
            feature_config_helper.FeatureConfigHelper(config_path)

        error_message = assert_raises.expected_exception_found
        assert_eq(
            True,
            error_message.args[0].startswith(
                "Feature indexes must be a list of increasing positive integers. "
                "Got indexes = [1, 1]"
            ),
        )
Esempio n. 13
0
    def test_retry_can_take_multiple_exceptions(self):
        max_tries = 4
        any_expected_exception_type = AnyException
        any_other_expected_exception_type = AnyOtherException

        expected_exceptions = (any_expected_exception_type,
                               any_other_expected_exception_type)

        for method in (self.create_any_function,
                       self.create_generator_function):
            any_function, fn_body = method(expected_exceptions, max_tries)
            fn_body.side_effect = any_expected_exception_type

            with AssertRaises(any_expected_exception_type):
                list(any_function())

            assert_eq(max_tries, fn_body.call_count)
            fn_body.reset_mock()

            fn_body.side_effect = any_other_expected_exception_type

            with AssertRaises(any_other_expected_exception_type):
                list(any_function())

            assert_eq(max_tries, fn_body.call_count)
Esempio n. 14
0
def test_typed_value() -> None:
    val = TypedValue(str)
    assert_is(str, val.typ)
    assert_eq("str", str(val))
    assert val.is_type(str)
    assert not val.is_type(int)

    assert_can_assign(val, val)
    assert_cannot_assign(val, TypedValue(int))
    assert_can_assign(val, KnownValue("x"))
    assert_can_assign(val, MultiValuedValue([val, KnownValue("x")]))
    assert_cannot_assign(val,
                         MultiValuedValue([KnownValue("x"),
                                           TypedValue(int)]))

    float_val = TypedValue(float)
    assert_eq("float", str(float_val))
    assert_can_assign(float_val, KnownValue(1.0))
    assert_can_assign(float_val, KnownValue(1))
    assert_cannot_assign(float_val, KnownValue(""))
    assert_can_assign(float_val, TypedValue(float))
    assert_can_assign(float_val, TypedValue(int))
    assert_cannot_assign(float_val, TypedValue(str))
    assert_can_assign(float_val, TypedValue(mock.Mock))

    assert_cannot_assign(float_val, SubclassValue(TypedValue(float)))
    assert_can_assign(TypedValue(type), SubclassValue(TypedValue(float)))
Esempio n. 15
0
def test_format_error():
    assert_is(None, asynq.debug.format_error(None))

    # Syntax highlighting adds color text between words
    asynq.debug.enable_traceback_syntax_highlight(False)
    e = RuntimeError()
    expected = 'RuntimeError\n'
    assert_eq(expected, asynq.debug.format_error(e))

    e._task = async_fn.asynq()
    formatted = asynq.debug.format_error(e)
    assert_in(expected, formatted)

    try:
        raise RuntimeError
    except RuntimeError:
        e._traceback = sys.exc_info()[2]

    formatted = asynq.debug.format_error(e)
    assert_in(expected, formatted)
    assert_in('Traceback', formatted)

    # Each single word, and unformatted text should be present
    asynq.debug.enable_traceback_syntax_highlight(True)

    expected = 'RuntimeError'
    formatted = asynq.debug.format_error(e)
    assert_in(expected, formatted)
    assert_in('Traceback', formatted)
Esempio n. 16
0
    def test_set(self):
        with self.scopes.add_scope(ScopeType.module_scope, scope_node=None):
            self.scopes.set("multivalue", KnownValue(1), None, None)
            assert_eq(KnownValue(1), self.scopes.get("multivalue", None, None))
            self.scopes.set("multivalue", KnownValue(2), None, None)
            assert_eq(
                MultiValuedValue([KnownValue(1), KnownValue(2)]),
                self.scopes.get("multivalue", None, None),
            )
            self.scopes.set("multivalue", KnownValue(3), None, None)
            assert_eq(
                MultiValuedValue([KnownValue(1), KnownValue(2), KnownValue(3)]),
                self.scopes.get("multivalue", None, None),
            )

            # if the values set are the same, don't make a MultiValuedValue
            self.scopes.set("same", KnownValue(1), None, None)
            assert_eq(KnownValue(1), self.scopes.get("same", None, None))
            self.scopes.set("same", KnownValue(1), None, None)
            assert_eq(KnownValue(1), self.scopes.get("same", None, None))

            # even if they are UNRESOLVED_VALUE
            self.scopes.set("unresolved", UNRESOLVED_VALUE, None, None)
            assert_is(UNRESOLVED_VALUE, self.scopes.get("unresolved", None, None))
            self.scopes.set("unresolved", UNRESOLVED_VALUE, None, None)
            assert_is(UNRESOLVED_VALUE, self.scopes.get("unresolved", None, None))
Esempio n. 17
0
 def test_typed_value_set(self):
     self.scopes.set("value", TypedValue(dict), None, None)
     assert_eq(TypedValue(dict), self.scopes.get("value", None, None))
     self.scopes.set(
         "value", DictIncompleteValue([]), None, None
     )  # subclass of TypedValue
     assert_eq(DictIncompleteValue([]), self.scopes.get("value", None, None))
Esempio n. 18
0
def test_typed_value():
    val = TypedValue(str)
    assert_is(str, val.typ)
    assert_eq("str", str(val))
    assert val.is_type(str)
    assert not val.is_type(int)

    assert val.is_value_compatible(TypedValue(str))
    assert not val.is_value_compatible(TypedValue(int))
    assert val.is_value_compatible(
        MultiValuedValue([KnownValue("x"), TypedValue(str)]))
    assert not val.is_value_compatible(
        MultiValuedValue([KnownValue("x"), TypedValue(int)]))

    float_val = TypedValue(float)
    assert_eq("float", str(float_val))
    assert float_val.is_value_compatible(KnownValue(1.0))
    assert float_val.is_value_compatible(KnownValue(1))
    assert not float_val.is_value_compatible(KnownValue(""))
    assert float_val.is_value_compatible(TypedValue(float))
    assert float_val.is_value_compatible(TypedValue(int))
    assert not float_val.is_value_compatible(TypedValue(str))
    assert float_val.is_value_compatible(TypedValue(value.mock.Mock))

    assert not float_val.is_value_compatible(SubclassValue(float))
    assert TypedValue(type).is_value_compatible(SubclassValue(float))
Esempio n. 19
0
def test_bitwise_resize():
    assert_eq(
        # fmt: off
        [1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0],
        # fmt: on
        encoders.bitwise_resize([0b111, 0b101, 0b001, 0b010, 0b000],
                                new_bitwidth=1),
    )
Esempio n. 20
0
def test_make_async_decorator():
    assert_eq(18, square(3))
    assert_eq(18, MyClass.square(3))
    assert_eq(18, square.asynq(3).value())
    assert_eq(18, MyClass.square.asynq(3).value())

    assert not is_pure_async_fn(square)
    assert_eq("@double_return_value()", square.name())
Esempio n. 21
0
def test_maybe_wrap_new():
    counter = Counter()
    with asynq.mock.patch('asynq.tests.test_mock.fn', counter.add_call):
        fn()
    assert_eq(1, counter.count)

    with asynq.mock.patch('asynq.tests.test_mock.fn', 'capybara'):
        assert_eq('capybara', fn)
Esempio n. 22
0
 def test_getargspec_py3_only():
     spec = inspect.ArgSpec(args=['a', 'b'],
                            varargs='args',
                            keywords=None,
                            defaults=None)
     assert_eq(spec, qcore.inspection.getargspec(fun_with_annotations))
     with AssertRaises(ValueError):
         qcore.inspection.getargspec(fun_with_kwonly_args)
Esempio n. 23
0
def test_maybe_wrap_new():
    counter = Counter()
    with asynq.mock.patch("asynq.tests.test_mock.fn", counter.add_call):
        fn()
    assert_eq(1, counter.count)

    with asynq.mock.patch("asynq.tests.test_mock.fn", "capybara"):
        assert_eq("capybara", fn)
Esempio n. 24
0
    def test_assert_if_not_unique(self):
        items = [1, 2, 3, 1]
        with AssertRaises(AssertionError) as assert_raises:
            utils.check_uniqueness(items)

        assertion_error = assert_raises.expected_exception_found
        assert_eq("There are duplicate objects in the list: {1: 2}.",
                  assertion_error.args[0])
Esempio n. 25
0
 def test_slots(self):
     obj1 = ObjectWithSlots(1)
     obj2 = ObjectWithSlots(2)
     obj3 = ObjectWithSlots(1)
     assert_eq(obj1, obj3)
     assert_eq(obj1, obj1)
     assert_ne(obj1, obj2)
     self._check_repr_and_str('ObjectWithSlots(oid=1)', obj1)
Esempio n. 26
0
def test_marker_object():
    assert_eq("text", str(qcore.MarkerObject("text")))

    with AssertRaises(TypeError):
        qcore.MarkerObject(b"bytes")

    # MarkerObjects should be unique
    assert_ne(qcore.MarkerObject("name"), qcore.MarkerObject("name"))
Esempio n. 27
0
    def test_retry_preserves_argspec(self):
        def fn(foo, bar, baz=None, **kwargs):
            pass

        decorated = aretry(Exception)(fn)

        assert_eq(inspect.getargspec(fn),
                  inspect.getargspec(get_original_fn(decorated)))
Esempio n. 28
0
def test_lrs():
    # NB: This test handles strings
    symbols = [2, 2, 0, 1, 0, 2, 0, 1, 2, 1, 2, 0, 1, 2, 1, 0, 0, 1, 0, 0, 0]

    import lrs

    assert_eq(0.6146,
              lrs.lrs(symbols, threshold=3).min_entropy,
              tolerance=0.0001)
    def test_raise_value_error_with_invalid_feature_to_extract(self, tmp_path):
        subset_features = ["a", "y", "z"]
        with AssertRaises(ValueError) as assert_raises:
            self.fm_helper.extract_config(selected_features=subset_features)

        error_message = assert_raises.expected_exception_found
        assert_eq(
            error_message.args[0], "Features ['y', 'z'] are not in the original config."
        )
Esempio n. 30
0
    def test_retry_retries_on_provided_exception(self):
        max_tries = 4
        function, fn_body = self.create_function(AnyException, max_tries)
        fn_body.side_effect = AnyException

        with AssertRaises(AnyException):
            function()

        assert_eq(max_tries, fn_body.call_count)