# Path: yithlibraryserver/datetimeservice/testing.py
# class FakeDatetimeService(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     def utcnow(self):
#         fake = os.environ['YITH_FAKE_DATETIME']
#         parts = [int(p) for p in fake.split('-')]
#         return datetime.datetime(*parts)
# 
# Path: yithlibraryserver/db.py
# class MongoDB(object):
#     """Simple wrapper to get pymongo real objects from the settings uri"""
# 
#     def __init__(self, db_uri=DEFAULT_MONGODB_URI,
#                  connection_factory=pymongo.Connection):
#         self.db_uri = urlparse.urlparse(db_uri)
#         self.connection = connection_factory(
#             host=self.db_uri.hostname or DEFAULT_MONGODB_HOST,
#             port=self.db_uri.port or DEFAULT_MONGODB_PORT,
#             tz_aware=True)
# 
#         if self.db_uri.path:
#             self.database_name = self.db_uri.path[1:]
#         else:
#             self.database_name = DEFAULT_MONGODB_NAME
# 
#     def get_connection(self):
#         return self.connection
# 
#     def get_database(self):
#         database = self.connection[self.database_name]
#         if self.db_uri.username and self.db_uri.password:
#             database.authenticate(self.db_uri.username, self.db_uri.password)
# 
#         return database
# 
# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'
# 
# Path: yithlibraryserver/contributions/models.py
# def include_sticker(amount):
#     return amount > 1
# 
# Path: yithlibraryserver/contributions/models.py
# def create_donation(request, data):
#     amount = int(data['amount'])
#     donation = {
#         'amount': amount,
#         'firstname': data['firstname'],
#         'lastname': data['lastname'],
#         'city': data['city'],
#         'country': data['country'],
#         'state': data['state'],
#         'street': data['street'],
#         'zip': data['zip'],
#         'email': data['email'],
#         'creation': request.datetime_service.utcnow(),
#     }
#     if include_sticker(amount):
#         donation['send_sticker'] = not ('no-sticker' in data)
#     else:
#         donation['send_sticker'] = False
# 
#     if request.user is not None:
#         donation['user'] = request.user['_id']
#     else:
#         donation['user'] = None
# 
#     _id = request.db.donations.insert(donation, safe=True)
#     donation['_id'] = _id
#     return donation


import datetime
import os
import unittest
from pyramid import testing
from yithlibraryserver.datetimeservice.testing import FakeDatetimeService
from yithlibraryserver.db import MongoDB
from yithlibraryserver.testing import MONGO_URI
from yithlibraryserver.contributions.models import include_sticker
from yithlibraryserver.contributions.models import create_donation
        self.request.user = None

        donation = create_donation(self.request, {
                'amount': 5,
                'firstname': 'John',
                'lastname': 'Doe',
                'city': 'Springfield',
                'country': 'Exampleland',
                'state': 'Example',
                'street': 'Main Street 10',
                'zip': '12345678',
                'email': 'john@example.com',
                })

        self.assertEqual(donation['firstname'], 'John')
        self.assertEqual(donation['lastname'], 'Doe')
        self.assertEqual(donation['city'], 'Springfield')
        self.assertEqual(donation['country'], 'Exampleland')
        self.assertEqual(donation['state'], 'Example')
        self.assertEqual(donation['street'], 'Main Street 10')
        self.assertEqual(donation['zip'], '12345678')
        self.assertEqual(donation['email'], 'john@example.com')
        self.assertEqual(donation['creation'],
                         datetime.datetime(2013, 1, 2, 10, 11, 2))
        self.assertEqual(donation['send_sticker'], True)
        self.assertEqual(donation['user'], None)

    def test_create_donation_with_user(self):
        self.request.user = {'_id': 'fake_user_id'}


# Path: yithlibraryserver/datetimeservice/testing.py
# class FakeDatetimeService(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     def utcnow(self):
#         fake = os.environ['YITH_FAKE_DATETIME']
#         parts = [int(p) for p in fake.split('-')]
#         return datetime.datetime(*parts)
# 
# Path: yithlibraryserver/db.py
# class MongoDB(object):
#     """Simple wrapper to get pymongo real objects from the settings uri"""
# 
#     def __init__(self, db_uri=DEFAULT_MONGODB_URI,
#                  connection_factory=pymongo.Connection):
#         self.db_uri = urlparse.urlparse(db_uri)
#         self.connection = connection_factory(
#             host=self.db_uri.hostname or DEFAULT_MONGODB_HOST,
#             port=self.db_uri.port or DEFAULT_MONGODB_PORT,
#             tz_aware=True)
# 
#         if self.db_uri.path:
#             self.database_name = self.db_uri.path[1:]
#         else:
#             self.database_name = DEFAULT_MONGODB_NAME
# 
#     def get_connection(self):
#         return self.connection
# 
#     def get_database(self):
#         database = self.connection[self.database_name]
#         if self.db_uri.username and self.db_uri.password:
#             database.authenticate(self.db_uri.username, self.db_uri.password)
# 
#         return database
# 
# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'
# 
# Path: yithlibraryserver/contributions/models.py
# def include_sticker(amount):
#     return amount > 1
# 
# Path: yithlibraryserver/contributions/models.py
# def create_donation(request, data):
#     amount = int(data['amount'])
#     donation = {
#         'amount': amount,
#         'firstname': data['firstname'],
#         'lastname': data['lastname'],
#         'city': data['city'],
#         'country': data['country'],
#         'state': data['state'],
#         'street': data['street'],
#         'zip': data['zip'],
#         'email': data['email'],
#         'creation': request.datetime_service.utcnow(),
#     }
#     if include_sticker(amount):
#         donation['send_sticker'] = not ('no-sticker' in data)
#     else:
#         donation['send_sticker'] = False
# 
#     if request.user is not None:
#         donation['user'] = request.user['_id']
#     else:
#         donation['user'] = None
# 
#     _id = request.db.donations.insert(donation, safe=True)
#     donation['_id'] = _id
#     return donation


# Path: yithlibraryserver/contributions/tests/test_models.py
import datetime
import os
import unittest
from pyramid import testing
from yithlibraryserver.datetimeservice.testing import FakeDatetimeService
from yithlibraryserver.db import MongoDB
from yithlibraryserver.testing import MONGO_URI
from yithlibraryserver.contributions.models import include_sticker
from yithlibraryserver.contributions.models import create_donation

        self.request.user = None

        donation = create_donation(self.request, {
                'amount': 5,
                'firstname': 'John',
                'lastname': 'Doe',
                'city': 'Springfield',
                'country': 'Exampleland',
                'state': 'Example',
                'street': 'Main Street 10',
                'zip': '12345678',
                'email': 'john@example.com',
                })

        self.assertEqual(donation['firstname'], 'John')
        self.assertEqual(donation['lastname'], 'Doe')
        self.assertEqual(donation['city'], 'Springfield')
        self.assertEqual(donation['country'], 'Exampleland')
        self.assertEqual(donation['state'], 'Example')
        self.assertEqual(donation['street'], 'Main Street 10')
        self.assertEqual(donation['zip'], '12345678')
        self.assertEqual(donation['email'], 'john@example.com')
        self.assertEqual(donation['creation'],
                         datetime.datetime(2013, 1, 2, 10, 11, 2))
        self.assertEqual(donation['send_sticker'], True)
        self.assertEqual(donation['user'], None)

    def test_create_donation_with_user(self):
        self.request.user = {'_id': 'fake_user_id'}


        donation = create_donation(self.request, {