def test_getstrategy(self): '''Local proof of concept''' scrud = StrategyCrud() strat = 'Buy Lower' scrud.addStrategy('Buy Lower') s = scrud.getStrategy(name="Buy Lower") self.assertEqual(strat, s.name)
def test_setlink(self): sCrud = StrategyCrud() dalink = 'https://fictional/web/site' strat = 'VWAP Support' sCrud.setLink(strat, dalink) links = sCrud.getLinks(strat) self.assertIn(dalink, links)
def __init__(self): super().__init__(parent=None) self.ui = Ui_Form() self.settings = QSettings('zero_substance', 'structjour') self.apiset = QSettings('zero_substance/stockapi', 'structjour') self.justloaded = False self.setWindowIcon(QIcon('structjour/images/ZSLogo.png')) defimage = "structjour/images/ZeroSubstanceCreation_220.png" if not os.path.exists(defimage): logging.info(f'Incorrect path of image: {defimage}') self.settings.setValue("defaultImage", defimage) self.ui.setupUi(self) self.ui.strategyAddBtn.pressed.connect(self.addStrategy) self.ui.strategyRemoveBtn.pressed.connect(self.removeStrategy) self.ui.strategyCb.currentTextChanged.connect(self.loadStrategy) self.ui.strategyNotes.textChanged.connect(self.stratNotesChanged) self.ui.preferred.released.connect(self.setPreferred) self.ui.notPreferred.released.connect(self.setPreferred) self.ui.chart1.clicked.connect(self.loadImage) self.ui.chart2.clicked.connect(self.loadImage) self.ui.strategyNotes.clicked.connect(self.saveNotes) self.ui.pageSelect.currentItemChanged.connect(self.changePage) self.ui.addLinkBtn.pressed.connect(self.addLinkToList) self.ui.removeLinkBtn.pressed.connect(self.removeLink) self.ui.linkList.currentTextChanged.connect(self.loadPage) self.strat = StrategyCrud() self.loadStrategies()
def test_setimage1(self): ''' Tests getImage1 and setImage1 ''' scrud = StrategyCrud() img = '/c/schnork/image/for/chart/1' scrud.setImage1("Schnork", img) res = scrud.getImage1("Schnork") self.assertEqual(img, res)
def test_setpreferred(self): scrud = StrategyCrud() strat = 'THE strategy for consistency' scrud.addStrategy(strat) prefs = scrud.getPreferred() self.assertIn(strat, [x[1] for x in prefs]) scrud.setPreferred(strat, pref=False) prefs = scrud.getPreferred() self.assertNotIn(strat, [x[1] for x in prefs])
def test_getLinks(self): scrud = StrategyCrud() strat = 'ABCD' link = 'https://A/website/explaining/ABCD' scrud.addStrategy(strat) scrud.setLink(strat, link) links = scrud.getLinks('ABCD') self.assertIn(link, links)
def test_getpreferred(self): scrud = StrategyCrud() strat1 = 'A great strategy' strat2 = 'A questionable strategy' scrud.addStrategy(strat1, preferred=True) scrud.addStrategy(strat2, preferred=False) prefs = scrud.getPreferred() self.assertIn(strat1, [x[1] for x in prefs]) self.assertNotIn(strat2, [x[1] for x in prefs])
def test_getdescription(self): scrud = StrategyCrud() strat1 = 'Rising Devil' strat2 = 'Rising Fred' scrud.addStrategy(strat1) scrud.addStrategy(strat2) desc = 'Stock is in uptrend for days-gaps down - falls to open - ...' scrud.setDescription(strat1, desc) d1 = scrud.getDescription(strat1) d2 = scrud.getDescription(strat2) self.assertEqual(d1.description, desc) self.assertIsNone(d2)
def test_setdescription(self): name = 'Mistake' desc1 = "The description for the mistake category is don't do that anymore. But still ..." scrud = StrategyCrud() scrud.setDescription(name, desc1) strat = Strategy.getStrategy(name) self.assertIsNotNone(strat) self.assertEqual(desc1, strat.description[0].description) desc2 = "The description for the mistake category is don't do that anymore. But still, everyone has some." scrud.setDescription(name, desc2) strat = Strategy.getStrategy(name) self.assertEqual(desc2, strat.description[0].description) descriptions = Description.getAllDescriptions() self.assertNotIn(desc1, [x.description for x in descriptions])
def setUpClass(cls): bu = Backup() bu.backup() if ModelBase.session: ModelBase.session.rollback() print( f'Files have been backed up. Most recent back is {bu.mostRecent()}' ) settings = QSettings('zero_substance', 'structjour') tdb = settings.value('tradeDb') # if tdb and os.path.exists(tdb): # os.remove(tdb) # sdb = settings.value('structjourDb') # if sdb and os.path.exists(sdb): # os.remove(sdb) # create the schema scrud = StrategyCrud() scrud.createTables() scrud.addStrategy('VWAP Support')
class StratControl(QDialog): ''' Control module for the ui created strategybrowser ''' def __init__(self): super().__init__(parent=None) self.ui = Ui_Form() self.settings = QSettings('zero_substance', 'structjour') self.apiset = QSettings('zero_substance/stockapi', 'structjour') self.justloaded = False self.setWindowIcon(QIcon('structjour/images/ZSLogo.png')) defimage = "structjour/images/ZeroSubstanceCreation_220.png" if not os.path.exists(defimage): logging.info(f'Incorrect path of image: {defimage}') self.settings.setValue("defaultImage", defimage) self.ui.setupUi(self) self.ui.strategyAddBtn.pressed.connect(self.addStrategy) self.ui.strategyRemoveBtn.pressed.connect(self.removeStrategy) self.ui.strategyCb.currentTextChanged.connect(self.loadStrategy) self.ui.strategyNotes.textChanged.connect(self.stratNotesChanged) self.ui.preferred.released.connect(self.setPreferred) self.ui.notPreferred.released.connect(self.setPreferred) self.ui.chart1.clicked.connect(self.loadImage) self.ui.chart2.clicked.connect(self.loadImage) self.ui.strategyNotes.clicked.connect(self.saveNotes) self.ui.pageSelect.currentItemChanged.connect(self.changePage) self.ui.addLinkBtn.pressed.connect(self.addLinkToList) self.ui.removeLinkBtn.pressed.connect(self.removeLink) self.ui.linkList.currentTextChanged.connect(self.loadPage) self.strat = StrategyCrud() self.loadStrategies() def loadPage(self, val): logging.debug(f'val {val}') self.ui.strategyBrowse.load(QUrl(val)) def removeLink(self): index = self.ui.linkList.currentIndex() url = self.ui.linkList.currentText() self.ui.linkList.removeItem(index) key = self.ui.strategyCb.currentText() self.strat.removeLink(key, url) def addLinkToList(self): link = self.ui.addLink.text() if link: self.ui.linkList.addItem(link) self.ui.addLink.setText('') index = self.ui.linkList.findText(link) self.ui.linkList.setCurrentIndex(index) key = self.ui.strategyCb.currentText() self.strat.setLink(key, link) def changePage(self, val): logging.debug(f"changePage {val}") if val.data(0) == 'Text and Images': self.ui.stackedWidget.setCurrentIndex(0) else: self.ui.stackedWidget.setCurrentIndex(1) def loadImage(self, x, event): ''' A signal from ClickLabel :params x: The object origin (a ClickLabel object) :params event: QContextMenuEvent object :Programming Notes: The mapTo thing -- Found no docs or discussions in answers. None of the other map{To/From}{Parent/Global}({pos/globalPos}) things mapped correctly. By Trial and error I finally tried None. That does not seem like it should be right but its the only argument to mapTo() that didn't fail quietly-crashing the program. The fact that it failed without comment is weird. I am expecting their undocumented code to change. ''' msg = f'loadIm1ge1 {x.objectName()} {event.pos()} {event.globalPos()}' logging.debug(msg) img = x cmenu = QMenu(img) # pi1 = cmenu.addAction("psych 1") # pi2 = cmenu.addAction("fractal 2") # pi3 = cmenu.addAction("starry night 3") pi4 = cmenu.addAction("Paste from clipboard") # pi5 = cmenu.addAction("Browse for picture") # This is the line in question and None arg is the crux action = cmenu.exec_(self.mapTo(None, event.globalPos())) if action == pi4: key = self.ui.strategyCb.currentText() logging.debug(f'Are we ready? {key, x.objectName()}') journalDir = self.settings.value('journal') if not journalDir: logging.warning( 'Please set your journal directory before adding images') return else: imagedir = os.path.join(journalDir, 'images/') if os.path.exists(journalDir): if not os.path.exists(imagedir): os.makedirs(imagedir) iName = 'strat_{}_{}.png'.format(key, x.objectName()) imageName = os.path.join(imagedir, iName) if x.objectName() == 'chart1': self.strat.setImage1(key, imageName) else: self.strat.setImage2(key, imageName) pname = self.pasteToLabel(x, imageName) if pname != imageName: if x.objectName() == 'chart1': self.strat.setImage1(key, imageName) else: self.strat.setImage2(key, imageName) def pasteToLabel(self, widg, name): ''' Rather than paste, we call a method that saves the clipboard to a file, then we open it with QPixmap ''' xlimg = XLImage() img, pname = xlimg.getPilImageNoDramaForReal(name) if not img: mbox = QMessageBox() msg = pname + " Failed to get an image. Please select and copy an image." mbox.setText(msg) mbox.exec() return pixmap = QPixmap(pname) widg.setPixmap(pixmap) return pname def saveNotes(self, event): desc = self.ui.strategyNotes.toPlainText() key = self.ui.strategyCb.currentText() self.strat.setDescription(key, desc) self.setWindowTitle('Strategy Browser') def setPreferred(self): val = self.ui.preferred.isChecked() key = self.ui.strategyCb.currentText() val = 1 if val is True else 0 strat = self.strat.getStrategy(key) if strat and strat[1] != val: self.strat.setPreferred(key, val) def stratNotesChanged(self): if not self.justloaded: self.setWindowTitle('Strategy Browser ... text edited') self.justloaded = False def loadStrategy(self, key): if not key: return strat = self.strat.getStrategy(key) if strat: if strat.preferred: self.ui.preferred.setChecked(True) else: self.ui.notPreferred.setChecked(True) desc = self.strat.getDescription(key) if desc: self.justloaded = True self.ui.strategyNotes.setText(desc.description) self.setWindowTitle('Strategy Browser') image1 = self.strat.getImage1(key) if not image1: image1 = self.settings.value('defaultImage') pixmap = QPixmap(image1) self.ui.chart1.setPixmap(pixmap) elif not os.path.exists(image1): self.strat.removeImage1(key) else: pixmap = QPixmap(image1) self.ui.chart1.setPixmap(pixmap) image2 = self.strat.getImage2(key) if not image2: image2 = self.settings.value('defaultImage') pixmap = QPixmap(image2) self.ui.chart2.setPixmap(pixmap) elif not os.path.exists(image2): self.strat.removeImage2(key) else: pixmap = QPixmap(image2) self.ui.chart2.setPixmap(pixmap) daLinks = self.strat.getLinks(key) self.ui.linkList.clear() for l in daLinks: self.ui.linkList.addItem(l) def loadStrategies(self): '''Load the strategy combo box from the db''' if not self.settings.value('structjourDb'): return strats = self.strat.getStrategies() self.ui.strategyCb.clear() for row in strats: self.ui.strategyCb.addItem(row[1]) # if row[3] == 1: # self.ui.preferred.setChecked(True) # else: # self.ui.notPreferred.setChecked(True) def addStrategy(self): addthis = self.ui.strategyAdd.text() if addthis: self.strat.addStrategy(addthis) self.loadStrategies() ix = self.ui.strategyCb.findText(addthis) self.ui.strategyCb.setCurrentIndex(ix) else: logging.info('Nothing strategy to add. No action taken') def removeStrategy(self): removethis = self.ui.strategyCb.currentText() if removethis: self.strat.removeStrategy(removethis) self.loadStrategies()
def test_removelink(self): '''Exercises all Link methods''' scrud = StrategyCrud() strat = 'VWAP Support' link = 'https://A/website/explaining/Everything/and/VWAPD' link2 = 'https://A/website/explaining/Everything/else/and/VWAPD' scrud.setLink(strat, link) res = scrud.getLinks(strat) self.assertIn(link, res) scrud.setLink(strat, link2) res2 = scrud.getLinks(strat) self.assertIn(link2, res2) self.assertTrue(len(res2) == len(res) + 1) scrud.setLink(strat, link) res3 = scrud.getLinks(strat) self.assertEqual(len(res2), len(res3)) scrud.removeLink(strat, link) scrud.removeLink(strat, link2) res4 = scrud.getLinks(strat) if res4: self.assertNotIn(link, res4) self.assertNotIn(link2, res4) scrud.removeLink('VWAP Support', 'https://fictional/web/site')
def test_removeimage(self): ''' Exercise all the image get set and remove functions ''' scrud = StrategyCrud() img1 = '/c/schnork/image/or/q/for/chart/1' img2 = '/c/schnork/image/or/q/for/chart/2' scrud.setImage1("Schnork", img1) scrud.setImage2("Schnork", img2) res1 = scrud.getImage1("Schnork") res2 = scrud.getImage2("Schnork") self.assertIn(img1, res1) self.assertIn(img2, res2) scrud.removeImage1('Schnork') scrud.removeImage2('Schnork') res1 = scrud.getImage1("Schnork") res2 = scrud.getImage2("Schnork") self.assertIsNone(res1) self.assertIsNone(res2)
def test_getid(self): '''Local proof of concept''' sCrud = StrategyCrud() id = sCrud.getId('VWAP Support') self.assertIsNotNone(id) self.assertGreaterEqual(id, 1)
def test_setimage2(self): scrud = StrategyCrud() img = '/c/schnork/image/for/chart/2' scrud.setImage2("Schnork", img) res = scrud.getImage2("Schnork") self.assertEqual(img, res)
def test_removestrategy(self): ''' Demonstrates Strategies relationship with Images, Description and Links and cascacde delete for each Not sure if the relationships work for creation (through a Strategy object) But then there is no call for it in the structjour strategy browser; each of the fields are created from a different button ''' scrud = StrategyCrud() stratName = 'Schnork' img = '/c/an/image/for/schnork.gif' desc = "Schnork is a strategy of percentages." link = 'https://A/thorough/description/of/the percentages/of/schnork.html' scrud.addStrategy(stratName) scrud.setImage1(stratName, img) scrud.setDescription(stratName, desc) scrud.setLink(stratName, link) strat = Strategy.getStrategy(stratName) self.assertIn(img, [x.name for x in strat.images]) self.assertIn(desc, [x.description for x in strat.description]) self.assertIn(link, [x.link for x in strat.links]) scrud.removeStrategy(stratName) strat = Strategy.getStrategy(stratName) # Tests have to get all links, descriptions, and images from their own table to test they were deleted links = Links.getAllLinks() descriptions = Description.getAllDescriptions() images = Images.getAllImages() self.assertIsNone(strat) self.assertNotIn(img, [x.name for x in images]) self.assertNotIn(img, [x.description for x in descriptions]) self.assertNotIn(img, [x.link for x in links])
def test_getstrategies(self): ''' Exercises addStratege and getStrategies. Get strategies gets a list of lists. TODO: It gets the format the the DBAPI got. Fix it later ''' scrud = StrategyCrud() strat1 = 'Buy Lower than that' strat2 = 'Sell higher' scrud.addStrategy(strat1) res1 = scrud.getStrategies() scrud.addStrategy(strat2) res2 = scrud.getStrategies() self.assertIn(strat1, [x[1] for x in res1]) self.assertIn(strat2, [x[1] for x in res2]) self.assertEqual(len(res2), len(res1) + 1) scrud.addStrategy(strat1) res3 = scrud.getStrategies() self.assertEqual(len(res2), len(res3))