Add test for version repo using helper scripts

This commit is contained in:
erik 2023-03-09 13:17:20 +01:00
parent 27f2bf2d2e
commit f49b44f5e0

View file

@ -1,6 +1,6 @@
from pathlib import Path
import sys import sys
import os import os
import test_helper as th
# getting the name of the directory # getting the name of the directory
# where the this file is present. # where the this file is present.
@ -17,131 +17,55 @@ sys.path.append(parent)
# now we can import the module in the parent # now we can import the module in the parent
# directory. # directory.
from domain import ReleaseType, Release from domain import ReleaseType
from infrastructure import ReleaseTypeRepository, VersionRepository, ReleaseRepository from infrastructure import ReleaseTypeRepository, VersionRepository, ReleaseRepository
from infrastructure_api import GitApi from mock_infrastructure_api import MockGitApi
class MyGitApi(GitApi):
def __init__(self, commit_string):
self.commit_string = commit_string
def get_latest_commit(self):
return self.commit_string
def test_gradle(tmp_path): def test_version_repository(tmp_path):
# init # init
file_name = 'config.gradle' th.copy_files(th.TEST_FILE_PATH, tmp_path)
with open(f'test/resources/{file_name}', 'r') as gradle_file: sut = VersionRepository(th.TEST_FILE_PATH)
contents = gradle_file.read() version = sut.get_version()
f = tmp_path / file_name #test
f.write_text(contents) assert version is not None
# test
repo = VersionRepository(f)
version = repo.get_version()
version = version.create_release_version(ReleaseType.SNAPSHOT)
repo.write_file(version.get_version_string())
# check
assert 'version = "12.4.678-SNAPSHOT"' in f.read_text()
def test_json(tmp_path):
# init
file_name = 'config.json'
with open(f'test/resources/{file_name}', 'r') as gradle_file:
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
# test
repo = VersionRepository(f)
version = repo.get_version()
version = version.create_release_version(ReleaseType.SNAPSHOT)
repo.write_file(version.get_version_string())
# check
assert '"version": "123.123.456-SNAPSHOT"' in f.read_text()
def test_clojure(tmp_path):
# init
file_name = 'config.clj'
with open(f'test/resources/{file_name}', 'r') as gradle_file:
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
# test
repo = VersionRepository(f)
version = repo.get_version()
version = version.create_release_version(ReleaseType.SNAPSHOT)
repo.write_file(version.get_version_string())
# check
assert '1.1.3-SNAPSHOT' in f.read_text()
def test_python(tmp_path):
# init
file_name = 'config.py'
with open(f'test/resources/{file_name}', 'r') as gradle_file:
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
# test
repo = VersionRepository(f)
version = repo.get_version()
version = version.create_release_version(ReleaseType.SNAPSHOT)
repo.write_file(version.get_version_string())
# check
assert '3.1.3-SNAPSHOT' in f.read_text()
def test_release_repository(tmp_path): def test_release_repository(tmp_path):
# init # init
file_name = 'config.json' th.copy_files( th.TEST_FILE_PATH, tmp_path)
with open(f'test/resources/{file_name}', 'r') as gradle_file: version_repo = VersionRepository(th.TEST_FILE_PATH)
contents = gradle_file.read() release_type_repo = ReleaseTypeRepository(MockGitApi('MINOR test'))
f = tmp_path / file_name
f.write_text(contents)
# test # test
sut = ReleaseRepository(VersionRepository(f), ReleaseTypeRepository(MyGitApi('MINOR test')), 'main') sut = ReleaseRepository(version_repo, release_type_repo, 'main')
release = sut.get_release() release = sut.get_release()
assert release is not None assert release is not None
def test_release_type_repository(): def test_release_type_repository():
sut = ReleaseTypeRepository(MyGitApi('MINOR test')) sut = ReleaseTypeRepository(MockGitApi('MINOR test'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type is ReleaseType.MINOR assert release_type is ReleaseType.MINOR
sut = ReleaseTypeRepository(MyGitApi('MINOR bla')) sut = ReleaseTypeRepository(MockGitApi('MINOR bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type is ReleaseType.MINOR assert release_type is ReleaseType.MINOR
sut = ReleaseTypeRepository(MyGitApi('Major bla')) sut = ReleaseTypeRepository(MockGitApi('Major bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type == ReleaseType.MAJOR assert release_type == ReleaseType.MAJOR
sut = ReleaseTypeRepository(MyGitApi('PATCH bla')) sut = ReleaseTypeRepository(MockGitApi('PATCH bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type == ReleaseType.PATCH assert release_type == ReleaseType.PATCH
sut = ReleaseTypeRepository(MyGitApi('SNAPSHOT bla')) sut = ReleaseTypeRepository(MockGitApi('SNAPSHOT bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type == ReleaseType.SNAPSHOT assert release_type == ReleaseType.SNAPSHOT
sut = ReleaseTypeRepository(MyGitApi('bla')) sut = ReleaseTypeRepository(MockGitApi('bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type == None assert release_type == None