nanovna-saver/test/test_touchstone.py

91 wiersze
3.6 KiB
Python
Czysty Zwykły widok Historia

2019-11-19 18:16:02 +00:00
# NanoVNASaver
# A python program to view and export Touchstone data from a NanoVNA
# Copyright (C) 2019. Rune B. Broberg
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import unittest
2019-11-24 17:12:51 +00:00
import logging
2019-11-19 18:16:02 +00:00
# Import targets to be tested
from NanoVNASaver.Touchstone import Options, Touchstone
2019-11-19 18:16:02 +00:00
class TestTouchstoneOptions(unittest.TestCase):
def setUp(self):
self.opts = Options()
def test_defaults(self):
self.assertEqual(self.opts.unit, "ghz")
self.assertEqual(self.opts.parameter, "s")
self.assertEqual(self.opts.format, "ma")
self.assertEqual(self.opts.resistance, 50)
self.assertEqual(self.opts.factor, 1000000000)
self.assertEqual(str(self.opts), "# GHZ S MA R 50")
def test_parse(self):
self.assertRaisesRegex(
TypeError, "Not an option line:",
self.opts.parse, "")
self.assertRaisesRegex(
TypeError, "Not an option line: !",
self.opts.parse, "!")
self.assertRaisesRegex(
TypeError, "Illegal option line: # ILLEGAL",
self.opts.parse, "# ILLEGAL")
self.assertRaisesRegex(
TypeError, "Illegal option line: # GHz mhz",
self.opts.parse, "# GHz mhz")
self.opts.parse('# khz')
self.assertEqual(str(self.opts), "# KHZ S MA R 50")
self.assertEqual(self.opts.factor, 1000)
self.opts.parse('# r 123 ri hz y')
self.assertEqual(str(self.opts), "# HZ Y RI R 123")
self.assertEqual(self.opts.factor, 1)
2019-11-19 18:16:02 +00:00
class TestTouchstoneTouchstone(unittest.TestCase):
def test_load(self):
ts = Touchstone("./test/data/valid.s1p")
ts.load()
self.assertEqual(str(ts.opts), "# HZ S RI R 50")
self.assertEqual(len(ts.s11data), 1010)
self.assertEqual(len(ts.s21data), 0)
2019-11-21 12:23:17 +00:00
self.assertEqual(ts.r, 50)
2019-11-19 18:16:02 +00:00
ts = Touchstone("./test/data/valid.s2p")
ts.load()
self.assertEqual(str(ts.opts), "# HZ S RI R 50")
self.assertEqual(len(ts.s11data), 1020)
self.assertEqual(len(ts.s21data), 1020)
self.assertEqual(len(ts.s12data), 1020)
self.assertEqual(len(ts.s22data), 1020)
self.assertIn("! Vector Network Analyzer VNA R2", ts.comments)
def test_load_scikit(self):
ts = Touchstone("./test/data/scikit_unordered.s2p")
2019-11-24 17:12:51 +00:00
with self.assertLogs(level=logging.WARNING) as cm:
ts.load()
self.assertEqual(cm.output, [
'WARNING:NanoVNASaver.Touchstone:Non integer resistance value: 50.0',
'WARNING:NanoVNASaver.Touchstone:Comment after header: !freq ReS11 ImS11 ReS21 ImS21 ReS12 ImS12 ReS22 ImS22',
'WARNING:NanoVNASaver.Touchstone:Frequency not ascending: 15000000.0 0.849810063 -0.4147357 -0.000306106 0.0041482 0.0 0.0 0.0 0.0',
'WARNING:NanoVNASaver.Touchstone:Reordering data',
])
self.assertEqual(str(ts.opts), "# HZ S RI R 50")
self.assertEqual(len(ts.s11data), 101)
self.assertIn("!freq ReS11 ImS11 ReS21 ImS21 ReS12 ImS12 ReS22 ImS22",
ts.comments)