def test_mandatory_fields(self): """ Validates whether mandatory properties and relations work """ machine = TestMachine() machine.extended = 'extended' machine.name = 'machine' machine.save() disk = TestDisk() # Modify relation to mandatory [_ for _ in disk._relations if _.name == 'machine'][0].mandatory = True # Continue test disk.name = None with self.assertRaises(MissingMandatoryFieldsException) as exception: disk.save() self.assertIn('name', exception.exception.message, 'Field name should be in exception message: {0}'.format(exception.exception.message)) self.assertIn('machine', exception.exception.message, 'Field machine should be in exception message: {0}'.format(exception.exception.message)) disk.name = 'disk' disk.machine = machine disk.save() disk.description = 'test' disk.storage = machine disk.save() # Restore relation [_ for _ in disk._relations if _.name == 'machine'][0].mandatory = False
def test_queries(self): """ Validates whether executing queries returns the expected results """ machine = TestMachine() machine.name = 'machine' machine.save() for i in xrange(0, 20): disk = TestDisk() disk.name = 'test_{0}'.format(i) disk.size = i if i < 10: disk.machine = machine else: disk.storage = machine disk.save() self.assertEqual(len(machine.disks), 10, 'query should find added machines') # pylint: disable=line-too-long list_1 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.AND, 'items': [('size', DataList.operator.EQUALS, 1)]}}).data # noqa self.assertEqual(list_1, 1, 'list should contain int 1') list_2 = DataList({'object': TestDisk, 'data': DataList.select.GUIDS, 'query': {'type': DataList.where_operator.AND, 'items': [('size', DataList.operator.EQUALS, 1)]}}).data # noqa found_object = Descriptor(TestDisk, list_2[0]).get_object(True) self.assertEqual(found_object.name, 'test_1', 'list should contain correct machine') list_3 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.AND, 'items': [('size', DataList.operator.GT, 3), ('size', DataList.operator.LT, 6)]}}).data # noqa self.assertEqual(list_3, 2, 'list should contain int 2') # disk 4 and 5 list_4 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.OR, 'items': [('size', DataList.operator.LT, 3), ('size', DataList.operator.GT, 6)]}}).data # noqa # at least disk 0, 1, 2, 7, 8, 9, 10-19 self.assertGreaterEqual(list_4, 16, 'list should contain >= 16') list_5 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.AND, 'items': [('machine.guid', DataList.operator.EQUALS, machine.guid), # noqa {'type': DataList.where_operator.OR, 'items': [('size', DataList.operator.LT, 3), ('size', DataList.operator.GT, 6)]}]}}).data # noqa self.assertEqual(list_5, 6, 'list should contain int 6') # disk 0, 1, 2, 7, 8, 9 list_6 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.AND, 'items': [('size', DataList.operator.LT, 3), ('size', DataList.operator.GT, 6)]}}).data # noqa self.assertEqual(list_6, 0, 'list should contain int 0') # no disks list_7 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.OR, 'items': [('machine.guid', DataList.operator.EQUALS, '123'), # noqa ('used_size', DataList.operator.EQUALS, -1), {'type': DataList.where_operator.AND, 'items': [('size', DataList.operator.GT, 3), ('size', DataList.operator.LT, 6)]}]}}).data # noqa self.assertEqual(list_7, 2, 'list should contain int 2') # disk 4 and 5 list_8 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.AND, 'items': [('machine.name', DataList.operator.EQUALS, 'machine'), # noqa ('name', DataList.operator.EQUALS, 'test_3')]}}).data # noqa self.assertEqual(list_8, 1, 'list should contain int 1') # disk 3 list_9 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.AND, 'items': [('size', DataList.operator.GT, 3), {'type': DataList.where_operator.AND, 'items': [('size', DataList.operator.LT, 6)]}]}}).data # noqa self.assertEqual(list_9, 2, 'list should contain int 2') # disk 4 and 5 list_10 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.OR, 'items': [('size', DataList.operator.LT, 3), {'type': DataList.where_operator.OR, 'items': [('size', DataList.operator.GT, 6)]}]}}).data # noqa # at least disk 0, 1, 2, 7, 8, 9, 10-19 self.assertGreaterEqual(list_10, 16, 'list should contain >= 16') list_11 = DataList({'object': TestDisk, 'data': DataList.select.COUNT, 'query': {'type': DataList.where_operator.AND, 'items': [('storage.name', DataList.operator.EQUALS, 'machine')]}}).data # noqa self.assertEqual(list_11, 10, 'list should contain int 10') # disk 10-19