<commit_before># -*- coding: utf-8 -*-

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
import unittest
from sacrud.tests.test_models import User, Profile, PHOTO_PATH
from sacrud.action import get_relations, delete_fileobj, read, update, delete
from sacrud.action import get_pk, index, create
from pyramid.testing import DummyRequest
from StringIO import StringIO
import glob
import os


class MockCGIFieldStorage(object):
    pass


class SacrudTests(unittest.TestCase):

    def setUp(self):
        engine = create_engine('sqlite:///:memory:')
        Session = sessionmaker(bind=engine)
        session = Session()
        # You probably need to create some tables and 
        # load some test data, do so here.
        self.session = session
        # To create tables, you typically do:
        #User.metadata.create_all(engine)
        User.metadata.create_all(engine)
        Profile.metadata.create_all(engine)

    def tearDown(self):
        def clear_files():
            for filename in glob.glob("%s/*.html" % (PHOTO_PATH, )):
                os.remove(os.path.join(PHOTO_PATH, filename))
        clear_files()

    def test_relations(self):
        user = User(u'Vasya', u'Pupkin', u"123")

        self.session.add(user)
        self.session.commit()
        profile = Profile(user=user)

        self.session.add(profile)
        self.session.commit()

        profile = self.session.query(Profile).get(1)
        self.assertEqual(get_relations(user), [('profile',
                                                [profile, ])])
        self.session.delete(profile)
        self.session.delete(user)

    def test_get_pk(self):
        pk = get_pk(User)
        self.assertEqual("id", pk)

    def test_index(self):
        user = User(u'Vasya', u'Pupkin', u"123")

        self.session.add(user)
        self.session.commit()

        result = index(self.session, User)
        self.assertEqual(result['pk'], 'id')
        self.assertEqual(result["prefix"], "crud")
        self.assertEqual(result["table"], User)
        self.assertEqual(result["row"], [user, ])
        self.session.delete(user)

    def test_create(self):

        request = DummyRequest()
        request['name'] = ["Vasya", ]
        request['fullname'] = ["<NAME>", ]
        request['password'] = ["<PASSWORD>", ]

        create(self.session, User, request)
        user = self.session.query(User).get(1)
        self.assertEqual(user.name, "Vasya")
        self.assertEqual(user.fullname, "<NAME>")
        self.assertEqual(user.password, "<PASSWORD>")

        request = DummyRequest()
        request['phone'] = ["213123123", ]
        request['cv'] = ["Vasya Pupkin was born in Moscow", ]
        request['married'] = ["true", ]
        request["salary"] = ["23.0", ]
        request["user_id"] = ["1", ]

        create(self.session, Profile, request)

        profile = self.session.query(Profile).get(1)
        self.assertEqual(profile.phone, "213123123")
        self.assertEqual(profile.cv, "Vasya Pupkin was born in Moscow")
        self.assertEqual(profile.married, True)
        self.assertEqual(profile.salary, float(23))
        self.assertEqual(profile.user.id, 1)

        self.session.delete(profile)
        self.session.delete(user)

    def test_read(self):
        user = User(u'Vasya', u'Pupkin', u"123")
        self.session.add(user)
        self.session.commit()

        result = read(self.session, User, 1)
        self.assertEqual(result['obj'].id, 1)
        self.assertEqual(result['pk'], "id")
        self.assertEqual(result['prefix'], "crud")
        self.assertEqual(result['table'], User)<commit_msg>delete file_obj test<commit_after># -*- coding: utf-8 -*-

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
import unittest
from sacrud.tests.test_models import User, Profile, PHOTO_PATH
from sacrud.action import get_relations, delete_fileobj, read, update, delete
from sacrud.action import get_pk, index, create
from pyramid.testing import DummyRequest
from StringIO import StringIO
import glob
import os


class MockCGIFieldStorage(object):
    pass


class SacrudTests(unittest.TestCase):

    def setUp(self):
        engine = create_engine('sqlite:///:memory:')
        Session = sessionmaker(bind=engine)
        session = Session()
        # You probably need to create some tables and 
        # load some test data, do so here.
        self.session = session
        # To create tables, you typically do:
        #User.metadata.create_all(engine)
        User.metadata.create_all(engine)
        Profile.metadata.create_all(engine)

    def tearDown(self):
        def clear_files():
            for filename in glob.glob("%s/*.html" % (PHOTO_PATH, )):
                os.remove(os.path.join(PHOTO_PATH, filename))
        clear_files()

    def test_relations(self):
        user = User(u'Vasya', u'Pupkin', u"123")

        self.session.add(user)
        self.session.commit()
        profile = Profile(user=user)

        self.session.add(profile)
        self.session.commit()

        profile = self.session.query(Profile).get(1)
        self.assertEqual(get_relations(user), [('profile',
                                                [profile, ])])
        self.session.delete(profile)
        self.session.delete(user)

    def test_get_pk(self):
        pk = get_pk(User)
        self.assertEqual("id", pk)

    def test_index(self):
        user = User(u'Vasya', u'Pupkin', u"123")

        self.session.add(user)
        self.session.commit()

        result = index(self.session, User)
        self.assertEqual(result['pk'], 'id')
        self.assertEqual(result["prefix"], "crud")
        self.assertEqual(result["table"], User)
        self.assertEqual(result["row"], [user, ])
        self.session.delete(user)

    def test_create(self):

        request = DummyRequest()
        request['name'] = ["Vasya", ]
        request['fullname'] = ["<NAME>", ]
        request['password'] = ["<PASSWORD>", ]

        create(self.session, User, request)
        user = self.session.query(User).get(1)
        self.assertEqual(user.name, "Vasya")
        self.assertEqual(user.fullname, "<NAME>")
        self.assertEqual(user.password, "<PASSWORD>")

        request = DummyRequest()
        request['phone'] = ["2<PASSWORD>", ]
        request['cv'] = ["Vasya Pupkin was born in Moscow", ]
        request['married'] = ["true", ]
        request["salary"] = ["23.0", ]
        request["user_id"] = ["1", ]

        upload = MockCGIFieldStorage()
        upload.file = StringIO('foo')
        upload.filename = 'foo.html'
        request["photo"] = [upload, ]

        create(self.session, Profile, request)

        profile = self.session.query(Profile).get(1)
        self.assertEqual(profile.phone, "213123123")
        self.assertEqual(profile.cv, "Vasya Pupkin was born in Moscow")
        self.assertEqual(profile.married, True)
        self.assertEqual(profile.salary, float(23))
        self.assertEqual(profile.user.id, 1)

        delete_fileobj(Profile, profile, "photo")
        self.session.delete(profile)
        self.session.delete(user)

    def test_read(self):
        user = User(u'Vasya', u'Pupkin', u"<PASSWORD>")
        self.session.add(user)
        self.session.commit()

        result = read(self.session, User, 1)
        self.assertEqual(result['obj'].id, 1)
        self.assertEqual(result['pk'], "id")
        self.assertEqual(result['prefix'], "crud")
        self.assertEqual(result['table'], User)
{'fraction_non_alphanumeric': 0.13802256138022562, 'fraction_numerical': 0.009157266091572662, 'mean_word_length': 2.614388489208633, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4087196', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2198, 'n_words': 454}