diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 0000000..5f78e07 --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,20 @@ +name: Tests +on: push + +jobs: + unit-tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Setup python + uses: actions/setup-python@v2 + with: + python-version: 3.11.3 + + - name: Install tools + run: python -m pip install --upgrade pip pytest sqlalchemy pandas python-dotenv + + - name: Test with unittest + working-directory: . + run: python3 -m unittest test_parse.py \ No newline at end of file diff --git a/combined.py b/combined.py index 0504607..43f1175 100644 --- a/combined.py +++ b/combined.py @@ -30,7 +30,7 @@ data_of_trip = response.json() parser = Parser(data_of_trip) -parser.write_to_database("cate") +parser.write_to_database(cate) # Pull stored results db_results = parser.pull_list(cate, cy) diff --git a/json_parsing.py b/json_parsing.py index 21663da..c907ee3 100644 --- a/json_parsing.py +++ b/json_parsing.py @@ -1,8 +1,6 @@ -import json import sqlalchemy as db import pandas as pd import os -import requests from dotenv import load_dotenv @@ -31,6 +29,7 @@ def write_to_database(self, tb_name): GROUP BY Name );""" connection.execute(db.text(remove_dupes)) + connection.commit() def pull_list(self, table_name, city): query = (f"SELECT * FROM {table_name} " diff --git a/test_JSON/sample2.txt b/test_JSON/sample2.txt new file mode 100644 index 0000000..9ca6362 --- /dev/null +++ b/test_JSON/sample2.txt @@ -0,0 +1,129 @@ +{ + "data": [ + { + "location_id": "13483637", + "name": "Bulla Gastrobar Plano", + "address_obj": { + "street1": "6007 Legacy Dr", + "street2": "Suite 180", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75024", + "address_string": "6007 Legacy Dr Suite 180, Plano, TX 75024" + } + }, + { + "location_id": "3186629", + "name": "Half Shells - Plano", + "address_obj": { + "street1": "7201 Bishop Rd.", + "street2": "Suite E-4", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75024-3641", + "address_string": "7201 Bishop Rd. Suite E-4, Plano, TX 75024-3641" + } + }, + { + "location_id": "10793449", + "name": "Sixty Vines - Plano", + "address_obj": { + "street1": "3701 Dallas Pkwy", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75093-7777", + "address_string": "3701 Dallas Pkwy, Plano, TX 75093-7777" + } + }, + { + "location_id": "464998", + "name": "Bob's Steak & Chop House - Plano", + "address_obj": { + "street1": "5760 Legacy Dr Suite B-1", + "street2": "Ste B1", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75024-7102", + "address_string": "5760 Legacy Dr Suite B-1 Ste B1, Plano, TX 75024-7102" + } + }, + { + "location_id": "24112709", + "name": "Ford's Garage Plano", + "address_obj": { + "street1": "3904 North Dallas Pkwy", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75093", + "address_string": "3904 North Dallas Pkwy, Plano, TX 75093" + } + }, + { + "location_id": "1580730", + "name": "Urban Crust", + "address_obj": { + "street1": "1006 E 15th Street", + "street2": "Old Downtown Plano", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75074-6222", + "address_string": "1006 E 15th Street Old Downtown Plano, Plano, TX 75074-6222" + } + }, + { + "location_id": "18454855", + "name": "Plano Restaurante", + "address_obj": { + "street1": "Rua da Bela Vista a Graca 126", + "city": "Lisbon", + "country": "Portugal", + "postalcode": "1170-059", + "address_string": "Rua da Bela Vista a Graca 126, Lisbon 1170-059 Portugal" + } + }, + { + "location_id": "15321599", + "name": "Knife Steakhouse Plano", + "address_obj": { + "street1": "6121 W Park Blvd", + "street2": "The District at Willow Bend", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75093", + "address_string": "6121 W Park Blvd The District at Willow Bend, Plano, TX 75093" + } + }, + { + "location_id": "2259652", + "name": "Whiskey Cake Kitchen & Bar", + "address_obj": { + "street1": "3601 Dallas Parkway", + "street2": "", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75093", + "address_string": "3601 Dallas Parkway, Plano, TX 75093" + } + }, + { + "location_id": "8097563", + "name": "Kona Grill - Plano", + "address_obj": { + "street1": "5973 W. Parker Rd", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75093", + "address_string": "5973 W. Parker Rd, Plano, TX 75093" + } + } + ] +} \ No newline at end of file diff --git a/test_JSON/sampleJSON.txt b/test_JSON/sampleJSON.txt new file mode 100644 index 0000000..9a5fe19 --- /dev/null +++ b/test_JSON/sampleJSON.txt @@ -0,0 +1,123 @@ +{ + "data": [ + { + "location_id": "20117197", + "name": "Self-Guided Scavenger Hunt: The Best of Plano, Texas", + "address_obj": { + "city": "Plano", + "state": "Texas", + "country": "United States", + "address_string": "Plano, TX" + } + }, + { + "location_id": "560646", + "name": "Historic Downtown Plano", + "address_obj": { + "street1": "998 E. 15th Street", + "street2": "", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75074-5808", + "address_string": "998 E. 15th Street, Plano, TX 75074-5808" + } + }, + { + "location_id": "56463", + "name": "Plano", + "address_obj": { + "state": "Texas", + "country": "United States", + "address_string": "Plano, TX" + } + }, + { + "location_id": "15087565", + "name": "Sandman Signature Plano-Frisco Hotel", + "address_obj": { + "street1": "8451 Parkwood Boulevard", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75024-5983", + "address_string": "8451 Parkwood Boulevard, Plano, TX 75024-5983" + } + }, + { + "location_id": "6468516", + "name": "Hilton Dallas/plano Granite Park", + "address_obj": { + "street1": "5805 Granite Parkway", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75024", + "address_string": "5805 Granite Parkway, Plano, TX 75024" + } + }, + { + "location_id": "5951655", + "name": "Cambria Hotel Plano Frisco", + "address_obj": { + "street1": "7500 Parkwood Blvd", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75024-5964", + "address_string": "7500 Parkwood Blvd, Plano, TX 75024-5964" + } + }, + { + "location_id": "1097444", + "name": "The Interurban Railway Museum", + "address_obj": { + "street1": "901 E 15th St", + "street2": "", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75074-5807", + "address_string": "901 E 15th St, Plano, TX 75074-5807" + } + }, + { + "location_id": "10793449", + "name": "Sixty Vines - Plano", + "address_obj": { + "street1": "3701 Dallas Pkwy", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75093-7777", + "address_string": "3701 Dallas Pkwy, Plano, TX 75093-7777" + } + }, + { + "location_id": "465130", + "name": "Love & War In Texas", + "address_obj": { + "street1": "601 E Plano Pkwy", + "street2": "", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75074-6733", + "address_string": "601 E Plano Pkwy, Plano, TX 75074-6733" + } + }, + { + "location_id": "560644", + "name": "Arbor Hills Nature Preserve", + "address_obj": { + "street1": "6701 West Parker Road", + "street2": "", + "city": "Plano", + "state": "Texas", + "country": "United States", + "postalcode": "75093", + "address_string": "6701 West Parker Road, Plano, TX 75093" + } + } + ] +} \ No newline at end of file diff --git a/test_parse.py b/test_parse.py new file mode 100644 index 0000000..d4ba2fb --- /dev/null +++ b/test_parse.py @@ -0,0 +1,38 @@ +import unittest +import sqlalchemy as db +import json +from json_parsing import Parser + + +class TestParse(unittest.TestCase): + + def setUp(self): + data = '' + with open('test_JSON/sampleJSON.txt', 'r') as file: + data = file.read() + self.parser = Parser(json.loads(data)) + self.engine = db.create_engine('sqlite:///tripadv.db') + self.parser.write_to_database("testing") + + def test_write_to_database(self): + # Run to make sure it only keeps unique values + self.parser.write_to_database("testing") + with self.engine.connect() as connection: + result = connection.\ + execute(db.text("SELECT COUNT(*) FROM testing;")).fetchall() + value = result[0][0] + self.assertEqual(value, 10) + + def test_pull_list(self): + db_list = self.parser.pull_list("testing", "Plano") + self.assertEqual(len(db_list), 9) + for entry in db_list: + self.assertIsInstance(entry, db.Row) + + def test_drop(self): + self.assertIn("testing", db.inspect(self.engine).get_table_names()) + query = "DROP TABLE IF EXISTS testing;" + with self.engine.connect() as connection: + connection.execute(db.text(query)) + connection.commit() + self.assertNotIn("testing", db.inspect(self.engine).get_table_names())