<commit_before>﻿namespace StaticQL.Test
open System
open Microsoft.VisualStudio.TestTools.UnitTesting
open StaticQL
open StaticQL.Mapping

[<TestClass>]
type TestRoundTrip() =
    let roundtrip (sql : string) =
        let userModel = UserModel.Load("../../user-model-1", ".")
        let parsed = CommandEffect.OfSQL(userModel.Model, "anonymous", sql)
        let indexer = { new IParameterIndexer with member __.ParameterIndex(_) = 0 }
        let fragments = userModel.Backend.ToCommandFragments(indexer, parsed.Statements)
        let str = CommandFragment.Stringize(fragments)
        Console.WriteLine(str)
        let parsedBack = CommandEffect.OfSQL(userModel.Model, "anonymous", str)
        let fragmentsBack = userModel.Backend.ToCommandFragments(indexer, parsedBack.Statements)
        let strBack = CommandFragment.Stringize(fragmentsBack)
        Console.WriteLine(String('-', 80))
        Console.WriteLine(strBack)
        Assert.AreEqual(str, strBack)

    [<TestMethod>]
    member __.TestSelect() =
        roundtrip """
            select * from Users u where u.Id = 1
        """
    [<TestMethod>]
    member __.TestFancySelect() =
        roundtrip """
            select g.*, u.*
            from Users u
            left join UserGroupMaps gm on gm.UserId = u.Id
            left join Groups g on g.Id = gm.GroupId
            where g.Name like '%grp%' escape '%'
        """<commit_msg>Add a few more round-trip tests.<commit_after>﻿namespace StaticQL.Test
open System
open Microsoft.VisualStudio.TestTools.UnitTesting
open StaticQL
open StaticQL.Mapping

[<TestClass>]
type TestRoundTrip() =
    let roundtrip (sql : string) =
        let userModel = UserModel.Load("../../user-model-1", ".")
        let parsed = CommandEffect.OfSQL(userModel.Model, "anonymous", sql)
        let indexer = { new IParameterIndexer with member __.ParameterIndex(_) = 0 }
        let fragments = userModel.Backend.ToCommandFragments(indexer, parsed.Statements)
        let str = CommandFragment.Stringize(fragments)
        Console.WriteLine(str)
        let parsedBack = CommandEffect.OfSQL(userModel.Model, "anonymous", str)
        let fragmentsBack = userModel.Backend.ToCommandFragments(indexer, parsedBack.Statements)
        let strBack = CommandFragment.Stringize(fragmentsBack)
        Console.WriteLine(String('-', 80))
        Console.WriteLine(strBack)
        Assert.AreEqual(str, strBack)

    [<TestMethod>]
    member __.TestSelect() =
        roundtrip """
            select * from Users u where u.Id = 1
        """
    [<TestMethod>]
    member __.TestFancySelect() =
        roundtrip """
            select g.*, u.*
            from Users u
            left join UserGroupMaps gm on gm.UserId = u.Id
            left join Groups g on g.Id = gm.GroupId
            where g.Name like '%grp%' escape '%'
        """
    [<TestMethod>]
    member __.TestCreate() =
        roundtrip """
            create table Foo
                ( bar int primary key not null
                , baz float32
                , foreign key (bar, baz) references Users(Email, Name)
                );
        """

    [<TestMethod>]
    member __.TestAlterAddColumn() =
        roundtrip """
            alter table UserGroupMaps
                add Tag int not null
        """

    [<TestMethod>]
    member __.TestAlterRename() =
        roundtrip """
            alter table UserGroupMaps rename to UserGroupAssociations
        """

    [<TestMethod>]
    member __.TestCreateView() =
        roundtrip """
            create temp view CoolUsers as select * from Users where name not like '%szany%'
        """
{'fraction_non_alphanumeric': 0.1054083885209713, 'fraction_numerical': 0.0033112582781456954, 'mean_word_length': 2.316559926806953, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5875028', 'n_tokens_mistral': 1051, 'n_tokens_neox': 1006, 'n_words': 262}