1+ using FlowSynx . Plugins . Sqlite ;
2+ using FlowSynx . Plugins . Sqlite . Models ;
3+ using FlowSynx . PluginCore ;
4+ using FlowSynx . Plugins . Sqlite . Services ;
5+ using Microsoft . Data . Sqlite ;
6+ using Moq ;
7+ using System . Reflection ;
8+
9+ public class SqlitePluginTests
10+ {
11+ private SqlitePlugin CreatePlugin ( string ? connectionString = null , Mock < IPluginLogger > ? loggerMock = null )
12+ {
13+ var guidProvider = new Mock < IGuidProvider > ( ) ;
14+ guidProvider . Setup ( g => g . NewGuid ( ) ) . Returns ( Guid . NewGuid ( ) ) ;
15+
16+ var reflectionGuard = new Mock < IReflectionGuard > ( ) ;
17+ reflectionGuard . Setup ( r => r . IsCalledViaReflection ( ) ) . Returns ( false ) ;
18+
19+ var plugin = new SqlitePlugin ( guidProvider . Object , reflectionGuard . Object )
20+ {
21+ Specifications = new SqlitePluginSpecifications
22+ {
23+ ConnectionString = connectionString ?? "Data Source=:memory:"
24+ }
25+ } ;
26+
27+ if ( loggerMock != null )
28+ plugin . Initialize ( loggerMock . Object ) . Wait ( ) ;
29+ else
30+ plugin . Initialize ( Mock . Of < IPluginLogger > ( ) ) . Wait ( ) ;
31+
32+ return plugin ;
33+ }
34+
35+ [ Fact ]
36+ public async Task Initialize_SetsIsInitialized ( )
37+ {
38+ var plugin = CreatePlugin ( ) ;
39+ Assert . True ( typeof ( SqlitePlugin ) . GetField ( "_isInitialized" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ! . GetValue ( plugin ) as bool ? ) ;
40+ }
41+
42+ [ Fact ]
43+ public async Task ExecuteAsync_ThrowsOnUnsupportedOperation ( )
44+ {
45+ var plugin = CreatePlugin ( ) ;
46+ var parameters = new PluginParameters
47+ {
48+ { "Operation" , "invalid" } ,
49+ { "Sql" , "SELECT 1" }
50+ } ;
51+
52+ await Assert . ThrowsAsync < NotSupportedException > ( ( ) => plugin . ExecuteAsync ( parameters , CancellationToken . None ) ) ;
53+ }
54+
55+ [ Fact ]
56+ public void GetSqlAndParameters_ThrowsOnMissingSql ( )
57+ {
58+ var plugin = CreatePlugin ( ) ;
59+ var input = new InputParameter { Sql = null ! } ;
60+ var method = typeof ( SqlitePlugin ) . GetMethod ( "GetSqlAndParameters" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ! ;
61+ var ex = Assert . Throws < TargetInvocationException > ( ( ) => method . Invoke ( plugin , new object [ ] { input } ) ) ;
62+ Assert . IsType < ArgumentException > ( ex . InnerException ) ;
63+ Assert . Contains ( "Missing 'sql' parameter" , ex . InnerException ! . Message ) ;
64+ }
65+
66+ [ Fact ]
67+ public void AddParameters_AddsVariousTypes ( )
68+ {
69+ var plugin = CreatePlugin ( ) ;
70+ var cmd = new SqliteCommand ( ) ;
71+ var parameters = new Dictionary < string , object >
72+ {
73+ { "int" , 1 } ,
74+ { "str" , "hello" } ,
75+ { "guid" , Guid . NewGuid ( ) } ,
76+ { "bool" , true } ,
77+ { "date" , DateTime . Now } ,
78+ { "bytes" , new byte [ ] { 1 , 2 , 3 } }
79+ } ;
80+ var method = typeof ( SqlitePlugin ) . GetMethod ( "AddParameters" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ! ;
81+ method . Invoke ( plugin , new object [ ] { cmd , parameters } ) ;
82+ Assert . Equal ( 6 , cmd . Parameters . Count ) ;
83+ }
84+
85+ [ Fact ]
86+ public void ThrowIfReflection_ThrowsIfCalledViaReflection ( )
87+ {
88+ var guidProvider = new Mock < IGuidProvider > ( ) ;
89+ var reflectionGuard = new Mock < IReflectionGuard > ( ) ;
90+ reflectionGuard . Setup ( r => r . IsCalledViaReflection ( ) ) . Returns ( true ) ;
91+
92+ var plugin = new SqlitePlugin ( guidProvider . Object , reflectionGuard . Object ) ;
93+ var method = typeof ( SqlitePlugin ) . GetMethod ( "ThrowIfReflection" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ! ;
94+ var ex = Assert . Throws < TargetInvocationException > ( ( ) => method . Invoke ( plugin , Array . Empty < object > ( ) ) ) ;
95+ Assert . IsType < InvalidOperationException > ( ex . InnerException ) ;
96+ }
97+
98+ [ Fact ]
99+ public void ThrowIfNotInitialized_ThrowsIfNotInitialized ( )
100+ {
101+ var plugin = CreatePlugin ( ) ;
102+ typeof ( SqlitePlugin ) . GetField ( "_isInitialized" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ! . SetValue ( plugin , false ) ;
103+ var method = typeof ( SqlitePlugin ) . GetMethod ( "ThrowIfNotInitialized" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ! ;
104+ var ex = Assert . Throws < TargetInvocationException > ( ( ) => method . Invoke ( plugin , Array . Empty < object > ( ) ) ) ;
105+ Assert . IsType < InvalidOperationException > ( ex . InnerException ) ;
106+ }
107+ }
0 commit comments