Welcome! First of all, fork This Repository to your Github Account.
After that git clone it to your machine.
Create a git branch using the name pattern test/<your-github-username>
Do the developing on this branch.
Requires python >= 3.8
Create an virtual environment:
This tutorial snippets assumes you uses Linux. For installation on other OS check python documentation on venv.
python3 -m venv venv
Activate it:
source venv/bin/activate
Install pytest:
pip install pytest
That's it you are done! To run the tests run:
pytest
One of our clients had an series of documents that may depend on others. He wants us to everytime he deletes an document all children documents are deleted as well. You've been designated to do this task. For this example, we're going to call the document as an Node
.
We're going to implement an Node
class that will have an id
and parent
, and an NodeManager
that will have an List of instance of the Node
s. This Manager will have the remove
method that removes an single Node
from the nodes
member, and remove_cascade
method that will removes all Node
s that has it as an parent, and all the Node
s that have been removed, must remove all Node
s that has it as an parent, and so on.
The requirements for the test are below. As you code, make sure that you run pytest
to see your current progress. When all tests are passing, go ahead to the finishing part.
Inside nodes/node.py you must write the Node
class.
Here are the requirements for an Node
:
- The
Node
class must declare theid
andparent
members. They Must be type hinted asint
egers. - An node
parent
cannot be greater than it'sid
. If this condition is not satisfied you should raise anValueError
exception; - An node
parent
cannot be the same as it's ownid
. If this condition is not satisfied you should raise anValueError
exception; - An
Node
should be instanced giving 2 integers like:Node(2, 1)
; If this condition is not satisfied you should raise anValueError
exception; - For debugging purposes we'll like that the
Node
representation is equals to the way we instance it. Done in the__repr__ method
;
Inside nodes/manager.py you must write the NodeManager
class.
Here are the requirements for the NodeManager
:
- Each
NodeManager
must declare thenodes
member. Must be type hinted as anList
ofNode
. - It must receive an
List
ofNode
to be instanced, thenodes
member should receive thisList
. If this condition is not satisfied you should raise anValueError
exception; - This object must not mutate the received
List
; - We should be able to see the number of nodes in the
nodes
member through theNodeManager
instance. Done in the__len__ method
. - We should be able to get an
Node
object from thenodes
member through theNodeManager
instance like an list. Done in the__getitem__ method
- It must implement two methods
remove
andremove_cascade
. They both receives anNode
object to be removed from thenodes
member and should returnNone
. - The
remove
andremove_cascade
method should raise anValueError
if the receivedNode
does not exist on thenodes
member. - The
remove
method must not remove its childrens. - The
remove_cascade
method should remove allNode
from thenodes
member that have the parent equal to the receivedNode
, this action must be performed again on the granchildren of the removedNode
. Example:
Given the List
[Node
] below:
[
Node(1, 0),
Node(2, 1),
Node(3, 2),
Node(4, 2),
Node(5, 2),
Node(6, 5),
Node(7, 6),
Node(8, 7),
Node(9, 0),
]
If you call remove_cascade
on the manager instance for the Node(2, 1)
the nodes
member must look like below:
[
Node(1, 0),
Node(9, 0),
]
This is expected because when we excluded the Node
with id
2, the Node
s that has id
s 3, 4, 5 as parent
must be excluded, and the same is for the Node
with id
6, that has the Node
with 5 as parent
and so on..
After you finish, push your local branch to your github repository and create a pull request to our master
branch.