Skip to main content
added 838 characters in body
Source Link
import itertools
import os
import re
from typing import List

class Command:
    """Base command class which all commands inherit from.

    Command class implements all common functions and utilities
    between commands, like file and directories handling, and specific
    commands like arbitrary, text, or picture commands inherit it.
    """

    def __init__(self, args):
        """Instantiate an object with the files/directory to work on."""
        self.dir_path = args.dir_path
        self._extract_files(args)
        self._files_paths = None

    def _extract_files(self, args):
        """
        Returns the files specified by the user, if any.
        Throws exception if no file matches user input.
        """
        self.files_list = None
        if args.name is not None:
            self.files_list = self._file_from_name(args.name)
        elif args.names_file is not None:
            self.files_list = self._names_from_file(args.names_file)
        elif args.rgx is not None:
            self.files_list = self._files_from_rgx(args.rgx)
        elif args.rgx_file is not None:
            self.files_list = self._rgx_from_file(args.rgx_file)
        else:
            self.files_list = self._extract_all_files()
        
        if len(self.files_list) == 0:
            raise FileNotFoundError("No files match")
    
    def _files_from_rgx(self, rgx):
        """
        Looks for files in the directory `self.dir_path` with names 
        matched to the provided regex and returns those files names.
        """
        f = []
        for (dir_path, dirnames, filenames) in os.walk(self.dir_path):
            f.extend(filenames)
            break
        prog = re.compile(rgx)
        m = []
        for file_name in f:
            matched = prog.match(file_name)
            # matched.span() => (match start index, match end index)
            # Check that the file name is matched as a whole
            if matched and matched.span()[1] == len(file_name):
                m.append(file_name)
        return m

import collections
import os
import pytest

from fipy.parser import parser
from fipy.commands.command import Command

class TestCommand:
    """Tests for the command class."""

    def _populate_tmp_dir(self, tmpdir, dirs, files):
        dirs = [tmpdir.mkdir(x) for x in dirs]
        files = [tmpdir.join(x).write('') for x in files]
    
    def test_files_from_rgx_no_match(self, tmpdir):
        dirs = ['abc', '1abc', 'x1.txt']
        files = ['x.txt', 'x.tsf', '1abc.png']
        self._populate_tmp_dir(tmpdir, dirs, files)
        args = parser.parse_args(
            ['-p', str(tmpdir), '-r', 's[0-9]*.txt',
            'any', 'rename', '-asc']
        )
        with pytest.raises(FileNotFoundError):
            command = Command(args)

And if I want to write unit tests, should I test write a test for the function and another for the constructor, or write tests for the constructor, which implicitly will enter the function which I have done in the test above?

import itertools
import os
import re
from typing import List

class Command:
    """Base command class which all commands inherit from.

    Command class implements all common functions and utilities
    between commands, like file and directories handling, and specific
    commands like arbitrary, text, or picture commands inherit it.
    """

    def __init__(self, args):
        """Instantiate an object with the files/directory to work on."""
        self.dir_path = args.dir_path
        self._extract_files(args)
        self._files_paths = None

    def _extract_files(self, args):
        """
        Returns the files specified by the user, if any.
        Throws exception if no file matches user input.
        """
        self.files_list = None
        if args.name is not None:
            self.files_list = self._file_from_name(args.name)
        elif args.names_file is not None:
            self.files_list = self._names_from_file(args.names_file)
        elif args.rgx is not None:
            self.files_list = self._files_from_rgx(args.rgx)
        elif args.rgx_file is not None:
            self.files_list = self._rgx_from_file(args.rgx_file)
        else:
            self.files_list = self._extract_all_files()
        
        if len(self.files_list) == 0:
            raise FileNotFoundError("No files match")
    
    def _files_from_rgx(self, rgx):
        """
        Looks for files in the directory `self.dir_path` with names 
        matched to the provided regex and returns those files names."""
        f = []
        for (dir_path, dirnames, filenames) in os.walk(self.dir_path):
            f.extend(filenames)
            break
        prog = re.compile(rgx)
        m = []
        for file_name in f:
            matched = prog.match(file_name)
            # matched.span() => (match start index, match end index)
            # Check that the file name is matched as a whole
            if matched and matched.span()[1] == len(file_name):
                m.append(file_name)
        return m

And if I want to write unit tests, should I test write a test for the function and another for the constructor, or write tests for the constructor, which implicitly will enter the function?

import itertools
import os
import re
from typing import List

class Command:
    """Base command class which all commands inherit from.

    Command class implements all common functions and utilities
    between commands, like file and directories handling, and specific
    commands like arbitrary, text, or picture commands inherit it.
    """

    def __init__(self, args):
        """Instantiate an object with the files/directory to work on."""
        self.dir_path = args.dir_path
        self._extract_files(args)
        self._files_paths = None

    def _extract_files(self, args):
        """
        Returns the files specified by the user, if any.
        Throws exception if no file matches user input.
        """
        self.files_list = None
        if args.name is not None:
            self.files_list = self._file_from_name(args.name)
        elif args.names_file is not None:
            self.files_list = self._names_from_file(args.names_file)
        elif args.rgx is not None:
            self.files_list = self._files_from_rgx(args.rgx)
        elif args.rgx_file is not None:
            self.files_list = self._rgx_from_file(args.rgx_file)
        else:
            self.files_list = self._extract_all_files()
        
        if len(self.files_list) == 0:
            raise FileNotFoundError("No files match")
    
    def _files_from_rgx(self, rgx):
        """
        Looks for files in the directory `self.dir_path` with names 
        matched to the provided regex and returns those files names.
        """
        f = []
        for (dir_path, dirnames, filenames) in os.walk(self.dir_path):
            f.extend(filenames)
            break
        prog = re.compile(rgx)
        m = []
        for file_name in f:
            matched = prog.match(file_name)
            # matched.span() => (match start index, match end index)
            # Check that the file name is matched as a whole
            if matched and matched.span()[1] == len(file_name):
                m.append(file_name)
        return m

import collections
import os
import pytest

from fipy.parser import parser
from fipy.commands.command import Command

class TestCommand:
    """Tests for the command class."""

    def _populate_tmp_dir(self, tmpdir, dirs, files):
        dirs = [tmpdir.mkdir(x) for x in dirs]
        files = [tmpdir.join(x).write('') for x in files]
    
    def test_files_from_rgx_no_match(self, tmpdir):
        dirs = ['abc', '1abc', 'x1.txt']
        files = ['x.txt', 'x.tsf', '1abc.png']
        self._populate_tmp_dir(tmpdir, dirs, files)
        args = parser.parse_args(
            ['-p', str(tmpdir), '-r', 's[0-9]*.txt',
            'any', 'rename', '-asc']
        )
        with pytest.raises(FileNotFoundError):
            command = Command(args)

And if I want to write unit tests, should I test write a test for the function and another for the constructor, or write tests for the constructor, which implicitly will enter the function which I have done in the test above?

added 2 characters in body; edited title
Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158

Writing unit tests for function s in the constructor Unit testing a commands class

First of all, is it a good practice to set 'self.dir_path'self.dir_path and make the function use it, or pass it to the function as a parameter? (_files_from_rgx_files_from_rgx function)

Writing unit tests for function s in the constructor

First of all, is it a good practice to set 'self.dir_path' and make the function use it, or pass it to the function as a parameter? (_files_from_rgx function)

Unit testing a commands class

First of all, is it a good practice to set self.dir_path and make the function use it, or pass it to the function as a parameter? (_files_from_rgx function)

added 1965 characters in body
Source Link
import itertools
import os
import re
from typing import List

class BaseCommand:
    """Base command class which all commands inherit from.

    Command class implements all common functions and utilities
    between commands, like file and directories handling, and specific
    commands like arbitrary, text, or picture commands inherit it.
    """

    def __init__(self, paramargs):
        """Instantiate an object with the files/directory to work on."""
        self.propdir_path = valargs.dir_path
        self._validate_extract_files(paramargs)
        self._files_paths = None

    def _validate_extract_files(self, paramargs):
    'Validation that uses  """
        Returns the files specified by the user, if any.
        Throws exception if no file matches user input.
        """
        self.propfiles_list = None
        if args.name is not None:
            self.files_list = self._file_from_name(args.name)
        elif args.names_file is not None:
            self.files_list = self._names_from_file(args.names_file)
        elif args.rgx is not None:
            self.files_list = self._files_from_rgx(args.rgx)
        elif args.rgx_file is not None:
            self.files_list = self._rgx_from_file(args.rgx_file)
        else:
            self.files_list = self._extract_all_files()
        
        if len(self.files_list) == 0:
            raise FileNotFoundError("No files match")
    
    def _files_from_rgx(self, rgx):
        """
        Looks for files in itsthe checks'directory `self.dir_path` with names 
        matched to the provided regex and returns those files names."""
        f = []
        for (dir_path, dirnames, filenames) in os.walk(self.dir_path):
            f.extend(filenames)
            break
        prog = re.compile(rgx)
        m = []
        for file_name in f:
            matched = prog.match(file_name)
            # matched.span() => (match start index, match end index)
            # Check that the file name is matched as a whole
            if matched and matched.span()[1] == len(file_name):
                m.append(file_name)
        return m

First of all, is it a good practice to set 'self.prop'dir_path' and make the function use it, or pass it to the function as a parameter? (_files_from_rgx function)

class Base:

def __init__(self, param):
    self.prop = val
    self._validate(param)

def _validate(self, param):
    'Validation that uses self.prop in its checks'

First of all, is it a good practice to set 'self.prop' and make the function use it, or pass it to the function?

import itertools
import os
import re
from typing import List

class Command:
    """Base command class which all commands inherit from.

    Command class implements all common functions and utilities
    between commands, like file and directories handling, and specific
    commands like arbitrary, text, or picture commands inherit it.
    """

    def __init__(self, args):
        """Instantiate an object with the files/directory to work on."""
        self.dir_path = args.dir_path
        self._extract_files(args)
        self._files_paths = None

    def _extract_files(self, args):
        """
        Returns the files specified by the user, if any.
        Throws exception if no file matches user input.
        """
        self.files_list = None
        if args.name is not None:
            self.files_list = self._file_from_name(args.name)
        elif args.names_file is not None:
            self.files_list = self._names_from_file(args.names_file)
        elif args.rgx is not None:
            self.files_list = self._files_from_rgx(args.rgx)
        elif args.rgx_file is not None:
            self.files_list = self._rgx_from_file(args.rgx_file)
        else:
            self.files_list = self._extract_all_files()
        
        if len(self.files_list) == 0:
            raise FileNotFoundError("No files match")
    
    def _files_from_rgx(self, rgx):
        """
        Looks for files in the directory `self.dir_path` with names 
        matched to the provided regex and returns those files names."""
        f = []
        for (dir_path, dirnames, filenames) in os.walk(self.dir_path):
            f.extend(filenames)
            break
        prog = re.compile(rgx)
        m = []
        for file_name in f:
            matched = prog.match(file_name)
            # matched.span() => (match start index, match end index)
            # Check that the file name is matched as a whole
            if matched and matched.span()[1] == len(file_name):
                m.append(file_name)
        return m

First of all, is it a good practice to set 'self.dir_path' and make the function use it, or pass it to the function as a parameter? (_files_from_rgx function)

Source Link
Loading