Skip to content

Commit

Permalink
Add tests on strings, colors, caching, git and yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
janhybs committed Nov 24, 2018
1 parent e02c1a8 commit 98f6d05
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/test_cached.py
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)
45 changes: 45 additions & 0 deletions tests/test_colors.py
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)')
49 changes: 49 additions & 0 deletions tests/test_git.py
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)
48 changes: 48 additions & 0 deletions tests/test_strings.py
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'
)

48 changes: 48 additions & 0 deletions tests/test_yaml.py
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')

1 change: 1 addition & 0 deletions tests/yaml/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
top-secret

0 comments on commit 98f6d05

Please sign in to comment.