SqliteDBTestProvider.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Linq;
  3. namespace PetaPoco.Tests.Integration.Databases.Sqlite
  4. {
  5. public class SqliteDBTestProvider : DBTestProvider
  6. {
  7. protected override string ConnectionName => "sqlite";
  8. protected override string ScriptResourceName => "PetaPoco.Tests.Integration.Scripts.SqliteBuildDatabase.sql";
  9. public IDatabase GetDatabase()
  10. {
  11. return Database;
  12. }
  13. protected override IDatabaseBuildConfiguration BuildFromConnectionName(string name)
  14. {
  15. return base.BuildFromConnectionName(name).UsingDefaultMapper<ConventionMapper>(m =>
  16. {
  17. m.FromDbConverter = (targetProperty, sourceType) =>
  18. {
  19. if (targetProperty != null && sourceType == typeof(long))
  20. {
  21. var type = !targetProperty.PropertyType.IsNullableType() ? targetProperty.PropertyType : targetProperty.PropertyType.GetGenericArguments().First();
  22. switch (Type.GetTypeCode(type))
  23. {
  24. case TypeCode.DateTime:
  25. return o => new DateTime((long) o, DateTimeKind.Utc);
  26. default:
  27. return o => Convert.ChangeType(o, Type.GetTypeCode(type));
  28. }
  29. }
  30. return null;
  31. };
  32. m.ToDbConverter = sourceProperty =>
  33. {
  34. var type = !sourceProperty.PropertyType.IsNullableType() ? sourceProperty.PropertyType : sourceProperty.PropertyType.GetGenericArguments().First();
  35. switch (Type.GetTypeCode(type))
  36. {
  37. case TypeCode.DateTime:
  38. return o => ((DateTime?) o)?.Ticks;
  39. }
  40. return null;
  41. };
  42. });
  43. }
  44. }
  45. }