def setUp(self):
     self.in_memory_db = 'sqlite:///:memory:'
     self.deckscraper = NetrunnerDeckScraper()
from utilities.netrunnerdb import NetrunnerCardScraper, NetrunnerDeckScraper

if __name__ == '__main__':
    netrunner_cards = NetrunnerCardScraper()
    netrunner_decklists = NetrunnerDeckScraper()
    netrunner_cards.main()
    netrunner_decklists.main()
class TestBasicDecksScraper(unittest.TestCase):

    def setUp(self):
        self.in_memory_db = 'sqlite:///:memory:'
        self.deckscraper = NetrunnerDeckScraper()

    def tearDown(self):
        pass

    def test_basic_deck_scraper_has_main_method(self):
        self.assertEqual(self.deckscraper.main(),
                         'main function',
                         'Main function currently should return said text')

    def test_basic_parameters_init(self):
        self.assertEqual(self.deckscraper.base_url,
                         'http://netrunnerdb.com',
                         'Base URL should be netrunnerdb.com')
        self.assertEqual(self.deckscraper.endpoint,
                         '/api/decklists/by_date/',
                         'Endpoint should be api/decklists/by_date')

    def test_class_gets_netrunner_json(self):
        decks = self.deckscraper._get_one_days_decks('2013-12-31')
        self.assertNotEqual(len(decks),
                            0,
                            'Should receive many decks for api call')

    def test_class_returns_parsed_dictionary(self):
        decks = self.deckscraper._get_one_days_decks('2013-12-31')
        parsed_deck = self.deckscraper._parse_deck_info(decks[0])
        [self.assertIn(column,
                       list(parsed_deck.keys()),
                       '%s should be in parsed deck dict' % column)
         for column in ['name', 'username', 'id',
                        'description', 'created_at']]

    def test_class_returns_parsed_decklist_dictionary(self):
        decks = self.deckscraper._get_one_days_decks('2013-12-31')
        parsed_decklist = self.deckscraper._parse_decklist_info(decks[0])
        self.assertIsInstance(parsed_decklist,
                              list,
                              'Should return dictionary')

    def test_database_mapping_decks_db(self):
        decks = Decks()
        self.assertEqual(decks.__tablename__,
                         'decks',
                         'Table should return expected tablename')

    def test_database_adds_test_info(self):
        Base()
        Cards()
        Decks()
        Decklists()
        engine = self.deckscraper._connect_to_db(self.in_memory_db)
        Base.metadata.create_all(engine)
        test_deck = Decks(
            deck_id = '88867',
            name = 'test deck',
            username = '******',
            description = 'test description',
            created_at = 'test created_at'
        )
        test_decklist = Decklists(
            deck_id = '1117',
            card_id = '00045',
            quantity = 0
        )
        self.assertEqual(test_deck.id,
                         None)
        self.assertEqual(test_decklist.id,
                         None)
        Session = sessionmaker(bind=engine)
        session = Session()
        session.add(test_deck)
        session.add(test_decklist)
        query = session.query(Decks)
        self.assertEqual(query.filter_by(name='test deck').first().name,
                         'test deck',
                         'Query to newly added data should be test deck')
        query = session.query(Decklists)
        self.assertEqual(query.filter_by(deck_id='1117').first().quantity,
                         0,
                         'Quantity for newly added data should be 0')

    def test_scraper_adds_deck_info_correctly(self):
        Base()
        Decks()
        engine = self.deckscraper._connect_to_db(self.in_memory_db)
        Base.metadata.create_all(engine)
        Session = sessionmaker(bind=engine)
        session = Session()
        decks = self.deckscraper._get_one_days_decks('2013-12-31')
        parsed_deck = self.deckscraper._parse_deck_info(decks[0])
        self.deckscraper.add_deck_info_to_db(parsed_deck, session)
        test_query = session.query(Decks).first()
        self.assertEqual(test_query.deck_id,
                         '1171')

    def test_scraper_adds_decklist_info_correctly(self):
        Base()
        Decklists()
        engine = self.deckscraper._connect_to_db(self.in_memory_db)
        Base.metadata.create_all(engine)
        Session = sessionmaker(bind=engine)
        session = Session()
        decks = self.deckscraper._get_one_days_decks('2013-12-31')
        parsed_decklist = self.deckscraper._parse_decklist_info(decks[0])
        self.deckscraper.add_decklist_info_to_db(parsed_decklist, session)
        test_query = session.query(Decklists).first()
        self.assertEqual(test_query.deck_id,
                          '1171',
                         test_query)