OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import unittest |
| 6 |
| 7 |
| 8 from testing_support.git.repo import GitRepo |
| 9 from testing_support.git.schema import GitRepoSchema |
| 10 |
| 11 |
| 12 class GitRepoSchemaTestBase(unittest.TestCase): |
| 13 """A TestCase with a built-in GitRepoSchema. |
| 14 |
| 15 Expects a class variable REPO_SCHEMA to be a GitRepoSchema string in the form |
| 16 described by that class. |
| 17 |
| 18 You may also set class variables in the form COMMIT_%(commit_name)s, which |
| 19 provide the content for the given commit_name commits. |
| 20 |
| 21 You probably will end up using either GitRepoReadOnlyTestBase or |
| 22 GitRepoReadWriteTestBase for real tests. |
| 23 """ |
| 24 REPO_SCHEMA = None |
| 25 |
| 26 @classmethod |
| 27 def getRepoContent(cls, commit): |
| 28 return getattr(cls, 'COMMIT_%s' % commit, None) |
| 29 |
| 30 @classmethod |
| 31 def setUpClass(cls): |
| 32 super(GitRepoSchemaTestBase, cls).setUpClass() |
| 33 assert cls.REPO_SCHEMA is not None |
| 34 cls.r_schema = GitRepoSchema(cls.REPO_SCHEMA, cls.getRepoContent) |
| 35 |
| 36 |
| 37 class GitRepoReadOnlyTestBase(GitRepoSchemaTestBase): |
| 38 """Injects a GitRepo object given the schema and content from |
| 39 GitRepoSchemaTestBase into TestCase classes which subclass this. |
| 40 |
| 41 This GitRepo will appear as self.repo, and will be deleted and recreated once |
| 42 for the duration of all the tests in the subclass. |
| 43 """ |
| 44 REPO_SCHEMA = None |
| 45 |
| 46 @classmethod |
| 47 def setUpClass(cls): |
| 48 super(GitRepoReadOnlyTestBase, cls).setUpClass() |
| 49 assert cls.REPO_SCHEMA is not None |
| 50 cls.repo = GitRepo(cls.r_schema) |
| 51 |
| 52 def setUp(self): |
| 53 self.repo.git('checkout', '-f', self.repo.last_commit) |
| 54 |
| 55 @classmethod |
| 56 def tearDownClass(cls): |
| 57 cls.repo.nuke() |
| 58 super(GitRepoReadOnlyTestBase, cls).tearDownClass() |
| 59 |
| 60 |
| 61 class GitRepoReadWriteTestBase(GitRepoSchemaTestBase): |
| 62 """Injects a GitRepo object given the schema and content from |
| 63 GitRepoSchemaTestBase into TestCase classes which subclass this. |
| 64 |
| 65 This GitRepo will appear as self.repo, and will be deleted and recreated for |
| 66 each test function in the subclass. |
| 67 """ |
| 68 REPO_SCHEMA = None |
| 69 |
| 70 def setUp(self): |
| 71 super(GitRepoReadWriteTestBase, self).setUp() |
| 72 self.repo = GitRepo(self.r_schema) |
| 73 |
| 74 def tearDown(self): |
| 75 self.repo.nuke() |
| 76 super(GitRepoReadWriteTestBase, self).tearDown() |
| 77 |
| 78 def assertSchema(self, schema_string): |
| 79 self.assertEqual(GitRepoSchema(schema_string).simple_graph(), |
| 80 self.repo.to_schema().simple_graph()) |
| 81 |
OLD | NEW |