tests/test-simplekeyvaluefile.py
author Boris Feld <boris.feld@octobus.net>
Mon, 14 Jan 2019 17:15:21 +0100
changeset 41277 61f9ef23a12f
parent 37939 6eca47f6319d
child 41759 aaad36b88298
permissions -rw-r--r--
dagop: minor python optimization to `headrevs` Less lookup and less function call never hurt. This provides a small speedup on various run of the 'heads()' revset. This also buys back some of the slow down we observed in the previous changesets for single value lookup. Performance number: 0) before dagop.headrevs usage 1) after dagop.headrevs usage 2) after this change revset: heads(all()) plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast 0) 0.036503 0.032564 0.030024 0.032378 0.030887 0.036367 0.031713 0.032205 0.036467 0.032286 0.030300 1) 0.036668 0.035347 108% 0.035611 118% 0.035358 109% 0.035726 115% 0.036411 0.035261 111% 0.036096 112% 0.036052 0.035095 108% 0.035792 118% 2) 0.034254 93% 0.034482 0.035003 0.034353 0.033754 94% 0.034689 0.034361 0.035059 0.034636 0.034662 0.035465 revset: heads(-10000:-1) plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast 0) 0.003936 0.003218 0.003227 0.003302 0.003328 0.003848 0.003305 0.003252 0.003839 0.003306 0.003279 1) 0.003870 0.003785 117% 0.003821 118% 0.003780 114% 0.003769 113% 0.003776 0.003792 114% 0.003805 117% 0.003810 0.003798 114% 0.003840 117% 2) 0.003666 94% 0.003577 94% 0.003632 0.003644 0.003614 0.003638 0.003652 0.003632 0.003661 0.003660 0.003658 revset: (-5000:-1000) and heads(-10000:-1) plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast 0) 0.004244 0.003368 0.003313 0.003367 0.003327 0.004325 0.003401 0.003379 0.004310 0.003359 0.003396 1) 0.003969 93% 0.003862 114% 0.003834 115% 0.003810 113% 0.003822 114% 0.003940 91% 0.003908 114% 0.003814 112% 0.003986 92% 0.003954 117% 0.003816 112% 2) 0.003728 93% 0.003638 94% 0.003659 0.003685 0.003628 94% 0.003716 94% 0.003653 93% 0.003655 0.003748 94% 0.003740 94% 0.003686 revset: heads(matching(tip, "author")) plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast 0) 7.574666 7.545950 7.570743 7.578697 7.525725 7.509929 7.443854 7.488442 7.452880 7.445411 7.689107 1) 7.549390 7.389162 7.529790 7.536297 7.450467 7.555347 7.404586 7.514948 7.542794 7.524787 7.536918 2) 7.568294 7.479326 7.578624 7.380375 7.440102 7.454218 7.515189 7.556511 7.524585 7.537566 7.507418 revset: heads(matching(tip, "author")) and -10000:-1 plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast 0) 7.512533 7.605877 7.382894 7.462109 7.420086 7.575034 7.448452 7.549374 7.457880 7.450308 7.515019 1) 7.548677 7.551832 7.629598 7.494857 7.550554 7.521838 7.451794 error 7.321781 7.546885 7.557523 2) 7.451985 7.541044 7.506563 7.470928 7.512618 7.474988 7.498887 7.547930 7.560276 7.618599 7.465442 revset: (-10000:-1) and heads(matching(tip, "author")) plain min max first last reverse rev..rst rev..ast sort sor..rst sor..ast 0) 7.465419 7.570089 7.439594 7.521221 7.498716 7.492922 7.479108 7.552397 7.407888 error 7.468264 1) 7.539866 7.548045 7.491761 7.517170 7.469824 7.501990 7.579102 7.502568 7.578102 7.555754 7.567622 2) 7.370463 7.514712 7.497024 7.679428 7.638138 7.490775 7.472273 7.652587 7.584139 7.511893 7.466384
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     1
from __future__ import absolute_import
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     2
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     3
import unittest
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     4
import silenttestrunner
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     5
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     6
from mercurial import (
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     7
    error,
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     8
    scmutil,
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     9
)
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    10
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    11
class mockfile(object):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    12
    def __init__(self, name, fs):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    13
        self.name = name
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    14
        self.fs = fs
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    15
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    16
    def __enter__(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    17
        return self
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    18
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    19
    def __exit__(self, *args, **kwargs):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    20
        pass
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    21
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    22
    def write(self, text):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    23
        self.fs.contents[self.name] = text
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    24
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    25
    def read(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    26
        return self.fs.contents[self.name]
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    27
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    28
class mockvfs(object):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    29
    def __init__(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    30
        self.contents = {}
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    31
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    32
    def read(self, path):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    33
        return mockfile(path, self).read()
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    34
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    35
    def readlines(self, path):
32269
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
    36
        # lines need to contain the trailing '\n' to mock the real readlines
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
    37
        return [l for l in mockfile(path, self).read().splitlines(True)]
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    38
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    39
    def __call__(self, path, mode, atomictemp):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    40
        return mockfile(path, self)
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    41
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    42
class testsimplekeyvaluefile(unittest.TestCase):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    43
    def setUp(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    44
        self.vfs = mockvfs()
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    45
32269
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
    46
    def testbasicwritingiandreading(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    47
        dw = {b'key1': b'value1', b'Key2': b'value2'}
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    48
        scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(dw)
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    49
        self.assertEqual(sorted(self.vfs.read(b'kvfile').split(b'\n')),
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    50
                         [b'', b'Key2=value2', b'key1=value1'])
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    51
        dr = scmutil.simplekeyvaluefile(self.vfs, b'kvfile').read()
32269
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
    52
        self.assertEqual(dr, dw)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    53
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    54
    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    55
        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    56
        # the regex version.
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    57
        assertRaisesRegex = (# camelcase-required
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    58
            unittest.TestCase.assertRaisesRegexp)
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    59
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    60
    def testinvalidkeys(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    61
        d = {b'0key1': b'value1', b'Key2': b'value2'}
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    62
        with self.assertRaisesRegex(error.ProgrammingError,
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
    63
                                     'keys must start with a letter.*'):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    64
            scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(d)
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
    65
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    66
        d = {b'key1@': b'value1', b'Key2': b'value2'}
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    67
        with self.assertRaisesRegex(error.ProgrammingError, 'invalid key.*'):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    68
            scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(d)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    69
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    70
    def testinvalidvalues(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    71
        d = {b'key1': b'value1', b'Key2': b'value2\n'}
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    72
        with self.assertRaisesRegex(error.ProgrammingError,  'invalid val.*'):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    73
            scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(d)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    74
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    75
    def testcorruptedfile(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    76
        self.vfs.contents[b'badfile'] = b'ababagalamaga\n'
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    77
        with self.assertRaisesRegex(error.CorruptedState,
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
    78
                                     'dictionary.*element.*'):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    79
            scmutil.simplekeyvaluefile(self.vfs, b'badfile').read()
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    80
32270
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
    81
    def testfirstline(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    82
        dw = {b'key1': b'value1'}
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    83
        scmutil.simplekeyvaluefile(self.vfs, b'fl').write(dw, firstline=b'1.0')
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    84
        self.assertEqual(self.vfs.read(b'fl'), b'1.0\nkey1=value1\n')
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    85
        dr = scmutil.simplekeyvaluefile(self.vfs, b'fl')\
32270
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
    86
                    .read(firstlinenonkeyval=True)
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    87
        self.assertEqual(dr, {b'__firstline': b'1.0', b'key1': b'value1'})
32270
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
    88
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    89
if __name__ == "__main__":
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    90
    silenttestrunner.main(__name__)