0

I've got an application which uses files of configurations for example and If I run the application all these files are found and there is no problem, but If I do it from my tests I've got this error:

Error
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/home/josecarlos/Workspace/python/reports/test/data_test.py", line 50, in test_players_teams_ss
    data = Data(params)
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 10, in __init__
    self.home_standard_stats = self.team_standard_stats(params["home"], params["league"])
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 53, in team_standard_stats
    id_season = self.get_id_season(id_league)
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 27, in get_id_season
    db = SearchData(args, "season=")
  File "/home/josecarlos/Workspace/python/reports/ddbb/feb/data/search.py", line 18, in __init__
    q = query.getQuery(query_name)
  File "/home/josecarlos/Workspace/python/reports/com/files/queryreader.py", line 14, in getQuery
    with open(self.getURL()) as file:
FileNotFoundError: [Errno 2] No such file or directory: './files/querys.sql'

This is my test file, data_test.py

import unittest
from com.data import Data
from constants import COMPETITIONS
import os

class TestDataMethods(unittest.TestCase):
    def test_season(self):
        #os.chdir("../") #We need to do this to go to the main directory of the app. If we don't do this, the code will run over ./test
        params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 771, "away": 775}
        season = Data.get_id_season(params["league"])

    def test_team_standard_stats(self):
        #os.chdir("../")
        params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 771, "away": 775}
        data = Data(params)
        home_tss = data.home_standard_stats
        self.assertEqual(home_tss.get_result().getTotalRows(), 1, "Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(int(home_tss.get_result().getData()[0]["t2p_int"]), 952, "Error al devolver el número de t2p_int")
        self.assertEqual(float(home_tss.get_result().getData()[0]["t2p_percentage"]), 43.38, "Error al devolver el número de t2p_percentage")
        self.assertEqual(float(home_tss.get_result().getData()[0]["ppa"]), 0.84, "Error al devolver los puntos por intento del equipo")
        params = {"report" : 1, "league" : COMPETITIONS.EUROLEAGUE, "home": 79, "away": 93}
        data = Data(params)
        home_tss = data.home_standard_stats
        self.assertEqual(home_tss.get_result().getTotalRows(), 1, "Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(int(home_tss.get_result().getData()[0]["t2p_int"]), 848, "Error al devolver el número de t2p_int")
        self.assertEqual(float(home_tss.get_result().getData()[0]["t2p_percentage"]), 49.53, "Error al devolver el número de t2p_percentage")
        self.assertEqual(float(home_tss.get_result().getData()[0]["ppa"]), 0.92, "Error al devolver los puntos por intento del equipo")

    def test_team_advanced_stats(self):
        #os.chdir("../")
        params = {"report": 1, "league": COMPETITIONS.LF1, "home": 770, "away": 769}
        data = Data(params)
        home_tas = data.home_advanced_stats
        self.assertEqual(home_tas.get_result().getTotalRows(), 1, "Local:: Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(float(home_tas.get_result().getData()[0]["ortg"]), 109.08, "Local:: Error al devolver el ORTG")
        params = {"report": 1, "league": COMPETITIONS.LF1, "home": 770, "away": 769}
        data = Data(params)
        away_tas = data.away_advanced_stats
        self.assertEqual(away_tas.get_result().getTotalRows(), 1, "Visitante:: Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(float(away_tas.get_result().getData()[0]["drtg"]), 81.96, "Visitante: Error al deveolver el DRTG")
        params = {"report": 1, "league": COMPETITIONS.EUROLEAGUE, "home": 79, "away": 93}
        data = Data(params)
        away_tas = data.away_advanced_stats
        self.assertEqual(away_tas.get_result().getTotalRows(), 1, "Visitante:: Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(float(away_tas.get_result().getData()[0]["drtg"]), 106.25, "Visitante: Error al deveolver el DRTG")

    def test_players_teams_ss(self):
        #os.chdir("../")
        params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 770, "away": 769}
        data = Data(params)
        home_pt_ss = data.players_team_ss(params["home"], params["league"])
        self.assertEqual(home_pt_ss.get_result().getTotalRows(), 13, "Error: Número de registros recibido no es correcto")
        self.assertEqual(home_pt_ss.get_result().getData()[8]["numero"], 44, "Error: El número de la jugadora no es correcto")
        self.assertEqual(float(home_pt_ss.get_result().getData()[3]["t2p_percentage"]), 55.03, "Error: El porcentaje de acierto de T2P no es correcto")
        params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 770, "away": 769}
        data = Data(params)
        away_pt_ss = data.players_team_ss(params["away"], params["league"])
        self.assertEqual(away_pt_ss.get_result().getTotalRows(), 15, "Error: Número de registros recibido no es correcto")
        self.assertEqual(away_pt_ss.get_result().getData()[8]["numero"], 3, "Error: El número de la jugadora no es correcto")
        self.assertEqual(float(away_pt_ss.get_result().getData()[3]["t2p_percentage"]), 37.25, "Error: El porcentaje de acierto de T2P no es correcto")


    def test_players_teams_as(self):
        pass


if __name__== "__main__":
    unittest.main()

And this is the file over we run the test, data.py

from ddbb.fiba.data.search import SearchDataFIBA
from ddbb.feb.data.search import SearchData
from constants import COMPETITIONS, SEASONS


class Data():
    def __init__(self, params):
        self.params = params
        #Recuperar datos de estadísticas estandard del equipo local y visitante
        self.home_standard_stats = self.team_standard_stats(params["home"], params["league"])
        self.away_standard_stats = self.team_standard_stats(params["away"], params["league"])
        #Recuperar datos de estadísticas avanzadas del equipo local y visitante
        self.home_advanced_stats = self.team_advanced_stats(params["home"], params["league"])
        self.away_advanced_stats = self.team_advanced_stats(params["away"], params["league"])
        #Recuperar datos de estadísticas estandard y avanzadas del las jugadoras del equipo local y visitante
        self.home_pt_ss = self.players_team_ss(params["home"], params["league"])
        #self.away_pt_ss = self.players_team_ss(params["away"], params["league"])
        #Recuperar datos de estadísticas estandard y avanzadas de las jugadoras del equipo local y visitante
        self.home_pt_as = self.players_team_as(params["home"], params["league"])
        self.away_pt_as = self.players_team_as(params["away"], params["league"])

    @staticmethod
    def get_id_season(league):
        '''Returns id season of the current season for the league passed like parameter'''
        if league == COMPETITIONS.LF1:
            args = [1]
            db = SearchData(args, "season=")
            return db.get_result().getData()[0]["id"]
        if league == COMPETITIONS.LF2:
            args = [2]
            db = SearchData(args, "season=")
            return db.get_result().getData()[0]["id"]
        if league == COMPETITIONS.EUROLEAGUE:
            args = [1]
            db = SearchDataFIBA(args, "season=")
            return db.get_result().getData()[0]["id"]
        if league == COMPETITIONS.EUROCUP:
            args = [2]
            db = SearchDataFIBA(args, "season=")
            return db.get_result().getData()[0]["id"]

    def team_advanced_stats(self, id_team, id_league):
        '''Return the advanced stats from a team in a whole season'''
        id_season = self.get_id_season(id_league)
        args = [id_team, id_team, id_season, id_team, id_team]
        if id_league == COMPETITIONS.LF1 or id_league == COMPETITIONS.LF2:
            return SearchData(args, "team.adv.stats=")
        if id_league == COMPETITIONS.EUROLEAGUE or id_league == COMPETITIONS.EUROLEAGUE:
            return SearchDataFIBA(args, "team.adv.stats=")

    def team_standard_stats(self, id_team, id_league):
        '''Return the standard stats from a team in a whole season'''
        id_season = self.get_id_season(id_league)
        args = [id_team, id_season]
        if id_league == COMPETITIONS.LF1 or id_league == COMPETITIONS.LF2:
            return SearchData(args, "team.std.stats=")
        if id_league == COMPETITIONS.EUROLEAGUE or id_league == COMPETITIONS.EUROCUP:
            return  SearchDataFIBA(args, "team.std.stats=")

    @staticmethod
    def players_team_ss(id_team, id_league):
        '''Return standard stats of all the players of a team for a whole season'''
        args = [id_team]
        if id_league == COMPETITIONS.LF1 or id_league == COMPETITIONS.LF2:
            return SearchData(args, "feb.players.std.stats=")
        if id_league == COMPETITIONS.EUROLEAGUE or id_league == COMPETITIONS.EUROCUP:
            return SearchDataFIBA(args, "fiba.players.std.stats=")

    @staticmethod
    def players_team_as(id_team, id_league):
        '''Returns advanced stats of all the players of a team for a whole season'''
        args = [id_team]
        if id_league == COMPETITIONS.LF1 or id_league == COMPETITIONS.LF2:
            return SearchData(args, "feb.players.adv.stats=")
        if id_league == COMPETITIONS.EUROLEAGUE or id_league == COMPETITIONS.EUROCUP:
            return SearchDataFIBA(args, "fiba.players.adv.stats=")

And this my structure of folders in the project:

enter image description here

What can I do to find the file?

Edit I

I have added this line to my code in test_sesion "os.chdir('../')"

The method right now is:

def test_season(self):
    os.chdir("../") #We need to do this to go to the main directory of the app. If we don't do this, the code will run over ./test
    params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 771, "away": 775}
    season = Data.get_id_season(params["league"])

This method access to data base and to do this needs to access fhe file /files/querys.sql and the test works fine!!!

If I run the test but adding the method tests test_team_standard_stats and test_team_advanced_stats the code of the test file is:

import unittest
from com.data import Data
from constants import COMPETITIONS
import os

class TestDataMethods(unittest.TestCase):
    def test_season(self):
        os.chdir("../") #We need to do this to go to the main directory of the app. If we don't do this, the code will run over ./test
        params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 771, "away": 775}
        season = Data.get_id_season(params["league"])

    def test_team_standard_stats(self):
        #os.chdir("../")
        params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 771, "away": 775}
        data = Data(params)
        home_tss = data.home_standard_stats
        self.assertEqual(home_tss.get_result().getTotalRows(), 1, "Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(int(home_tss.get_result().getData()[0]["t2p_int"]), 952, "Error al devolver el número de t2p_int")
        self.assertEqual(float(home_tss.get_result().getData()[0]["t2p_percentage"]), 43.38, "Error al devolver el número de t2p_percentage")
        self.assertEqual(float(home_tss.get_result().getData()[0]["ppa"]), 0.84, "Error al devolver los puntos por intento del equipo")
        params = {"report" : 1, "league" : COMPETITIONS.EUROLEAGUE, "home": 79, "away": 93}
        data = Data(params)
        home_tss = data.home_standard_stats
        self.assertEqual(home_tss.get_result().getTotalRows(), 1, "Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(int(home_tss.get_result().getData()[0]["t2p_int"]), 848, "Error al devolver el número de t2p_int")
        self.assertEqual(float(home_tss.get_result().getData()[0]["t2p_percentage"]), 49.53, "Error al devolver el número de t2p_percentage")
        self.assertEqual(float(home_tss.get_result().getData()[0]["ppa"]), 0.92, "Error al devolver los puntos por intento del equipo")

    def test_team_advanced_stats(self):
        params = {"report": 1, "league": COMPETITIONS.LF1, "home": 770, "away": 769}
        data = Data(params)
        home_tas = data.home_advanced_stats
        self.assertEqual(home_tas.get_result().getTotalRows(), 1, "Local:: Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(float(home_tas.get_result().getData()[0]["ortg"]), 109.08, "Local:: Error al devolver el ORTG")
        params = {"report": 1, "league": COMPETITIONS.LF1, "home": 770, "away": 769}
        data = Data(params)
        away_tas = data.away_advanced_stats
        self.assertEqual(away_tas.get_result().getTotalRows(), 1, "Visitante:: Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(float(away_tas.get_result().getData()[0]["drtg"]), 81.96, "Visitante: Error al deveolver el DRTG")
        params = {"report": 1, "league": COMPETITIONS.EUROLEAGUE, "home": 79, "away": 93}
        data = Data(params)
        away_tas = data.away_advanced_stats
        self.assertEqual(away_tas.get_result().getTotalRows(), 1, "Visitante:: Error al devolver datos. No se devuelve 1 única fila de datos")
        self.assertEqual(float(away_tas.get_result().getData()[0]["drtg"]), 106.25, "Visitante: Error al deveolver el DRTG")

    # def test_players_teams_ss(self):
    #     params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 770, "away": 769}
    #     data = Data(params)
    #     home_pt_ss = data.players_team_ss(params["home"], params["league"])
    #     self.assertEqual(home_pt_ss.get_result().getTotalRows(), 13, "Error: Número de registros recibido no es correcto")
    #     self.assertEqual(home_pt_ss.get_result().getData()[8]["numero"], 44, "Error: El número de la jugadora no es correcto")
    #     self.assertEqual(float(home_pt_ss.get_result().getData()[3]["t2p_percentage"]), 55.03, "Error: El porcentaje de acierto de T2P no es correcto")
    #     params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 770, "away": 769}
    #     data = Data(params)
    #     away_pt_ss = data.players_team_ss(params["away"], params["league"])
    #     self.assertEqual(away_pt_ss.get_result().getTotalRows(), 15, "Error: Número de registros recibido no es correcto")
    #     self.assertEqual(away_pt_ss.get_result().getData()[8]["numero"], 3, "Error: El número de la jugadora no es correcto")
    #     self.assertEqual(float(away_pt_ss.get_result().getData()[3]["t2p_percentage"]), 37.25, "Error: El porcentaje de acierto de T2P no es correcto")

    #
    # def test_players_teams_as(self):
    #     pass


if __name__== "__main__":
    unittest.main()

¡¡¡The test works fine!!!

But if I add the method test_players_teams_ss to the test then this doesn't work :( I don't understand anything :(

Error
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/home/josecarlos/Workspace/python/reports/test/data_test.py", line 10, in test_season
    season = Data.get_id_season(params["league"])
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 27, in get_id_season
    db = SearchData(args, "season=")
  File "/home/josecarlos/Workspace/python/reports/ddbb/feb/data/search.py", line 18, in __init__
    q = query.getQuery(query_name)
  File "/home/josecarlos/Workspace/python/reports/com/files/queryreader.py", line 14, in getQuery
    with open(self.getURL()) as file:
FileNotFoundError: [Errno 2] No such file or directory: './files/querys.sql'

How it does possible?

Edit II:

I have made more tests over my test file and I don't understand anything. I have commented all the code except the code of the method test test_players_teams_ss I remember that this test didn't work before. Ok, I have added this line to the method os.chdir('../')

The code of the method is:

def test_players_teams_ss(self):
    os.chdir("../")
    params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 770, "away": 769}
    data = Data(params)
    home_pt_ss = data.players_team_ss(params["home"], params["league"])
    self.assertEqual(home_pt_ss.get_result().getTotalRows(), 13, "Error: Número de registros recibido no es correcto")
    self.assertEqual(home_pt_ss.get_result().getData()[8]["numero"], 44, "Error: El número de la jugadora no es correcto")
    self.assertEqual(float(home_pt_ss.get_result().getData()[3]["t2p_percentage"]), 55.03, "Error: El porcentaje de acierto de T2P no es correcto")
    params = {"report" : 1, "league" : COMPETITIONS.LF1, "home": 770, "away": 769}
    data = Data(params)
    away_pt_ss = data.players_team_ss(params["away"], params["league"])
    self.assertEqual(away_pt_ss.get_result().getTotalRows(), 15, "Error: Número de registros recibido no es correcto")
    self.assertEqual(away_pt_ss.get_result().getData()[8]["numero"], 3, "Error: El número de la jugadora no es correcto")
    self.assertEqual(float(away_pt_ss.get_result().getData()[3]["t2p_percentage"]), 37.25, "Error: El porcentaje de acierto de T2P no es correcto")

If I run the test with this only method the test ... works fine!!!

Then, If I uncomment the rest of the code of the other methods and run the test this ... doesn't work!!!

Error
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/home/josecarlos/Workspace/python/reports/test/data_test.py", line 10, in test_season
    season = Data.get_id_season(params["league"])
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 27, in get_id_season
    db = SearchData(args, "season=")
  File "/home/josecarlos/Workspace/python/reports/ddbb/feb/data/search.py", line 18, in __init__
    q = query.getQuery(query_name)
  File "/home/josecarlos/Workspace/python/reports/com/files/queryreader.py", line 14, in getQuery
    with open(self.getURL()) as file:
FileNotFoundError: [Errno 2] No such file or directory: './files/querys.sql'


Error
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/home/josecarlos/Workspace/python/reports/test/data_test.py", line 32, in test_team_advanced_stats
    data = Data(params)
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 10, in __init__
    self.home_standard_stats = self.team_standard_stats(params["home"], params["league"])
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 53, in team_standard_stats
    id_season = self.get_id_season(id_league)
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 27, in get_id_season
    db = SearchData(args, "season=")
  File "/home/josecarlos/Workspace/python/reports/ddbb/feb/data/search.py", line 18, in __init__
    q = query.getQuery(query_name)
  File "/home/josecarlos/Workspace/python/reports/com/files/queryreader.py", line 14, in getQuery
    with open(self.getURL()) as file:
FileNotFoundError: [Errno 2] No such file or directory: './files/querys.sql'


Error
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/home/josecarlos/Workspace/python/reports/test/data_test.py", line 15, in test_team_standard_stats
    data = Data(params)
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 10, in __init__
    self.home_standard_stats = self.team_standard_stats(params["home"], params["league"])
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 53, in team_standard_stats
    id_season = self.get_id_season(id_league)
  File "/home/josecarlos/Workspace/python/reports/com/data.py", line 27, in get_id_season
    db = SearchData(args, "season=")
  File "/home/josecarlos/Workspace/python/reports/ddbb/feb/data/search.py", line 18, in __init__
    q = query.getQuery(query_name)
  File "/home/josecarlos/Workspace/python/reports/com/files/queryreader.py", line 14, in getQuery
    with open(self.getURL()) as file:
FileNotFoundError: [Errno 2] No such file or directory: './files/querys.sql'

Really, I don't understand anything, it's a madness!!!

José Carlos
  • 2,850
  • 12
  • 59
  • 95

1 Answers1

0

please make sure that working directory in your IDE is correct

Usually when you run the code in IDE, it opens the terminal in your default directory. IDE in its terminal gives the absolute path to run your code. To Solve this:

  1. Either you can try to change the current working directory of your IDE or
  2. You can use the absolute path in your code when you reference other files.
Satyam Gaba
  • 23
  • 2
  • 8