Skip to content

Commit

Permalink
Phase2 - Working with files
Browse files Browse the repository at this point in the history
Create project structure
Add xml parser and convert to json functions
Create tests to check functionality
  • Loading branch information
Oleg Averkin committed Apr 10, 2019
1 parent 0f9ea3c commit bcbc968
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea/
.DS_Store
.pytest_cache
__pycache__
__pycache__
42 changes: 42 additions & 0 deletions file_samples/json/expected_test_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"PERSONS": {
"PERSON": [
{
"FIRST_NAME": "Ivan",
"LAST_NAME": "Ivanov",
"YEAR_OF_BIRTH": "1991",
"MONTH_OF_BIRTH": "Jan",
"DAY_OF_BIRTH": "33",
"COMPANY": "Huhhle",
"PROJECT": "ABC",
"ROLE": "TL",
"ROOM": "11",
"HOBBY": "Gamer"
},
{
"FIRST_NAME": "Ivan",
"LAST_NAME": "Ivanov",
"YEAR_OF_BIRTH": "YOUR_YYYY",
"MONTH_OF_BIRTH": "YOUR_Month",
"DAY_OF_BIRTH": "33",
"COMPANY": "YOUR_COMPANY",
"PROJECT": "ABC",
"ROLE": "TL",
"ROOM": "11",
"HOBBY": "Gamer"
},
{
"FIRST_NAME": "Ivan",
"LAST_NAME": "Ivanov",
"YEAR_OF_BIRTH": "2000",
"MONTH_OF_BIRTH": "May",
"DAY_OF_BIRTH": "33",
"COMPANY": "TEST_COMPANY",
"PROJECT": "ABC",
"ROLE": "TL",
"ROOM": "11",
"HOBBY": "Gamer"
}
]
}
}
38 changes: 38 additions & 0 deletions file_samples/xml/expected_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<PERSONS>
<PERSON>
<FIRST_NAME>Ivan</FIRST_NAME>
<LAST_NAME>Ivanov</LAST_NAME>
<YEAR_OF_BIRTH>1991</YEAR_OF_BIRTH>
<MONTH_OF_BIRTH>Jan</MONTH_OF_BIRTH>
<DAY_OF_BIRTH>33</DAY_OF_BIRTH>
<COMPANY>Huhhle</COMPANY>
<PROJECT>ABC</PROJECT>
<ROLE>TL</ROLE>
<ROOM>11</ROOM>
<HOBBY>Gamer</HOBBY>
</PERSON>
<PERSON>
<FIRST_NAME>Ivan</FIRST_NAME>
<LAST_NAME>Ivanov</LAST_NAME>
<YEAR_OF_BIRTH>YOUR_YYYY</YEAR_OF_BIRTH>
<MONTH_OF_BIRTH>YOUR_Month</MONTH_OF_BIRTH>
<DAY_OF_BIRTH>33</DAY_OF_BIRTH>
<COMPANY>YOUR_COMPANY</COMPANY>
<PROJECT>ABC</PROJECT>
<ROLE>TL</ROLE>
<ROOM>11</ROOM>
<HOBBY>Gamer</HOBBY>
</PERSON>
<PERSON>
<FIRST_NAME>Ivan</FIRST_NAME>
<LAST_NAME>Ivanov</LAST_NAME>
<YEAR_OF_BIRTH>2000</YEAR_OF_BIRTH>
<MONTH_OF_BIRTH>May</MONTH_OF_BIRTH>
<DAY_OF_BIRTH>33</DAY_OF_BIRTH>
<COMPANY>TEST_COMPANY</COMPANY>
<PROJECT>ABC</PROJECT>
<ROLE>TL</ROLE>
<ROOM>11</ROOM>
<HOBBY>Gamer</HOBBY>
</PERSON>
</PERSONS>
38 changes: 38 additions & 0 deletions file_samples/xml/test_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<PERSONS>
<PERSON>
<FIRST_NAME>Lector</FIRST_NAME>
<LAST_NAME>Pythonov</LAST_NAME>
<YEAR_OF_BIRTH>1991</YEAR_OF_BIRTH>
<MONTH_OF_BIRTH>Jan</MONTH_OF_BIRTH>
<DAY_OF_BIRTH>31</DAY_OF_BIRTH>
<COMPANY>Huhhle</COMPANY>
<PROJECT>Cleaning department</PROJECT>
<ROLE>Looker at window</ROLE>
<ROOM>123</ROOM>
<HOBBY>Adulation</HOBBY>
</PERSON>
<PERSON>
<FIRST_NAME>YOUR_FIRST_NAME</FIRST_NAME>
<LAST_NAME>YOUR_LAST_NAME</LAST_NAME>
<YEAR_OF_BIRTH>YOUR_YYYY</YEAR_OF_BIRTH>
<MONTH_OF_BIRTH>YOUR_Month</MONTH_OF_BIRTH>
<DAY_OF_BIRTH>YOUR_DD</DAY_OF_BIRTH>
<COMPANY>YOUR_COMPANY</COMPANY>
<PROJECT>YOUR_PROJECT</PROJECT>
<ROLE>YOUR_ROLE</ROLE>
<ROOM>YOUR_ROOM</ROOM>
<HOBBY>YOUR_HOBBY</HOBBY>
</PERSON>
<PERSON>
<FIRST_NAME>TEST_FIRST_NAME</FIRST_NAME>
<LAST_NAME>TEST_LAST_NAME</LAST_NAME>
<YEAR_OF_BIRTH>2000</YEAR_OF_BIRTH>
<MONTH_OF_BIRTH>May</MONTH_OF_BIRTH>
<DAY_OF_BIRTH>11</DAY_OF_BIRTH>
<COMPANY>TEST_COMPANY</COMPANY>
<PROJECT>TEST_PROJECT</PROJECT>
<ROLE>TEST_ROLE</ROLE>
<ROOM>TEST_ROOM</ROOM>
<HOBBY>TEST_HOBBY</HOBBY>
</PERSON>
</PERSONS>
13 changes: 11 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
appdirs==1.4.0
astroid==1.4.9
atomicwrites==1.3.0
attrs==19.1.0
click==6.7
coverage==4.3.4
coveralls==1.1
docopt==0.6.2
fibonacci==1.0.0
isort==4.2.5
lazy-object-proxy==1.2.2
pytest==4.4.0
lxml==4.3.3
mccabe==0.6.1
more-itertools==7.0.0
nose==1.3.7
packaging==16.8
pluggy==0.9.0
py==1.8.0
pyparsing==2.1.10
pytest==4.4.0
requests==2.13.0
six==1.10.0
wrapt==1.10.8
wrapt==1.10.8
xmltodict==0.12.0
pytest==4.4.0
File renamed without changes.
Empty file added src/files/__init__.py
Empty file.
57 changes: 57 additions & 0 deletions src/files/fileProcessing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from lxml import etree
import os
import json
import xmltodict


initial_xml_file = os.path.abspath("file_samples/xml/test_data.xml")
updated_xml_file = os.path.abspath("file_samples/xml/new_data.xml")
new_json = os.path.abspath("file_samples/json/updated_test_data.json")
# expected_json = os.path.abspath("file_samples/json/expected_test_data.json")
# expected_xml_file = os.path.abspath("file_samples/xml/expected_data.xml")

first_name = 'Ivan'
last_name = 'Ivanov'
project = 'ABC'
role = 'TL'
birthday = '33'
room = '11'
hobby = 'Gamer'


def parse_xml(file):
return etree.parse(file)


def change_values(file):
tree = parse_xml(file)
root = tree.getroot()
for elem in root.iter('FIRST_NAME'):
elem.text = first_name
for elem in root.iter('LAST_NAME'):
elem.text = last_name
for elem in root.iter('PROJECT'):
elem.text = project
for elem in root.iter('DAY_OF_BIRTH'):
elem.text = birthday
for elem in root.iter('ROLE'):
elem.text = role
for elem in root.iter('ROOM'):
elem.text = room
for elem in root.iter('HOBBY'):
elem.text = hobby
tree.write(updated_xml_file)


def convert_xml_to_json():
with open(updated_xml_file, 'r') as f:
xmlString = f.read()
jsonString = json.dumps(xmltodict.parse(xmlString), indent=4)

with open(new_json, 'w') as f:
f.write(jsonString)


change_values(initial_xml_file)
convert_xml_to_json()

Empty file added tests/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion test_fib.py → tests/test_fib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fibonacci import generateFibonacci
from src.fibonacci import generateFibonacci


def test_fibonacci():
Expand Down
74 changes: 74 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from src.files.fileProcessing import parse_xml, change_values, convert_xml_to_json
import filecmp
import os
import json


initial_xml_file = os.path.abspath("file_samples/xml/test_data.xml")
updated_xml_file = os.path.abspath("file_samples/xml/new_data.xml")
new_json = os.path.abspath("file_samples/json/updated_test_data.json")
expected_json = os.path.abspath("file_samples/json/expected_test_data.json")
expected_xml_file = os.path.abspath("file_samples/xml/expected_data.xml")


class TestParser:

def test_initial_xml_file_exists(self):
assert os.path.isfile(initial_xml_file)

def test_parse_xml(self):
assert parse_xml(initial_xml_file)
os.remove(updated_xml_file)

def test_updated_xml_file_exists(self):
change_values(initial_xml_file)
assert os.path.isfile(updated_xml_file)
os.remove(updated_xml_file)

def test_compare_updated_and_expected_xml(self):
change_values(initial_xml_file)
assert filecmp.cmp(updated_xml_file, expected_xml_file) == True
os.remove(updated_xml_file)

def test_negative_compare_updated_and_expected_xml(self):
change_values(initial_xml_file)
assert filecmp.cmp(updated_xml_file, initial_xml_file) == False
os.remove(updated_xml_file)

def test_check_updated_first_name_field_xml(self):
change_values(initial_xml_file)
tree = parse_xml(updated_xml_file)
root = tree.getroot()
for elem in root.iter('FIRST_NAME'):
assert elem.text == 'Ivan'
os.remove(updated_xml_file)

def test_check_updated_project_field_xml(self):
change_values(initial_xml_file)
tree = parse_xml(updated_xml_file)
root = tree.getroot()
for elem in root.iter('PROJECT'):
assert elem.text == 'ABC'
os.remove(updated_xml_file)

def test_new_json_file_exists(self):
change_values(initial_xml_file)
convert_xml_to_json()
assert os.path.isfile(new_json)

def test_compare_new_and_expected_json(self):
change_values(initial_xml_file)
convert_xml_to_json()
assert os.path.isfile(new_json)
assert filecmp.cmp(new_json, expected_json) == True

def test_check_updated_first_name_field_json(self):
change_values(initial_xml_file)
convert_xml_to_json()
with open(new_json) as f:
data = json.load(f)
assert data["PERSONS"]["PERSON"][0]["FIRST_NAME"] == "Ivan"




0 comments on commit bcbc968

Please sign in to comment.