Пример #1
0
class EquipmentServiceTest(unittest.TestCase):
    def setUp(self):
        self.mock_ddbb = mock.Mock(spec=Sql)
        self.equipment_service = EquipmentService(self.mock_ddbb)

    def tearDown(self):
        pass

    def test_get_equipment_item(self):
        self.mock_ddbb.select.return_value = [(1, u"Test Description", True, 500, 200, u"Test notes.")]
        item = self.equipment_service.get_equipment_item(1)
        self.assertEquals(1, item.id)
        self.assertEquals("Test Description", item.description)
        self.assertTrue(item.active)
        self.assertEquals(500, item.life_expectancy)
        self.assertEquals(200, item.prior_usage)
        self.assertEquals("Test notes.", item.notes)

    def test_get_equipment_item_non_unicode(self):
        self.mock_ddbb.select.return_value = [(1, "Test Description", True, 500, 200, "Test notes.")]
        item = self.equipment_service.get_equipment_item(1)
        self.assertEquals("Test Description", item.description)
        self.assertEquals("Test notes.", item.notes)

    def test_get_equipment_item_non_existant(self):
        self.mock_ddbb.select.return_value = []
        item = self.equipment_service.get_equipment_item(1)
        self.assertEquals(None, item)

    def test_get_all_equipment(self):
        self.mock_ddbb.select.return_value = [
            (1, u"Test item 1", True, 500, 200, u"Test notes 1."),
            (2, u"Test item 2", False, 600, 300, u"Test notes 2."),
        ]
        items = self.equipment_service.get_all_equipment()
        item = items[0]
        self.assertEquals(1, item.id)
        self.assertEquals("Test item 1", item.description)
        self.assertTrue(item.active)
        self.assertEquals(500, item.life_expectancy)
        self.assertEquals(200, item.prior_usage)
        self.assertEquals("Test notes 1.", item.notes)
        item = items[1]
        self.assertEquals(2, item.id)
        self.assertEquals("Test item 2", item.description)
        self.assertFalse(item.active)
        self.assertEquals(600, item.life_expectancy)
        self.assertEquals(300, item.prior_usage)
        self.assertEquals("Test notes 2.", item.notes)

    def test_get_all_equipment_non_existant(self):
        self.mock_ddbb.select.return_value = []
        items = self.equipment_service.get_all_equipment()
        self.assertEquals([], items)

    def test_get_active_equipment(self):
        self.mock_ddbb.select.return_value = [
            (1, u"Test item 1", True, 500, 200, u"Test notes 1."),
            (2, u"Test item 2", True, 600, 300, u"Test notes 2."),
        ]
        items = self.equipment_service.get_active_equipment()
        item = items[0]
        self.assertEquals(1, item.id)
        self.assertEquals("Test item 1", item.description)
        self.assertTrue(item.active)
        self.assertEquals(500, item.life_expectancy)
        self.assertEquals(200, item.prior_usage)
        self.assertEquals("Test notes 1.", item.notes)
        item = items[1]
        self.assertEquals(2, item.id)
        self.assertEquals("Test item 2", item.description)
        self.assertTrue(item.active)
        self.assertEquals(600, item.life_expectancy)
        self.assertEquals(300, item.prior_usage)
        self.assertEquals("Test notes 2.", item.notes)

    def test_get_active_equipment_non_existant(self):
        self.mock_ddbb.select.return_value = []
        items = self.equipment_service.get_active_equipment()
        self.assertEquals([], items)

    def test_store_equipment(self):
        equipment = Equipment()
        equipment.description = u"test description"
        equipment_ids = []

        def mock_select(table, columns, where):
            if columns == "id":
                return equipment_ids
            else:
                return [(2, u"", 1, 0, 0, u"")]

        def update_equipment_ids(*args):
            equipment_ids.append([1])

        self.mock_ddbb.select = mock.Mock(wraps=mock_select)
        self.mock_ddbb.insert.side_effect = update_equipment_ids
        stored_equipment = self.equipment_service.store_equipment(equipment)
        self.mock_ddbb.insert.assert_called_with(
            "equipment", "description,active,life_expectancy,prior_usage,notes", ["test description", 1, 0, 0, ""]
        )
        self.assertEquals(2, stored_equipment.id)

    def test_store_equipment_duplicate_description(self):
        self.mock_ddbb.select.return_value = [(1,)]
        equipment = Equipment()
        equipment.description = u"test item"
        try:
            self.equipment_service.store_equipment(equipment)
            self.fail("Should not be able to store new item with non-unique description.")
        except (EquipmentServiceException):
            pass

    def test_update_equipment(self):
        equipment = Equipment()
        equipment.id = 1
        equipment.description = u"new description"
        self.mock_ddbb.select.return_value = [(1, u"old description", 1, 0, 0, u"")]
        self.equipment_service.store_equipment(equipment)
        self.mock_ddbb.update.assert_called_with(
            "equipment",
            "description,active,life_expectancy,prior_usage,notes",
            ["new description", 1, 0, 0, ""],
            "id = 1",
        )

    def test_update_equipment_non_existant(self):
        self.mock_ddbb.select.return_value = []
        equipment = Equipment()
        equipment.id = 1
        try:
            self.equipment_service.store_equipment(equipment)
            self.fail("Should not be able to update an item which did not previously exist.")
        except (EquipmentServiceException):
            pass

    def test_update_equipment_duplicate_description(self):
        self.mock_ddbb.select.return_value = [(1, u"test item", True, 500, 200, u"Test notes.")]
        equipment = Equipment()
        equipment.id = 2
        equipment.description = u"test item"
        try:
            self.equipment_service.store_equipment(equipment)
            self.fail("Should not be able to change item description to non-unique value.")
        except (EquipmentServiceException):
            pass

    def test_get_equipment_usage(self):
        self.mock_ddbb.select.return_value = [(250,)]
        equipment = Equipment()
        equipment.id = 1
        usage = self.equipment_service.get_equipment_usage(equipment)
        self.assertEquals(250, usage)

    def test_get_equipment_usage_none(self):
        self.mock_ddbb.select.return_value = [(None,)]
        equipment = Equipment()
        equipment.id = 1
        usage = self.equipment_service.get_equipment_usage(equipment)
        self.assertEquals(0, usage)
Пример #2
0
class EquipmentServiceTest(unittest.TestCase):
    
    def setUp(self):
        self.mock_ddbb = DDBB()
        self.mock_ddbb.connect()
        self.mock_ddbb.create_tables()
        self.equipment_service = EquipmentService(self.mock_ddbb)
        self.equipment_table = DeclarativeBase.metadata.tables['equipment']
        
    def tearDown(self):
        self.mock_ddbb.disconnect()
        self.mock_ddbb.drop_tables()
    
    def test_get_equipment_item(self):
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 500,
                                                "notes": u"Test notes.",
                                           "description": u"Test Description",
                                           "prior_usage": 200, "active": True})
        item = self.equipment_service.get_equipment_item(1)
        self.assertEquals(1, item.id)
        self.assertEquals("Test Description", item.description)
        self.assertTrue(item.active)
        self.assertEquals(500, item.life_expectancy)
        self.assertEquals(200, item.prior_usage)
        self.assertEquals("Test notes.", item.notes)
    
    def test_get_equipment_item_non_unicode(self):
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 500,
                                                "notes": u"Test notes.",
                                           "description": u"Test Description",
                                           "prior_usage": 200, "active": True})
        item = self.equipment_service.get_equipment_item(1)
        self.assertEquals("Test Description", item.description)
        self.assertEquals("Test notes.", item.notes)
    
    def test_get_equipment_item_non_existant(self):
        item = self.equipment_service.get_equipment_item(1)
        self.assertEquals(None, item)
        
    def test_get_all_equipment(self):
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 500,
                                                "notes": u"Test notes 1.",
                                           "description": u"Test item 1",
                                           "prior_usage": 200, "active": True})
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 600,
                                                "notes": u"Test notes 2.",
                                           "description": u"Test item 2",
                                           "prior_usage": 300, "active": False})
        items = self.equipment_service.get_all_equipment()
        item = items[0]
        self.assertEquals(1, item.id)
        self.assertEquals("Test item 1", item.description)
        self.assertTrue(item.active)
        self.assertEquals(500, item.life_expectancy)
        self.assertEquals(200, item.prior_usage)
        self.assertEquals("Test notes 1.", item.notes)
        item = items[1]
        self.assertEquals(2, item.id)
        self.assertEquals("Test item 2", item.description)
        self.assertFalse(item.active)
        self.assertEquals(600, item.life_expectancy)
        self.assertEquals(300, item.prior_usage)
        self.assertEquals("Test notes 2.", item.notes)
        
    def test_get_all_equipment_non_existant(self):
        items = self.equipment_service.get_all_equipment()
        self.assertEquals([], items)
        
    def test_get_active_equipment(self):
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 500,
                                                "notes": u"Test notes 1.",
                                           "description": u"Test item 1",
                                           "prior_usage": 200, "active": True})
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 600,
                                                "notes": u"Test notes 2.",
                                           "description": u"Test item 2",
                                           "prior_usage": 300, "active": True})
        items = self.equipment_service.get_active_equipment()
        item = items[0]
        self.assertEquals(1, item.id)
        self.assertEquals("Test item 1", item.description)
        self.assertTrue(item.active)
        self.assertEquals(500, item.life_expectancy)
        self.assertEquals(200, item.prior_usage)
        self.assertEquals("Test notes 1.", item.notes)
        item = items[1]
        self.assertEquals(2, item.id)
        self.assertEquals("Test item 2", item.description)
        self.assertTrue(item.active)
        self.assertEquals(600, item.life_expectancy)
        self.assertEquals(300, item.prior_usage)
        self.assertEquals("Test notes 2.", item.notes)
        
    def test_get_active_equipment_non_existant(self):
        items = self.equipment_service.get_active_equipment()
        self.assertEquals([], items)
        
    def test_store_equipment(self):
        equipment = Equipment()
        equipment.description = u"test description"
        stored_equipment = self.equipment_service.store_equipment(equipment)
        self.assertEquals(1, stored_equipment.id)
        
    def test_store_equipment_duplicate_description(self):
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 500,
                                                "notes": u"Test notes.",
                                           "description": u"test item",
                                           "prior_usage": 200, "active": True})
        equipment = Equipment()
        equipment.description = u"test item"
        try:
            self.equipment_service.store_equipment(equipment)
            self.fail("Should not be able to store new item with non-unique description.")
        except(EquipmentServiceException):
            pass
        
    def test_update_equipment(self):
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 500,
                                                "notes": u"Test notes.",
                                           "description": u"old description",
                                           "prior_usage": 200, "active": True})
        equipment = self.equipment_service.get_equipment_item(1)
        equipment.description = u"new description"
        self.equipment_service.store_equipment(equipment)
        equipment = self.equipment_service.get_equipment_item(1)
        self.assertEquals("new description", equipment.description)
        
    def test_update_equipment_duplicate_description(self):
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 500,
                                                "notes": u"Test notes.",
                                           "description": u"test item",
                                           "prior_usage": 200, "active": True})
        equipment = Equipment()
        equipment.id = 2
        equipment.description = u"test item"
        try:
            self.equipment_service.store_equipment(equipment)
            self.fail("Should not be able to change item description to non-unique value.")
        except(EquipmentServiceException):
            pass
        
    def test_get_equipment_usage(self):
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 500,
                                                "notes": u"Test notes.",
                                           "description": u"test item",
                                           "prior_usage": 0, "active": True})
        self.mock_ddbb.session.execute("insert into records (distance,sport) values (250,1)")
        self.mock_ddbb.session.execute("insert into record_equipment (record_id,equipment_id) values (1,1)")
        equipment = self.equipment_service.get_equipment_item(1)
        usage = self.equipment_service.get_equipment_usage(equipment)
        self.assertEquals(250, usage)
        
    def test_get_equipment_usage_none(self):
        self.mock_ddbb.session.execute(self.equipment_table.insert(),
                                           {"life_expectancy": 500,
                                                "notes": u"Test notes.",
                                           "description": u"test item",
                                           "prior_usage": 0, "active": True})
        equipment = self.equipment_service.get_equipment_item(1)
        usage = self.equipment_service.get_equipment_usage(equipment)
        self.assertEquals(0, usage)

    def test_get_equipment_prior_usage(self):
        equipment = Equipment()
        equipment.id = 1
        equipment.prior_usage = 250
        usage = self.equipment_service.get_equipment_usage(equipment)
        self.assertEquals(250, usage)

    def test_get_equipment_prior_usage(self):
        equipment = Equipment()
        equipment.id = 1
        equipment.prior_usage = 250
        usage = self.equipment_service.get_equipment_usage(equipment)
        self.assertEquals(250, usage)
Пример #3
0
class EquipmentServiceTest(unittest.TestCase):
    
    def setUp(self):
        self.mock_ddbb = mock.Mock(spec=Sql)
        self.equipment_service = EquipmentService(self.mock_ddbb)
        
    def tearDown(self):
        pass
    
    def test_get_equipment_item(self):
        self.mock_ddbb.select.return_value = [(1, u"Test Description", True, 500, 200, u"Test notes.")]
        item = self.equipment_service.get_equipment_item(1)
        self.assertEquals(1, item.id)
        self.assertEquals("Test Description", item.description)
        self.assertTrue(item.active)
        self.assertEquals(500, item.life_expectancy)
        self.assertEquals(200, item.prior_usage)
        self.assertEquals("Test notes.", item.notes)
    
    def test_get_equipment_item_non_unicode(self):
        self.mock_ddbb.select.return_value = [(1, "Test Description", True, 500, 200, "Test notes.")]
        item = self.equipment_service.get_equipment_item(1)
        self.assertEquals("Test Description", item.description)
        self.assertEquals("Test notes.", item.notes)
    
    def test_get_equipment_item_non_existant(self):
        self.mock_ddbb.select.return_value = []
        item = self.equipment_service.get_equipment_item(1)
        self.assertEquals(None, item)
        
    def test_get_all_equipment(self):
        self.mock_ddbb.select.return_value = [(1, u"Test item 1", True, 500, 200, u"Test notes 1."),
                                              (2, u"Test item 2", False, 600, 300, u"Test notes 2.")]
        items = self.equipment_service.get_all_equipment()
        item = items[0]
        self.assertEquals(1, item.id)
        self.assertEquals("Test item 1", item.description)
        self.assertTrue(item.active)
        self.assertEquals(500, item.life_expectancy)
        self.assertEquals(200, item.prior_usage)
        self.assertEquals("Test notes 1.", item.notes)
        item = items[1]
        self.assertEquals(2, item.id)
        self.assertEquals("Test item 2", item.description)
        self.assertFalse(item.active)
        self.assertEquals(600, item.life_expectancy)
        self.assertEquals(300, item.prior_usage)
        self.assertEquals("Test notes 2.", item.notes)
        
    def test_get_all_equipment_non_existant(self):
        self.mock_ddbb.select.return_value = []
        items = self.equipment_service.get_all_equipment()
        self.assertEquals([], items)
        
    def test_get_active_equipment(self):
        self.mock_ddbb.select.return_value = [(1, u"Test item 1", True, 500, 200, u"Test notes 1."),
                                              (2, u"Test item 2", True, 600, 300, u"Test notes 2.")]
        items = self.equipment_service.get_active_equipment()
        item = items[0]
        self.assertEquals(1, item.id)
        self.assertEquals("Test item 1", item.description)
        self.assertTrue(item.active)
        self.assertEquals(500, item.life_expectancy)
        self.assertEquals(200, item.prior_usage)
        self.assertEquals("Test notes 1.", item.notes)
        item = items[1]
        self.assertEquals(2, item.id)
        self.assertEquals("Test item 2", item.description)
        self.assertTrue(item.active)
        self.assertEquals(600, item.life_expectancy)
        self.assertEquals(300, item.prior_usage)
        self.assertEquals("Test notes 2.", item.notes)
        
    def test_get_active_equipment_non_existant(self):
        self.mock_ddbb.select.return_value = []
        items = self.equipment_service.get_active_equipment()
        self.assertEquals([], items)
        
    def test_store_equipment(self):
        equipment = Equipment()
        equipment.description = u"test description"
        equipment_ids = []
        def mock_select(table, columns, where):
            if columns == "id":
                return equipment_ids
            else:
                return [(2, u"", 1, 0, 0,u"")]
        def update_equipment_ids(*args):
            equipment_ids.append([1])
        self.mock_ddbb.select = mock.Mock(wraps=mock_select)
        self.mock_ddbb.insert.side_effect = update_equipment_ids
        stored_equipment = self.equipment_service.store_equipment(equipment)
        self.mock_ddbb.insert.assert_called_with("equipment", 
                                                 "description,active,life_expectancy,prior_usage,notes", 
                                                 ["test description", 1, 0, 0,"" ])
        self.assertEquals(2, stored_equipment.id)
        
    def test_store_equipment_duplicate_description(self):
        self.mock_ddbb.select.return_value = [(1,)]
        equipment = Equipment()
        equipment.description = u"test item"
        try:
            self.equipment_service.store_equipment(equipment)
            self.fail("Should not be able to store new item with non-unique description.")
        except(EquipmentServiceException):
            pass
        
    def test_update_equipment(self):
        equipment = Equipment()
        equipment.id = 1
        equipment.description = u"new description"
        self.mock_ddbb.select.return_value =  [(1, u"old description", 1, 0, 0,u"")]
        self.equipment_service.store_equipment(equipment)
        self.mock_ddbb.update.assert_called_with("equipment", 
                                                 "description,active,life_expectancy,prior_usage,notes", 
                                                 ["new description", 1, 0, 0,"" ], 
                                                 "id = 1")
        
    def test_update_equipment_non_existant(self):
        self.mock_ddbb.select.return_value = []
        equipment = Equipment()
        equipment.id = 1
        try:
            self.equipment_service.store_equipment(equipment)
            self.fail("Should not be able to update an item which did not previously exist.")
        except(EquipmentServiceException):
            pass
        
    def test_update_equipment_duplicate_description(self):
        self.mock_ddbb.select.return_value = [(1, u"test item", True, 500, 200, u"Test notes.")]
        equipment = Equipment()
        equipment.id = 2
        equipment.description = u"test item"
        try:
            self.equipment_service.store_equipment(equipment)
            self.fail("Should not be able to change item description to non-unique value.")
        except(EquipmentServiceException):
            pass
        
    def test_get_equipment_usage(self):
        self.mock_ddbb.select.return_value = [(250,)]
        equipment = Equipment()
        equipment.id = 1
        usage = self.equipment_service.get_equipment_usage(equipment)
        self.assertEquals(250, usage)
        
    def test_get_equipment_usage_none(self):
        self.mock_ddbb.select.return_value = [(None,)]
        equipment = Equipment()
        equipment.id = 1
        usage = self.equipment_service.get_equipment_usage(equipment)
        self.assertEquals(0, usage)