from datetime import datetime
import unittest
import sqlite3

import database


class Post(database.Model):
    __tablename__ = 'post'

    id = database.PrimaryKeyField()
    title = database.CharField(100)
    content = database.TextField()
    pub_date = database.DateField()

    author_id = database.ForeignKeyField('author')

    def __repr__(self):
        return '<Post %s>' % self.title


class Author(database.Model):
    id = database.PrimaryKeyField()
    name = database.CharField(100)

    posts = database.ForeignKeyReverseField(Post)

    def __repr__(self):
        return '<Author %s>' % self.name


def get_connection():
    pass


class BaseTests(unittest.TestCase):

    def test_add(self):
        post = Post(title="My first post.",
                    content="Test my first post", pub_date=datetime.now())
        database.db.add(post)
        database.db.commit()

    def test_get(self):
        p1 = Post.get(id=1)
        p2 = Post.get(id=1, content="hello")

    def test_select(self):
        p1 = Post.select('*').where(id=2).all()
        p2 = Post.select().where("id > 0").all()
        p3 = Post.select().first()

    def test_delete(self):
        p = Post.delete(id=1).commit()
        p = Post.delete('id > 0').commit()

    def test_update(self):
        p = Post.update(id=1).set(title="new title").commit()
        p = Post.update("id > 2").set(
            title="new title", content="new content").commit()

    def test_foreignkeyfields(self):
        posts = Author.select().first().posts.all()

if __name__ == '__main__':
    pass

@@ -72,16 +72,362 @@
 tabase%0A%0A
+class Post_Tag_Re(database.Model):%0A    %22%22%22%0A    Many to many relationship test.%0A    %22%22%22%0A    id = database.PrimaryKeyField()%0A    post_id = database.ForeignKeyField('post')%0A    tag_id = database.ForeignKeyField('tag')%0A%0A    def __repr__(self):%0A        return '%3CRelation table post_id = %25s, tag_id = %25s%3E' %25(%0A            post_id, tag_id%0A            )%0A%0A
 %0Aclass P
@@ -669,16 +669,91 @@
 author')
+%0A    tags = database.ManyToManyField(rel = 'post_tag_re', to_table = 'tag')
 %0A%0A    de
@@ -958,20 +958,22 @@
 seField(
-P
+'p
 ost
+'
 )%0A%0A    d
@@ -1033,16 +1033,256 @@
 f.name%0A%0A
+class Tag(database.Model):%0A    id = database.PrimaryKeyField()%0A    name = database.CharField(100)%0A%0A    posts = database.ManyToManyField(rel = 'post_tag_re', to_table = 'post')%0A%0A    def __repr__(self):%0A        return '%3CTag %25s%3E' %25 self.name%0A%0A
 %0Adef get
@@ -2136,32 +2136,32 @@
 eyfields(self):%0A
-
         posts = 
@@ -2197,16 +2197,744 @@
 .all()%0A%0A
+    def test_many_to_many(self):%0A        posts = Tag.select().first().posts.all()%0A        tags = Post.select().first().tags.all()%0A%0A    %0A    def test_mtom_append(self):%0A        p = Post.get(id = 1)%5B0%5D%0A        p.tags.append(Tag.select().first())%0A%0A    def test_mtom_remove(self):%0A        pass%0A%0A    def test_mtom_count(self):%0A        pass%0A%0A    def test_orderby(self):%0A        pass%0A%0A    def test_like(self):%0A        pass%0A%0A%0Aclass TestBasicFunction(unittest.TestCase):%0A%0A    def test_count(self):%0A        c1 = Post.select().count()%0A        c2 = Post.select().where(%22id %3E 3%22).count()%0A%0A    def test_max(self):%0A        pass%0A%0A    def test_min(self):%0A        pass%0A%0A    def test_avg(self):%0A        pass%0A%0A    def test_sum(self):%0A        pass%0A%0A
 if __nam

update test prototype.