Skip to content

Commit

Permalink
Added tests for file creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jan 31, 2015
1 parent 6ae91a7 commit f5423f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
3 changes: 0 additions & 3 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ def __init__(self, db):
"""
from os import path, stat

# TODO: Create test to make sure file is created if none exists. Test should verify that an empty {} is placed in the file.
# TODO: Test that when a file exists but is empty, a {} is added.

# Create the file if it does not exist or is empty
if not path.exists(db) or stat(db).st_size == 0:
new_file = open(db, "w+")
Expand Down
39 changes: 39 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,45 @@ def tearDown(self):

class Tests(BaseTestCase):

def test_create_none_exists(self):
"""
A file should be created if none exists
and an empty {} is placed in the file.
"""
from os import path
import os

database = Database("new.db")

data = ""
with open ("new.db", "r") as db:
data = db.read()

self.assertTrue(path.exists("new.db"))
self.assertTrue("{}" in data)

os.remove("new.db")

def test_empty_file_initialized(self):
"""
Test that when a file exists but is empty,
a {} is added.
"""
import os

# Create an new empty file
open("empty.db", 'a').close()

database = Database("empty.db")

data = ""
with open ("empty.db", "r") as db:
data = db.read()

self.assertTrue("{}" in data)

os.remove("empty.db")

def test_get_key(self):
key = self.database.data(key="url")

Expand Down

0 comments on commit f5423f6

Please sign in to comment.