<commit_before>import unittest
import os
from models import *


class TestPublisherModel(unittest.TestCase):

    def setUp(self):
        db.connect()

    def tearDown(self):
        q = Publisher.delete()
        q.execute()
        db.close()

    # add_publisher()
    def test_add_publisher_returns_publisher(self):
        new_publisher = Publisher.add_publisher(
            name="Books from Holland", city="Amsterdam")

        self.assertTrue(isinstance(new_publisher, Publisher))

    def test_add_publisher_stored_in_db(self):
        publisher_name = "Written by U"
        city = "Amsterdam"
        Publisher.add_publisher(name=publisher_name, city=city)
        publisher_in_db = Publisher.get(
            Publisher.name == publisher_name)
        self.assertEqual(publisher_name, publisher_in_db.name)

    def test_add_publisher_name_length(self):
        publisher_name = "a" * 266
        city = "Amsterdam"
        publisher = Publisher.add_publisher(name=publisher_name,
                                            city=city)
        # publisher should be None
        self.assertFalse(publisher)

    def test_add_publisher_city_length(self):
        publisher_name = "British Books"
        city = "a" * 266
        publisher = Publisher.add_publisher(name=publisher_name,
                                            city=city)
        self.assertFalse(publisher)

    # select_all()
    def test_select_all_publishers(self):
        Publisher.create(name="The German Writters",
                         city="Berlin")
        publishers = Publisher.select_all()
        self.assertTrue(any(publisher.name == "The German Writters"
                        for publisher in publishers))

    def test_select_all_publishers_no_entries(self):
        publishers = Publisher.select_all()
        self.assertTrue(publishers is None)

    # update_selected()
    def test_update_publisher(self):
        publisher = Publisher.create(name="test", city="city")
        Publisher.update_selected(publisher, "new_test", "new_city")
        new_publisher = Publisher.get(Publisher.name == "new_test")
        self.assertEqual(new_publisher.city, "new_city")
        self.assertEqual(new_publisher.name, "new_test")

    def test_update_only_name(self):
        publisher = Publisher.create(name="name", city="city")
        Publisher.update_selected(publisher, "new_name")
        new_publisher = Publisher.get(Publisher.name == "new_name")
        self.assertEqual(new_publisher.name, "new_name")
        self.assertEqual(new_publisher.city, "city")

    def test_update_only_city(self):
        publisher = Publisher.create(name="name", city="city")
        Publisher.update_selected(publisher, city="new_city")
        new_publisher = Publisher.get(Publisher.name == "name")
        self.assertEqual(new_publisher.name, "name")
        self.assertEqual(new_publisher.city, "new_city")

    def test_update_string_length(self):
        publisher = Publisher.create(name="name", city="city")
        Publisher.update_selected(publisher, "a" * 266, "a" * 266)
        new_publisher = Publisher.get(Publisher.name == "name",
                                      Publisher.city == "city")
        self.assertEqual(new_publisher.name, "name")
        self.assertEqual(new_publisher.city, "city")

    def test_update_not_existing(self):
        publisher_updated = Publisher.update_selected(666, "new_name")
        self.assertFalse(publisher_updated)

    # delete_selected()
    def test_delete_existing_publisher(self):
        publisher = Publisher.create(name="name", city="city")
        self.assertTrue(Publisher.delete_selected(publisher.id))

    def test_delete_non_existing_publisher(self):
        # publisher = Publisher.create(name="name", city="city")
        self.assertFalse(Publisher.delete_selected(666))


if __name__ == '__main__':
    db.init(host=os.getenv('DB_HOST', 'localhost'),
            user='unittest',
            password='<PASSWORD>',
            database='test_db',
            charset='utf8')
    unittest.main()<commit_msg>Add unittest for Author model<commit_after>import unittest
import os
from models import *


class TestPublisherModel(unittest.TestCase):

    def setUp(self):
        db.connect()

    def tearDown(self):
        q = Publisher.delete()
        q.execute()
        db.close()

    # add_publisher()
    def test_add_publisher_returns_publisher(self):
        new_publisher = Publisher.add_publisher(
            name="Books from Holland", city="Amsterdam")

        self.assertTrue(isinstance(new_publisher, Publisher))

    def test_add_publisher_stored_in_db(self):
        publisher_name = "Written by U"
        city = "Amsterdam"
        Publisher.add_publisher(name=publisher_name, city=city)
        publisher_in_db = Publisher.get(
            Publisher.name == publisher_name)
        self.assertEqual(publisher_name, publisher_in_db.name)

    def test_add_publisher_name_length(self):
        publisher_name = "a" * 266
        city = "Amsterdam"
        publisher = Publisher.add_publisher(name=publisher_name,
                                            city=city)
        # publisher should be None
        self.assertFalse(publisher)

    def test_add_publisher_city_length(self):
        publisher_name = "British Books"
        city = "a" * 266
        publisher = Publisher.add_publisher(name=publisher_name,
                                            city=city)
        self.assertFalse(publisher)

    # select_all()
    def test_select_all_publishers(self):
        Publisher.create(name="The German Writters",
                         city="Berlin")
        publishers = Publisher.select_all()
        self.assertTrue(any(publisher.name == "The German Writters"
                        for publisher in publishers))

    def test_select_all_publishers_no_entries(self):
        publishers = Publisher.select_all()
        self.assertTrue(publishers is None)

    # update_selected()
    def test_update_publisher(self):
        publisher = Publisher.create(name="test", city="city")
        Publisher.update_selected(publisher, "new_test", "new_city")
        new_publisher = Publisher.get(Publisher.name == "new_test")
        self.assertEqual(new_publisher.city, "new_city")
        self.assertEqual(new_publisher.name, "new_test")

    def test_update_only_name(self):
        publisher = Publisher.create(name="name", city="city")
        Publisher.update_selected(publisher, "new_name")
        new_publisher = Publisher.get(Publisher.name == "new_name")
        self.assertEqual(new_publisher.name, "new_name")
        self.assertEqual(new_publisher.city, "city")

    def test_update_only_city(self):
        publisher = Publisher.create(name="name", city="city")
        Publisher.update_selected(publisher, city="new_city")
        new_publisher = Publisher.get(Publisher.name == "name")
        self.assertEqual(new_publisher.name, "name")
        self.assertEqual(new_publisher.city, "new_city")

    def test_update_string_length(self):
        publisher = Publisher.create(name="name", city="city")
        Publisher.update_selected(publisher, "a" * 266, "a" * 266)
        new_publisher = Publisher.get(Publisher.name == "name",
                                      Publisher.city == "city")
        self.assertEqual(new_publisher.name, "name")
        self.assertEqual(new_publisher.city, "city")

    def test_update_not_existing(self):
        publisher_updated = Publisher.update_selected(666, "new_name")
        self.assertFalse(publisher_updated)

    # delete_selected()
    def test_delete_existing_publisher(self):
        publisher = Publisher.create(name="name", city="city")
        self.assertTrue(Publisher.delete_selected(publisher.id))

    def test_delete_non_existing_publisher(self):
        # publisher = Publisher.create(name="name", city="city")
        self.assertFalse(Publisher.delete_selected(666))

class TestAuthorModel(unittest.TestCase):

    def setUp(self):
        db.connect()

    def tearDown(self):
        q = Author.delete()
        q.execute()
        db.close


if __name__ == '__main__':
    db.init(host=os.getenv('DB_HOST', 'localhost'),
            user='unittest',
            password='<PASSWORD>',
            database='test_db',
            charset='utf8')
    unittest.main()
{'fraction_non_alphanumeric': 0.10907127429805616, 'fraction_numerical': 0.004559635229181665, 'mean_word_length': 2.828663298116674, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2792304', 'n_tokens_mistral': 2503, 'n_tokens_neox': 2404, 'n_words': 390}