From 78e08c1ee03de0e561757a12dddf9350dffb7e29 Mon Sep 17 00:00:00 2001
From: Alexandra Hobbs <ahobbs@contractor.usgs.gov>
Date: Fri, 22 Nov 2024 11:15:37 -0700
Subject: [PATCH] working test

---
 geomagio/api/db/MetadataDatabaseFactory.py |  1 -
 test/db/MetadataDatabaseFactory_test.py    | 58 ++++++++++++++++++----
 2 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/geomagio/api/db/MetadataDatabaseFactory.py b/geomagio/api/db/MetadataDatabaseFactory.py
index 7e7fb9ab..19463248 100644
--- a/geomagio/api/db/MetadataDatabaseFactory.py
+++ b/geomagio/api/db/MetadataDatabaseFactory.py
@@ -12,7 +12,6 @@ from .metadata_table import metadata as metadata_table
 
 class MetadataDatabaseFactory(object):
     def __init__(self, database: Database):
-        print("init")
         self.database = database
 
     async def create_metadata(self, meta: Metadata) -> Metadata:
diff --git a/test/db/MetadataDatabaseFactory_test.py b/test/db/MetadataDatabaseFactory_test.py
index 272304ea..6a707280 100644
--- a/test/db/MetadataDatabaseFactory_test.py
+++ b/test/db/MetadataDatabaseFactory_test.py
@@ -1,17 +1,15 @@
 import unittest
-from unittest.mock import patch
+from unittest.mock import AsyncMock, patch, MagicMock
+from databases import Database
 
 from geomagio.api.db import MetadataDatabaseFactory
 from geomagio.metadata import Metadata, MetadataCategory
 from geomagio.api.db.metadata_table import metadata as metadata_table
 
-class TestMetadataDatabaseFactoryClass(unittest.IsolatedAsyncioTestCase):
-
-    @patch('databases.Database.connect')
-    async def test_create_metadata(self, mock_connect_to_db):
-        mock_connection = mock_connect_to_db.return_value
-        mock_connection.execute.return_value = {"id": 1}
+class TestMetadataDatabaseFactory(unittest.IsolatedAsyncioTestCase):
 
+    @patch("databases.Database.execute", new_callable=AsyncMock)
+    async def test_create_metadata(self, mock_execute):
         test_data = Metadata(
             category=MetadataCategory.INSTRUMENT,
             created_by="test_metadata.py",
@@ -39,7 +37,47 @@ class TestMetadataDatabaseFactoryClass(unittest.IsolatedAsyncioTestCase):
                 },
             },
         )
-        
-        await MetadataDatabaseFactory(database=mock_connection).create_metadata(test_data)
 
-        mock_connection.execute.assert_called_once()
+        db = Database("sqlite:///:memory:")
+
+        await MetadataDatabaseFactory(database=db).create_metadata(test_data)
+
+        expected_values = {
+            "category": "instrument",
+            "created_by": "test_metadata.py",
+            "network": "NT",
+            "station": "BDT",
+            "metadata": {
+                "type": "FGE",
+                "channels": {
+                    "U": [{"channel": "U_Volt", "offset": 0, "scale": 313.2}],
+                    "V": [{"channel": "V_Volt", "offset": 0, "scale": 312.3}],
+                    "W": [{"channel": "W_Volt", "offset": 0, "scale": 312.0}],
+                },
+                "electronics": {
+                    "serial": "E0542",
+                    "x-scale": 313.2,
+                    "y-scale": 312.3,
+                    "z-scale": 312.0,
+                    "temperature-scale": 0.01,
+                },
+                "sensor": {
+                    "serial": "S0419",
+                    "x-constant": 36958,
+                    "y-constant": 36849,
+                    "z-constant": 36811,
+                },
+            },
+            "data_valid": True,
+            "priority": 1,
+            "status": "new"
+        }
+
+        expected_insert = metadata_table.insert().values(**expected_values)
+
+        mock_execute.assert_called_once()
+        mock_params = mock_execute.call_args.args[0].compile().params
+
+        expected_params = expected_insert.compile().params
+
+        assert mock_params == expected_params
-- 
GitLab