def muterun(command): try: from Naked.toolshed.types import NakedObject response_obj = NakedObject() response = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True) response_obj.stdout = response response_obj.exitcode = 0 response_obj.stderr = b"" return response_obj except subprocess.CalledProcessError as cpe: response_obj.stdout = b"" response_obj.stderr = cpe.output if cpe.returncode: response_obj.exitcode = cpe.returncode else: response_obj.exitcode = 1 return response_obj except Exception as e: if DEBUG_FLAG: sys.stderr.write( "Naked Framework Error: unable to run the shell command with the mute_run() function (Naked.toolshed.shell.py)." ) raise e
def test_nobj_auto_generated_type_attribute(self): no = NakedObject() self.assertTrue( hasattr(no, '_naked_type_') ) # contains the automatically generated type attribute self.assertEqual( no._naked_type_, 'NakedObject') # contains correct string for the attribute
def test_nobj_del_attribute(self): no = NakedObject({'key': 'value'}) self.assertEqual(no.key, 'value') del no.key with self.assertRaises( AttributeError ): # raises AttributeError on attempt to access with dot syntax without attribute test = no.test
def test_nobj_getattrdict_method(self): test_attr_dict = {'item1': 'val1', 'item2': 'val2'} no = NakedObject(test_attr_dict) self.assertEqual(no._getAttributeDict(), { 'item1': 'val1', 'item2': 'val2', '_naked_type_': 'NakedObject' })
def nobj(attributes={}): try: return NakedObject(attributes) except Exception as e: if DEBUG_FLAG: print( "Naked Framework Error: unable to create a NakedObject with the requested argument using the nobj() function (Naked.toolshed.casts.py)." ) raise e
def setUp(self): self.teststring = "this is a test" self.testdict = {'key1': 'val1', 'key2': 'val2'} self.testlist = ['item1', 'item2', 'item3'] self.testset = {'item1', 'item2', 'item3'} self.frozenset = frozenset(self.testset) self.testtuple = ('item1', 'item2', 'item3') self.stringtype = type(self.teststring) self.dicttype = type(self.testdict) self.listtype = type(self.testlist) self.settype = type(self.testset) self.fsettype = type(self.frozenset) self.tupletype = type(self.testtuple) self.nobj = NakedObject(self.testdict) self.xstring = XString(self.teststring, self.testdict) self.xdict = XDict(self.testdict, self.testdict) self.xlist = XList(self.testlist, self.testdict) self.xsets = XSet(self.testset, self.testdict) self.xfsets = XFSet(self.testset, self.testdict) self.xtuple = XTuple(self.testtuple, self.testdict) self.nobjtype = type(self.nobj) self.xstringtype = type(self.xstring) self.xdicttype = type(self.xdict) self.xlisttype = type(self.xlist) self.xsettype = type(self.xsets) self.xfsettype = type(self.xfsets) self.xtupletype = type(self.xtuple) self.test_nobj = nobj(self.testdict) self.test_xstring = xstr(self.teststring, self.testdict) self.test_xdict = xd(self.testdict, self.testdict) self.test_xlist = xl(self.testlist, self.testdict) self.test_xset = xset(self.testset, self.testdict) self.test_xfset = xfset(self.frozenset, self.testdict) self.test_xtuple = xt(self.testtuple, self.testdict)
def test_nobj_equality_attributes_fail_on_attrname(self): no = NakedObject({'key': 'value'}) nd = NakedObject({'bogus': 'value'}) self.assertFalse(no._equal_attributes(nd))
def test_nobj_attribute_default_constructor(self): no = NakedObject({'key': 'value'}) self.assertTrue(hasattr(no, 'key')) #has attribute key self.assertEqual(no.key, 'value') #with appropriate value
def test_nobj_equality_type_fail(self): no = NakedObject({'key': 'value'}) nd = "test string" self.assertFalse( no._equal_type(nd)) # different classes are not equal classes
def test_nobj_equality(self): no = NakedObject({'key': 'value'}) nd = no self.assertTrue(no == nd) # the same object self.assertFalse(no != nd)
def test_nobj_has_attr_true(self): no = NakedObject({'key': 'value'}) self.assertTrue(hasattr(no, 'key')) # True for attr that exists
def test_nobj_get_attribute_noexist(self): no = NakedObject({'key': 'value'}) with self.assertRaises( AttributeError ): # raisees AttributeError on getattr attempt without attribute test = getattr(no, 'bogus')
def test_nobj_get_attribute_exists(self): no = NakedObject({'key': 'value'}) self.assertEqual(getattr(no, 'key'), no.key)
def test_nobj_equals_emptyobjs(self): no = NakedObject() nd = NakedObject() self.assertTrue( no.equals(nd) ) # NakedObjects that do not have attributes set at instantiation are equal
def test_nobj_equals_diffobj(self): no = NakedObject({'key': 'value'}) nd = "test string" self.assertFalse( no.equals(nd) ) # new object that is of different type meets False condition on equals() method
def test_nobj_equals_newobj_diff_attrval_type(self): no = NakedObject({'key': '1'}) nd = NakedObject({'key': 1}) self.assertFalse( no.equals(nd) ) # different attribute value types meets False condition on equals() method
def test_nobj_equals_newobj_diff_attrval(self): no = NakedObject({'key': 'value'}) nd = NakedObject({'key': 'bogus'}) self.assertFalse( no.equals(nd) ) # new object with different attribute values meets False condition on equals() method
def test_nobj_equals_newobj(self): no = NakedObject({'key': 'value'}) nd = NakedObject({'key': 'value'}) self.assertTrue( no.equals(nd) ) # new object that has same type and attributes meets True condition on equals() method
def test_nobj_equals(self): no = NakedObject({'key': 'value'}) nd = no self.assertTrue( no.equals(nd) ) # variable that points to same object meets True condition on equals() method
def test_nobj_set_attribute_dot(self): no = NakedObject() no.key = 'value' self.assertTrue(hasattr(no, 'key')) #has attr key self.assertEqual(no.key, 'value') #with appropriate value
def test_nobj_set_attribute_dict(self): no = NakedObject({'key': 'value'}) no.key2 = 'value2' # add new attribute self.assertTrue(hasattr(no, 'key2')) self.assertEqual(no.key2, 'value2')
def test_nobj_equality_type(self): no = NakedObject({'key': 'value'}) nd = no self.assertTrue(no._equal_type( nd)) # variable that points to same object is same class
def test_nobj_get_attribute_dot_noexist(self): no = NakedObject({'key': 'value'}) with self.assertRaises(AttributeError): test = no.bogus
def test_nobj_type_method(self): no = NakedObject({'key': 'value'}) self.assertEqual(no.type(), 'NakedObject')
def test_nobj_equality_attributes(self): no = NakedObject({'key': 'value'}) nd = NakedObject({'key': 'value'}) self.assertTrue( no._equal_attributes(nd)) # attributes are the same
def test_nobj_constructor_empty(self): no = NakedObject() self.assertTrue(no)
def test_nobj_has_attr_false(self): no = NakedObject({'key': 'value'}) self.assertFalse(hasattr( no, 'bogus')) # False for attr that does not exist
def test_nobj_constructor_type(self): no = NakedObject({'key': 'value'}) self.assertEqual(type(no), type( NakedObject())) # <class 'Naked.toolshed.types.NakedObject'>
def test_nobj_constructor(self): no = NakedObject({'key': 'value'}) self.assertTrue(no)
def test_nobj_equality_type_newobj(self): no = NakedObject({'key': 'value'}) nd = NakedObject({'key': 'value'}) self.assertTrue( no._equal_type(nd)) # new object of same class is same class