/
test_core.py
80 lines (62 loc) · 2.55 KB
/
test_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import sys
from django.test import TestCase
from git import Repo
from django_unmigrate.core import GitError, get_added_migrations, get_main_branch, get_parents_from_targets, get_targets
from dunm_sandbox.meta import COMMITS, PARENTS
class GetMainBranchTestCase(TestCase):
"""
Tests core.get_main_branch
"""
def test_master(self):
main_branch = get_main_branch()
self.assertEqual(main_branch, "master")
def test_main(self):
pathname = os.path.dirname(sys.argv[0])
repo = Repo(os.path.abspath(pathname), search_parent_directories=True)
repo.git.branch("main")
get_main_branch.cache_clear()
main_branch = get_main_branch()
self.assertEqual(main_branch, "main")
repo.git.branch("-d", "main")
get_main_branch.cache_clear()
class GetAddedMigrationsTestCase(TestCase):
"""
Tests core.get_added_migrations
"""
def test_plain(self):
response = get_added_migrations()
self.assertEqual(response, [])
def test_by_commit(self):
for commit, expected_migrations in COMMITS.items():
response = get_added_migrations(commit)
self.assertEqual(set(response), set(expected_migrations))
def test_by_commit_with_error(self):
with self.assertRaises(GitError):
get_added_migrations("hopefully-non-existing-ref")
class GetParentsFromTargetsTestCase(TestCase):
"""
Tests core.get_parents_from_targets
"""
def test_by_migration(self):
for child, expected_parents in PARENTS.items():
parents = get_parents_from_targets([child])
self.assertEqual(set(parents), set(expected_parents))
def test_overlapping_targets(self):
parents = get_parents_from_targets(
[("myapp", "0005_auto_20200502_0149"), ("myapp", "0004_merge_20200502_0148")]
)
self.assertEqual(set(parents), {("myapp", "0003_mymodel_is_active")})
class GetTargetsTestCase(TestCase):
"""
Tests core.get_targets
"""
def test_by_commit(self):
for commit, expected_migrations in COMMITS.items():
expected_migrations.sort() # We know tuple-sorting our migrations yields chronological order
expected_parents = []
if expected_migrations:
expected_parents = PARENTS[expected_migrations[0]]
(added_targets, parent_target) = get_targets(ref=commit)
self.assertEqual(set(added_targets), set(expected_migrations))
self.assertEqual(set(parent_target), set(expected_parents))