I am trying to learn best practices for OOP and unit testing. I have implemented a simple User class here which checks if the user exists in the database. If yes, then it loads the user data into the instance. If not, then it should create a new entry in the database.
- How should I create unit tests for the methods? For example, for
check_user_exists()should I mock for
existsor three of them? I tried to research for this but none of the tutorials for mocking in Python really gave me any true understanding of how good mocking is done.
- In each of the methods, all the database connection related codes are repeated (i.e.
conn.close()). Can these be refactored away? My guess would be to create a
Databaseclass, but I’m not sure about that.
- Is it weird for
Userclass to have a
database_pathattribute? So far this is the best that I have come up with. Please give any suggestions on how to structure a class where it has to always query databases.
import sqlite3 class User: def __init__(self, username, database_path): self.username = username self.database_path = database_path if not self.check_user_exists(): self.create_user() else: self.get_user() def check_user_exists(self): conn = sqlite3.connect(self.database_path) cursor = conn.cursor() cursor.execute('SELECT rowid FROM user WHERE name = ?', (self.username,)) exists = cursor.fetchone() conn.close() if exists is None: return False else: return True def create_user(self): conn = sqlite3.connect(self.database_path) cursor = conn.cursor() cursor.execute("INSERT INTO user (name) VALUES (?)", (self.username,)) conn.commit() conn.close() def get_user(self): conn = sqlite3.connect(self.database_path) cursor = conn.cursor() cursor.execute("SELECT data FROM user WHERE name=?", (self.username,)) self.data = cursor.fetchone() conn.close()
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!