Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BfabricTestCaseReadEndPoints #53

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 38 additions & 45 deletions bfabric/tests/test_bfabric_read.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,64 @@
#!/usr/bin/env python3
# -*- coding: latin1 -*-

"""
unittest by <[email protected]>
2019-09-11
"""

import base64
import unittest
import bfabric
import os
import json
import os
import unittest

class BfabricTestCaseReadEndPoints(unittest.TestCase):

with open('groundtruth.json') as json_file:
groundtruth = json.load(json_file)
import bfabric

bfapp = bfabric.Bfabric(verbose=False)

def __init__(self, *args, **kwargs):
super(BfabricTestCaseReadEndPoints, self).__init__(*args, **kwargs)
class BfabricTestCaseReadEndPoints(unittest.TestCase):
@classmethod
def setUpClass(cls):
path = os.path.join(os.path.dirname(__file__), "groundtruth.json")
with open(path) as json_file:
cls.ground_truth = json.load(json_file)
cls.bfapp = bfabric.Bfabric(verbose=False)

# input: entity = endpoint = table = ...
def read(self, endpoint):
self.assertIn(endpoint, self.groundtruth)
for (query, groundtruth) in self.groundtruth[endpoint]:
"""Executes read queries for `endpoint` and compares results with ground truth."""
self.assertIn(endpoint, self.ground_truth)
for query, ground_truth in self.ground_truth[endpoint]:
res = self.bfapp.read_object(endpoint=endpoint, obj=query)
for gtattr, gtvalue in groundtruth.items():
#if self.verbosity>2:
# print ("{}:{} = {} \t?\t{}".format(endpoint, gtattr, gtvalue, getattr(res[0], gtattr)))
for gt_attr, gt_value in ground_truth.items():
with self.subTest(endpoint=endpoint, query=query):
# sort results for comparison by id
res = sorted(res, key=lambda x: x._id)

# to make it deterministic!
try:
res = sorted(res, key = lambda x: x.id)
except:
res = sorted(res, key = lambda x: x._id)

msg = "test for {} failed.".format(endpoint)
# cast all values to string
self.assertEqual("{}".format(gtvalue), "{}".format(getattr(res[0], gtattr)), msg)
# cast all values to strings (i.e. ignoring int/float/string differences)
self.assertEqual(str(gt_value), str(res[0][gt_attr]))

def test_user(self):
self.read('user')
self.read("user")

def test_container(self):
self.read('container')
self.read("container")

def test_project(self):
self.read('project')
self.read("project")

def test_application(self):
self.read('application')
self.read("application")

def test_sample(self):
self.read('sample')
self.read("sample")

def test_workunit(self):
self.read('workunit')
self.read("workunit")

def test_resource(self):
self.read('resource')
self.read("resource")

def test_executable(self):
self.read('executable')
def test_annotation(self):
self.read('annotation')
self.read("executable")

if __name__ == '__main__':
# unittest.main(verbosity=2)
suite = unittest.TestSuite()
for entity in ['user', 'container', 'project', 'application', 'sample', 'workunit', 'resource', 'executable', 'annotation']:
suite.addTest(BfabricTestCaseReadEndPoints('test_{}'.format(entity)))
def test_annotation(self):
self.read("annotation")

runner = unittest.TextTestRunner(verbosity=3)
runner.run(suite )

if __name__ == "__main__":
unittest.main(verbosity=2)
Loading