-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests on strings, colors, caching, git and yaml
- Loading branch information
Showing
6 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/bin/python3 | ||
# author: Jan Hybs | ||
|
||
import tests | ||
|
||
from unittest import TestCase | ||
|
||
from cfg.config import global_configuration | ||
|
||
|
||
class TestCached(TestCase): | ||
|
||
def test_cached(self): | ||
global_configuration.cache_type = 'TTLCache' | ||
global_configuration.cache_opts = dict( | ||
maxsize=10, | ||
ttl=10 * 60, | ||
) | ||
|
||
from utils.caching import cached | ||
|
||
foo_i = dict(i=0) | ||
|
||
@cached() | ||
def foo(j): | ||
foo_i['i'] += 1 | ||
return foo_i['i'] | ||
|
||
self.assertEqual(foo(1), 1) | ||
self.assertEqual(foo(1), 1) | ||
for i in range(12): foo(i) | ||
self.assertEqual(foo(1), 13) | ||
|
||
|
||
global_configuration.cache_type = None | ||
bar_i = dict(i=0) | ||
|
||
@cached() | ||
def bar(j): | ||
bar_i['i'] += 1 | ||
return bar_i['i'] | ||
|
||
self.assertEqual(bar(1), 1) | ||
self.assertEqual(bar(1), 2) | ||
for i in range(12): bar(i) | ||
self.assertEqual(bar(1), 15) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/bin/python3 | ||
# author: Jan Hybs | ||
|
||
import tests | ||
|
||
from unittest import TestCase | ||
from utils import colors | ||
|
||
|
||
class TestColors(TestCase): | ||
def test_hex2rgb(self): | ||
self.assertTupleEqual(colors.hex2rgb('#FF0000'), (255, 0, 0)) | ||
self.assertTupleEqual(colors.hex2rgb('#00FF00'), (0, 255, 0)) | ||
self.assertTupleEqual(colors.hex2rgb('#0000FF'), (0, 0, 255)) | ||
|
||
self.assertTupleEqual(colors.hex2rgb('#009BFF'), (0, 155, 255)) | ||
self.assertTupleEqual(colors.hex2rgb('#000000'), (0, 0, 0)) | ||
self.assertTupleEqual(colors.hex2rgb('#ffFFff'), (255, 255, 255)) | ||
|
||
def test_rgb2hex(self): | ||
self.assertEqual(colors.rgb2hex((255, 0, 0)), '#FF0000'.lower()) | ||
self.assertEqual(colors.rgb2hex((0, 255, 0)), '#00FF00'.lower()) | ||
self.assertEqual(colors.rgb2hex((0, 0, 255)), '#0000FF'.lower()) | ||
|
||
self.assertEqual(colors.rgb2hex((0, 155, 255)), '#009BFF'.lower()) | ||
self.assertEqual(colors.rgb2hex((0, 0, 0)), '#000000'.lower()) | ||
self.assertEqual(colors.rgb2hex((255, 255, 255)), '#ffFFff'.lower()) | ||
|
||
def test_rgb2html(self): | ||
self.assertEqual(colors.rgb2html((255, 0, 0)), 'rgb(255, 0, 0)') | ||
self.assertEqual(colors.rgb2html((0, 255, 0)), 'rgb(0, 255, 0)') | ||
self.assertEqual(colors.rgb2html((0, 0, 255)), 'rgb(0, 0, 255)') | ||
|
||
self.assertEqual(colors.rgb2html((0, 155, 255)), 'rgb(0, 155, 255)') | ||
self.assertEqual(colors.rgb2html((0, 0, 0)), 'rgb(0, 0, 0)') | ||
self.assertEqual(colors.rgb2html((255, 255, 255)), 'rgb(255, 255, 255)') | ||
|
||
self.assertEqual(colors.rgb2html((255, 255, 255), 0.5), 'rgba(255, 255, 255, 0.50)') | ||
self.assertEqual(colors.rgb2html((255, 255, 255), 0.125), 'rgba(255, 255, 255, 0.12)') | ||
|
||
def test_configurable_html_color(self): | ||
lambda_func = colors.configurable_html_color((255, 255, 255)) | ||
self.assertEqual(lambda_func(0.5), 'rgba(255, 255, 255, 0.50)') | ||
self.assertEqual(lambda_func(0.13), 'rgba(255, 255, 255, 0.13)') | ||
self.assertEqual(lambda_func(None), 'rgb(255, 255, 255)') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/bin/python3 | ||
# author: Jan Hybs | ||
|
||
import os | ||
from os.path import join, abspath | ||
|
||
import tests | ||
|
||
from unittest import TestCase | ||
|
||
from utils.files import StdoutType | ||
from structures.project_step_git import ProjectStepGit | ||
from vcs.git import Git | ||
|
||
|
||
class TestGit(TestCase): | ||
def test_git_config(self): | ||
git_config = ProjectStepGit( | ||
url='https://github.com/janhybs/ci-hpc.git', | ||
) | ||
# default values | ||
self.assertEqual(git_config.branch, 'master') | ||
self.assertEqual(git_config.commit, '') | ||
self.assertEqual(git_config.repo, 'ci-hpc') | ||
|
||
git_config = ProjectStepGit( | ||
url='git@github.com:janhybs/ci-hpc-foobar.git', | ||
commit='foobar' | ||
) | ||
# default values | ||
self.assertEqual(git_config.branch, 'master') | ||
self.assertEqual(git_config.commit, 'foobar') | ||
self.assertEqual(git_config.repo, 'ci-hpc-foobar') | ||
|
||
def test_git(self): | ||
git_config = ProjectStepGit( | ||
url='https://github.com/janhybs/ci-hpc.git', | ||
stdout=StdoutType.NONE, | ||
|
||
) | ||
git = Git(git_config) | ||
self.assertEqual( | ||
abspath(git.dir), | ||
abspath(join(os.getcwd(), git_config.repo)) | ||
) | ||
|
||
git.info() | ||
commits = git.log(5) | ||
self.assertEqual(len(commits), 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/python3 | ||
# author: Jan Hybs | ||
|
||
import tests | ||
|
||
from unittest import TestCase | ||
from utils import strings | ||
|
||
|
||
class TestStrings(TestCase): | ||
|
||
def test_to_json(self): | ||
self.assertEqual(strings.to_json(None), 'null') | ||
self.assertEqual(strings.to_json(dict()), '{}') | ||
self.assertEqual(strings.to_json( | ||
dict(b=1, a=2)), | ||
'{\n "a": 2,\n "b": 1\n}' | ||
) | ||
|
||
import numpy as np | ||
|
||
self.assertEqual( | ||
strings.to_json(np.array([1/3, 2/3, 3/3])), | ||
'[\n 0.33,\n 0.67,\n 1.0\n]' | ||
) | ||
|
||
import datetime | ||
|
||
self.assertEqual( | ||
strings.to_json(datetime.datetime(2018, 4, 22, 12, 00, 35)), | ||
'"2018/04/22-12:00:35"' | ||
) | ||
|
||
def test_to_yaml(self): | ||
self.assertEqual(strings.to_yaml(dict(a=None)), 'a: null\n') | ||
self.assertEqual(strings.to_yaml(dict()), '{}\n') | ||
self.assertEqual(strings.to_yaml( | ||
dict(b=1, a=2)), | ||
'a: 2\nb: 1\n' | ||
) | ||
|
||
import datetime | ||
|
||
self.assertEqual( | ||
strings.to_yaml(dict(a=datetime.datetime(2018, 4, 22, 12, 00, 35))), | ||
'a: 2018-04-22 12:00:35\n' | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/bin/python3 | ||
# author: Jan Hybs | ||
|
||
import tests | ||
|
||
import yaml | ||
from unittest import TestCase | ||
|
||
from cfg.config import global_configuration | ||
from utils import extend_yaml | ||
|
||
repeat_yaml = ''' | ||
foo: !repeat a 5 | ||
''' | ||
|
||
range_yaml = ''' | ||
foo: !range 1 5 | ||
bar: !range 1 2 6 | ||
''' | ||
|
||
sh_yaml = ''' | ||
foo: !sh yaml/foo.txt | ||
''' | ||
|
||
|
||
class TestExtendYaml(TestCase): | ||
|
||
def test_extend(self): | ||
extend_yaml.extend() | ||
self.assertIn('!range', yaml.Loader.yaml_constructors) | ||
self.assertIn('!repeat', yaml.Loader.yaml_constructors) | ||
self.assertIn('!sh', yaml.Loader.yaml_constructors) | ||
|
||
def test_repeat(self): | ||
extend_yaml.extend() | ||
|
||
result = yaml.load(repeat_yaml) | ||
self.assertEqual(result.get('foo'), 'a'*5) | ||
|
||
result = yaml.load(range_yaml) | ||
self.assertTupleEqual(tuple(result.get('foo')), tuple(range(1, 5))) | ||
self.assertTupleEqual(tuple(result.get('bar')), tuple(range(1, 2, 6))) | ||
|
||
global_configuration.project_cfg_dir = tests.__dir__ | ||
|
||
result = yaml.load(sh_yaml) | ||
self.assertEqual(result.get('foo'), 'top-secret') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
top-secret |