trnslator/tests/test_utils.py

66 lines
1.5 KiB
Python
Raw Normal View History

2020-06-24 15:21:26 -04:00
import pytest
2020-06-24 15:46:32 -04:00
import os
2020-06-24 15:40:32 -04:00
import numpy as np
2020-06-24 15:21:26 -04:00
2020-06-24 15:46:32 -04:00
from translater import utils, timeit, settings
2020-06-24 15:40:32 -04:00
from geomeppy.geom.polygons import Polygon3D
2020-06-24 16:06:41 -04:00
from datetime import datetime
2020-06-24 15:21:26 -04:00
2020-06-24 15:22:28 -04:00
def test_rotate(config):
# Shift list elements to the left
l1 = [1, 2, 3] # list
n = 1 # shift 1 position to the left
l2 = utils.rotate(l1, n)
2020-06-24 15:40:32 -04:00
assert l2 == [2, 3, 1]
2020-06-24 15:25:58 -04:00
@timeit
2020-06-24 16:06:41 -04:00
def test_lcm():
2020-06-24 15:22:50 -04:00
# This function takes two integers and returns the L.C.M.
x = 10
y = 50
lcm = utils.lcm(x, y)
2020-06-24 16:06:41 -04:00
assert lcm == 50
2020-06-24 15:22:50 -04:00
2020-06-24 15:40:32 -04:00
def test_float_round(config):
# Makes sure a variable is a float and round it at "n" decimals
num = 40.24
n = 1
float_num = utils.float_round(num, n)
2020-06-24 15:22:28 -04:00
2020-06-24 15:40:32 -04:00
assert float_num == 40.2
2020-06-24 15:21:26 -04:00
2020-06-24 15:46:32 -04:00
def test_angle(config):
2020-06-24 15:41:00 -04:00
# Calculate the angle between 2 vectors
# Polygon1 & vector1
poly1 = Polygon3D(
[(215.5, 5.0, 0.5), (215.5, 5.0, 2.0), (217.0, 5.0, 2.0), (217.0, 5.0, 0.5)]
)
v1 = poly1.normal_vector
v2 = v1
angle = utils.angle(v1, v2, acute=False)
assert angle == 2 * np.pi
2020-06-24 15:46:32 -04:00
def test_write_lines(config):
# Delete file if exists, then write lines in it
2020-06-24 16:06:41 -04:00
path = os.path.join(settings.data_folder, "write_lines.txt")
2020-06-24 15:46:32 -04:00
lines = ["Test to write lines in file", "2nd line", "end of document"]
utils.write_lines(path, lines)
assert os.path.exists(path)
def test_date_transform(config):
# Simple function transforming one-based hours (1->24) into zero-based hours (0->23)
date_str = "08:10"
new_date = utils.date_transform(date_str)
2020-06-24 16:06:41 -04:00
assert new_date == datetime(1900, 1, 1, 7, 10)