<commit_before>import tagm
import sqlite3

db = None

def init_dummy_db():
    global db
    
    db = tagm.TagmDB( ':memory:' )

def test_add_tags():
    init_dummy_db()
    
    db.add( [ 'a', 'b', 'c' ], [ 'obj1', 'obj2' ] )
    db.add( [ 'd', 'e' ], [ 'obj1' ] )
    db.add( [ 'f', 'g' ], [ 'obj2' ] )
    
    # TODO: Test for add with find instead of objects, ie:
    # db.add( ['h'], find = [ 'a', 'c' ] )
    # should tag both obj 1 and 2 with h
    
    # TODO: Should it be passed as [ ['i','j'] ] instead..?
    #       Thus leave the delimiter up to downstream?
    db.add( [ [ 'i', 'j' ] ], [ 'obj1' ] )
    db.add( [ 'i' ], [ 'obj2' ] )

def test_get_objs_by_tags():
    test_add_tags()
    
    res = db.get( [ 'a', 'b' ] )
    assert res == [ 'obj1', 'obj2' ]
    
    res = db.get( [ 'a', 'd' ] )
    assert res == [ 'obj1' ]

    res = db.get( [ 'a', 'f' ] )
    assert res == [ 'obj2' ]

    res = db.get( [ 'd', 'f' ] )
    assert res == []
    
    res = db.get( [ 'a', 'i' ], subtags = True )
    assert res == [ 'obj1', 'obj2' ]
    
    res = db.get( [ 'a', [ 'i', 'j' ] ] )
    assert res == [ 'obj1' ]
    
    res = db.get( [] )
    assert res == [ 'obj1', 'obj2' ]
    
    # TODO: Should this raise an exception? Ie TagNotFoundError
    #       Requires TagmDB.get to not handle TagNotFoundError
    #       Question is, is that desired behavior?
    res = db.get( [ 'h' ] )
    assert res == []
    
def test_get_tags_by_tags():
    test_add_tags()
    
    res = db.get( [ 'a', 'b' ], True )
    assert res == [ [ 'c' ], [ 'd' ], [ 'e' ], [ 'i', 'j' ], [ 'f' ], [ 'g' ], [ 'i' ] ]
    
    res = db.get( ['a', 'b', 'd' ], True )
    assert res == [ [ 'c' ], [ 'e' ], [ 'i', 'j' ] ]

    res = db.get( ['a', 'b', 'f' ], True )
    assert res == [ [ 'c' ], [ 'g' ], [ 'i' ] ]

    res = db.get( ['a', 'b', 'e', 'f' ], True )
    assert res == []
    
    res = db.get( [ 'i' ], True )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ], [ 'f' ], [ 'g' ] ]

    res = db.get( [ 'i' ], True, True )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ], [ 'f' ], [ 'g' ], [ 'd' ], [ 'e' ], [ 'i', 'j' ] ]

def test_get_tags_by_objs():
    test_add_tags()
    
    res = db.get_obj_tags( [ 'obj1' ] )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ], [ 'd' ], [ 'e' ], [ 'i', 'j' ] ]

    res = db.get_obj_tags( [ 'obj2' ] )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ], [ 'f' ], [ 'g' ], [ 'i' ] ]

    res = db.get_obj_tags( [ 'obj1', 'obj2' ] )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ] ]
    
    res = db.get_obj_tags( [] )
    assert res == []

    # TODO: Test for non existing objs
    #       Ie:
    # res = db.get_obj_tags( [ 'obj3' ] )
    # assert res == []
    # -- or --
    # check that ObjNotFoundError got raised

def run_test( test_func ):
    print 'Running %s...' % test_func.__name__,
    
    test_func()
    
    print 'Passed!'

def run_all_tests():
    run_test( test_add_tags )
    
    run_test( test_get_objs_by_tags )
    
    run_test( test_get_tags_by_tags )
    
    run_test( test_get_tags_by_objs )

if __name__ == '__main__':
    run_all_tests()<commit_msg>Converted add tests to unittests<commit_after>#!/usr/bin/env python2
import tagm
import unittest

class TagmTestCase( unittest.TestCase ):
    def setUp( self ):
        self.db = tagm.TagmDB( ':memory:' )

class TestAdd( TagmTestCase ):
    def test_add_single_obj( self ):
        self.assertIsNone( self.db.add( [ 'a' ], [ 'obj1' ] ) )

    def test_add_multiple_obj( self ):
        self.assertIsNone( self.db.add( [ 'a' ], [ 'obj1', 'obj2' ] ) )
    
    def test_add_multiple_tags( self ):
        self.assertIsNone( self.db.add( [ 'a', 'b' ], [ 'obj1' ] ) )
    
    def test_add_multiple_both( self ):
        self.assertIsNone( self.db.add( [ 'a', 'b' ], [ 'obj1', 'obj2' ] ) )
    
    def test_add_subtag( self ):
        self.assertIsNone( self.db.add( [ [ 'a', 'b' ] ], [ 'obj1' ] ) )

def test_get_objs_by_tags():
    test_add_tags()
    
    res = db.get( [ 'a', 'b' ] )
    assert res == [ 'obj1', 'obj2' ]
    
    res = db.get( [ 'a', 'd' ] )
    assert res == [ 'obj1' ]

    res = db.get( [ 'a', 'f' ] )
    assert res == [ 'obj2' ]

    res = db.get( [ 'd', 'f' ] )
    assert res == []
    
    res = db.get( [ 'a', 'i' ], subtags = True )
    assert res == [ 'obj1', 'obj2' ]
    
    res = db.get( [ 'a', [ 'i', 'j' ] ] )
    assert res == [ 'obj1' ]
    
    res = db.get( [] )
    assert res == [ 'obj1', 'obj2' ]
    
    # TODO: Should this raise an exception? Ie TagNotFoundError
    #       Requires TagmDB.get to not handle TagNotFoundError
    #       Question is, is that desired behavior?
    res = db.get( [ 'h' ] )
    assert res == []
    
def test_get_tags_by_tags():
    test_add_tags()
    
    res = db.get( [ 'a', 'b' ], True )
    assert res == [ [ 'c' ], [ 'd' ], [ 'e' ], [ 'i', 'j' ], [ 'f' ], [ 'g' ], [ 'i' ] ]
    
    res = db.get( ['a', 'b', 'd' ], True )
    assert res == [ [ 'c' ], [ 'e' ], [ 'i', 'j' ] ]

    res = db.get( ['a', 'b', 'f' ], True )
    assert res == [ [ 'c' ], [ 'g' ], [ 'i' ] ]

    res = db.get( ['a', 'b', 'e', 'f' ], True )
    assert res == []
    
    res = db.get( [ 'i' ], True )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ], [ 'f' ], [ 'g' ] ]

    res = db.get( [ 'i' ], True, True )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ], [ 'f' ], [ 'g' ], [ 'd' ], [ 'e' ], [ 'i', 'j' ] ]

def test_get_tags_by_objs():
    test_add_tags()
    
    res = db.get_obj_tags( [ 'obj1' ] )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ], [ 'd' ], [ 'e' ], [ 'i', 'j' ] ]

    res = db.get_obj_tags( [ 'obj2' ] )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ], [ 'f' ], [ 'g' ], [ 'i' ] ]

    res = db.get_obj_tags( [ 'obj1', 'obj2' ] )
    assert res == [ [ 'a' ], [ 'b' ], [ 'c' ] ]
    
    res = db.get_obj_tags( [] )
    assert res == []

    # TODO: Test for non existing objs
    #       Ie:
    # res = db.get_obj_tags( [ 'obj3' ] )
    # assert res == []
    # -- or --
    # check that ObjNotFoundError got raised

def run_test( test_func ):
    print 'Running %s...' % test_func.__name__,
    
    test_func()
    
    print 'Passed!'

def run_all_tests():
    run_test( test_add_tags )
    
    run_test( test_get_objs_by_tags )
    
    run_test( test_get_tags_by_tags )
    
    run_test( test_get_tags_by_objs )

if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.22736576917020943, 'fraction_numerical': 0.007085498346717053, 'mean_word_length': 2.0450623202301053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '579587', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2634, 'n_words': 613}