Beispiel #1
0
    def test_load_existent(self):
        file_name = self.temp_dir + "/foo.yml"
        with open(file_name, "w") as fp:
            fp.write("foo: bar")

        kvf = KVFile(file_name)
        assert len(kvf) == 1
        assert kvf.get("foo", "") == "bar"
Beispiel #2
0
class PerformanceRecorder:
    def __init__(self, file_name, record_name, capture_traceback):
        self.file_name = file_name
        self.record_name = record_name

        self.record = []
        self.db_recorder = AllDBRecorder(self.on_op)
        self.cache_recorder = AllCacheRecorder(self.on_op)
        self.capture_traceback = capture_traceback

    def __enter__(self):
        self.db_recorder.__enter__()
        self.cache_recorder.__enter__()
        self.load_recordings()

    def __exit__(self, exc_type, exc_value, exc_traceback):
        self.cache_recorder.__exit__(exc_type, exc_value, exc_traceback)
        self.db_recorder.__exit__(exc_type, exc_value, exc_traceback)

        if exc_type is None:
            self.save_or_assert()

    def on_op(self, op):
        record = {op.name: op.query}

        if self.capture_traceback and self.capture_traceback(op):
            record["traceback"] = traceback.StackSummary.from_list(
                op.traceback).format()

        self.record.append(record)

    def load_recordings(self):
        self.records_file = KVFile(self.file_name)

    def save_or_assert(self):
        orig_record = self.records_file.get(self.record_name, None)
        if perf_rec_settings.MODE == "none":
            assert (
                orig_record is not None
            ), "Original performance record does not exist for {}".format(
                self.record_name)

        if orig_record is not None:
            msg = "Performance record did not match for {}".format(
                self.record_name)
            if not pytest_plugin.in_pytest:
                msg += "\n{}".format(record_diff(orig_record, self.record))
            assert self.record == orig_record, msg

        self.records_file.set_and_save(self.record_name, self.record)

        if perf_rec_settings.MODE == "all":
            assert (
                orig_record is not None
            ), "Original performance record did not exist for {}".format(
                self.record_name)
Beispiel #3
0
    def test_load_non_dictionary(self):
        file_name = self.temp_dir + "/foo.yml"
        with open(file_name, "w") as fp:
            fp.write("[not, a, dictionary]")

        with pytest.raises(TypeError) as excinfo:
            KVFile(file_name)
        assert "not a dictionary" in str(excinfo.value)
Beispiel #4
0
    def test_load_non_dictionary(self):
        file_name = self.temp_dir + '/foo.yml'
        with open(file_name, 'w') as fp:
            fp.write('[not, a, dictionary]')

        with pytest.raises(TypeError) as excinfo:
            KVFile(file_name)
        assert 'not a dictionary' in six.text_type(excinfo.value)
Beispiel #5
0
    def test_load_second_same(self):
        kvf = KVFile(self.temp_dir + "/foo.yml")
        kvf.set_and_save("foo", "bar")
        kvf2 = KVFile(self.temp_dir + "/foo.yml")

        assert len(kvf2) == 1
        assert kvf2.get("foo", "") == "bar"
Beispiel #6
0
    def test_sets_dont_cause_append_duplication(self):
        file_name = self.temp_dir + '/foo.yml'
        kvf = KVFile(file_name)
        kvf.set_and_save('foo', 'bar')
        kvf.set_and_save('foo2', 'bar')

        with open(file_name, 'r') as fp:
            lines = fp.readlines()
            fp.seek(0)
            data = yaml.safe_load(fp)

        assert len(lines) == 2
        assert data == {'foo': 'bar', 'foo2': 'bar'}
Beispiel #7
0
    def test_sets_dont_cause_append_duplication(self):
        file_name = self.temp_dir + "/foo.yml"
        kvf = KVFile(file_name)
        kvf.set_and_save("foo", "bar")
        kvf.set_and_save("foo2", "bar")

        with open(file_name, "r") as fp:
            lines = fp.readlines()
            fp.seek(0)
            data = yaml.safe_load(fp)

        assert len(lines) == 2
        assert data == {"foo": "bar", "foo2": "bar"}
Beispiel #8
0
    def test_get_after_set_same(self):
        kvf = KVFile(self.temp_dir + "/foo.yml")
        kvf.set_and_save("foo", "bar")

        assert len(kvf) == 1
        assert kvf.get("foo", "") == "bar"
Beispiel #9
0
    def test_load_whitespace_empty(self):
        file_name = self.temp_dir + "/foo.yml"
        with open(file_name, "w") as fp:
            fp.write(" \n")

        assert len(KVFile(file_name)) == 0
Beispiel #10
0
 def test_load_non_existent_is_empty(self):
     kvf = KVFile(self.temp_dir + "/foo.yml")
     assert len(kvf) == 0
     default = object()
     assert kvf.get("foo", default) is default
Beispiel #11
0
 def setUp(self):
     super(KVFileTests, self).setUp()
     KVFile._clear_load_cache()
     self.temp_dir = mkdtemp()
Beispiel #12
0
    def test_load_second_same(self):
        kvf = KVFile(self.temp_dir + '/foo.yml')
        kvf.set_and_save('foo', 'bar')
        kvf2 = KVFile(self.temp_dir + '/foo.yml')

        assert kvf2.get('foo', '') == 'bar'
Beispiel #13
0
    def test_load_existent(self):
        file_name = self.temp_dir + '/foo.yml'
        with open(file_name, 'w') as fp:
            fp.write('foo: bar')

        assert KVFile(file_name).get('foo', '') == 'bar'
Beispiel #14
0
 def test_load_non_existent_is_empty(self):
     kvf = KVFile(self.temp_dir + '/foo.yml')
     default = object()
     assert kvf.get('foo', default) is default
Beispiel #15
0
 def test_load_no_permissions(self):
     with pytest.raises(IOError):
         KVFile("/")
Beispiel #16
0
 def load_recordings(self):
     self.records_file = KVFile(self.file_name)
Beispiel #17
0
class PerformanceRecorder:
    def __init__(self, file_name, record_name):
        self.file_name = file_name
        self.record_name = record_name

        self.record = []
        self.db_recorder = AllDBRecorder(self.on_db_op)
        self.cache_recorder = AllCacheRecorder(self.on_cache_op)

    def __enter__(self):
        self.db_recorder.__enter__()
        self.cache_recorder.__enter__()
        self.load_recordings()

    def __exit__(self, exc_type, exc_value, exc_traceback):
        self.cache_recorder.__exit__(exc_type, exc_value, exc_traceback)
        self.db_recorder.__exit__(exc_type, exc_value, exc_traceback)

        if exc_type is None:
            self.save_or_assert()

    def on_db_op(self, db_op):
        name_parts = ["db"]
        if db_op.alias != DEFAULT_DB_ALIAS:
            name_parts.append(db_op.alias)
        name = "|".join(name_parts)

        self.record.append({name: db_op.sql})

    def on_cache_op(self, cache_op):
        name_parts = ["cache"]
        if cache_op.alias != DEFAULT_CACHE_ALIAS:
            name_parts.append(cache_op.alias)
        name_parts.append(cache_op.operation)
        name = "|".join(name_parts)

        self.record.append({name: cache_op.key_or_keys})

    def load_recordings(self):
        self.records_file = KVFile(self.file_name)

    def save_or_assert(self):
        orig_record = self.records_file.get(self.record_name, None)

        if perf_rec_settings.MODE == "none":
            assert (
                orig_record is not None
            ), "Original performance record does not exist for {}".format(
                self.record_name)

        if orig_record is not None:
            msg = "Performance record did not match for {}".format(
                self.record_name)
            if not pytest_plugin.in_pytest:
                msg += "\n{}".format(record_diff(orig_record, self.record))
            assert self.record == orig_record, msg

        self.records_file.set_and_save(self.record_name, self.record)

        if perf_rec_settings.MODE == "all":
            assert (
                orig_record is not None
            ), "Original performance record did not exist for {}".format(
                self.record_name)