Skip to content

Commit

Permalink
Merge pull request #8 from jrperin/fix/tests
Browse files Browse the repository at this point in the history
fixing tests
  • Loading branch information
jrperin authored Aug 21, 2023
2 parents ba1ab8c + d4c42ae commit aafe65d
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 27 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ $ pip install -r requirements.txt

**Unit Tests:**
``` bash
# Note: Run the tests from main folter of the project
# Ex.: cd cobol-copybook.jsonifier/

# Running only the unittest
$ python -m unittest discover

Expand Down
Empty file added __init__.py
Empty file.
16 changes: 10 additions & 6 deletions tests/extractors/test_field_extractor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import unittest
import sys

sys.path.append("./src/")
sys.path.append("./src/coboljsonifier/")

from coboljsonifier.extractors.book_item import BookItem
from coboljsonifier.extractors.field_extractor import FieldEmpty, FieldSimpleNumeric1Decimals2, FieldSimpleNumericDecimals1, FieldSimpleNumericDecimals2, \
Expand Down Expand Up @@ -52,11 +56,11 @@ class TestFieldExtractor(unittest.TestCase):
signal_numeric1_decimals2.format = "S9(3)V9(2)"

# 10
simple_numeric1_decimals3 = BookItem( )
simple_numeric1_decimals3.type = "NUMERIC"
simple_numeric1_decimals3.level = "3"
simple_numeric1_decimals3.name = "BOOK-SIMPLE-NUMERIC1-DECIMALS2"
simple_numeric1_decimals3.format = "9(10)V"
signal_numeric1_decimals3 = BookItem( )
signal_numeric1_decimals3.type = "NUMERIC"
signal_numeric1_decimals3.level = "3"
signal_numeric1_decimals3.name = "BOOK-SIGNAL-NUMERIC1-DECIMALS3"
signal_numeric1_decimals3.format = "S9(10)V"

# A01
signal_numeric_masked1 = BookItem( )
Expand Down Expand Up @@ -112,7 +116,7 @@ def test_field_signal_numeric1_decimals2(self):
# 10
def test_field_signal_numeric1_decimals3(self):
self.assertEqual(FieldSignalNumeric1Decimals3().extract(self.signal_numeric1_decimals3).size, 10)
self.assertEqual(FieldSignalNumeric1Decimals3().extract(self.signal_numeric1_decimals3).decimals, 3)
self.assertEqual(FieldSignalNumeric1Decimals3().extract(self.signal_numeric1_decimals3).decimals, 0)

# A01
def test_field_numeric_masked1(self):
Expand Down
14 changes: 7 additions & 7 deletions tests/fields/test_numeric_binary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest

from decimal import Decimal
from coboljsonifier.fields.field_numeric_binary import FieldNumericBinary


Expand All @@ -12,25 +12,25 @@ class TestFieldNumericBinary(unittest.TestCase):

def test_parsing(self):

# Testa valor 00123 sem casas decimais
# Testing value 00123 without decimal places
binary_positive=FieldNumericBinary('NUMERIC_BINARY', 'FIELD-NUMERIC-BINARY', 4, 0)
binary_positive.parse(self.binary_123)
self.assertEqual(binary_positive.value, {'FIELD-NUMERIC-BINARY' : 123})

# Testa valor negativo -00123 sem casas decimais
# Testing negative value -00123 without decimal places
binary_negative=FieldNumericBinary('NUMERIC_BINARY', 'FIELD-NUMERIC-BINARY', 4, 0)
binary_negative.parse(self.binary_negative_123)
self.assertEqual(binary_negative.value, {'FIELD-NUMERIC-BINARY': -123})

# Testa valor 00123 com 2 casas decimais
# Testing value 00123 with 2 decimal places
binary_positive_decimals = FieldNumericBinary('NUMERIC_BINARY', 'FIELD-NUMERIC-BINARY', 4, 2)
binary_positive_decimals.parse(self.binary_123)
self.assertEqual(binary_positive_decimals.value, {'FIELD-NUMERIC-BINARY': 1.23})
self.assertEqual(binary_positive_decimals.value, {'FIELD-NUMERIC-BINARY': Decimal('1.23')})

# Testa valor negativo -00123 com 2 casas decimais
# Testing negative value -00123 with 2 decimal places
binary_negative_decimals = FieldNumericBinary('NUMERIC_BINARY', 'FIELD-NUMERIC-BINARY', 4, 2)
binary_negative_decimals.parse(self.binary_negative_123)
self.assertEqual(binary_negative_decimals.value, {'FIELD-NUMERIC-BINARY': -1.23})
self.assertEqual(binary_negative_decimals.value, {'FIELD-NUMERIC-BINARY': Decimal("-1.23")})

if __name__ == '__main__':
unittest.main()
10 changes: 5 additions & 5 deletions tests/fields/test_numeric_comp3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ def test_parsing(self):
# Testa valor 1234567C (C = positivo) sem casas decimais
comp3obj = FieldNumericComp3('NUMERIC_COMP3', 'FIELD-NUMERIC-COMP3', 4, 0)
comp3obj.parse(self.comp3_positive_value)
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal(1234567)})
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal("1234567")})

# Testa valor 1234567C (C = positivo) com 2 casas decimais
comp3obj = FieldNumericComp3('NUMERIC_COMP3', 'FIELD-NUMERIC-COMP3', 4, 2)
comp3obj.parse(self.comp3_positive_value)
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal(12345.67)})
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal("12345.67")})

# Testa valor 1234567C (C = positivo) com 4 casas decimais
comp3obj = FieldNumericComp3('NUMERIC_COMP3', 'FIELD-NUMERIC-COMP3', 4, 4)
comp3obj.parse(self.comp3_positive_value)
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal(123.4567)})
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal("123.4567")})

# Testa valor 1234567D (D = negativo) com 2 casas decimais
comp3obj = FieldNumericComp3('NUMERIC_COMP3', 'FIELD-NUMERIC-COMP3', 4, 2)
comp3obj.parse(self.comp3_negative_value)
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal(-12345.67)})
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal("-12345.67")})

# Testa valor 1234567F (F = s/ sinal) com 2 casas decimais
comp3obj = FieldNumericComp3('NUMERIC_COMP3', 'FIELD-NUMERIC-COMP3', 4, 2)
comp3obj.parse(self.comp3_no_signal_value)
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal(12345.67)})
self.assertEqual(comp3obj.value, {'FIELD-NUMERIC-COMP3': Decimal("12345.67")})


# Verify later how could check it...
Expand Down
6 changes: 3 additions & 3 deletions tests/fields/test_numeric_ebcdic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ def test_parsing(self):
# Testa valor positivo 00123 = F0F0F1F2C3 - 2 decimals
numeric_obj = FieldNumericEbcdic('NUMERIC', 'FIELD-NUMERIC-EBCDIC', 5, 2)
numeric_obj.parse(self.value_positive)
self.assertEqual(numeric_obj.value, {'FIELD-NUMERIC-EBCDIC' : Decimal(1.23)})
self.assertEqual(numeric_obj.value, {'FIELD-NUMERIC-EBCDIC' : Decimal("1.23")})

# Testa valor negativo -00123 = F0F0F1F2D3 - 2 decimals
numeric_obj = FieldNumericEbcdic('NUMERIC', 'FIELD-NUMERIC-EBCDIC', 5, 2)
numeric_obj.parse(self.value_negative)
self.assertEqual(numeric_obj.value, {'FIELD-NUMERIC-EBCDIC' : Decimal(-1.23)})
self.assertEqual(numeric_obj.value, {'FIELD-NUMERIC-EBCDIC' : Decimal("-1.23")})

# Testa valor sem sinal 00123 = F0F0F1F2F3 - 2 decimals
numeric_obj = FieldNumericEbcdic('NUMERIC', 'FIELD-NUMERIC-EBCDIC', 5, 2)
numeric_obj.parse(self.value_no_signal)
self.assertEqual(numeric_obj.value, {'FIELD-NUMERIC-EBCDIC' : Decimal(1.23)})
self.assertEqual(numeric_obj.value, {'FIELD-NUMERIC-EBCDIC' : Decimal("1.23")})

# def test_values(self):
# self.assertRaises(ValueError, FieldNumericEbcdic, b'\xA0\xF0\xF1\xF2\xF3', 2)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_copybook_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ class TestCopybookExtractor(unittest.TestCase):

def test_open_book_file_exists(self):
# If file exists the object will be generated
ce = CopybookExtractor('coboljsonifier/tests/test_files/book_tests_OK.cob').dict_book_structure
ce = CopybookExtractor('tests/test_files/book_tests_OK.cob').dict_book_structure
# print(json.dumps(ce, indent=2))
self.assertTrue(ce)

def test_open_book_file_not_exists(self):
# if file no exists will raise an error
self.assertRaises(IOError, CopybookExtractor, 'coboljsonifier/tests/test_files/book_tests_NOT_EXISTS.cob')
self.assertRaises(IOError, CopybookExtractor, 'tests/test_files/book_tests_NOT_EXISTS.cob')


def test_values(self):
# Checks if copybook has redefines
self.assertRaises(Exception, CopybookExtractor, 'coboljsonifier/tests/test_files/book_tests_NOK_redefines.cob')
self.assertRaises(Exception, CopybookExtractor, 'tests/test_files/book_tests_NOK_redefines.cob')
# Checks if copybook has bad structure formation
self.assertRaises(Exception, CopybookExtractor, 'coboljsonifier/tests/test_files/book_tests_NOK_bad_format.cob')
self.assertRaises(Exception, CopybookExtractor, 'tests/test_files/book_tests_NOK_bad_format.cob')
# Checks if the maximum binary lengh of 18 was overpassed
# ce = CopybookExtractor('coboljsonifier/tests/test_files/book_tests_NOK_binary_greater_18.cob').dict_book_structure
# ce = CopybookExtractor('tests/test_files/book_tests_NOK_binary_greater_18.cob').dict_book_structure
# print(ce)
self.assertRaises(ValueError, CopybookExtractor, 'coboljsonifier/tests/test_files/book_tests_NOK_binary_greater_18.cob')
self.assertRaises(ValueError, CopybookExtractor, 'tests/test_files/book_tests_NOK_binary_greater_18.cob')


if __name__ == '__main__':
Expand Down
File renamed without changes.

0 comments on commit aafe65d

Please sign in to comment.