Skip to content

Commit

Permalink
fix problems in unittests after putting into separated folders
Browse files Browse the repository at this point in the history
  • Loading branch information
soudehMasoudian committed Nov 22, 2022
1 parent 057c48c commit 6d17ad6
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 198 deletions.
28 changes: 28 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os


class Config:
"""
This class defines some general configs
Attributes
----------
TEST_MIXTURE_METADATA_EXTRACTION_EXPECTED_VALUE: str
get the expected value to compare with the actual one
"""

TEST_MIXTURE_METADATA_EXTRACTION_EXPECTED_VALUE = "{'operator_date': '2014-06-30', 'tester_name': 'Werner', 'specimen_name': 'BA-Losert MI', 'cement--QuantityInMix': 330.0, 'cement--BulkDensity': 3.123, 'cement--Volume': 105.7, 'cement--Annotation': 'CEM I 42.5 R', 'water_total--QuantityInMix': 175.0, 'water_total--BulkDensity': 1.0, 'water_total--Volume': 175.0, 'water_cement_ratio': 0.5303030303030303, 'water_effective--QuantityInMix': nan, 'water_effective--BulkDensity': nan, 'water_effective--Volume': nan, 'air_content--QuantityInMix': 0.0, 'air_content--BulkDensity': 0.0, 'air_content--Volume': 20.0, 'addition1--QuantityInMix': 273.0, 'addition1--BulkDensity': 2.74, 'addition1--Volume': 99.6, 'addition1--Annotation': 'Medenbach - Kalksteinmehl', 'admixture--QuantityInMix': 5.61, 'admixture--BulkDensity': 1.05, 'admixture--Volume': 5.3, 'admixture--Annotation': 'FM 595 BASF', 'aggregate--QuantityInMix': 1564.0, 'aggregate--BulkDensity': nan, 'aggregate--Volume': 594.4000000000001}"

TEST_METADATA_EXTRACTION_EXPECTED_VALUE = {'experimentName': 'BA-Losert MI E-Modul 28d v. 04.08.14 Probe 4',
'software_specification': 'MTS793|MPT|DEU|1|2|,|.|:|49|1|1|A',
'operator_timestamp': '13:25:39',
'operator_date': '01.09.2014',
'tester_name': 'Kh',
'specimen_name': 'BA-Losert E-Modul 28d v. 04.08.14 Probe 4',
'remark': 'Kraftgeregelt 3,9 kN/s',
'weight': 5342.0,
'weight_unit': 'g',
'diameter': 98.6,
'length': 300.3,
'length_unit': 'mm',
'mix_file': '2014_08_05 Rezeptur_MI.xlsx'}
Empty file.
222 changes: 113 additions & 109 deletions tests/data_extraction/test_mapping_script.py
Original file line number Diff line number Diff line change
@@ -1,117 +1,121 @@
import pytest
import unittest
import rdflib
import datetime
import os
import yaml

from pathlib import Path

from lebedigital.mapping.emodul_mapping import generate_knowledge_graph, get_date_time_value

def query_and_test_result(typeOfQuery, predicate, prop, g):
print(f"""Query for {typeOfQuery}:""")
q = f"""
SELECT ?o WHERE {{
{predicate} {prop} ?o .
}}
"""
print(q)
result = g.query(q)
return result

def print_next():
print("Pass")
print("###################Next Query#####################################")

def print_first():
print("###################First Query####################################")

def test_generate_knowledge_graph():
metadataPath = 'testMetaData.yaml'
filename = "knowledgeGraph.ttl"
print("Start testing:")
#Generate a testMetadata file
print("Generate metadata file for test:")
target_data = {'experimentName': 'BA-Losert MI E-Modul 28d v. 04.08.14 Probe 4',
'software_specification': 'MTS793|MPT|DEU|1|2|,|.|:|49|1|1|A',
'operator_timestamp': '13:25:39',
'operator_date': '01.09.2014',
'tester_name': 'Kh',
'specimen_name': 'BA-Losert E-Modul 28d v. 04.08.14 Probe 4',
'remark': 'Kraftgeregelt 3,9 kN/s',
'weight': 5342.0,
'weight_unit': 'g',
'diameter': 98.6,
'length': 300.3,
'length_unit': 'mm',
'mix_file': '2014_08_05 Rezeptur_MI.xlsx'}

with open(metadataPath, 'w') as yamlFile:
yaml.dump(target_data, yamlFile)
print("Generate knowledgeGraph:")
generate_knowledge_graph(metadataPath, filename)

g = rdflib.Graph()
result = g.parse(filename, format='ttl')
print("Query knowledgeGraph:")

print_first()

result = query_and_test_result('diameter','ns1:informationbearingentity1',
'ns7:has_decimal_value', g)
for row in result:
assert target_data['diameter'] == float(row.o)

print_next()

result = query_and_test_result('length', 'ns1:informationbearingentity2',
'ns7:has_decimal_value', g)
for row in result:
assert target_data['length'] == float(row.o)

print_next()

result = query_and_test_result('weight', 'ns1:informationbearingentity3',
'ns7:has_decimal_value', g)
for row in result:
assert target_data['weight'] == float(row.o)

print_next()

time = get_date_time_value(target_data)
result = query_and_test_result('operator_time', 'ns1:informationbearingentity6',
'ns1:has_datetime_value', g)
for row in result:
assert time == datetime.datetime.strptime(str(row.o).replace('T',' '), "%Y-%m-%d %H:%M:%S")

print_next()

result = query_and_test_result('operatorName', 'ns1:informationbearingentity7',
'ns7:has_text_value', g)
for row in result:
assert target_data['tester_name'] == str(row.o)

print_next()
result = query_and_test_result('rawFileName', 'ns1:informationbearingentity4',
'ns7:has_text_value', g)
for row in result:
assert target_data['experimentName'] == str(row.o)

print_next()
result = query_and_test_result('rawFileName', 'ns1:informationbearingentity5',
'ns7:has_text_value', g)
rawPath = os.path.join(Path(__file__).parents[2], 'usecases',
'MinimumWorkingExample', 'Data', 'E-modul', target_data['experimentName'])

for row in result:
assert rawPath == str(row.o)

print('Pass all tests')

print("Deleting test files:")
os.remove(filename)
os.remove(metadataPath)

print("End testing")

#test_generate_knowledge_graph()
class TestMappingScript(unittest.TestCase):

def query_and_test_result(self, typeOfQuery, predicate, prop, g):
print(f"""Query for {typeOfQuery}:""")
q = f"""
SELECT ?o WHERE {{
{predicate} {prop} ?o .
}}
"""
print(q)
result = g.query(q)
return result


def print_next(self):
print("Pass")
print("###################Next Query#####################################")


def print_first(self):
print("###################First Query####################################")


def test_generate_knowledge_graph(self):
metadataPath = 'testMetaData.yaml'
filename = "knowledgeGraph.ttl"
print("Start testing:")
# Generate a testMetadata file
print("Generate metadata file for test:")
target_data = {'experimentName': 'BA-Losert MI E-Modul 28d v. 04.08.14 Probe 4',
'software_specification': 'MTS793|MPT|DEU|1|2|,|.|:|49|1|1|A',
'operator_timestamp': '13:25:39',
'operator_date': '01.09.2014',
'tester_name': 'Kh',
'specimen_name': 'BA-Losert E-Modul 28d v. 04.08.14 Probe 4',
'remark': 'Kraftgeregelt 3,9 kN/s',
'weight': 5342.0,
'weight_unit': 'g',
'diameter': 98.6,
'length': 300.3,
'length_unit': 'mm',
'mix_file': '2014_08_05 Rezeptur_MI.xlsx'}

with open(metadataPath, 'w') as yamlFile:
yaml.dump(target_data, yamlFile)
print("Generate knowledgeGraph:")
generate_knowledge_graph(metadataPath, filename)

g = rdflib.Graph()
result = g.parse(filename, format='ttl')
print("Query knowledgeGraph:")

self.print_first()

result = self.query_and_test_result('diameter', 'ns1:informationbearingentity1',
'ns7:has_decimal_value', g)
for row in result:
self.assertEqual(target_data['diameter'], float(row.o))

self.print_next()

result = self.query_and_test_result('length', 'ns1:informationbearingentity2',
'ns7:has_decimal_value', g)
for row in result:
self.assertEqual(target_data['length'], float(row.o))

self.print_next()

result = self.query_and_test_result('weight', 'ns1:informationbearingentity3',
'ns7:has_decimal_value', g)
for row in result:
self.assertEqual(target_data['weight'], float(row.o))

self.print_next()

time = get_date_time_value(target_data)
result = self.query_and_test_result('operator_time', 'ns1:informationbearingentity6',
'ns1:has_datetime_value', g)
for row in result:
self.assertEqual(time, datetime.datetime.strptime(str(row.o).replace('T', ' '), "%Y-%m-%d %H:%M:%S"))

self.print_next()

result = self.query_and_test_result('operatorName', 'ns1:informationbearingentity7',
'ns7:has_text_value', g)
for row in result:
self.assertEqual(target_data['tester_name'], str(row.o))

self.print_next()
result = self.query_and_test_result('rawFileName', 'ns1:informationbearingentity4',
'ns7:has_text_value', g)
for row in result:
self.assertEqual(target_data['experimentName'], str(row.o))

self.print_next()
result = self.query_and_test_result('rawFileName', 'ns1:informationbearingentity5',
'ns7:has_text_value', g)
rawPath = os.path.join(Path(__file__).parents[2], 'usecases',
'MinimumWorkingExample', 'Data', 'E-modul', target_data['experimentName'])

for row in result:
self.assertEqual(rawPath, str(row.o))

print('Pass all tests')

print("Deleting test files:")
os.remove(filename)
os.remove(metadataPath)

print("End testing")

# test_generate_knowledge_graph()
39 changes: 14 additions & 25 deletions tests/data_extraction/test_metadata_extraction_emodul.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
import pytest
import unittest
from config import Config

from lebedigital.raw_data_processing.youngs_modulus_data \
.emodul_metadata_extraction import extract_metadata_emodulus
class TestMetaDataExtractionEmodul(unittest.TestCase):
def test_metadata_extraction_emodul(self):
"""Tesing the metadata extraction on a single example"""

def test_metadata_extraction_emodul():
"""Tesing the metadata extraction on a single example"""
# setting up the test example
input = '../usecases/MinimumWorkingExample/Data/E-modul/BA-Losert MI E-Modul 28d v. 04.08.14 Probe 4'
mix_file = 'mix.dat'
specimen_file = 'specimen.dat'

# setting up the test example
input = '../usecases/MinimumWorkingExample/Data/E-modul/BA-Losert MI E-Modul 28d v. 04.08.14 Probe 4'
mix_file = 'mix.dat'
specimen_file = 'specimen.dat'
target_data = Config.TEST_METADATA_EXTRACTION_EXPECTED_VALUE

target_data = {'experimentName': 'BA-Losert MI E-Modul 28d v. 04.08.14 Probe 4',
'software_specification': 'MTS793|MPT|DEU|1|2|,|.|:|49|1|1|A',
'operator_timestamp': '13:25:39',
'operator_date': '01.09.2014',
'tester_name': 'Kh',
'specimen_name': 'BA-Losert E-Modul 28d v. 04.08.14 Probe 4',
'remark': 'Kraftgeregelt 3,9 kN/s',
'weight': 5342.0,
'weight_unit': 'g',
'diameter': 98.6,
'length': 300.3,
'length_unit': 'mm',
'mix_file': '2014_08_05 Rezeptur_MI.xlsx'}
# run extraction and getting a dictionary with metadata
test_data = extract_metadata_emodulus(input, specimen_file, mix_file)

# run extraction and getting a dictionary with metadata
test_data = extract_metadata_emodulus(input, specimen_file, mix_file)

# checking if result is correct
assert test_data == target_data
# checking if result is correct
self.assertEqual(test_data, target_data)
82 changes: 27 additions & 55 deletions tests/data_extraction/test_mixture_metadata_extraction.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,27 @@
import pytest
from lebedigital.raw_data_processing.metadata_extraction \
.mixture_metadata_extraction import extraction


def test_extraction():
"""Tesing the mixture metadata extraction on a single example"""

# setting up the test example
input = '../usecases/MinimumWorkingExample/Data/Mischungen/2014_08_05 Rezeptur_MIII.xlsx'
target_data = {'Cement -- Quantity in mix': 330, 'Cement -- Specific gravity': 3.123,
'Cement -- Annotation': 'CEM I 42,5 R', 'Water (total) -- Quantity in mix':
175, 'Water (total) -- Specific gravity': 1, 'Water (total) -- Annotation':
'---', 'Water (effective) -- Quantity in mix':
'---', 'Water (effective) -- Specific gravity':
'---', 'Water (effective) -- Annotation': '---', 'Air -- Quantity in mix':
'---', 'Air -- Specific gravity': '---', 'Air -- Annotation': '---',
'Supplementary cementious materials Kalksteinmehl -- Quantity in mix': 147,
'Supplementary cementious materials Kalksteinmehl -- Specific gravity': 2.74,
'Supplementary cementious materials Kalksteinmehl -- Annotation':
'Medenbach', 'Admixture -- Quantity in mix': 4.95,
'Admixture -- Specific gravity': 1.05, 'Admixture -- Annotation':
'FM 595 BASF', 'Total -- Quantity in mix': 656.95,
'Total -- Specific gravity': '---', 'Total -- Annotation': '---',
'Zuschlag (total) -- Quantity in mix': 1685,
'Zuschlag (total) -- Specific gravity': '---',
'Zuschlag (total) -- Annotation': '---', '0 / 0,3 -- Quantity in mix': 0,
'0 / 0,3 -- Specific gravity': 2.65, '0 / 0,3 -- Annotation': 'Quarz',
'0,1 / 0,5 -- Quantity in mix': 337, '0,1 / 0,5 -- Specific gravity': 2.63,
'0,1 / 0,5 -- Annotation': 'Okrilla ', '0,5 / 1,0 -- Quantity in mix': 194,
'0,5 / 1,0 -- Specific gravity': 2.63,
'0,5 / 1,0 -- Annotation': 'Okrilla ', '1,0 / 2,0 -- Quantity in mix': 194,
'1,0 / 2,0 -- Specific gravity': 2.63, '1,0 / 2,0 -- Annotation': 'Okrilla ',
'2,0 / 4,0 -- Quantity in mix': 126, '2,0 / 4,0 -- Specific gravity': 2.63,
'2,0 / 4,0 -- Annotation': 'Okrilla ', '4,0 / 8,0 -- Quantity in mix': 126,
'4,0 / 8,0 -- Specific gravity': 2.63, '4,0 / 8,0 -- Annotation': 'Okrilla ',
'8,0 / 16,0 -- Quantity in mix': 708, '8,0 / 16,0 -- Specific gravity': 2.63,
'8,0 / 16,0 -- Annotation': 'Okrilla ', 'nan -- Quantity in mix': '---',
'nan -- Specific gravity': '---', 'nan -- Annotation': '---',
'Fresh concrete -- Quantity in mix': 2341.95,
'Fresh concrete -- Specific gravity': '---', 'Fresh concrete -- Annotation':
'---', 'Mehlkornanteil -- Quantity in mix': '---',
'Mehlkornanteil -- Specific gravity': '---', 'Mehlkornanteil -- Annotation':
'---', 'Mörtelanteil -- Quantity in mix': '---',
'Mörtelanteil -- Specific gravity': '---', 'Mörtelanteil -- Annotation': '---',
'Date': '2014-06-30', 'Editor': 'Werner', 'Requester': 'BAM 7.0',
'Project number': 'BA-Losert', 'Specimen': 'BA-Losert MIII',
'Betonsorte u -festigkeitsklasse': '---', 'Wasserzementwert': '0,53 w/b = 0,37',
'Konsistenz': 'SVB', 'Sieblinie n. DIN 1045': 'SVB', 'Körnungsziffer': 3.88,
'Vol Leim/Zuschlag': 56.00624024960999}
# run extraction and getting a dictionary with metadata
test_data = extraction(input, None)

# checking if result is correct
assert test_data == target_data
import os
from lebedigital.raw_data_processing.mixture \
.mixture_metadata_extraction import extract_metadata_mixture
from config import Config
import unittest


class TestMixtureMetadataExtraction(unittest.TestCase):
def test_extraction(self):
"""Testing the mixture metadata extraction on a single example"""

# setting up the test example input
input_path = '../usecases/MinimumWorkingExample/Data/Mischungen/2014_08_05 Rezeptur_MI.xlsx'

target_data = Config.TEST_MIXTURE_METADATA_EXTRACTION_EXPECTED_VALUE
# run extraction and getting a dictionary with metadata
test_data = extract_metadata_mixture(input_path, None)

# make sure if the path exists
self.assertTrue(os.path.exists(input_path))

# checking if result is correct
self.assertEqual(test_data.__str__(), target_data)


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit 6d17ad6

Please sign in to comment.